1.\" $OpenBSD: tty.4,v 1.37 2010/07/31 20:26:21 jmc 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: July 31 2010 $ 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.Aq Pa 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.Pp 223.It Dv TIOCGETD Fa int *ldisc 224Return the current line discipline in the integer pointed to by 225.Fa ldisc . 226.It Dv TIOCSBRK Fa void 227Set the terminal hardware into BREAK condition. 228.It Dv TIOCCBRK Fa void 229Clear the terminal hardware BREAK condition. 230.It Dv TIOCSDTR Fa void 231Assert data terminal ready (DTR). 232.It Dv TIOCCDTR Fa void 233Clear data terminal ready (DTR). 234.It Dv TIOCGPGRP Fa int *tpgrp 235Return the current process group the terminal is associated 236with in the integer pointed to by 237.Fa tpgrp . 238This is the underlying call that implements the 239.Xr termios 4 240.Fn tcgetpgrp 241call. 242.It Dv TIOCSPGRP Fa int *tpgrp 243Associate the terminal with the process group (as an integer) pointed to by 244.Fa tpgrp . 245This is the underlying call that implements the 246.Xr termios 4 247.Fn tcsetpgrp 248call. 249.It Dv TIOCGETA Fa struct termios *term 250Place the current value of the termios state associated with the 251device in the termios structure pointed to by 252.Fa term . 253This is the underlying call that implements the 254.Xr termios 4 255.Fn tcgetattr 256call. 257.It Dv TIOCSETA Fa struct termios *term 258Set the termios state associated with the device immediately. 259This is the underlying call that implements the 260.Xr termios 4 261.Fn tcsetattr 262call with the 263.Dv TCSANOW 264option. 265.It Dv TIOCSETAW Fa struct termios *term 266First wait for any output to complete, then set the termios state 267associated with the device. 268This is the underlying call that implements the 269.Xr termios 4 270.Fn tcsetattr 271call with the 272.Dv TCSADRAIN 273option. 274.It Dv TIOCSETAF Fa struct termios *term 275First wait for any output to complete, clear any pending input, 276then set the termios state associated with the device. 277This is the underlying call that implements the 278.Xr termios 4 279.Fn tcsetattr 280call with the 281.Dv TCSAFLUSH 282option. 283.It Dv TIOCOUTQ Fa int *num 284Place the current number of characters in the output queue in the 285integer pointed to by 286.Fa num . 287.It Dv TIOCSTI Fa char *cp 288Simulate typed input. 289Pretend as if the terminal received the character pointed to by 290.Fa cp . 291.It Dv TIOCNOTTY Fa void 292This call is obsolete but left for compatibility. 293In the past, when a process that didn't have a controlling terminal 294(see 295.Em The Controlling Terminal 296in 297.Xr termios 4 ) 298first opened a terminal device, it acquired that terminal as its 299controlling terminal. 300For some programs this was a hazard as they didn't want a controlling 301terminal in the first place, and this provided a mechanism to disassociate 302the controlling terminal from the calling process. 303It 304.Em must 305be called by opening the file 306.Pa /dev/tty 307and calling 308.Dv TIOCNOTTY 309on that file descriptor. 310.Pp 311The current system does not allocate a controlling terminal to 312a process on an 313.Fn open 314call: there is a specific ioctl called 315.Dv TIOCSCTTY 316to make a terminal the controlling 317terminal. 318In addition, a program can 319.Fn fork 320and call the 321.Fn setsid 322system call which will place the process into its own session - which 323has the effect of disassociating it from the controlling terminal. 324This is the new and preferred method for programs to lose their controlling 325terminal. 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.Aq Pa 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. 369The 370.Va winsize 371structure is defined in 372.Aq Pa sys/ioctl.h . 373.It Dv TIOCSWINSZ Fa struct winsize *ws 374Set the window size associated with the terminal to be the value in 375the 376.Va winsize 377structure pointed to by 378.Fa ws 379(see above). 380.It Dv TIOCCONS Fa int *on 381If 382.Fa on 383points to a non-zero integer, redirect kernel console output 384.Po 385kernel 386.Fn printf Ns s 387.Pc 388to this terminal. 389If 390.Fa on 391points to a zero integer, redirect kernel console output back to the normal 392console. 393This is usually used on workstations to redirect kernel messages 394to a particular window. 395.It Dv TIOCMSET Fa int *state 396The integer pointed to by 397.Fa state 398contains bits that correspond to modem state. 399Following is a list of defined variables and the modem state they represent: 400.Pp 401.Bl -tag -width TIOCMXCTS -compact 402.It TIOCM_LE 403Line Enable. 404.It TIOCM_DTR 405Data Terminal Ready. 406.It TIOCM_RTS 407Request To Send. 408.It TIOCM_ST 409Secondary Transmit. 410.It TIOCM_SR 411Secondary Receive. 412.It TIOCM_CTS 413Clear To Send. 414.It TIOCM_CAR 415Carrier Detect. 416.It TIOCM_CD 417Carrier Detect (synonym). 418.It TIOCM_RNG 419Ring Indication. 420.It TIOCM_RI 421Ring Indication (synonym). 422.It TIOCM_DSR 423Data Set Ready. 424.El 425.Pp 426This call sets the terminal modem state to that represented by 427.Fa state . 428Not all terminals may support this. 429.It Dv TIOCMGET Fa int *state 430Return the current state of the terminal modem lines as represented 431above in the integer pointed to by 432.Fa state . 433.It Dv TIOCMBIS Fa int *state 434The bits in the integer pointed to by 435.Fa state 436represent modem state as described above; however, the state is OR-ed 437in with the current state. 438.It Dv TIOCMBIC Fa int *state 439The bits in the integer pointed to by 440.Fa state 441represent modem state as described above; however, each bit which is on 442in 443.Fa state 444is cleared in the terminal. 445.It Dv TIOCGTSTAMP Fa struct timeval *timeval 446Return the (single) timestamp. 447.It Dv TIOCSTSTAMP Fa struct tstamps *tstamps 448Chooses the conditions which will cause the current system time to be 449immediately copied to the terminal timestamp storage. 450This is often used to determine exactly the moment at which one or 451more of these events occurred, though only one can be monitored. 452Only 453.Dv TIOCM_CTS 454and 455.Dv TIOCM_CAR 456are honoured in 457.Va tstamps.ts_set 458and 459.Va tstamps.ts_clr ; 460these indicate which raising and lowering events on the respective lines 461should cause a timestamp capture. 462.It Dv TIOCSFLAGS Fa int *state 463The bits in the integer pointed to by 464.Fa state 465contain bits that correspond to serial port state. 466Following is a list of defined variables and the serial port state they 467represent: 468.Pp 469.Bl -tag -width TIOCFLAG_SOFTCAR -compact 470.It TIOCFLAG_SOFTCAR 471Ignore hardware carrier. 472.It TIOCFLAG_CLOCAL 473Set clocal on open. 474.It TIOCFLAG_CRTSCTS 475Set crtscts on open. 476.It TIOCFLAG_MDMBUF 477Set mdmbuf on open. 478.El 479.Pp 480This call sets the serial port state to that represented by 481.Fa state . 482Not all serial ports may support this. 483.It Dv TIOCGFLAGS Fa int *state 484Return the current state of the serial port as represented 485above in the integer pointed to by 486.Fa state . 487.El 488.Sh FILES 489.Bl -tag -width /dev/tty -compact 490.It Pa /dev/tty 491controlling terminal, if any 492.El 493.Sh SEE ALSO 494.Xr stty 1 , 495.Xr tty 1 , 496.Xr ioctl 2 , 497.Xr pty 4 , 498.Xr termios 4 , 499.Xr ttys 5 , 500.Xr getty 8 501.Sh HISTORY 502The cua support is inspired by similar support in SunOS. 503The NMEA 0183 line discipline was added in 504.Ox 4.0 505by 506.An Marc Balmer Aq mbalmer@openbsd.org . 507