1.\" Copyright (c) 1991, 1992, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.Dd April 3, 2022 29.Dt TTY 4 30.Os 31.Sh NAME 32.Nm tty 33.Nd general terminal interface 34.Sh SYNOPSIS 35.In sys/ioctl.h 36.Sh DESCRIPTION 37This section describes the interface to the terminal drivers 38in the system. 39.Ss Terminal Special Files 40Each hardware terminal port on the system usually has a terminal special device 41file associated with it in the directory ``/dev/'' (for 42example, ``/dev/tty03''). 43When a user logs into 44the system on one of these hardware terminal ports, the system has already 45opened the associated device and prepared the line for normal interactive 46use (see 47.Xr getty 8 . ) 48There is also a special case of a terminal file that connects not to 49a hardware terminal port, but to another program on the other side. 50These special terminal devices are called 51.Em ptys 52and provide the mechanism necessary to give users the same interface to the 53system when logging in over a network (using 54.Xr telnet 1 55for example). 56Even in these cases the details of how the terminal 57file was opened and set up is already handled by special software 58in the system. 59Thus, users do not normally need to worry about the details of 60how these lines are opened or used. 61Also, these lines are often used 62for dialing out of a system (through an out-calling modem), but again 63the system provides programs that hide the details of accessing 64these terminal special files (see 65.Xr tip 1 ) . 66.Pp 67When an interactive user logs in, the system prepares the line to 68behave in a certain way (called a 69.Em "line discipline" ) , 70the particular details of which is described in 71.Xr stty 1 72at the command level, and in 73.Xr termios 4 74at the programming level. 75A user may be concerned with changing 76settings associated with his particular login terminal and should refer 77to the preceding man pages for the common cases. 78The remainder of this man page is concerned 79with describing details of using and controlling terminal devices 80at a low level, such as that possibly required by a program wishing 81to provide features similar to those provided by the system. 82.Ss Terminal File Operations 83All of the following operations are invoked using the 84.Xr ioctl 2 85system call. 86Refer to that man page for a description of the 87.Em request 88and 89.Em argp 90parameters. 91In addition to the ioctl 92.Em requests 93defined here, the specific line discipline 94in effect will define other 95.Em requests 96specific to it (actually 97.Xr termios 4 98defines them as function calls, not ioctl 99.Em requests . ) 100The following section lists the available ioctl requests. 101The name of the request, a description of its purpose, and the typed 102.Em argp 103parameter (if any) 104are listed. 105For example, the first entry says 106.Pp 107.D1 Em "TIOCSPGRP int *tpgrp" 108.Pp 109and would be called on the terminal associated with 110file descriptor zero by the following code fragment: 111.Bd -literal 112 int pgrp; 113 114 pgrp = getpgrp(); 115 ioctl(0, TIOCSPGRP, &pgrp); 116.Ed 117.Ss Terminal File Request Descriptions 118.Bl -tag -width TIOCGWINSZ 119.It Dv TIOCSETD Fa int *ldisc 120This call is obsolete but left for compatibility. 121Before 122.Fx 8.0 , 123it would change to the new line discipline pointed to by 124.Fa ldisc . 125.It Dv TIOCGETD Fa int *ldisc 126Return the current line discipline in the integer pointed to by 127.Fa ldisc . 128.It Dv TIOCSBRK Fa void 129Set the terminal hardware into BREAK condition. 130.It Dv TIOCCBRK Fa void 131Clear the terminal hardware BREAK condition. 132.It Dv TIOCSDTR Fa void 133Assert data terminal ready (DTR). 134.It Dv TIOCCDTR Fa void 135Clear data terminal ready (DTR). 136.It Dv TIOCGPGRP Fa int *tpgrp 137Return the current process group with which the terminal is associated 138in the integer pointed to by 139.Fa tpgrp . 140This is the underlying call that implements the 141.Xr termios 4 142.Fn tcgetattr 143call. 144.It Dv TIOCSPGRP Fa int *tpgrp 145Associate the terminal with the process group (as an integer) pointed to by 146.Fa tpgrp . 147This is the underlying call that implements the 148.Xr termios 4 149.Fn tcsetattr 150call. 151.It Dv TIOCGETA Fa struct termios *term 152Place the current value of the termios state associated with the 153device in the termios structure pointed to by 154.Fa term . 155This is the underlying call that implements the 156.Xr termios 4 157.Fn tcgetattr 158call. 159.It Dv TIOCSETA Fa struct termios *term 160Set the termios state associated with the device immediately. 161This is the underlying call that implements the 162.Xr termios 4 163.Fn tcsetattr 164call with the 165.Dv TCSANOW 166option. 167.It Dv TIOCSETAW Fa struct termios *term 168First wait for any output to complete, then set the termios state 169associated with the device. 170This is the underlying call that implements the 171.Xr termios 4 172.Fn tcsetattr 173call with the 174.Dv TCSADRAIN 175option. 176.It Dv TIOCSETAF Fa struct termios *term 177First wait for any output to complete, clear any pending input, 178then set the termios state associated with the device. 179This is the underlying call that implements the 180.Xr termios 4 181.Fn tcsetattr 182call with the 183.Dv TCSAFLUSH 184option. 185.It Dv TIOCOUTQ Fa int *num 186Place the current number of characters in the output queue in the 187integer pointed to by 188.Fa num . 189.It Dv TIOCSTI Fa char *cp 190Simulate typed input. 191Pretend as if the terminal received the character pointed to by 192.Fa cp . 193.It Dv TIOCNOTTY Fa void 194In the past, when a process that did not have a controlling terminal (see 195.Em The Controlling Terminal 196in 197.Xr termios 4 ) 198first opened a terminal device, it acquired that terminal as its 199controlling terminal. 200For some programs this was a hazard as they 201did not want a controlling terminal in the first place, and this 202provides a mechanism to disassociate the controlling terminal from 203the calling process. 204It 205.Em must 206be called by opening the file 207.Pa /dev/tty 208and calling 209.Dv TIOCNOTTY 210on that file descriptor. 211.Pp 212The current system does not allocate a controlling terminal to 213a process on an 214.Fn open 215call: there is a specific ioctl called 216.Dv TIOCSCTTY 217to make a terminal the controlling 218terminal. 219In addition, a program can 220.Fn fork 221and call the 222.Fn setsid 223system call which will place the process into its own session - which 224has the effect of disassociating it from the controlling terminal. 225This is the new and preferred method for programs to lose their controlling 226terminal. 227.Pp 228However, environmental restrictions may prohibit the process from being able to 229.Fn fork 230and call the 231.Fn setsid 232system call to disassociate it from the controlling terminal. 233In this case, it must use 234.Dv TIOCNOTTY . 235.It Dv TIOCSTOP Fa void 236Stop output on the terminal (like typing ^S at the keyboard). 237.It Dv TIOCSTART Fa void 238Start output on the terminal (like typing ^Q at the keyboard). 239.It Dv TIOCSCTTY Fa void 240Make the terminal the controlling terminal for the process (the process 241must not currently have a controlling terminal). 242.It Dv TIOCDRAIN Fa void 243Wait until all output is drained, or until the drain wait timeout expires. 244.It Dv TIOCGDRAINWAIT Fa int *timeout 245Return the current drain wait timeout in seconds. 246.It Dv TIOCSDRAINWAIT Fa int *timeout 247Set the drain wait timeout in seconds. 248A value of zero disables timeouts. 249The default drain wait timeout is controlled by the tunable 250.Xr sysctl 8 251OID 252.Va kern.tty_drainwait . 253.It Dv TIOCEXCL Fa void 254Set exclusive use on the terminal. 255No further opens are permitted except by root. 256Of course, this means that programs that are run by 257root (or setuid) will not obey the exclusive setting - which limits 258the usefulness of this feature. 259.It Dv TIOCNXCL Fa void 260Clear exclusive use of the terminal. 261Further opens are permitted. 262.It Dv TIOCFLUSH Fa int *what 263If the value of the int pointed to by 264.Fa what 265contains the 266.Dv FREAD 267bit as defined in 268.In sys/file.h , 269then all characters in the input queue are cleared. 270If it contains the 271.Dv FWRITE 272bit, then all characters in the output queue are cleared. 273If the value of the integer is zero, then it behaves as if both the 274.Dv FREAD 275and 276.Dv FWRITE 277bits were set (i.e., clears both queues). 278.It Dv TIOCGWINSZ Fa struct winsize *ws 279Put the window size information associated with the terminal in the 280.Va winsize 281structure pointed to by 282.Fa ws . 283The window size structure contains the number of rows and columns (and pixels 284if appropriate) of the devices attached to the terminal. 285It is set by user software 286and is the means by which most full\&-screen oriented programs determine the 287screen size. 288The 289.Va winsize 290structure is defined in 291.In sys/ioctl.h . 292.It Dv TIOCSWINSZ Fa struct winsize *ws 293Set the window size associated with the terminal to be the value in 294the 295.Va winsize 296structure pointed to by 297.Fa ws 298(see above). 299.It Dv TIOCCONS Fa int *on 300If 301.Fa on 302points to a non-zero integer, redirect kernel console output (kernel printf's) 303to this terminal. 304If 305.Fa on 306points to a zero integer, redirect kernel console output back to the normal 307console. 308This is usually used on workstations to redirect kernel messages 309to a particular window. 310.It Dv TIOCMSET Fa int *state 311The integer pointed to by 312.Fa state 313contains bits that correspond to modem state. 314Following is a list of defined variables and the modem state they represent: 315.Pp 316.Bl -tag -width TIOCMXCTS -compact 317.It TIOCM_LE 318Line Enable. 319.It TIOCM_DTR 320Data Terminal Ready. 321.It TIOCM_RTS 322Request To Send. 323.It TIOCM_ST 324Secondary Transmit. 325.It TIOCM_SR 326Secondary Receive. 327.It TIOCM_CTS 328Clear To Send. 329.It TIOCM_CAR 330Carrier Detect. 331.It TIOCM_CD 332Carrier Detect (synonym). 333.It TIOCM_RNG 334Ring Indication. 335.It TIOCM_RI 336Ring Indication (synonym). 337.It TIOCM_DSR 338Data Set Ready. 339.El 340.Pp 341This call sets the terminal modem state to that represented by 342.Fa state . 343Not all terminals may support this. 344.It Dv TIOCMGET Fa int *state 345Return the current state of the terminal modem lines as represented 346above in the integer pointed to by 347.Fa state . 348.It Dv TIOCMBIS Fa int *state 349The bits in the integer pointed to by 350.Fa state 351represent modem state as described above, however the state is OR-ed 352in with the current state. 353.It Dv TIOCMBIC Fa int *state 354The bits in the integer pointed to by 355.Fa state 356represent modem state as described above, however each bit which is on 357in 358.Fa state 359is cleared in the terminal. 360.El 361.Sh IMPLEMENTATION NOTES 362The total number of input and output bytes 363through all terminal devices 364are available via the 365.Va kern.tty_nin 366and 367.Va kern.tty_nout 368read-only 369.Xr sysctl 8 370variables. 371.Sh SEE ALSO 372.Xr stty 1 , 373.Xr ioctl 2 , 374.Xr ng_tty 4 , 375.Xr pty 4 , 376.Xr termios 4 , 377.Xr getty 8 378.Sh HISTORY 379A console typewriter device 380.Pa /dev/tty 381and asynchronous communication interfaces 382.Pa /dev/tty[0-5] 383first appeared in 384.At v1 . 385