Re: how does a pts for child process get set?



pacman@xxxxxxxxxxxxx (Alan Curry) writes:
In article <87hbmztklx.fsf@xxxxxxxxxxxxxxxxxxxx>,
Phil Carmody <thefatphil_demunged@xxxxxxxxxxx> wrote:
|
|> How did it get this information?
|
|'ps' probably got it by looking inside /proc/$$/fd

Nope. It's the 7th field in /proc/$$/stat.

That's the major/minor device number (256*major+minor), but what
was being asked for looked like its textual /dev filesystem name.
A quick strace of ps indicates that is reads stat, guesses what
the path of the tty is, fails, and then starts sniffing in
/proc/$$/fd/

read(6, "23273 (ps) R 23272 23272 9185 348"..., 1023) = 198
close(6) = 0
open("/proc/23273/status", O_RDONLY) = 6
read(6, "Name:\tps\nState:\tR (running)\nSleep"..., 1023) = 639
close(6) = 0
stat64("/dev/pts1", 0xbf9ee77c) = -1 ENOENT (No such file or directory)
stat64("/dev/pts", {st_mode=S_IFDIR|0755, st_size=0, ...}) = 0
readlink("/proc/23273/fd/2", "/dev/pts/1"..., 127) = 10
stat64("/dev/pts/1", {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 1), ...}) = 0
write(1, "23273 pts/1 00:00:00 ps\n"..., 2723273 pts/1 00:00:00 ps
) = 27

A process can have a controlling tty even if it has no open file descriptors,
and it can have file descriptors referring to a tty without having a
controlling tty (by using O_NOCTTY during the open, or TIOCNOTTY afterward).

That's certainly true.

Phil
--
I find the easiest thing to do is to k/f myself and just troll away
-- David Melville on r.a.s.f1
.