TTY(4)
β¨ TTY(4) β general terminal interface β¨
okay so bestie, this is the manual page for tty β the general terminal interface. It's giving kernel interfaces, it's giving serial ports, it's giving your terminal's whole personality. Let's get into it. π
π NAME π
tty β 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 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 telnet(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. Also, these lines are often used for dialing out of a system (through an out-calling modem), but again the system provides programs that hide the details of accessing these terminal special files (see tip(1)).
When an interactive user logs in, the system prepares the line to behave in a certain way (called a line discipline), the particular details of which is described in stty(1) at the command level, and in termios(4) at the programming level. A user may be concerned with changing settings associated with his particular login terminal and should 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.
π¦ 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
TIOCSPGRP int *tpgrp
and would be called on the terminal associated with file descriptor zero by the following code fragment:
int pgrp;
pgrp = getpgrp();
ioctl(0, TIOCSPGRP, &pgrp);
π Terminal File Request Descriptions π
Now the main event, bestie. Every request, every vibe. No cap. β¨
TIOCSETD int *ldiscβ This call is obsolete but left for compatibility. Before FreeBSD 8.0, it would change to the new line discipline pointed to byldisc.TIOCGETD int *ldiscβ Return the current line discipline in the integer pointed to byldisc.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 with which the terminal is associated in the integer pointed to bytpgrp. This is the underlying call that implements thetermios(4)tcgetattr()call.TIOCSPGRP int *tpgrpβ Associate the terminal with the process group (as an integer) pointed to bytpgrp. This is the underlying call that implements thetermios(4)tcsetattr()call.TIOCGETA struct termios *termβ Place the current value of thetermiosstate associated with the device in thetermiosstructure pointed to byterm. This is the underlying call that implements thetermios(4)tcgetattr()call.TIOCSETA struct termios *termβ Set thetermiosstate associated with the device immediately. This is the underlying call that implements thetermios(4)tcsetattr()call with theTCSANOWoption.TIOCSETAW struct termios *termβ First wait for any output to complete, then set thetermiosstate associated with the device. This is the underlying call that implements thetermios(4)tcsetattr()call with theTCSADRAINoption.TIOCSETAF struct termios *termβ First wait for any output to complete, clear any pending input, then set thetermiosstate associated with the device. This is the underlying call that implements thetermios(4)tcsetattr()call with theTCSAFLUSHoption.TIOCOUTQ int *numβ Place the current number of characters in the output queue in the integer pointed to bynum.TIOCSTI char *cpβ Simulate typed input. Pretend as if the terminal received the character pointed to bycp.TIOCNOTTY voidβ In the past, when a process that did not have a controlling terminal (see The Controlling Terminal intermios(4)) first opened a terminal device, it acquired that terminal as its controlling terminal. For some programs this was a hazard as they did not want a controlling terminal in the first place, and this provides a mechanism to disassociate the controlling terminal from the calling process. It must be called by opening the file/dev/ttyand callingTIOCNOTTYon that file descriptor.
The current system does not allocate a controlling terminal to a process on an open() call: there is a specific ioctl called TIOCSCTTY to make a terminal the controlling terminal. In addition, a program can fork() and call the setsid() 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.
However, environmental restrictions may prohibit the process from being able to fork() and call the setsid() system call to disassociate it from the controlling terminal. In this case, it must use TIOCNOTTY.
TIOCSTOP voidβ Stop output on the terminal (like typing^Sat the keyboard).TIOCSTART voidβ Start output on the terminal (like typing^Qat 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, or until the drain wait timeout expires.TIOCGDRAINWAIT int *timeoutβ Return the current drain wait timeout in seconds.TIOCSDRAINWAIT int *timeoutβ Set the drain wait timeout in seconds. A value of zero disables timeouts. The default drain wait timeout is controlled by the tunablesysctl(8)OIDkern.tty_drainwait.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 theintpointed to bywhatcontains theFREADbit as defined in<sys/file.h>, then all characters in the input queue are cleared. If it contains theFWRITEbit, then all characters in the output queue are cleared. If the value of the integer is zero, then it behaves as if both theFREADandFWRITEbits were set (i.e., clears both queues).TIOCGWINSZ struct winsize *wsβ Put the window size information associated with the terminal in thewinsizestructure pointed to byws. 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. Thewinsizestructure is defined in<sys/ioctl.h>.TIOCSWINSZ struct winsize *wsβ Set the window size associated with the terminal to be the value in thewinsizestructure pointed to byws(see above).TIOCCONS int *onβ Ifonpoints to a non-zero integer, redirect kernel console output (kernelprintf's) to this terminal. Ifonpoints 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 bystatecontains 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 bystate.TIOCMBIS int *stateβ The bits in the integer pointed to bystaterepresent modem state as described above, however the state is OR-ed in with the current state.TIOCMBICint *stateβ okay sostatepoints at an integer, and the bits set in it describe modem state exactly like we just talked about β¨. But here's the twist bestie: every bit that's on instategets cleared on the terminal. So like, you're not setting anything, you're un-setting it π . Iconic little reverse card behavior, fr fr.
π«Ά IMPLEMENTATION NOTES π«Ά
So if you ever wanna know how many bytes have gone in and out across every terminal device combined, the vibes are literally just sitting there in two read-only sysctl(8) variables:
kern.tty_ninβ total input bytes through all terminal devices π₯kern.tty_noutβ total output bytes through all terminal devices π€
That's it, that's the whole note. Read-only, so don't try to write to them, they will not be taking suggestions π.
π SEE ALSO π
Go be nosy, bestie, these are the besties-in-arms of this page π:
And that's the whole page, you made it to the end ππ¦ This lil' beauty ships from Debian, dated November 27, 2019, and lives its life as TTY(4). Go forth and ioctl, you absolute legend ππ
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1991, 1992, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms