Testing strace write
and strace mesg
reveals a slight difference in how they identify your tty.
write
will first do readlink("/proc/self/fd/0", ...)
to find the name of the tty
and then do a stat
call on the resulting path.
mesg
on the other hand will call fstat(1, ...)
which skips the step of using readlink
and doesn't rely on the /proc
file system.
Notice that they also look at different file descriptors 0
vs 1
. Normally an interactive shell will have file descriptors 0
, 1
, and 2
all referencing the same tty. If you have somehow gotten your shell running with 0
and 1
referencing something different for example if you had redirected one of them, that could explain the discrepancy between the output from mesg
and write
.
If the file descriptors are both referencing the same tty, another possible reason for the discrepancy could be that your /proc
mount is not behaving as expected or the character device inode for your tty has been replaced.