I'm working through questions from Unix The Textbook (chapter 8, #16, page 207):

Give chmod command lines that perform the same tasks that the mesg n and mesg y commands do. (Hint: Every hardware device, including your terminal, has an associated file in the /dev directory.)

I believe the answer is:

mesg n = chmod 770 /dev/stdout

mesg y = chmod 777 /dev/stdout

But I was wondering what happens if you use chmod 000 /dev/stdin?

Do you get locked out of entering commands in the terminal?

  • 2
    It's important to understand that permissions are only checked when a file is opened. Therefore, no matter what your shell's standard input happens to be, changing its permissions won't lock you out of entering commands, because both the shell and the terminal emulator or ssh or whatever have already opened the communications channel. mesg n can use permissions to affect future writes only because write doesn't already have the terminal open. – zwol Jun 11 '17 at 14:49

No, /dev/stdin and /dev/stdout are the wrong device. These are not terminal devices, they're aliases for standard input and standard output respectively. Standard input and standard output are, by definition, file descriptors that applications expect to be open and have a conventional meaning (file descriptor 0 and 1 respectively, there's also 2 which is standard error). Devices such as /dev/stdin and /dev/stdout are useful when an application requires a file name, but the user of the application wants it to access a particular file descriptor rather than opening some file. Depending on the unix variant, they might not even be device files; for example, on Linux, they're symbolic links to /proc/self/fd/0 and friends, and these are in turn “magic” symbolic links to whatever file the process has already open on that file descriptor.

Changing the permissions of /dev/stdin and /dev/stdout would only change what happens when these file names are used explicitly. It doesn't affect anything related to the terminal, and it doesn't affect normal use of standard input and standard output, since the permissions only matter when opening a particular filename.

What mesg does is to change the permissions of the process's controlling terminal. For an application that's running in a terminal, the terminal is open on standard input, standard ouput and standard error (file descriptors 0, 1 and 2). You can use the command tty to see what the terminal device is. mesg n is equivalent to chmod g-w "$(tty)" and mesg y is equivalent to chmod g+w "$(tty)".

  • 2
    tty doesn't report the controlling terminal, but the terminal open on stdin if any. So on Linux where /dev/stdin is not a device but a special symlink to the file open on /dev/stdin, chmot "$(tty)" would be more or less the same as chmod /dev/stdin (if stdin is a tty device, and would attempt to change the mode of a not a tty file in the current directory otherwise). See /dev/$(ps -o tty= -p "$$") for the controlling terminal. – Stéphane Chazelas Jun 11 '17 at 5:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.