1.\" $OpenBSD: tty.4,v 1.53 2019/09/07 10:28:27 schwarze Exp $ 2.\" $NetBSD: tty.4,v 1.4 1996/03/19 04:26:01 paulus Exp $ 3.\" 4.\" Copyright (c) 1991, 1992, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)tty.4 8.3 (Berkeley) 4/19/94 32.\" 33.Dd $Mdocdate: September 7 2019 $ 34.Dt TTY 4 35.Os 36.Sh NAME 37.Nm tty , 38.Nm cua 39.Nd general terminal interface 40.Sh SYNOPSIS 41.In sys/ioctl.h 42.Sh DESCRIPTION 43This section describes the interface to the terminal drivers 44in the system. 45.Ss Terminal Special Files 46Each hardware terminal port (such as a serial port) on the system usually has a 47terminal special device file associated with it in the directory 48.Pa /dev/ 49(for 50example, 51.Pa /dev/tty03 ) . 52When a user logs into 53the system on one of these hardware terminal ports, the system has already 54opened the associated device and prepared the line for normal interactive 55use (see 56.Xr getty 8 ) . 57There is also a special case of a terminal file that connects not to 58a hardware terminal port, but to another program on the other side. 59These special terminal devices are called 60.Em ptys 61and provide the mechanism necessary to give users the same interface to the 62system when logging in over a network (using 63.Xr ssh 1 64or 65.Xr telnet 1 66for example). 67Even in these cases the details of how the terminal 68file was opened and set up is already handled by special software 69in the system. 70Thus, users do not normally need to worry about the details of 71how these lines are opened or used. 72.Pp 73For hardware terminal ports, dial-out is supported through matching 74device nodes called calling units. 75For instance, the terminal called 76.Pa /dev/tty03 77would have a matching calling unit called 78.Pa /dev/cua03 . 79These two devices are normally differentiated by creating the calling 80unit device node with a minor number 128 greater than the dial-in 81device node. 82Whereas the dial-in device (the 83.Em tty ) 84normally 85requires a hardware signal to indicate to the system that it is active, 86the dial-out device (the 87.Em cua ) 88does not, and hence can communicate unimpeded 89with a device such as a modem, or with another system over a serial link. 90This means that a process like 91.Xr getty 8 92will wait on a dial-in device until a connection is established. 93Meanwhile, a dial-out connection can be established on the dial-out 94device (for the very same hardware terminal port) without disturbing 95anything else on the system. 96The 97.Xr getty 8 98process does not even notice that anything is happening on the terminal 99port. 100If a connecting call comes in after the dial-out connection has finished, the 101.Xr getty 8 102process will deal with it properly, without having noticed the 103intervening dial-out action. 104For more information on dial-out, see 105.Xr cu 1 . 106.Pp 107When an interactive user logs in, the system prepares the line to 108behave in a certain way (called a 109.Em "line discipline" ) , 110the particular details of which are described in 111.Xr stty 1 112at the command level, and in 113.Xr termios 4 114at the programming level. 115A user may be concerned with changing settings associated with his particular 116login terminal and should refer to the preceding man pages for the common 117cases. 118The remainder of this man page is concerned with describing details of using 119and controlling terminal devices at a low level, such as that possibly 120required by a program wishing to provide features similar to those provided 121by the system. 122.Ss Line disciplines 123A terminal file is used like any other file in the system in that 124it can be opened, read, and written to using standard system 125calls. 126For each existing terminal file, there is a software processing module 127called a 128.Em "line discipline" 129associated with it. 130The 131.Em "line discipline" 132essentially glues the low level device driver code with the high 133level generic interface routines (such as 134.Xr read 2 135and 136.Xr write 2 ) , 137and is responsible for implementing the semantics associated 138with the device. 139When a terminal file is first opened by a program, the default 140.Em "line discipline" 141called the 142.Dv termios 143line discipline is associated with the file. 144This is the primary line discipline that is used in most cases and provides 145the semantics that users normally associate with a terminal. 146When the 147.Dv termios 148line discipline is in effect, the terminal file behaves and is 149operated according to the rules described in 150.Xr termios 4 . 151Please refer to that man page for a full description of the terminal 152semantics. 153The operations described here 154generally represent features common 155across all 156.Em "line disciplines" , 157although some of these calls may not 158make sense in conjunction with a line discipline other than 159.Dv termios , 160and some may not be supported by the underlying 161hardware (or lack thereof, as in the case of ptys). 162.Ss Terminal File Operations 163All of the following operations are invoked using the 164.Xr ioctl 2 165system call. 166Refer to that man page for a description of the 167.Em request 168and 169.Em argp 170parameters. 171In addition to the ioctl 172.Em requests 173defined here, the specific line discipline 174in effect will define other 175.Em requests 176specific to it (actually 177.Xr termios 4 178defines them as function calls, not ioctl 179.Em requests ) . 180The following section lists the available ioctl requests. 181The name of the request, a description of its purpose, and the typed 182.Em argp 183parameter (if any) 184are listed. 185For example, the first entry says 186.Pp 187.D1 Em "TIOCSETD int *ldisc" 188.Pp 189and would be called on the terminal associated with 190file descriptor zero by the following code fragment: 191.Bd -literal 192 int ldisc; 193 194 ldisc = TTYDISC; 195 ioctl(0, TIOCSETD, &ldisc); 196.Ed 197.Ss Terminal File Request Descriptions 198.Bl -tag -width TIOCGWINSZ 199.It Dv TIOCSETD Fa int *ldisc 200Change to the new line discipline pointed to by 201.Fa ldisc . 202The available line disciplines currently available are: 203.Pp 204.Bl -tag -width TIOCGWINSZ -compact 205.It TTYDISC 206Termios interactive line discipline. 207.It PPPDISC 208Point-to-Point Protocol line discipline. 209.It NMEADISC 210NMEA 0183 line discipline. 211.It MSTSDISC 212Meinberg Standard Time String line discipline. 213.El 214.It Dv TIOCGETD Fa int *ldisc 215Return the current line discipline in the integer pointed to by 216.Fa ldisc . 217.It Dv TIOCSBRK Fa void 218Set the terminal hardware into BREAK condition. 219.It Dv TIOCCBRK Fa void 220Clear the terminal hardware BREAK condition. 221.It Dv TIOCSDTR Fa void 222Assert data terminal ready (DTR). 223.It Dv TIOCCDTR Fa void 224Clear data terminal ready (DTR). 225.It Dv TIOCGPGRP Fa int *tpgrp 226Return the current process group the terminal is associated 227with in the integer pointed to by 228.Fa tpgrp . 229This is the underlying call that implements the 230.Xr tcgetpgrp 3 231call. 232.It Dv TIOCSPGRP Fa int *tpgrp 233Associate the terminal with the process group (as an integer) pointed to by 234.Fa tpgrp . 235This is the underlying call that implements the 236.Xr tcsetpgrp 3 237call. 238.It Dv TIOCGETA Fa struct termios *term 239Place the current value of the termios state associated with the 240device in the termios structure pointed to by 241.Fa term . 242This is the underlying call that implements the 243.Xr tcgetattr 3 244call. 245.It Dv TIOCSETA Fa struct termios *term 246Set the termios state associated with the device immediately. 247This is the underlying call that implements the 248.Xr tcsetattr 3 249call with the 250.Dv TCSANOW 251option. 252.It Dv TIOCSETAW Fa struct termios *term 253First wait for any output to complete, then set the termios state 254associated with the device. 255This is the underlying call that implements the 256.Xr tcsetattr 3 257call with the 258.Dv TCSADRAIN 259option. 260.It Dv TIOCSETAF Fa struct termios *term 261First wait for any output to complete, clear any pending input, 262then set the termios state associated with the device. 263This is the underlying call that implements the 264.Xr tcsetattr 3 265call with the 266.Dv TCSAFLUSH 267option. 268.It Dv TIOCOUTQ Fa int *num 269Place the current number of characters in the output queue in the 270integer pointed to by 271.Fa num . 272.It Dv TIOCNOTTY Fa void 273This call is obsolete but left for compatibility. 274In the past, when a process that didn't have a controlling terminal 275.Po 276see 277.Em The Controlling Terminal 278in 279.Xr termios 4 280.Pc 281first opened a terminal device, it acquired that terminal as its 282controlling terminal. 283For some programs this was a hazard as they didn't want a controlling 284terminal in the first place, and this provided a mechanism to disassociate 285the controlling terminal from the calling process. 286It 287.Em must 288be called by opening the file 289.Pa /dev/tty 290and calling 291.Dv TIOCNOTTY 292on that file descriptor. 293.Pp 294The current system does not allocate a controlling terminal to 295a process on an 296.Xr open 2 297call: there is a specific ioctl called 298.Dv TIOCSCTTY 299to make a terminal the controlling 300terminal. 301In addition, a program can 302.Xr fork 2 303and call the 304.Xr setsid 2 305system call which will place the process into its own session - which 306has the effect of disassociating it from the controlling terminal. 307This is the new and preferred method for programs to lose their controlling 308terminal. 309.It Dv TIOCSETVERAUTH Fa int *secs 310Indicate that the current user has successfully authenticated to this session. 311Future authentication checks may then be bypassed by performing a 312.Dv TIOCCHKVERAUTH 313check. 314The verified authentication status will expire after 315.Fa secs 316seconds. 317Only root may perform this operation. 318.It Dv TIOCCLRVERAUTH Fa void 319Clear any verified auth status associated with this session. 320.It Dv TIOCCHKVERAUTH Fa void 321Check the verified auth status of this session. 322The calling process must have the same real user ID and 323parent process as the process which called 324.Dv TIOCSETVERAUTH . 325A zero return indicates success. 326.It Dv TIOCSTOP Fa void 327Stop output on the terminal (like typing ^S at the keyboard). 328.It Dv TIOCSTART Fa void 329Start output on the terminal (like typing ^Q at the keyboard). 330.It Dv TIOCSCTTY Fa void 331Make the terminal the controlling terminal for the process (the process 332must not currently have a controlling terminal). 333.It Dv TIOCDRAIN Fa void 334Wait until all output is drained. 335.It Dv TIOCEXCL Fa void 336Set exclusive use on the terminal. 337No further opens are permitted except by root. 338Of course, this means that programs that are run by root (or setuid) 339will not obey the exclusive setting - which limits the usefulness 340of this feature. 341.It Dv TIOCNXCL Fa void 342Clear exclusive use of the terminal. 343Further opens are permitted. 344.It Dv TIOCFLUSH Fa int *what 345If the value of the int pointed to by 346.Fa what 347contains the 348.Dv FREAD 349bit as defined in 350.In sys/fcntl.h , 351then all characters in the input queue are cleared. 352If it contains the 353.Dv FWRITE 354bit, then all characters in the output queue are cleared. 355If the value of the integer is zero, then it behaves as if both the 356.Dv FREAD 357and 358.Dv FWRITE 359bits were set (i.e., clears both queues). 360.It Dv TIOCGWINSZ Fa struct winsize *ws 361Put the window size information associated with the terminal in the 362.Va winsize 363structure pointed to by 364.Fa ws . 365The window size structure contains the number of rows and columns (and pixels 366if appropriate) of the devices attached to the terminal. 367It is set by user software and is the means by which most full\&-screen 368oriented programs determine the screen size. 369.It Dv TIOCSWINSZ Fa struct winsize *ws 370Set the window size associated with the terminal to be the value in 371the 372.Va winsize 373structure pointed to by 374.Fa ws 375(see above). 376.It Dv TIOCCONS Fa int *on 377If 378.Fa on 379points to a non-zero integer, redirect kernel console output 380.Po 381see 382.Xr printf 9 383.Pc 384to this terminal. 385If 386.Fa on 387points to a zero integer, redirect kernel console output back to the normal 388console. 389This is usually used on workstations to redirect kernel messages 390to a particular window. 391.It Dv TIOCMSET Fa int *state 392The integer pointed to by 393.Fa state 394contains bits that correspond to modem state. 395Following is a list of defined variables and the modem state they represent: 396.Pp 397.Bl -tag -width TIOCMXCTS -compact 398.It TIOCM_LE 399Line Enable. 400.It TIOCM_DTR 401Data Terminal Ready. 402.It TIOCM_RTS 403Request To Send. 404.It TIOCM_ST 405Secondary Transmit. 406.It TIOCM_SR 407Secondary Receive. 408.It TIOCM_CTS 409Clear To Send. 410.It TIOCM_CAR 411Carrier Detect. 412.It TIOCM_CD 413Carrier Detect (synonym). 414.It TIOCM_RNG 415Ring Indication. 416.It TIOCM_RI 417Ring Indication (synonym). 418.It TIOCM_DSR 419Data Set Ready. 420.El 421.Pp 422This call sets the terminal modem state to that represented by 423.Fa state . 424Not all terminals may support this. 425.It Dv TIOCMGET Fa int *state 426Return the current state of the terminal modem lines as represented 427above in the integer pointed to by 428.Fa state . 429.It Dv TIOCMBIS Fa int *state 430The bits in the integer pointed to by 431.Fa state 432represent modem state as described above; however, the state is OR-ed 433in with the current state. 434.It Dv TIOCMBIC Fa int *state 435The bits in the integer pointed to by 436.Fa state 437represent modem state as described above; however, each bit which is on 438in 439.Fa state 440is cleared in the terminal. 441.It Dv TIOCGTSTAMP Fa struct timeval *timeval 442Return the (single) timestamp. 443.It Dv TIOCSTSTAMP Fa struct tstamps *tstamps 444Chooses the conditions which will cause the current system time to be 445immediately copied to the terminal timestamp storage. 446This is often used to determine exactly the moment at which one or 447more of these events occurred, though only one can be monitored. 448Only 449.Dv TIOCM_CTS 450and 451.Dv TIOCM_CAR 452are honoured in 453.Va tstamps.ts_set 454and 455.Va tstamps.ts_clr ; 456these indicate which raising and lowering events on the respective lines 457should cause a timestamp capture. 458.It Dv TIOCSFLAGS Fa int *state 459The bits in the integer pointed to by 460.Fa state 461contain bits that correspond to serial port state. 462Following is a list of defined variables and the serial port state they 463represent: 464.Pp 465.Bl -tag -width TIOCFLAG_SOFTCAR -compact 466.It TIOCFLAG_SOFTCAR 467Ignore hardware carrier. 468.It TIOCFLAG_CLOCAL 469Set clocal on open. 470.It TIOCFLAG_CRTSCTS 471Set crtscts on open. 472.It TIOCFLAG_MDMBUF 473Set mdmbuf on open. 474.El 475.Pp 476This call sets the serial port state to that represented by 477.Fa state . 478Not all serial ports may support this. 479.It Dv TIOCGFLAGS Fa int *state 480Return the current state of the serial port as represented 481above in the integer pointed to by 482.Fa state . 483.It Dv TIOCSTAT Fa void 484Causes the kernel to write a status message to the terminal that displays the 485current load average, 486the name of the command in the foreground, 487its process ID, 488the symbolic wait channel, 489the number of user and system seconds used, 490the percentage of CPU the process is getting, 491and the resident set size of the process. 492.El 493.Sh FILES 494.Bl -tag -width /dev/tty -compact 495.It Pa /dev/tty 496controlling terminal, if any 497.El 498.Sh SEE ALSO 499.Xr cu 1 , 500.Xr stty 1 , 501.Xr tty 1 , 502.Xr ioctl 2 , 503.Xr pty 4 , 504.Xr termios 4 , 505.Xr ttys 5 , 506.Xr getty 8 507.Sh HISTORY 508A console typewriter device 509.Pa /dev/tty 510and asynchronous communication interfaces 511.Pa /dev/tty[0-5] 512first appeared in 513.At v1 . 514The cua support is inspired by similar support in SunOS. 515