9

Based on what I have read about pseudoterminals in Linux, there are two types of pseudoterminals: BSD-style pseudoterminals (which is deprecated) and UNIX 98 pseudoterminals.

I have created two images that shows my understanding of these two types of pseudoterminals.

The following image shows how the BSD-style pseudoterminals works (please correct me if the image is wrong):

enter image description here

This type of pseudoterminals is not hard to understand, each terminal is connected to a unique master driver.


But in the UNIX 98 pseudoterminals, things are a little more confusing. The following image shows how I think this type of pseudoterminals works:

enter image description here

So basically all terminals use the same master driver (/dev/ptmx), but I am not sure how the master driver knows how to do the following:

  • If data is being sent from one of the terminal processes, how does the master driver knows to which TTY slave driver the data should be passed to?

  • If data is being sent from one of the TTY slave drivers, how does the master driver knows to which terminal process the data should be passed to?

Does the master driver knows how to do this in the way that I have shown in the image (i.e. the master driver have a mapping table that maps each terminal PID to its corresponding TTY slave driver)?

user7681202
  • 1,313
  • 3
  • 14
  • 27

1 Answers1

10

You are curiously fascinated by names. /dev/ptmx is not a "driver", it's just a name in the filesystem, which has a special meaning.

A process opens a new master pty by calling posix_openpt(), which returns a file descriptor; the same effect can be achieved by calling open() on /dev/ptmx. Each time a process calls open() of /dev/ptmx a new pseudoterminal is created; the pseudoterminal is destroyed when there are no more processes having this file descriptor open. This file descriptor refers to the master side of the pseudoterminal, and can be passed to descendant processes like any other file descriptor.

For more detailed information see unix.stackexchange.com/questions/117981. (Hat tip to @JdeBP for the suggestion.)

Once a process has a file descriptor referring to a master side of the pseudoterminal, it can find out the name of the slave side of the pseudoterminal by calling ptsname(), and can pass this name to any process it wants to control through the pseudoterminal.

AlexP
  • 10,455
  • I didn't mean that /dev/ptmx is a driver in the image, I was just implying that the /dev/ptmx device file points to the master driver. – user7681202 Nov 21 '17 at 11:30
  • 1
    @user7681202: The point is that it does not point to the master "driver". It "points" to a routine which creates a new pseudoterminal. More specifically, when a process opens this special file a kernel routine creates a new pseudoterminal; when all processes have closed the file descriptor corresponding to the master side of the pseudoterminal another kernel routine destroys it. – AlexP Nov 21 '17 at 11:50
  • I thought that a device file can only point to a driver! This article also uses the term "driver" to refer to the master side of a pty ("...a node for the master side driver /dev/ptmx and..."): https://docs.oracle.com/cd/E19253-01/816-4855/termsub15-14/index.html. – user7681202 Nov 21 '17 at 12:17
  • You might want to point to https://unix.stackexchange.com/questions/117981/ for further reading. It has diagrams. – JdeBP Nov 21 '17 at 13:17
  • "This file descriptor refers to the master side of the pseudoterminal" Does each process that creates a new pseudoterminal will have a file descriptor that points to a unique master object/driver, or are all of the file descriptors points to the same master object/driver like I have showed in my second image? – user7681202 Nov 25 '17 at 08:58
  • @user7681202: The question is meaningless. File descriptors are small integers, used as indices in a table. Each pseudoterminal has a master side and a slave side, which are its own, not shared with other pseudo terminals. Yes, there is only one routine in the kernel dealing with writing to the master side of a pseudoterminal, which works for all pseudoterminals; in the same way there is only one routine in the kernel dealing with writing to a regular file, which works for all regular files; there is only one routine dealing with writing to a disk, which works for all disks etc. – AlexP Nov 25 '17 at 10:34
  • Let me clarify my question: A process that is talking to the slave side (for example: bash) will have in its fd table the fd 0 (stdin) and fd 1 (stdout) and fd 2 (stderr) all point to /dev/pts/0, Now if you have another bash process, it will have its fd 0 and fd 1 and fd 2 all point to /dev/pts/1, do we agree so far? Now let's talk about the master side, say we have two gnome-terminal processes running, what will the fd 0 and fd 1 and fd 2 in the fd table for each gnome-terminal process point to, will they point to /dev/ptmx? – user7681202 Nov 25 '17 at 11:21
  • @user7681202: gnome-terminal usually does not have the file descriptors 0, 1, and 2 open; and in the case it has them open, they may likely be on the slave slave side of the pseudoterminal from where it was launched. The two terminal emulators in which the two bashes are running will each have a file descriptor representing the master side of the two pseudoterminals; yes, both file descriptors were obtained by opening /dev/ptmx; nevertheless they are different pseudoterminals with no relationship between them. (continued) – AlexP Nov 25 '17 at 13:22
  • @user7681202: If two processes each have a file descriptor obtained by opening the same filename, for example, /tmp/some-file.txt, it does not mean that they have the same file open. It may be the case that the first process opened the file, then removed it without closing it; then the second process opened the same filename. They each have a different file opened, although they both opened the same name. Not to mention namespaces... Exactly because all master sides of all ptys are obtained by opening the same filename there is the function ptsname() to get the name of the slave side. – AlexP Nov 25 '17 at 13:24
  • I opened multiple terminal processes, and I viewed the fd table for each process using the following command: ls -l /proc/<PID_goes_here>/fd. Each process had an entry like: 16 -> /dev/ptmx. So you're telling me that these multiple processes do not all talk to /dev/ptmx, but rather each process talk to a unique master object/driver, and the /dev/ptmx name that they all have in their fd tables is just the name of the file that each process originally opened and which they no longer use? – user7681202 Nov 25 '17 at 17:26