xref: /openbsd/share/man/man4/tty.4 (revision 5dea098c)
1.\"	$OpenBSD: tty.4,v 1.55 2022/02/18 23:17:15 jsg 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: February 18 2022 $
34.Dt TTY 4
35.Os
36.Sh NAME
37.Nm tty ,
38.Nm cua
39.Nd general terminal interface
40.Sh SYNOPSIS
41.In 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 (such as a serial port) on the system usually has a
47terminal special device file 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, or with another system over a serial link.
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 cu 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" ) ,
110described in
111.Xr stty 1
112at the command level, and in
113.Xr termios 4
114at the programming level.
115To change settings associated with a login terminal,
116refer to the preceding man pages for the common cases.
117The remainder of this man page is concerned with describing details of using
118and controlling terminal devices at a low level, such as that possibly
119required by a program wishing to provide features similar to those provided
120by the system.
121.Ss Line disciplines
122A terminal file is used like any other file in the system in that
123it can be opened, read, and written to using standard system
124calls.
125For each existing terminal file, there is a software processing module
126called a
127.Em "line discipline"
128associated with it.
129The
130.Em "line discipline"
131essentially glues the low level device driver code with the high
132level generic interface routines (such as
133.Xr read 2
134and
135.Xr write 2 ) ,
136and is responsible for implementing the semantics associated
137with the device.
138When a terminal file is first opened by a program, the default
139.Em "line discipline"
140called the
141.Dv termios
142line discipline is associated with the file.
143This is the primary line discipline that is used in most cases and provides
144the semantics that users normally associate with a terminal.
145When the
146.Dv termios
147line discipline is in effect, the terminal file behaves and is
148operated according to the rules described in
149.Xr termios 4 .
150Refer to that man page for a full description of the terminal
151semantics.
152The operations described here
153generally represent features common
154across all
155.Em "line disciplines" ,
156although some of these calls may not
157make sense in conjunction with a line discipline other than
158.Dv termios ,
159and some may not be supported by the underlying
160hardware (or lack thereof, as in the case of ptys).
161.Ss Terminal File Operations
162All of the following operations are invoked using the
163.Xr ioctl 2
164system call.
165Refer to that man page for a description of the
166.Em request
167and
168.Em argp
169parameters.
170In addition to the ioctl
171.Em requests
172defined here, the specific line discipline
173in effect will define other
174.Em requests
175specific to it (actually
176.Xr termios 4
177defines them as function calls, not ioctl
178.Em requests ) .
179The following section lists the available ioctl requests.
180The name of the request, a description of its purpose, and the typed
181.Em argp
182parameter (if any)
183are listed.
184For example, the first entry says
185.Pp
186.D1 Em "TIOCSETD int *ldisc"
187.Pp
188and would be called on the terminal associated with
189file descriptor zero by the following code fragment:
190.Bd -literal
191	int ldisc;
192
193	ldisc = TTYDISC;
194	ioctl(0, TIOCSETD, &ldisc);
195.Ed
196.Ss Terminal File Request Descriptions
197.Bl -tag -width TIOCGWINSZ
198.It Dv TIOCSETD Fa int *ldisc
199Change to the new line discipline pointed to by
200.Fa ldisc .
201The available line disciplines currently available are:
202.Pp
203.Bl -tag -width TIOCGWINSZ -compact
204.It TTYDISC
205Termios interactive line discipline.
206.It PPPDISC
207Point-to-Point Protocol line discipline.
208.It NMEADISC
209NMEA 0183 line discipline.
210.It MSTSDISC
211Meinberg Standard Time String line discipline.
212.El
213.It Dv TIOCGETD Fa int *ldisc
214Return the current line discipline in the integer pointed to by
215.Fa ldisc .
216.It Dv TIOCSBRK Fa void
217Set the terminal hardware into BREAK condition.
218.It Dv TIOCCBRK Fa void
219Clear the terminal hardware BREAK condition.
220.It Dv TIOCSDTR Fa void
221Assert data terminal ready (DTR).
222.It Dv TIOCCDTR Fa void
223Clear data terminal ready (DTR).
224.It Dv TIOCGPGRP Fa int *tpgrp
225Return the current process group the terminal is associated
226with in the integer pointed to by
227.Fa tpgrp .
228This is the underlying call that implements the
229.Xr tcgetpgrp 3
230call.
231.It Dv TIOCSPGRP Fa int *tpgrp
232Associate the terminal with the process group (as an integer) pointed to by
233.Fa tpgrp .
234This is the underlying call that implements the
235.Xr tcsetpgrp 3
236call.
237.It Dv TIOCGETA Fa struct termios *term
238Place the current value of the termios state associated with the
239device in the termios structure pointed to by
240.Fa term .
241This is the underlying call that implements the
242.Xr tcgetattr 3
243call.
244.It Dv TIOCSETA Fa struct termios *term
245Set the termios state associated with the device immediately.
246This is the underlying call that implements the
247.Xr tcsetattr 3
248call with the
249.Dv TCSANOW
250option.
251.It Dv TIOCSETAW Fa struct termios *term
252First wait for any output to complete, then set the termios state
253associated with the device.
254This is the underlying call that implements the
255.Xr tcsetattr 3
256call with the
257.Dv TCSADRAIN
258option.
259.It Dv TIOCSETAF Fa struct termios *term
260First wait for any output to complete, clear any pending input,
261then set the termios state associated with the device.
262This is the underlying call that implements the
263.Xr tcsetattr 3
264call with the
265.Dv TCSAFLUSH
266option.
267.It Dv TIOCOUTQ Fa int *num
268Place the current number of characters in the output queue in the
269integer pointed to by
270.Fa num .
271.It Dv TIOCNOTTY Fa void
272This call is obsolete but left for compatibility.
273In the past, when a process that didn't have a controlling terminal
274.Po
275see
276.Em The Controlling Terminal
277in
278.Xr termios 4
279.Pc
280first opened a terminal device, it acquired that terminal as its
281controlling terminal.
282For some programs this was a hazard as they didn't want a controlling
283terminal in the first place, and this provided a mechanism to disassociate
284the controlling terminal from the calling process.
285It
286.Em must
287be called by opening the file
288.Pa /dev/tty
289and calling
290.Dv TIOCNOTTY
291on that file descriptor.
292.Pp
293The current system does not allocate a controlling terminal to
294a process on an
295.Xr open 2
296call: there is a specific ioctl called
297.Dv TIOCSCTTY
298to make a terminal the controlling
299terminal.
300In addition, a program can
301.Xr fork 2
302and call the
303.Xr setsid 2
304system call which will place the process into its own session - which
305has the effect of disassociating it from the controlling terminal.
306This is the new and preferred method for programs to lose their controlling
307terminal.
308.It Dv TIOCSETVERAUTH Fa int *secs
309Indicate that the current user has successfully authenticated to this session.
310Future authentication checks may then be bypassed by performing a
311.Dv TIOCCHKVERAUTH
312check.
313The verified authentication status will expire after
314.Fa secs
315seconds.
316Only root may perform this operation.
317.It Dv TIOCCLRVERAUTH Fa void
318Clear any verified auth status associated with this session.
319.It Dv TIOCCHKVERAUTH Fa void
320Check the verified auth status of this session.
321The calling process must have the same real user ID and
322parent process as the process which called
323.Dv TIOCSETVERAUTH .
324A zero return indicates success.
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.
368.It Dv TIOCSWINSZ Fa struct winsize *ws
369Set the window size associated with the terminal to be the value in
370the
371.Va winsize
372structure pointed to by
373.Fa ws
374(see above).
375.It Dv TIOCCONS Fa int *on
376If
377.Fa on
378points to a non-zero integer, redirect kernel console output
379.Po
380see
381.Xr printf 9
382.Pc
383to this terminal.
384If
385.Fa on
386points to a zero integer, redirect kernel console output back to the normal
387console.
388This is usually used on workstations to redirect kernel messages
389to a particular window.
390.It Dv TIOCMSET Fa int *state
391The integer pointed to by
392.Fa state
393contains bits that correspond to modem state.
394Following is a list of defined variables and the modem state they represent:
395.Pp
396.Bl -tag -width TIOCMXCTS -compact
397.It TIOCM_LE
398Line Enable.
399.It TIOCM_DTR
400Data Terminal Ready.
401.It TIOCM_RTS
402Request To Send.
403.It TIOCM_ST
404Secondary Transmit.
405.It TIOCM_SR
406Secondary Receive.
407.It TIOCM_CTS
408Clear To Send.
409.It TIOCM_CAR
410Carrier Detect.
411.It TIOCM_CD
412Carrier Detect (synonym).
413.It TIOCM_RNG
414Ring Indication.
415.It TIOCM_RI
416Ring Indication (synonym).
417.It TIOCM_DSR
418Data Set Ready.
419.El
420.Pp
421This call sets the terminal modem state to that represented by
422.Fa state .
423Not all terminals may support this.
424.It Dv TIOCMGET Fa int *state
425Return the current state of the terminal modem lines as represented
426above in the integer pointed to by
427.Fa state .
428.It Dv TIOCMBIS Fa int *state
429The bits in the integer pointed to by
430.Fa state
431represent modem state as described above; however, the state is OR-ed
432in with the current state.
433.It Dv TIOCMBIC Fa int *state
434The bits in the integer pointed to by
435.Fa state
436represent modem state as described above; however, each bit which is on
437in
438.Fa state
439is cleared in the terminal.
440.It Dv TIOCGTSTAMP Fa struct timeval *timeval
441Return the (single) timestamp.
442.It Dv TIOCSTSTAMP Fa struct tstamps *tstamps
443Chooses the conditions which will cause the current system time to be
444immediately copied to the terminal timestamp storage.
445This is often used to determine exactly the moment at which one or
446more of these events occurred, though only one can be monitored.
447Only
448.Dv TIOCM_CTS
449and
450.Dv TIOCM_CAR
451are honoured in
452.Va tstamps.ts_set
453and
454.Va tstamps.ts_clr ;
455these indicate which raising and lowering events on the respective lines
456should cause a timestamp capture.
457.It Dv TIOCSFLAGS Fa int *state
458The bits in the integer pointed to by
459.Fa state
460contain bits that correspond to serial port state.
461Following is a list of defined variables and the serial port state they
462represent:
463.Pp
464.Bl -tag -width TIOCFLAG_SOFTCAR -compact
465.It TIOCFLAG_SOFTCAR
466Ignore hardware carrier.
467.It TIOCFLAG_CLOCAL
468Set clocal on open.
469.It TIOCFLAG_CRTSCTS
470Set crtscts on open.
471.It TIOCFLAG_MDMBUF
472Set mdmbuf on open.
473.El
474.Pp
475This call sets the serial port state to that represented by
476.Fa state .
477Not all serial ports may support this.
478.It Dv TIOCGFLAGS Fa int *state
479Return the current state of the serial port as represented
480above in the integer pointed to by
481.Fa state .
482.It Dv TIOCSTAT Fa void
483Causes the kernel to write a status message to the terminal that displays the
484current load average,
485the name of the command in the foreground,
486its process ID,
487the symbolic wait channel,
488the number of user and system seconds used,
489the percentage of CPU the process is getting,
490and the resident set size of the process.
491.El
492.Sh FILES
493.Bl -tag -width /dev/tty -compact
494.It Pa /dev/tty
495controlling terminal, if any
496.El
497.Sh SEE ALSO
498.Xr cu 1 ,
499.Xr stty 1 ,
500.Xr tty 1 ,
501.Xr ioctl 2 ,
502.Xr pty 4 ,
503.Xr termios 4 ,
504.Xr ttys 5 ,
505.Xr getty 8
506.Sh HISTORY
507A console typewriter device
508.Pa /dev/tty
509and asynchronous communication interfaces
510.Pa /dev/tty[0-5]
511first appeared in
512.At v1 .
513The cua support is inspired by similar support in SunOS.
514