0

In 1970's, we had hardware terminals CUI.

Now in my Linux box, I see seven terminal emulators, where GUI is occupying 7th terminal emulator(Ctrl+Alt+F7).

For example, this is my first terminal(Ctrl+Alt+F1) emulator

$ tty
/dev/tty0

On >cat ENTER in user-space,cat process waits on it's stdin to receive input from /dev/tty0 file. I could not view /dev/tty0 file using any editor.

Both stdin and stdout of cat process are working with /dev/tty0 file.

Question:

Using some editor, Is this terminal file /dev/tty0 accessible in user space?

overexchange
  • 1,536

4 Answers4

5

You are accessing the terminal file all the time. But this doesn't do what you probably think it does.

When you write to a disk file, and then later read from it, what you read is what you wrote. This is not the case for special files such as terminals. A file is something that can be written to and read from¹; the link between what gets written and what gets read depends on the nature of the file. With a character device file, there's usually no link at all.

Terminal files connect two entities that play different roles: the terminal itself, and an application that wants to interact with a user. The terminal can either be a piece of hardware, represented by the kernel, or a terminal emulator, which is a process. When the application writes data, the terminal gets to read it (and normally displays it to the user in some fashion); when the application reads data, the data comes from the terminal (normally this is input from the user).

In your case, the application is successively the shell, cat, the editor, etc. The editor may try to read from the terminal, but if so it's waiting for your input, and it may or may not react properly to the end of input (which you'd signal by pressing Ctrl+D at the beginning of a line). Once again, reading from the terminal will not give you things that were output to the terminal.

In the case of a console, data that is written to the terminal is drawn on the screen. With Linux consoles, the text displayed on /dev/tty1 can be read back from the device /dev/vcs1 for as long as it remains displayable (so you only get what's displayed on the screen, plus what's still available by scrolling back; anything that isn't reachable by scrolling back is lost forever).

Note that this is a facility offered by the Linux terminal interface. A terminal could simply draw the pixels and forget the text. Most terminal emulators don't provide any similar facility.

¹ Most file are like this, actually, but not all. Directories can't be written through the normal interface, and on many unix variants can't be read either. Some devices don't support reading or writing, only ioctl.

  • Last sentence. With character addressable screens it is necessary for the terminal to remember what it's written where. (This is a function of the terminal emulator, not the device driver.) – Chris Davies Apr 09 '17 at 06:55
3

Yes, however the terminal file is not a regular file.

If you list the extended properties of a terminal file, you'll see:

$ ls /dev/tty1 -lah
crw--w---- 1 root tty 4, 0 Apr  6 13:07 /dev/tty1

notice the first character in the permission section is a letter "c", this indicates that the file is a character special device. In contrast, a regular file would have a "-" in there, a directory a "d", a block special device a "b", a symbolic link an "l", and a unix domain socket an "s".

It's important to note that in Unix systems like Linux, a filesystem is actually more like a namespace for interactions with kernel object, where you can name for various kernel object so they can be referred to unambiguously by various parts of the system. These kernel objects are not just regular files that are backed by persistent storage, but also things like hardware device, or sockets, etc.

A character special device is a kernel object that interface with the userspace by reading and writing streams of characters, they are accessed by doing read() and write() system call.

For example, in my machine with Ctrl+Alt+F1 (which is /dev/tty1 in my machine, not tty0), I could read off whatever being typed to the virtual console using a program that uses the read() syscall like cat:

  1. In graphical emulator, sudo cat /dev/tty1
  2. Switch to virtual console emulator with Ctrl+Alt+F1
  3. Type something into the virtual console emulator
  4. Switch back to graphical UI with Ctrl+Alt+F7
  5. In the graphical emulator, I'd see whatever I typed in the virtual console emulator as the output of cat

The other way around, I could also display characters in the virtual console using a program that uses write() syscall like tee:

  1. In graphical emulator, echo "hello world" | sudo tee /dev/tty1
  2. Switch to virtual console emulator with Ctrl+Alt+F1
  3. In the virtual console emulator, I'd see "hello world"

Most regular text editors will refuse to actually open non-regular special files because they are programmed to check the file type and refuse to edit non-regular files. This is to prevent "accidents" because it makes little sense to edit special devices in regular text editors. Additionally, most regular text editors also doesn't actually just call write() to the existing file, but instead they'd write to a new file and rename() the swap file to replace the old file. Even when you have a text editor that supports writing in-place, they'd typically try to truncate the file first, which of course isn't supported in a special character device.

Lie Ryan
  • 1,228
2

In 1970's, we had hardware terminals CUI.

Nobody used the term CUI in the 70's though.

Now in my Linux box, I see seven terminal emulators, where GUI is occupying 7th terminal emulator(Ctrl+Alt+F7).

These terminal emulators were already available in the 80's, and there was no GUI.

Using some editor, Is this terminal file /dev/tty0 accessible in user space?

This question makes no sense. All files are accessible in user space by design, that's the whole point of files, especially the ones in /dev. /dev/tty0 being a device, it can't be edited as its "content" is essentially ephemeral. What you read from it is what you eventually type in the console and what you write on it is displayed on the screen.

jlliagre
  • 61,204
1

Yes, you certainly can access any TTY as a file like any device in unix. I would mark this question as potentially duplicate as this answer does a good job explaining how to do what you are asking in a similar situation.

To summarize what that answer says, you can interact with other TTYs using echo and cat just as you would on any other file. The reason you didnt see anything when opening the file is because like many special files, only new data is available.