TTY(4) | Device Drivers Manual | TTY(4)

# NAME

`tty`, `cua` — general terminal interface

# SYNOPSIS

`#include <sys/ioctl.h>`

# DESCRIPTION

This section describes the interface to the terminal drivers in the system.

## Terminal Special Files

Each hardware terminal port (such as a serial port) on the system usually has a terminal special device file associated with it in the directory /dev/ (for example, /dev/tty03). When a user logs into the system on one of these hardware terminal ports, the system has already opened the associated device and prepared the line for normal interactive use (see getty(8)). There is also a special case of a terminal file that connects not to a hardware terminal port, but to another program on the other side. These special terminal devices are called ptys and provide the mechanism necessary to give users the same interface to the system when logging in over a network (using ssh(1) for example). Even in these cases the details of how the terminal file was opened and set up is already handled by special software in the system. Thus, users do not normally need to worry about the details of how these lines are opened or used.

For hardware terminal ports, dial-out is supported through matching device nodes called calling units. For instance, the terminal called /dev/tty03 would have a matching calling unit called /dev/cua03. These two devices are normally differentiated by creating the calling unit device node with a minor number 128 greater than the dial-in device node. Whereas the dial-in device (the tty) normally requires a hardware signal to indicate to the system that it is active, the dial-out device (the cua) does not, and hence can communicate unimpeded with a device such as a modem, or with another system over a serial link. This means that a process like getty(8) will wait on a dial-in device until a connection is established. Meanwhile, a dial-out connection can be established on the dial-out device (for the very same hardware terminal port) without disturbing anything else on the system. The getty(8) process does not even notice that anything is happening on the terminal port. If a connecting call comes in after the dial-out connection has finished, the getty(8) process will deal with it properly, without having noticed the intervening dial-out action. For more information on dial-out, see cu(1).

When an interactive user logs in, the system prepares the line to behave in a certain way (called a line discipline), described in stty(1) at the command level, and in termios(4) at the programming level. To change settings associated with a login terminal, refer to the preceding man pages for the common cases. The remainder of this man page is concerned with describing details of using and controlling terminal devices at a low level, such as that possibly required by a program wishing to provide features similar to those provided by the system.

## Line disciplines

A terminal file is used like any other file in the system in that it can be opened, read, and written to using standard system calls. For each existing terminal file, there is a software processing module called a line discipline associated with it. The line discipline essentially glues the low level device driver code with the high level generic interface routines (such as read(2) and write(2)), and is responsible for implementing the semantics associated with the device. When a terminal file is first opened by a program, the default line discipline called the `termios` line discipline is associated with the file. This is the primary line discipline that is used in most cases and provides the semantics that users normally associate with a terminal. When the `termios` line discipline is in effect, the terminal file behaves and is operated according to the rules described in termios(4). Refer to that man page for a full description of the terminal semantics. The operations described here generally represent features common across all line disciplines, although some of these calls may not make sense in conjunction with a line discipline other than `termios`, and some may not be supported by the underlying hardware (or lack thereof, as in the case of ptys).

## Terminal File Operations

All of the following operations are invoked using the ioctl(2) system call. Refer to that man page for a description of the request and argp parameters. In addition to the ioctl requests defined here, the specific line discipline in effect will define other requests specific to it (actually termios(4) defines them as function calls, not ioctl requests). The following section lists the available ioctl requests. The name of the request, a description of its purpose, and the typed argp parameter (if any) are listed. For example, the first entry says

TIOCSETD int *ldisc

and would be called on the terminal associated with file descriptor zero by the following code fragment:

    	int ldisc;

    	ldisc = TTYDISC;
    	ioctl(0, TIOCSETD, &ldisc);

## Terminal File Request Descriptions

`TIOCSETD` int *ldisc
  Change to the new line discipline pointed to by ldisc. The available line disciplines currently available are:

  TTYDISC
    Termios interactive line discipline.
  PPPDISC
    Point-to-Point Protocol line discipline.
  NMEADISC
    NMEA 0183 line discipline.
  MSTSDISC
    Meinberg Standard Time String line discipline.
`TIOCGETD` int *ldisc
  Return the current line discipline in the integer pointed to by ldisc.
`TIOCSBRK` void
  Set the terminal hardware into BREAK condition.
`TIOCCBRK` void
  Clear the terminal hardware BREAK condition.
`TIOCSDTR` void
  Assert data terminal ready (DTR).
`TIOCCDTR` void
  Clear data terminal ready (DTR).
`TIOCGPGRP` int *tpgrp
  Return the current process group the terminal is associated with in the integer pointed to by tpgrp. This is the underlying call that implements the tcgetpgrp(3) call.
`TIOCSPGRP` int *tpgrp
  Associate the terminal with the process group (as an integer) pointed to by tpgrp. This is the underlying call that implements the tcsetpgrp(3) call.
`TIOCGETA` struct termios *term
  Place the current value of the termios state associated with the device in the termios structure pointed to by term. This is the underlying call that implements the tcgetattr(3) call.
`TIOCSETA` struct termios *term
  Set the termios state associated with the device immediately. This is the underlying call that implements the tcsetattr(3) call with the `TCSANOW` option.
`TIOCSETAW` struct termios *term
  First wait for any output to complete, then set the termios state associated with the device. This is the underlying call that implements the tcsetattr(3) call with the `TCSADRAIN` option.
`TIOCSETAF` struct termios *term
  First wait for any output to complete, clear any pending input, then set the termios state associated with the device. This is the underlying call that implements the tcsetattr(3) call with the `TCSAFLUSH` option.
`TIOCOUTQ` int *num
  Place the current number of characters in the output queue in the integer pointed to by num.
`TIOCNOTTY` void
  This call is obsolete but left for compatibility. In the past, when a process that didn't have a controlling terminal (see The Controlling Terminal in termios(4)) first opened a terminal device, it acquired that terminal as its controlling terminal. For some programs this was a hazard as they didn't want a controlling terminal in the first place, and this provided a mechanism to disassociate the controlling terminal from the calling process. It must be called by opening the file /dev/tty and calling `TIOCNOTTY` on that file descriptor.

  The current system does not allocate a controlling terminal to a process on an open(2) call: there is a specific ioctl called `TIOCSCTTY` to make a terminal the controlling terminal. In addition, a program can fork(2) and call the setsid(2) system call which will place the process into its own session - which has the effect of disassociating it from the controlling terminal. This is the new and preferred method for programs to lose their controlling terminal.

`TIOCSETVERAUTH` int *secs
  Indicate that the current user has successfully authenticated to this session. Future authentication checks may then be bypassed by performing a `TIOCCHKVERAUTH` check. The verified authentication status will expire after secs seconds. Only root may perform this operation.
`TIOCCLRVERAUTH` void
  Clear any verified auth status associated with this session.
`TIOCCHKVERAUTH` void
  Check the verified auth status of this session. The calling process must have the same real user ID and parent process as the process which called `TIOCSETVERAUTH`. A zero return indicates success.
`TIOCSTOP` void
  Stop output on the terminal (like typing ^S at the keyboard).
`TIOCSTART` void
  Start output on the terminal (like typing ^Q at the keyboard).
`TIOCSCTTY` void
  Make the terminal the controlling terminal for the process (the process must not currently have a controlling terminal).
`TIOCDRAIN` void
  Wait until all output is drained.
`TIOCEXCL` void
  Set exclusive use on the terminal. No further opens are permitted except by root. Of course, this means that programs that are run by root (or setuid) will not obey the exclusive setting - which limits the usefulness of this feature.
`TIOCNXCL` void
  Clear exclusive use of the terminal. Further opens are permitted.
`TIOCFLUSH` int *what
  If the value of the int pointed to by what contains the `FREAD` bit as defined in `<sys/fcntl.h>`, then all characters in the input queue are cleared. If it contains the `FWRITE` bit, then all characters in the output queue are cleared. If the value of the integer is zero, then it behaves as if both the `FREAD` and `FWRITE` bits were set (i.e., clears both queues).
`TIOCGWINSZ` struct winsize *ws
  Put the window size information associated with the terminal in the winsize structure pointed to by ws. The window size structure contains the number of rows and columns (and pixels if appropriate) of the devices attached to the terminal. It is set by user software and is the means by which most full-screen oriented programs determine the screen size.
`TIOCSWINSZ` struct winsize *ws
  Set the window size associated with the terminal to be the value in the winsize structure pointed to by ws (see above).
`TIOCCONS` int *on
  If on points to a non-zero integer, redirect kernel console output (see printf(9)) to this terminal. If on points to a zero integer, redirect kernel console output back to the normal console. This is usually used on workstations to redirect kernel messages to a particular window.
`TIOCMSET` int *state
  The integer pointed to by state contains bits that correspond to modem state. Following is a list of defined variables and the modem state they represent:

  TIOCM_LE
    Line Enable.
  TIOCM_DTR
    Data Terminal Ready.
  TIOCM_RTS
    Request To Send.
  TIOCM_ST
    Secondary Transmit.
  TIOCM_SR
    Secondary Receive.
  TIOCM_CTS
    Clear To Send.
  TIOCM_CAR
    Carrier Detect.
  TIOCM_CD
    Carrier Detect (synonym).
  TIOCM_RNG
    Ring Indication.
  TIOCM_RI
    Ring Indication (synonym).
  TIOCM_DSR
    Data Set Ready.

  This call sets the terminal modem state to that represented by state. Not all terminals may support this.

`TIOCMGET` int *state
  Return the current state of the terminal modem lines as represented above in the integer pointed to by state.
`TIOCMBIS` int *state
  The bits in the integer pointed to by state represent modem state as described above; however, the state is OR-ed in with the current state.
`TIOCMBIC` int *state
  The bits in the integer pointed to by state represent modem state as described above; however, each bit which is on in state is cleared in the terminal.
`TIOCGTSTAMP` struct timeval *timeval
  Return the (single) timestamp.
`TIOCSTSTAMP` struct tstamps *tstamps
  Chooses the conditions which will cause the current system time to be immediately copied to the terminal timestamp storage. This is often used to determine exactly the moment at which one or more of these events occurred, though only one can be monitored. Only `TIOCM_CTS` and `TIOCM_CAR` are honoured in tstamps.ts_set and tstamps.ts_clr; these indicate which raising and lowering events on the respective lines should cause a timestamp capture.
`TIOCSFLAGS` int *state
  The bits in the integer pointed to by state contain bits that correspond to serial port state. Following is a list of defined variables and the serial port state they represent:

  TIOCFLAG_SOFTCAR
    Ignore hardware carrier.
  TIOCFLAG_CLOCAL
    Set clocal on open.
  TIOCFLAG_CRTSCTS
    Set crtscts on open.
  TIOCFLAG_MDMBUF
    Set mdmbuf on open.

  This call sets the serial port state to that represented by state. Not all serial ports may support this.

`TIOCGFLAGS` int *state
  Return the current state of the serial port as represented above in the integer pointed to by state.
`TIOCSTAT` void
  Causes the kernel to write a status message to the terminal that displays the current load average, the name of the command in the foreground, its process ID, the symbolic wait channel, the number of user and system seconds used, the percentage of CPU the process is getting, and the resident set size of the process.

# FILES

/dev/tty
  controlling terminal, if any

# SEE ALSO

cu(1), stty(1), tty(1), ioctl(2), pty(4), termios(4), ttys(5), getty(8)

# HISTORY

A console typewriter device /dev/tty and asynchronous communication interfaces /dev/tty[0-5] first appeared in Version 1 AT&T UNIX. The cua support is inspired by similar support in SunOS.

August 16, 2024 | Debian