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