Remote Login
rlogin Overview
- Previously, we studied about various characteristics of terminal: line handling, pseudo-terminal device, control characters, and much more. We wrote a simple program which has a flow as shown below:
+-------------------+
| shell |
+-------------------+
| | <-+
stdout, stderr | | stdin |
| | |- IPC
| | |
(sleeping) V | <-+
+-----------+ +-------------------+
| login | fork, exec | recording |
| shell |-------------->| process |
+-----------+ wait +-------------------+
| ^
stdout, stderr | | stdin
| |
+-----------+-----------+---------------+
| | | |
| V | |
| +-------------------+ |
| | terminal line | |
| | discipline | |
| +-------------------+ |
| | ^ |
| | | | kernel
| | | |
| V | |
| +-------------------+ |
| | terminal | |
| | device driver | |
| +-------------------+ |
| | ^ |
| | | |
| | | |
+-----------+-----------+---------------+
| |
| |
V |
+-------------------+
| user at a |
| terminal |
+-------------------+
Figure 15.1: Recording process overview
Notice the Inter-Process Communication (IPC) between the recording process and the shell process. We started out with a simple stream pipe and modified it in the sequence as follows:
- First attempt: Used a basic stream pipe as a means of IPC. The command
tty,stty, and other Graphical User Interface (GUI) showed the flaw with this approach. - Second attempt: Use of pseudo-terminal (pty) as IPC. The pty-master device is connected to the recording process and the pty-slave device is connected to the shell process. The default behavior of terminal is "cooked" mode which enables characteristics including line echoing and such. We need to disable such characteristics. In essence, we want the Line Discipline module connected to the pty-master device to just pass characters to pty-slave device without any interpretation. The Line Discipline module connected to the pty-slave device must be the only one which interprets special characters.
- Third attempt: Saved the mode of the Line Discipline on the pty-master end and converted it into "raw" mode. The mode we saved earlier is set to the Line Discipline module on the pty-slave end. We still encounter one issue. Use of command such as
ps -xshowed that the terminal associated with the shell process is not shown (although the use ofttyshowed that shell process had it's own tty device assoicated with it.) We want the process handling the shell to disassociate from the recording process. - Fourth attempt: To detach the process which handles shell (which will be referred as "child process") from the recording process, it seemed to be implementation defined. For instance, in 4.3BSD, the child process would need to open the special file
/dev/ttyand callioctlwith the commandTIOCNOTTYto request to detach from the process's controlling terminal. System V, on the other hand, required the child process to callsetpgrpto set up a new process group, making the child process the process group leader, which disassociates the child process from its controlling terminal. Then, the pty-slave device is opened, making it the controlling terminal. On macOS (and possibly Linux), the child process will callsetsidbeforeopening the pty-slave device. The notion of session is similar to process group, as it's used by the kernel to allow job contol. This call ensured the calling process (child process in our case) will create a new session and makes the calling process the session leader. The session leader will not have any controlling terminal and the file itopens later (without theO_NOCTTY) will be the controlling terminal.