Mut...@dastardlyhq.com:
Do I understand correctly, that you think of the following
scenario?
Say, there is a "bash" running in an "xterm". Run the following
shell command to get a long‐running job which determines the
pathname of its pseudo terminal, waits one day, and finally opens
the pathname of its past pseudo terminal, writing a greeting to it,
then exit:
(
tty="$(tty)" &&
sleep -- "$(( 24 * 3600 ))" &&
printf '%s\n' 'Hello, world!' > "$tty"
) &
Running the command
ps -o ppid -o sid -o tty -o tpgid -o pgid -o pid \
-o stat -o args --sort=sid,pgid,stime -s 12217
from a different session group will show the long‐running job
12314. (The "printf" command is not shown already as it won't be
started before the "sleep" will have exited.):
PPID SID TT TPGID PGID PID STAT COMMAND
12209 12217 pts/2 12217 12217 12217 Ss+ /bin/bash
12217 12217 pts/2 12217 12314 12314 S /bin/bash
12314 12217 pts/2 12217 12314 12317 S sleep -- 86400
After exiting the shell (and the xterm), the job will keep running
orphaned:
PPID SID TT TPGID PGID PID STAT COMMAND
1 12217 ? -1 12314 12314 S /bin/bash
12314 12217 ? -1 12314 12317 S sleep -- 86400
Now, imagine, that a new xterm is opened, running a shell, and, by
incident, the pseudo terminal it gets will have the same name
"/dev/pts/2". This should be possible, because the old "/dev/pts/2"
entry has already been removed from the directory, i. e., it
wouldn't be "reserved" any more.
As the orphaned job will explicitly open "/dev/pts/2", it would
write to the new pseudo terminal that happens to have the same name
like the old had.
Therefore, wouldn't it be more sensible to not have the name
"/dev/pts/2" removed from the (virtual) directory as long as the old
process session has not ceased to exist in the system? That would
prevent the pseudo terminal multiplexer to create a new "/dev/pts/2"
by accident, thus prevent the "printf" command to write to the new
"/dev/pts/2".
Of course the "printf" command should have used "/dev/tty" rather
than "/dev/pts/2" in the redirection, that is, the output of the
"tty" command should be considered to be obsolete as soon as it is
received (unless one knows that the controlling terminal is still
alive).