3

What is a chmod equivalent for mesg y and mesg n?

MandyLB
  • 145
  • It is unclear what you mean. Do you want to make mesg settings depend on user(s) and group(s)? – Ned64 May 05 '18 at 22:29

1 Answers1

8

Note: As JdeBP points out in comments, it is non-trivial to reproduce the behaviour of mesg with chmod.

The Rationale section in the POSIX specification for mesg says

The terminal changed by mesg is 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 rw-------) and mesg y adds the group write permission again.

The 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.

Kusalananda
  • 333,661
  • You could change "$(tty)" to "\tty`"to make it more portable to other shells thanbash(e.g. the code does not work intcsh`). – Ned64 May 05 '18 at 22:33
  • 2
    @Ned64 Um, no. I'm running this in /bin/sh (not bash) which is a shell that exists on all Unix systems. /bin/sh understands $(...) as a command substitution (unless the system is very old indeed, and has skipped more than a few updates). For more about that, see https://unix.stackexchange.com/questions/126927/have-backticks-i-e-cmd-in-sh-shells-been-deprecated – Kusalananda May 05 '18 at 22:37
  • @Kusalananda what is the difference between running chmod g-w and chmod g-w "$(tty)"? (New to UNIX and trying to make sure I understand) – MandyLB May 05 '18 at 22:39
  • ll ($tty) yields Badly placed ()'s. in my shell. Anyway, please ll /bin/sh - in GNU/Linux often a link to bash, they share a syntax. ksh, tcsh, ... vary in syntax. – Ned64 May 05 '18 at 22:39
  • 1
    @Ned64 I believe I used $(tty), not ($tty). My /bin/sh happens to be ksh in POSIX mode. – Kusalananda May 05 '18 at 22:40
  • Sorry. ll $(tty) yields Illegal variable name.. Interesting, so it does work in ksh. It does not in csh+tcsh. – Ned64 May 05 '18 at 22:41
  • @MandyLB The tty command returns the current terminal device. This is the file that changes permission when you use mesg. The command chmod g-w "$(tty)" removes the write permission for the current terminal device. chmod g-w by itself is nonsensical as the command is not given a filename to change permission on. – Kusalananda May 05 '18 at 22:43
  • @Ned64 csh and tcsh uses a totally different shell dialect from the sh shells (bash, ksh, dash, zsh, etc.). They can't really be compared, and trying to write polyglot shell code that works (without errors) in both sh and csh shells is really something you should avoid. – Kusalananda May 05 '18 at 22:45
  • 1
    A subtlety of mesg not reflected in this answer is that mesg does not operate upon the controlling terminal. Depending from which operating system's mesg it is, it operates upon the standard error or the first of standard input/output/error that is a terminal device. This is non-trivial to duplicate, and would not involve the tty command. One example in one mesg manual page even demonstrates standard input redirected from some other terminal and not the controlling terminal. – JdeBP May 05 '18 at 23:11
  • @JdeBP You are absolutely correct. I will amend the answer by referring to your comment. – Kusalananda May 05 '18 at 23:16