What is a
chmod equivalent for
mesg y and
What is a
Note: As JdeBP points out in comments, it is non-trivial to reproduce the behaviour of
The terminal changed by
mesgis that associated with the standard input, output, or error, rather than the controlling terminal for the session. This is because users logged in more than once should be able to change any of their login terminals without having to stop the job running in those sessions. This is not a security problem involving the terminals of other users because appropriate privileges would be required to affect the terminal of another user.
The method of checking each of the first three file descriptors in sequence until a terminal is found was adopted from System V.
The naive approach would be to look at the changes in permissions on the shell's controlling terminal (the device that the current interactive shell's standard input is attached to) when
mesg is used.
The controlling terminal of the shell is returned by the
tty command. To figure out what
mesg n and
mesg y are doing with the permissions on the device, we can use
ls -l on it and see what changes:
$ ls -l "$(tty)" crw--w---- 1 myself tty 5, 1 May 6 00:21 /dev/ttyp1 $ mesg n $ ls -l "$(tty)" crw------- 1 myself tty 5, 1 May 6 00:21 /dev/ttyp1 $ mesg y $ ls -l "$(tty)" crw--w---- 1 myself tty 5, 1 May 6 00:21 /dev/ttyp1
So it looks like
mesg n removes the group write permission for the device (
rw--w---- changes to
mesg y adds the group write permission again.
chmod equivalent would, in my case, be
$ chmod g-w "$(tty)" # for "mesg n" $ chmod g+w "$(tty)" # for "mesg y"
Add salt and pepper appropriately for you particular flavour of Unix.
Note, though, that if the shell's standard input is not a terminal,
tty would return the string
not a tty and the
chmod would fail. In this scenario,
mesg would go on to try with the terminal device for the shell's standard output and then standard error (in that order). This is why the above is a naive approach.