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