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
.



Relevant Pages

  • Re: pty programming: EOF, ICANON, ...
    ... In article, pacman@xxxxxxxxxxxxx (Alan Curry) ... Barry Margolin wrote: ... going to do to the tty. ... As protection against the worst case, ...
    (comp.unix.programmer)
  • Re: How to lock current->signal->tty
    ... The non-leader case for TIOCNOTTY in the base kernel is different ... that path so the tty locking actually ends up in the tty layer. ... passing the file struct to the security hook instead of the tty struct. ... extern void tty_unhangup; ...
    (Linux-Kernel)
  • [PATCH 4/4] tty: In tiocsctty when we steal a tty hang it up.
    ... tty of another session our controlling tty. ... extension that steals the tty from another process group does ... tty of the processes that were part of the session. ...
    (Linux-Kernel)
  • [PATCH 3/4] tty: Introduce no_tty and use it in selinux
    ... selinux when we drop a controlling tty because of inadequate ... is the essence of that ioctl and calls it from the selinux code. ... extern void disassociate_ctty; ...
    (Linux-Kernel)
  • Re: How to lock current->signal->tty
    ... The non-leader case for TIOCNOTTY in the base kernel is different ... that path so the tty locking actually ends up in the tty layer. ...
    (Linux-Kernel)