semantics of /dev/tty?

Mikael Pettersson mikpe at mina.liu.se
Sat Feb 11 08:54:12 AEST 1989


Let me begin with a quote from the man page for tty(4):

	The file /dev/tty is, in each process, a synonym for that
	process' controlling terminal.

Well, I have come to the conclusion that this is not quite true.
Consider the case where a program tries to determine whether as given
file descriptor `fd' denotes the controlling terminal (*). One strategy
might be to fstat() both `fd' and `0' (assuming that standard input
is connected to the terminal of course) and then check whether they both
produce the same value in the `st_rdev' field in their stat structs.
However, if `fd' comes from an open() on "/dev/tty", fstat() will
report the <major,minor> of /dev/tty, *not* the ones of the controlling tty.

Q1: Why does fstat() (and stat()) fail this "synonym" requirement?

Q2: Given that fstat() is useless for the purpose of determining operational
equivalence between two (terminal) file descriptors, what would be the `best'
way to implement this test? I have hacked up a temporary solution using
ioctl() calls to see whether a change in one terminals line discipline also
changed the other's. Are there any better (i.e. less kludgey) solutions?

/Mike
--
(*) This is not just an academic problem: I have a very much real-world
application (part of a run-time system) that really *needs* this functionality.
-- 
Mikael Pettersson, Dept of Comp & Info Sci, University of Linkoping, Sweden
email: mpe at ida.liu.se  or  ..!{mcvax,munnari,uunet}!enea!liuida!mpe



More information about the Comp.unix.questions mailing list