If a process is in the foreground process-group of its controlling terminal, read works as described in ``Input processing and reading data''. If any process in a background process-group attempts to read from its controlling terminal when job-control is supported, the signal SIGTTIN is sent to its process-group unless one of these special cases apply:
The default action of the signal SIGTTIN is to stop the process to which it is sent (see signal(5)).
If a process is in the foreground process-group of its controlling terminal, write works as described in ``Writing data and output processing''. If any process in a background process-group attempts to write onto its controlling terminal when the flag TOSTOP is set in the c_lflag field of the termios structure, the signal SIGTTOU is sent to the process-group unless one of these special cases apply:
If the flag TOSTOP is clear, attempting to write the controlling terminal proceeds without sending the signal SIGTTOU.
Certain calls that set terminal parameters are treated the same as write calls, except that the flag TOSTOP is ignored; thus, the effect is the same as terminal write calls when the flag TOSTOP is set (see tcgetattr in termios(3C) and tcsetattr in termios(3C)).
If the implementation supports job-control, unless otherwise noted, processes in a background process-group are restricted in their use of the terminal-control-functions (see termios(3C)). Attempts to perform these functions cause the process-group to be sent the signal SIGTTOU. If the calling process either ignores or blocks the signal SIGTTOU, attempting to perform a control-function proceeds without sending the signal SIGTTOU.
The default action of the signal SIGTTOU is to stop the process to which it is sent (see signal(5)).
All terminal-control-functions operate on an open file-descriptor and they affect the underlying terminal-device-file denoted by the file-descriptor, not the open-file-description that represents it.
If a member of a background process-group attempts to invoke an ioctl on its controlling terminal, and that ioctl modifies terminal parameters (e.g., TIOCSPGRP, TCSETA, TCSETAW or TCSETAF) its process-group is sent SIGTTOU, which normally causes the members of that process-group to stop.
The terminal access controls described in this section apply only to a process accessing its controlling terminal because these controls are for the purpose of job-control, not security, and job-control relates only to a controlling terminal for a process. Normal file-access-permissions handle security. A process accessing a terminal other than the controlling terminal is effectively treated the same as a member of the foreground process-group.
If a process in a background orphaned process-group calls read or write, stopping the process-group is undesirable, as it is no longer under the control of a job-control shell that can put it into foreground again. Accordingly, calls to read and write by such processes receive an immediate return error.
The terminal-driver must repeatedly do a foreground/background/orphaned process-group check until either the process-group of the calling process is orphaned or the calling process moves into the foreground. If a calling process is in the background and should receive a job-control signal, the terminal-driver sends the appropriate signal (SIGTTIN or SIGTTOU) to every process in the process-group of the calling process then lets the calling process receive the signal immediately, usually by blocking the process so it reacts to the signal right away. Note, however, that after the process catches the signal and the terminal-driver regains control, the driver must repeat the foreground/background/orphaned process-group check. The process may still be in the background, either because a job-control shell continued the process in the background, or because the process caught the signal and did nothing.
The terminal-driver repeatedly does the foreground/background/orphaned process-group check whenever a process tries to access the terminal. For write or the line-control functions, the check is done on entering the function. For read, the check is done not only on entering the function but also after blocking the process to wait for input data (if necessary). If the process calling read is in the foreground, the terminal-driver tries to get data from the input-queue, and if the queue is empty, blocks the process to wait for data. When data are input and the terminal-driver regains control, it must repeat the foreground/background/orphaned process-group check again because the process may have moved to the background from the foreground while it blocked to wait for input data.