1.\" $OpenBSD: tty.4,v 1.40 2014/01/21 03:15:46 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: January 21 2014 $ 34.Dt TTY 4 35.Os 36.Sh NAME 37.Nm tty , 38.Nm cua 39.Nd general terminal interface 40.Sh SYNOPSIS 41.Fd #include <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 on the system usually has a terminal special device 47file 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. 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 tip 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 are listed in 203.In sys/ttycom.h 204and currently are: 205.Pp 206.Bl -tag -width TIOCGWINSZ -compact 207.It TTYDISC 208Termios interactive line discipline. 209.It TABLDISC 210Tablet line discipline. 211.It SLIPDISC 212Serial IP line discipline. 213.It PPPDISC 214Point-to-Point Protocol line discipline. 215.It STRIPDISC 216Starmode Radio IP line discipline. 217.It NMEADISC 218NMEA 0183 line discipline. 219.It MSTSDISC 220Meinberg Standard Time String line discipline. 221.El 222.It Dv TIOCGETD Fa int *ldisc 223Return the current line discipline in the integer pointed to by 224.Fa ldisc . 225.It Dv TIOCSBRK Fa void 226Set the terminal hardware into BREAK condition. 227.It Dv TIOCCBRK Fa void 228Clear the terminal hardware BREAK condition. 229.It Dv TIOCSDTR Fa void 230Assert data terminal ready (DTR). 231.It Dv TIOCCDTR Fa void 232Clear data terminal ready (DTR). 233.It Dv TIOCGPGRP Fa int *tpgrp 234Return the current process group the terminal is associated 235with in the integer pointed to by 236.Fa tpgrp . 237This is the underlying call that implements the 238.Xr termios 4 239.Fn tcgetpgrp 240call. 241.It Dv TIOCSPGRP Fa int *tpgrp 242Associate the terminal with the process group (as an integer) pointed to by 243.Fa tpgrp . 244This is the underlying call that implements the 245.Xr termios 4 246.Fn tcsetpgrp 247call. 248.It Dv TIOCGETA Fa struct termios *term 249Place the current value of the termios state associated with the 250device in the termios structure pointed to by 251.Fa term . 252This is the underlying call that implements the 253.Xr termios 4 254.Fn tcgetattr 255call. 256.It Dv TIOCSETA Fa struct termios *term 257Set the termios state associated with the device immediately. 258This is the underlying call that implements the 259.Xr termios 4 260.Fn tcsetattr 261call with the 262.Dv TCSANOW 263option. 264.It Dv TIOCSETAW Fa struct termios *term 265First wait for any output to complete, then set the termios state 266associated with the device. 267This is the underlying call that implements the 268.Xr termios 4 269.Fn tcsetattr 270call with the 271.Dv TCSADRAIN 272option. 273.It Dv TIOCSETAF Fa struct termios *term 274First wait for any output to complete, clear any pending input, 275then set the termios state associated with the device. 276This is the underlying call that implements the 277.Xr termios 4 278.Fn tcsetattr 279call with the 280.Dv TCSAFLUSH 281option. 282.It Dv TIOCOUTQ Fa int *num 283Place the current number of characters in the output queue in the 284integer pointed to by 285.Fa num . 286.It Dv TIOCSTI Fa char *cp 287Simulate typed input. 288Pretend as if the terminal received the character pointed to by 289.Fa cp . 290.It Dv TIOCNOTTY Fa void 291This call is obsolete but left for compatibility. 292In the past, when a process that didn't have a controlling terminal 293(see 294.Em The Controlling Terminal 295in 296.Xr termios 4 ) 297first opened a terminal device, it acquired that terminal as its 298controlling terminal. 299For some programs this was a hazard as they didn't want a controlling 300terminal in the first place, and this provided a mechanism to disassociate 301the controlling terminal from the calling process. 302It 303.Em must 304be called by opening the file 305.Pa /dev/tty 306and calling 307.Dv TIOCNOTTY 308on that file descriptor. 309.Pp 310The current system does not allocate a controlling terminal to 311a process on an 312.Fn open 313call: there is a specific ioctl called 314.Dv TIOCSCTTY 315to make a terminal the controlling 316terminal. 317In addition, a program can 318.Fn fork 319and call the 320.Fn setsid 321system call which will place the process into its own session - which 322has the effect of disassociating it from the controlling terminal. 323This is the new and preferred method for programs to lose their controlling 324terminal. 325.It Dv TIOCSTOP Fa void 326Stop output on the terminal (like typing ^S at the keyboard). 327.It Dv TIOCSTART Fa void 328Start output on the terminal (like typing ^Q at the keyboard). 329.It Dv TIOCSCTTY Fa void 330Make the terminal the controlling terminal for the process (the process 331must not currently have a controlling terminal). 332.It Dv TIOCDRAIN Fa void 333Wait until all output is drained. 334.It Dv TIOCEXCL Fa void 335Set exclusive use on the terminal. 336No further opens are permitted except by root. 337Of course, this means that programs that are run by root (or setuid) 338will not obey the exclusive setting - which limits the usefulness 339of this feature. 340.It Dv TIOCNXCL Fa void 341Clear exclusive use of the terminal. 342Further opens are permitted. 343.It Dv TIOCFLUSH Fa int *what 344If the value of the int pointed to by 345.Fa what 346contains the 347.Dv FREAD 348bit as defined in 349.In sys/fcntl.h , 350then all characters in the input queue are cleared. 351If it contains the 352.Dv FWRITE 353bit, then all characters in the output queue are cleared. 354If the value of the integer is zero, then it behaves as if both the 355.Dv FREAD 356and 357.Dv FWRITE 358bits were set (i.e., clears both queues). 359.It Dv TIOCGWINSZ Fa struct winsize *ws 360Put the window size information associated with the terminal in the 361.Va winsize 362structure pointed to by 363.Fa ws . 364The window size structure contains the number of rows and columns (and pixels 365if appropriate) of the devices attached to the terminal. 366It is set by user software and is the means by which most full\&-screen 367oriented programs determine the screen size. 368The 369.Va winsize 370structure is defined in 371.In sys/ioctl.h . 372.It Dv TIOCSWINSZ Fa struct winsize *ws 373Set the window size associated with the terminal to be the value in 374the 375.Va winsize 376structure pointed to by 377.Fa ws 378(see above). 379.It Dv TIOCCONS Fa int *on 380If 381.Fa on 382points to a non-zero integer, redirect kernel console output 383.Po 384kernel 385.Fn printf Ns s 386.Pc 387to this terminal. 388If 389.Fa on 390points to a zero integer, redirect kernel console output back to the normal 391console. 392This is usually used on workstations to redirect kernel messages 393to a particular window. 394.It Dv TIOCMSET Fa int *state 395The integer pointed to by 396.Fa state 397contains bits that correspond to modem state. 398Following is a list of defined variables and the modem state they represent: 399.Pp 400.Bl -tag -width TIOCMXCTS -compact 401.It TIOCM_LE 402Line Enable. 403.It TIOCM_DTR 404Data Terminal Ready. 405.It TIOCM_RTS 406Request To Send. 407.It TIOCM_ST 408Secondary Transmit. 409.It TIOCM_SR 410Secondary Receive. 411.It TIOCM_CTS 412Clear To Send. 413.It TIOCM_CAR 414Carrier Detect. 415.It TIOCM_CD 416Carrier Detect (synonym). 417.It TIOCM_RNG 418Ring Indication. 419.It TIOCM_RI 420Ring Indication (synonym). 421.It TIOCM_DSR 422Data Set Ready. 423.El 424.Pp 425This call sets the terminal modem state to that represented by 426.Fa state . 427Not all terminals may support this. 428.It Dv TIOCMGET Fa int *state 429Return the current state of the terminal modem lines as represented 430above in the integer pointed to by 431.Fa state . 432.It Dv TIOCMBIS Fa int *state 433The bits in the integer pointed to by 434.Fa state 435represent modem state as described above; however, the state is OR-ed 436in with the current state. 437.It Dv TIOCMBIC Fa int *state 438The bits in the integer pointed to by 439.Fa state 440represent modem state as described above; however, each bit which is on 441in 442.Fa state 443is cleared in the terminal. 444.It Dv TIOCGTSTAMP Fa struct timeval *timeval 445Return the (single) timestamp. 446.It Dv TIOCSTSTAMP Fa struct tstamps *tstamps 447Chooses the conditions which will cause the current system time to be 448immediately copied to the terminal timestamp storage. 449This is often used to determine exactly the moment at which one or 450more of these events occurred, though only one can be monitored. 451Only 452.Dv TIOCM_CTS 453and 454.Dv TIOCM_CAR 455are honoured in 456.Va tstamps.ts_set 457and 458.Va tstamps.ts_clr ; 459these indicate which raising and lowering events on the respective lines 460should cause a timestamp capture. 461.It Dv TIOCSFLAGS Fa int *state 462The bits in the integer pointed to by 463.Fa state 464contain bits that correspond to serial port state. 465Following is a list of defined variables and the serial port state they 466represent: 467.Pp 468.Bl -tag -width TIOCFLAG_SOFTCAR -compact 469.It TIOCFLAG_SOFTCAR 470Ignore hardware carrier. 471.It TIOCFLAG_CLOCAL 472Set clocal on open. 473.It TIOCFLAG_CRTSCTS 474Set crtscts on open. 475.It TIOCFLAG_MDMBUF 476Set mdmbuf on open. 477.El 478.Pp 479This call sets the serial port state to that represented by 480.Fa state . 481Not all serial ports may support this. 482.It Dv TIOCGFLAGS Fa int *state 483Return the current state of the serial port as represented 484above in the integer pointed to by 485.Fa state . 486.El 487.Sh FILES 488.Bl -tag -width /dev/tty -compact 489.It Pa /dev/tty 490controlling terminal, if any 491.El 492.Sh SEE ALSO 493.Xr stty 1 , 494.Xr tty 1 , 495.Xr ioctl 2 , 496.Xr pty 4 , 497.Xr termios 4 , 498.Xr ttys 5 , 499.Xr getty 8 500.Sh HISTORY 501The cua support is inspired by similar support in SunOS. 502The NMEA 0183 line discipline was added in 503.Ox 4.0 504by 505.An Marc Balmer Aq Mt mbalmer@openbsd.org . 506