On 1/4/24 21:32, Anthony Carrico wrote:
Here is the output:
$ ./a.out
0 is a tty called /dev/pts/1 device ID 136 1
1 is a tty called /dev/pts/1 device ID 136 1
2 is a tty called /dev/pts/1 device ID 136 1
3 is a tty called /dev/tty device ID 5 0
By definition, /dev/tty is the ctty, and obviously 0, 1, and 2 are also
connected to the controlling terminal, because I didn't redirect them
but the process doesn't know that, and they don't match by name or by
device ID, so how can the process be sure that they are connected to its
ctty?
$ ./a.out </dev/null 2>/dev/null
1 is a tty called /dev/pts/1 device ID 136 1
3 is a tty called /dev/tty device ID 5 0
Ok, now stdin and stdout aren't ttys at all, as expeced.
Also, check this out:
$ ./a.out </dev/tty
0 is a tty called /dev/tty device ID 5 0
1 is a tty called /dev/pts/1 device ID 136 1
2 is a tty called /dev/pts/1 device ID 136 1
3 is a tty called /dev/tty device ID 5 0
...when I explicitly redirect /dev/tty to stdin, it does match.
So how does a process know if the stdio fds are connected to its
controlling terminal? Am I forgetting something obvious?
I have found no Posix way, but Linux, the proc(5) man page says the
seventh field in /proc/[pid]/stat is:
(7) tty_nr %d
The controlling terminal of the process. (The minor device number is
contained in the combination of bits 31 to 20 and 7 to 0; the major
device number is in bits 15 to 8.)
--
Anthony Carrico