xref: /openbsd-src/share/man/man4/tty.4 (revision b7bdf16b8b48972b24b0bebd7bbce6d56bf8a7cd)
1.\"	$OpenBSD: tty.4,v 1.56 2024/08/16 16:10:27 florian 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: August 16 2024 $
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
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.
70.Pp
71For hardware terminal ports, dial-out is supported through matching
72device nodes called calling units.
73For instance, the terminal called
74.Pa /dev/tty03
75would have a matching calling unit called
76.Pa /dev/cua03 .
77These two devices are normally differentiated by creating the calling
78unit device node with a minor number 128 greater than the dial-in
79device node.
80Whereas the dial-in device (the
81.Em tty )
82normally
83requires a hardware signal to indicate to the system that it is active,
84the dial-out device (the
85.Em cua )
86does not, and hence can communicate unimpeded
87with a device such as a modem, or with another system over a serial link.
88This means that a process like
89.Xr getty 8
90will wait on a dial-in device until a connection is established.
91Meanwhile, a dial-out connection can be established on the dial-out
92device (for the very same hardware terminal port) without disturbing
93anything else on the system.
94The
95.Xr getty 8
96process does not even notice that anything is happening on the terminal
97port.
98If a connecting call comes in after the dial-out connection has finished, the
99.Xr getty 8
100process will deal with it properly, without having noticed the
101intervening dial-out action.
102For more information on dial-out, see
103.Xr cu 1 .
104.Pp
105When an interactive user logs in, the system prepares the line to
106behave in a certain way (called a
107.Em "line discipline" ) ,
108described in
109.Xr stty 1
110at the command level, and in
111.Xr termios 4
112at the programming level.
113To change settings associated with a login terminal,
114refer to the preceding man pages for the common cases.
115The remainder of this man page is concerned with describing details of using
116and controlling terminal devices at a low level, such as that possibly
117required by a program wishing to provide features similar to those provided
118by the system.
119.Ss Line disciplines
120A terminal file is used like any other file in the system in that
121it can be opened, read, and written to using standard system
122calls.
123For each existing terminal file, there is a software processing module
124called a
125.Em "line discipline"
126associated with it.
127The
128.Em "line discipline"
129essentially glues the low level device driver code with the high
130level generic interface routines (such as
131.Xr read 2
132and
133.Xr write 2 ) ,
134and is responsible for implementing the semantics associated
135with the device.
136When a terminal file is first opened by a program, the default
137.Em "line discipline"
138called the
139.Dv termios
140line discipline is associated with the file.
141This is the primary line discipline that is used in most cases and provides
142the semantics that users normally associate with a terminal.
143When the
144.Dv termios
145line discipline is in effect, the terminal file behaves and is
146operated according to the rules described in
147.Xr termios 4 .
148Refer to that man page for a full description of the terminal
149semantics.
150The operations described here
151generally represent features common
152across all
153.Em "line disciplines" ,
154although some of these calls may not
155make sense in conjunction with a line discipline other than
156.Dv termios ,
157and some may not be supported by the underlying
158hardware (or lack thereof, as in the case of ptys).
159.Ss Terminal File Operations
160All of the following operations are invoked using the
161.Xr ioctl 2
162system call.
163Refer to that man page for a description of the
164.Em request
165and
166.Em argp
167parameters.
168In addition to the ioctl
169.Em requests
170defined here, the specific line discipline
171in effect will define other
172.Em requests
173specific to it (actually
174.Xr termios 4
175defines them as function calls, not ioctl
176.Em requests ) .
177The following section lists the available ioctl requests.
178The name of the request, a description of its purpose, and the typed
179.Em argp
180parameter (if any)
181are listed.
182For example, the first entry says
183.Pp
184.D1 Em "TIOCSETD int *ldisc"
185.Pp
186and would be called on the terminal associated with
187file descriptor zero by the following code fragment:
188.Bd -literal
189	int ldisc;
190
191	ldisc = TTYDISC;
192	ioctl(0, TIOCSETD, &ldisc);
193.Ed
194.Ss Terminal File Request Descriptions
195.Bl -tag -width TIOCGWINSZ
196.It Dv TIOCSETD Fa int *ldisc
197Change to the new line discipline pointed to by
198.Fa ldisc .
199The available line disciplines currently available are:
200.Pp
201.Bl -tag -width TIOCGWINSZ -compact
202.It TTYDISC
203Termios interactive line discipline.
204.It PPPDISC
205Point-to-Point Protocol line discipline.
206.It NMEADISC
207NMEA 0183 line discipline.
208.It MSTSDISC
209Meinberg Standard Time String line discipline.
210.El
211.It Dv TIOCGETD Fa int *ldisc
212Return the current line discipline in the integer pointed to by
213.Fa ldisc .
214.It Dv TIOCSBRK Fa void
215Set the terminal hardware into BREAK condition.
216.It Dv TIOCCBRK Fa void
217Clear the terminal hardware BREAK condition.
218.It Dv TIOCSDTR Fa void
219Assert data terminal ready (DTR).
220.It Dv TIOCCDTR Fa void
221Clear data terminal ready (DTR).
222.It Dv TIOCGPGRP Fa int *tpgrp
223Return the current process group the terminal is associated
224with in the integer pointed to by
225.Fa tpgrp .
226This is the underlying call that implements the
227.Xr tcgetpgrp 3
228call.
229.It Dv TIOCSPGRP Fa int *tpgrp
230Associate the terminal with the process group (as an integer) pointed to by
231.Fa tpgrp .
232This is the underlying call that implements the
233.Xr tcsetpgrp 3
234call.
235.It Dv TIOCGETA Fa struct termios *term
236Place the current value of the termios state associated with the
237device in the termios structure pointed to by
238.Fa term .
239This is the underlying call that implements the
240.Xr tcgetattr 3
241call.
242.It Dv TIOCSETA Fa struct termios *term
243Set the termios state associated with the device immediately.
244This is the underlying call that implements the
245.Xr tcsetattr 3
246call with the
247.Dv TCSANOW
248option.
249.It Dv TIOCSETAW Fa struct termios *term
250First wait for any output to complete, then set the termios state
251associated with the device.
252This is the underlying call that implements the
253.Xr tcsetattr 3
254call with the
255.Dv TCSADRAIN
256option.
257.It Dv TIOCSETAF Fa struct termios *term
258First wait for any output to complete, clear any pending input,
259then set the termios state associated with the device.
260This is the underlying call that implements the
261.Xr tcsetattr 3
262call with the
263.Dv TCSAFLUSH
264option.
265.It Dv TIOCOUTQ Fa int *num
266Place the current number of characters in the output queue in the
267integer pointed to by
268.Fa num .
269.It Dv TIOCNOTTY Fa void
270This call is obsolete but left for compatibility.
271In the past, when a process that didn't have a controlling terminal
272.Po
273see
274.Em The Controlling Terminal
275in
276.Xr termios 4
277.Pc
278first opened a terminal device, it acquired that terminal as its
279controlling terminal.
280For some programs this was a hazard as they didn't want a controlling
281terminal in the first place, and this provided a mechanism to disassociate
282the controlling terminal from the calling process.
283It
284.Em must
285be called by opening the file
286.Pa /dev/tty
287and calling
288.Dv TIOCNOTTY
289on that file descriptor.
290.Pp
291The current system does not allocate a controlling terminal to
292a process on an
293.Xr open 2
294call: there is a specific ioctl called
295.Dv TIOCSCTTY
296to make a terminal the controlling
297terminal.
298In addition, a program can
299.Xr fork 2
300and call the
301.Xr setsid 2
302system call which will place the process into its own session - which
303has the effect of disassociating it from the controlling terminal.
304This is the new and preferred method for programs to lose their controlling
305terminal.
306.It Dv TIOCSETVERAUTH Fa int *secs
307Indicate that the current user has successfully authenticated to this session.
308Future authentication checks may then be bypassed by performing a
309.Dv TIOCCHKVERAUTH
310check.
311The verified authentication status will expire after
312.Fa secs
313seconds.
314Only root may perform this operation.
315.It Dv TIOCCLRVERAUTH Fa void
316Clear any verified auth status associated with this session.
317.It Dv TIOCCHKVERAUTH Fa void
318Check the verified auth status of this session.
319The calling process must have the same real user ID and
320parent process as the process which called
321.Dv TIOCSETVERAUTH .
322A zero return indicates success.
323.It Dv TIOCSTOP Fa void
324Stop output on the terminal (like typing ^S at the keyboard).
325.It Dv TIOCSTART Fa void
326Start output on the terminal (like typing ^Q at the keyboard).
327.It Dv TIOCSCTTY Fa void
328Make the terminal the controlling terminal for the process (the process
329must not currently have a controlling terminal).
330.It Dv TIOCDRAIN Fa void
331Wait until all output is drained.
332.It Dv TIOCEXCL Fa void
333Set exclusive use on the terminal.
334No further opens are permitted except by root.
335Of course, this means that programs that are run by root (or setuid)
336will not obey the exclusive setting - which limits the usefulness
337of this feature.
338.It Dv TIOCNXCL Fa void
339Clear exclusive use of the terminal.
340Further opens are permitted.
341.It Dv TIOCFLUSH Fa int *what
342If the value of the int pointed to by
343.Fa what
344contains the
345.Dv FREAD
346bit as defined in
347.In sys/fcntl.h ,
348then all characters in the input queue are cleared.
349If it contains the
350.Dv FWRITE
351bit, then all characters in the output queue are cleared.
352If the value of the integer is zero, then it behaves as if both the
353.Dv FREAD
354and
355.Dv FWRITE
356bits were set (i.e., clears both queues).
357.It Dv TIOCGWINSZ Fa struct winsize *ws
358Put the window size information associated with the terminal in the
359.Va winsize
360structure pointed to by
361.Fa ws .
362The window size structure contains the number of rows and columns (and pixels
363if appropriate) of the devices attached to the terminal.
364It is set by user software and is the means by which most full\&-screen
365oriented programs determine the screen size.
366.It Dv TIOCSWINSZ Fa struct winsize *ws
367Set the window size associated with the terminal to be the value in
368the
369.Va winsize
370structure pointed to by
371.Fa ws
372(see above).
373.It Dv TIOCCONS Fa int *on
374If
375.Fa on
376points to a non-zero integer, redirect kernel console output
377.Po
378see
379.Xr printf 9
380.Pc
381to this terminal.
382If
383.Fa on
384points to a zero integer, redirect kernel console output back to the normal
385console.
386This is usually used on workstations to redirect kernel messages
387to a particular window.
388.It Dv TIOCMSET Fa int *state
389The integer pointed to by
390.Fa state
391contains bits that correspond to modem state.
392Following is a list of defined variables and the modem state they represent:
393.Pp
394.Bl -tag -width TIOCMXCTS -compact
395.It TIOCM_LE
396Line Enable.
397.It TIOCM_DTR
398Data Terminal Ready.
399.It TIOCM_RTS
400Request To Send.
401.It TIOCM_ST
402Secondary Transmit.
403.It TIOCM_SR
404Secondary Receive.
405.It TIOCM_CTS
406Clear To Send.
407.It TIOCM_CAR
408Carrier Detect.
409.It TIOCM_CD
410Carrier Detect (synonym).
411.It TIOCM_RNG
412Ring Indication.
413.It TIOCM_RI
414Ring Indication (synonym).
415.It TIOCM_DSR
416Data Set Ready.
417.El
418.Pp
419This call sets the terminal modem state to that represented by
420.Fa state .
421Not all terminals may support this.
422.It Dv TIOCMGET Fa int *state
423Return the current state of the terminal modem lines as represented
424above in the integer pointed to by
425.Fa state .
426.It Dv TIOCMBIS Fa int *state
427The bits in the integer pointed to by
428.Fa state
429represent modem state as described above; however, the state is OR-ed
430in with the current state.
431.It Dv TIOCMBIC Fa int *state
432The bits in the integer pointed to by
433.Fa state
434represent modem state as described above; however, each bit which is on
435in
436.Fa state
437is cleared in the terminal.
438.It Dv TIOCGTSTAMP Fa struct timeval *timeval
439Return the (single) timestamp.
440.It Dv TIOCSTSTAMP Fa struct tstamps *tstamps
441Chooses the conditions which will cause the current system time to be
442immediately copied to the terminal timestamp storage.
443This is often used to determine exactly the moment at which one or
444more of these events occurred, though only one can be monitored.
445Only
446.Dv TIOCM_CTS
447and
448.Dv TIOCM_CAR
449are honoured in
450.Va tstamps.ts_set
451and
452.Va tstamps.ts_clr ;
453these indicate which raising and lowering events on the respective lines
454should cause a timestamp capture.
455.It Dv TIOCSFLAGS Fa int *state
456The bits in the integer pointed to by
457.Fa state
458contain bits that correspond to serial port state.
459Following is a list of defined variables and the serial port state they
460represent:
461.Pp
462.Bl -tag -width TIOCFLAG_SOFTCAR -compact
463.It TIOCFLAG_SOFTCAR
464Ignore hardware carrier.
465.It TIOCFLAG_CLOCAL
466Set clocal on open.
467.It TIOCFLAG_CRTSCTS
468Set crtscts on open.
469.It TIOCFLAG_MDMBUF
470Set mdmbuf on open.
471.El
472.Pp
473This call sets the serial port state to that represented by
474.Fa state .
475Not all serial ports may support this.
476.It Dv TIOCGFLAGS Fa int *state
477Return the current state of the serial port as represented
478above in the integer pointed to by
479.Fa state .
480.It Dv TIOCSTAT Fa void
481Causes the kernel to write a status message to the terminal that displays the
482current load average,
483the name of the command in the foreground,
484its process ID,
485the symbolic wait channel,
486the number of user and system seconds used,
487the percentage of CPU the process is getting,
488and the resident set size of the process.
489.El
490.Sh FILES
491.Bl -tag -width /dev/tty -compact
492.It Pa /dev/tty
493controlling terminal, if any
494.El
495.Sh SEE ALSO
496.Xr cu 1 ,
497.Xr stty 1 ,
498.Xr tty 1 ,
499.Xr ioctl 2 ,
500.Xr pty 4 ,
501.Xr termios 4 ,
502.Xr ttys 5 ,
503.Xr getty 8
504.Sh HISTORY
505A console typewriter device
506.Pa /dev/tty
507and asynchronous communication interfaces
508.Pa /dev/tty[0-5]
509first appeared in
510.At v1 .
511The cua support is inspired by similar support in SunOS.
512