Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Terminal

A terminal is a character device with a line discipline and a set of line settings. A program reads and writes bytes, and the line discipline turns them into lines, signals and echoes. A terminal has a carrier, such as a serial port, but the carrier is separate: Serial port describes it.

The settings

The state of the line discipline is struct __k_termios:

FieldMeaning
c_iflagInput flags: parity, line breaks, flow control
c_oflagOutput flags: newline and tab processing
c_cflagControl flags: data bits, stop bits, parity, CLOCAL, CRTSCTS
c_lflagLocal flags: ICANON, ECHO, ISIG, IEXTEN
c_ccThe 20 control characters
c_ispeed, c_ospeedSpeeds in bits per second

An output speed of 0 hangs up the line, and an input speed of 0 follows the output speed. The control characters have fixed indices in c_cc. VINTR is at 0, VQUIT is at 1, VERASE is at 2 and VSUSP is at 9 for the signals and the editing. VMIN is at 14 and VTIME is at 15 for a read in noncanonical mode. __k_POSIX_VDISABLE turns one off.

The requests

RequestArgumentMeaning
__k_TCGETS__k_termiosRead the settings
__k_TCSETS__k_termiosSet them now
__k_TCSETSW__k_termiosSet them after the output drains
__k_TCSETSF__k_termiosSet them after the output drains and the input is flushed
__k_TIOCGWINSZ__k_winsizeRead the size of the window
__k_TIOCSWINSZ__k_winsizeSet the size of the window
__k_TIOCGPGRP__k_pid_tRead the foreground process group
__k_TIOCSPGRP__k_pid_tSet the foreground process group
__k_TIOCGSID__k_pid_tRead the session id
__k_TIOCSCTTY__k_int32_tMake the terminal the controlling terminal of the caller
__k_TIOCNOTTYNoneLeave the controlling terminal
__k_TCFLSH__k_int32_tFlush the input, the output or both
__k_TCXONC__k_int32_tStop or start the input or the output
__k_TCDRAINNoneWait until the output drains
__k_TCSENDBREAK__k_int32_tSend a break for that many milliseconds
__k_TIOCEXCLNoneRefuse later opens by other files
__k_TIOCNXCLNoneAllow them again
__k_TIOCOUTQ__k_int32_tRead the bytes still to send

__k_TIOCSCTTY needs a session leader. A nonzero argument takes the terminal from another session, and that needs CAP_SYS_ADMIN. The flush argument is one of __k_TCIFLUSH, __k_TCOFLUSH and __k_TCIOFLUSH, and the flow argument is one of __k_TCOOFF, __k_TCOON, __k_TCIOFF and __k_TCION.

Limits

  • The 20 control characters are fixed by __k_NCCS.
  • On a terminal whose carrier is a serial port, TCSETS with a nonzero c_ospeed sets the line of the port. It sets the speed, the data bits, the stop bits, the parity and CRTSCTS. A zero c_ospeed hangs up and drops DTR.
  • A request that the terminal does not know fails with ENOTTY. A break on a terminal whose carrier has no break succeeds and does nothing.

Source