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