]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/ioctl_tty.2
sched_setattr.2: tfix
[thirdparty/man-pages.git] / man2 / ioctl_tty.2
CommitLineData
fea681da
MK
1.\" Copyright 2002 Walter Harms <walter.harms@informatik.uni-oldenburg.de>
2.\" and Andries Brouwer <aeb@cwi.nl>.
2297bf0e 3.\"
38f20bb9 4.\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
b18188c5 5.\" Distributed under GPL
38f20bb9 6.\" %%%LICENSE_END
fea681da 7.\"
4b8c67d9 8.TH IOCTL_TTY 2 2017-09-15 "Linux" "Linux Programmer's Manual"
fea681da 9.SH NAME
a78f6752 10ioctl_tty \- ioctls for terminals and serial lines
fea681da 11.SH SYNOPSIS
0daa9e92 12.B "#include <termios.h>"
68e4db0a 13.PP
c13182ef 14.BI "int ioctl(int " fd ", int " cmd ", ...);"
fea681da
MK
15.SH DESCRIPTION
16The
0b80cf56 17.BR ioctl (2)
fea681da 18call for terminals and serial ports accepts many possible command arguments.
c6fa0841
MK
19Most require a third argument, of varying type, here called
20.I argp
21or
22.IR arg .
dd3568a1 23.PP
fea681da
MK
24Use of
25.I ioctl
d603cc27 26makes for nonportable programs.
c13182ef 27Use the POSIX interface described in
fea681da
MK
28.BR termios (3)
29whenever possible.
73d8cece 30.SS Get and set terminal attributes
fea681da
MK
31.TP
32.BI "TCGETS struct termios *" argp
33Equivalent to
34.IR "tcgetattr(fd, argp)" .
35.br
36Get the current serial port settings.
37.TP
38.BI "TCSETS const struct termios *" argp
39Equivalent to
40.IR "tcsetattr(fd, TCSANOW, argp)" .
41.br
42Set the current serial port settings.
43.TP
44.BI "TCSETSW const struct termios *" argp
45Equivalent to
46.IR "tcsetattr(fd, TCSADRAIN, argp)" .
47.br
48Allow the output buffer to drain, and
49set the current serial port settings.
50.TP
51.BI "TCSETSF const struct termios *" argp
52Equivalent to
53.IR "tcsetattr(fd, TCSAFLUSH, argp)" .
54.br
55Allow the output buffer to drain, discard pending input, and
56set the current serial port settings.
dd3568a1 57.PP
097585ed
MK
58The following four ioctls are just like
59.BR TCGETS ,
60.BR TCSETS ,
61.BR TCSETSW ,
62.BR TCSETSF ,
fea681da 63except that they take a
5049da5b 64.I "struct termio\ *"
fea681da 65instead of a
5049da5b 66.IR "struct termios\ *" .
52a5ae2b 67.IP
fea681da 68.BI "TCGETA struct termio *" argp
52a5ae2b 69.IP
fea681da 70.BI "TCSETA const struct termio *" argp
52a5ae2b 71.IP
fea681da 72.BI "TCSETAW const struct termio *" argp
52a5ae2b 73.IP
fea681da 74.BI "TCSETAF const struct termio *" argp
73d8cece 75.SS Locking the termios structure
4961e71d
MK
76The
77.I termios
132249c4 78structure of a terminal can be locked.
4961e71d
MK
79The lock is itself a
80.I termios
c7094399 81structure, with nonzero bits or fields indicating a
fea681da
MK
82locked value.
83.TP
84.BI "TIOCGLCKTRMIOS struct termios *" argp
4961e71d
MK
85Gets the locking status of the
86.I termios
87structure of the terminal.
fea681da
MK
88.TP
89.BI "TIOCSLCKTRMIOS const struct termios *" argp
4961e71d
MK
90Sets the locking status of the
91.I termios
92structure of the terminal.
cb476d88 93Only a process with the
c0b75059 94.BR CAP_SYS_ADMIN
cb476d88 95capability can do this.
73d8cece 96.SS Get and set window size
fea681da
MK
97Window sizes are kept in the kernel, but not used by the kernel
98(except in the case of virtual consoles, where the kernel will
99update the window size when the size of the virtual console changes,
75b94dc3 100for example, by loading a new font).
efeece04 101.PP
cba5091d
MK
102The following constants and structure are defined in
103.IR <sys/ioctl.h> .
fea681da
MK
104.TP
105.BI "TIOCGWINSZ struct winsize *" argp
106Get window size.
107.TP
108.BI "TIOCSWINSZ const struct winsize *" argp
109Set window size.
dd3568a1 110.PP
fea681da 111The struct used by these ioctls is defined as
efeece04 112.PP
3f89ebc0 113.in +4n
b76974c1 114.EX
fea681da 115struct winsize {
cf0a9ace
MK
116 unsigned short ws_row;
117 unsigned short ws_col;
118 unsigned short ws_xpixel; /* unused */
119 unsigned short ws_ypixel; /* unused */
fea681da 120};
b76974c1 121.EE
3f89ebc0 122.in
efeece04 123.PP
8bd58774
MK
124When the window size changes, a
125.B SIGWINCH
126signal is sent to the
fea681da 127foreground process group.
73d8cece 128.SS Sending a break
fea681da
MK
129.TP
130.BI "TCSBRK int " arg
131Equivalent to
132.IR "tcsendbreak(fd, arg)" .
133.br
134If the terminal is using asynchronous serial data transmission, and
135.I arg
136is zero, then send a break (a stream of zero bits) for between
c13182ef
MK
1370.25 and 0.5 seconds.
138If the terminal is not using asynchronous
fea681da
MK
139serial data transmission, then either a break is sent, or the function
140returns without doing anything.
141When
142.I arg
c7094399 143is nonzero, nobody knows what will happen.
efeece04 144.IP
72cedc06 145(SVr4, UnixWare, Solaris, Linux treat
fea681da 146.I "tcsendbreak(fd,arg)"
c7094399 147with nonzero
fea681da
MK
148.I arg
149like
150.IR "tcdrain(fd)" .
151SunOS treats
152.I arg
153as a multiplier, and sends a stream of bits
154.I arg
155times as long as done for zero
156.IR arg .
72cedc06 157DG/UX and AIX treat
fea681da 158.I arg
c7094399 159(when nonzero) as a time interval measured in milliseconds.
fea681da
MK
160HP-UX ignores
161.IR arg .)
162.TP
163.BI "TCSBRKP int " arg
097585ed
MK
164So-called "POSIX version" of
165.BR TCSBRK .
c7094399 166It treats nonzero
fea681da
MK
167.I arg
168as a timeinterval measured in deciseconds, and does nothing
169when the driver does not support breaks.
170.TP
0daa9e92 171.B "TIOCSBRK void"
fea681da
MK
172Turn break on, that is, start sending zero bits.
173.TP
0daa9e92 174.B "TIOCCBRK void"
fea681da 175Turn break off, that is, stop sending zero bits.
73d8cece 176.SS Software flow control
fea681da
MK
177.TP
178.BI "TCXONC int " arg
179Equivalent to
180.IR "tcflow(fd, arg)" .
181.br
182See
183.BR tcflow (3)
097585ed
MK
184for the argument values
185.BR TCOOFF ,
186.BR TCOON ,
187.BR TCIOFF ,
188.BR TCION .
73d8cece 189.SS Buffer count and flushing
fea681da
MK
190.TP
191.BI "FIONREAD int *" argp
192Get the number of bytes in the input buffer.
193.TP
194.BI "TIOCINQ int *" argp
097585ed
MK
195Same as
196.BR FIONREAD .
fea681da
MK
197.TP
198.BI "TIOCOUTQ int *" argp
199Get the number of bytes in the output buffer.
200.TP
201.BI "TCFLSH int " arg
202Equivalent to
203.IR "tcflush(fd, arg)" .
204.br
205See
206.BR tcflush (3)
097585ed
MK
207for the argument values
208.BR TCIFLUSH ,
209.BR TCOFLUSH ,
210.BR TCIOFLUSH .
73d8cece 211.SS Faking input
fea681da
MK
212.TP
213.BI "TIOCSTI const char *" argp
214Insert the given byte in the input queue.
73d8cece 215.SS Redirecting console output
fea681da 216.TP
0daa9e92 217.B "TIOCCONS void"
fea681da
MK
218Redirect output that would have gone to
219.I /dev/console
220or
221.I /dev/tty0
132249c4 222to the given terminal.
b218b023 223If that was a pseudoterminal master, send it to the slave.
b5fa38a3
MK
224In Linux before version 2.6.10,
225anybody can do this as long as the output was not redirected yet;
cb476d88 226since version 2.6.10, only a process with the
b5fa38a3 227.BR CAP_SYS_ADMIN
cb476d88 228capability may do this.
b5fa38a3 229If output was redirected already
097585ed
MK
230.B EBUSY
231is returned,
b5fa38a3 232but redirection can be stopped by using this ioctl with
fea681da
MK
233.I fd
234pointing at
235.I /dev/console
236or
237.IR /dev/tty0 .
73d8cece 238.SS Controlling terminal
fea681da
MK
239.TP
240.BI "TIOCSCTTY int " arg
132249c4 241Make the given terminal the controlling terminal of the calling process.
a1ffe9f5 242The calling process must be a session leader and not have a
132249c4 243controlling terminal already.
9ab0de3c
MK
244For this case,
245.I arg
246should be specified as zero.
efeece04 247.IP
132249c4 248If this terminal is already the controlling terminal
5cc100b9 249of a different session group, then the ioctl fails with
097585ed 250.BR EPERM ,
cb476d88 251unless the caller has the
c0b75059 252.BR CAP_SYS_ADMIN
cb476d88 253capability and
fea681da 254.I arg
132249c4
MK
255equals 1, in which case the terminal is stolen, and all processes that had
256it as controlling terminal lose it.
fea681da 257.TP
17434a0d 258.B "TIOCNOTTY void"
132249c4
MK
259If the given terminal was the controlling terminal of the calling process,
260give up this controlling terminal.
c13182ef 261If the process was session leader,
8bd58774
MK
262then send
263.B SIGHUP
264and
265.B SIGCONT
266to the foreground process group
132249c4 267and all processes in the current session lose their controlling terminal.
73d8cece 268.SS Process group and session ID
fea681da
MK
269.TP
270.BI "TIOCGPGRP pid_t *" argp
271When successful, equivalent to
272.IR "*argp = tcgetpgrp(fd)" .
273.br
132249c4 274Get the process group ID of the foreground process group on this terminal.
fea681da
MK
275.TP
276.BI "TIOCSPGRP const pid_t *" argp
277Equivalent to
278.IR "tcsetpgrp(fd, *argp)" .
279.br
132249c4 280Set the foreground process group ID of this terminal.
fea681da
MK
281.TP
282.BI "TIOCGSID pid_t *" argp
132249c4 283Get the session ID of the given terminal.
ad94e27c 284This fails with the error
097585ed 285.B ENOTTY
a23d8efa 286if the terminal is not a master pseudoterminal
132249c4 287and not our controlling terminal.
c13182ef 288Strange.
73d8cece 289.SS Exclusive mode
fea681da 290.TP
0daa9e92 291.B "TIOCEXCL void"
132249c4 292Put the terminal into exclusive mode.
fea681da
MK
293No further
294.BR open (2)
295operations on the terminal are permitted.
ad94e27c 296(They fail with
097585ed 297.BR EBUSY ,
cb476d88 298except for a process with the
c0b75059
MK
299.BR CAP_SYS_ADMIN
300capability.)
fea681da 301.TP
8b17edbe 302.BI "TIOCGEXCL int *" argp
164b4016 303(since Linux 3.8)
8b17edbe
MK
304If the terminal is currently in exclusive mode,
305place a nonzero value in the location pointed to by
306.IR argp ;
307otherwise, place zero in
164b4016 308.IR *argp .
8b17edbe 309.TP
0daa9e92 310.B "TIOCNXCL void"
fea681da 311Disable exclusive mode.
73d8cece 312.SS Line discipline
fea681da
MK
313.TP
314.BI "TIOCGETD int *" argp
132249c4 315Get the line discipline of the terminal.
fea681da
MK
316.TP
317.BI "TIOCSETD const int *" argp
132249c4 318Set the line discipline of the terminal.
73d8cece 319.SS Pseudoterminal ioctls
fea681da
MK
320.TP
321.BI "TIOCPKT const int *" argp
322Enable (when
323.RI * argp
c7094399 324is nonzero) or disable packet mode.
b218b023 325Can be applied to the master side of a pseudoterminal only (and will return
097585ed
MK
326.B ENOTTY
327otherwise).
c13182ef 328In packet mode, each subsequent
fea681da 329.BR read (2)
c7094399 330will return a packet that either contains a single nonzero control byte,
f81fb444 331or has a single byte containing zero (\(aq\0\(aq) followed by data
b218b023 332written on the slave side of the pseudoterminal.
097585ed
MK
333If the first byte is not
334.B TIOCPKT_DATA
335(0), it is an OR of one
fea681da 336or more of the following bits:
efeece04 337.IP
fea681da
MK
338.nf
339TIOCPKT_FLUSHREAD The read queue for the terminal is flushed.
340TIOCPKT_FLUSHWRITE The write queue for the terminal is flushed.
341TIOCPKT_STOP Output to the terminal is stopped.
342TIOCPKT_START Output to the terminal is restarted.
8bb93cd4
MK
343TIOCPKT_DOSTOP The start and stop characters are \fB^S\fP/\fB^Q\fP.
344TIOCPKT_NOSTOP The start and stop characters are not \fB^S\fP/\fB^Q\fP.
fea681da 345.fi
efeece04 346.IP
fea681da
MK
347While this mode is in use, the presence
348of control status information to be read
349from the master side may be detected by a
350.BR select (2)
3352f579
MK
351for exceptional conditions or a
352.BR poll (2)
353for the
354.I POLLPRI
355event.
efeece04 356.IP
fea681da
MK
357This mode is used by
358.BR rlogin (1)
359and
360.BR rlogind (8)
f81fb444 361to implement a remote-echoed,
8bb93cd4 362locally \fB^S\fP/\fB^Q\fP flow-controlled remote login.
a3bee85c 363.TP
118c56a8 364.BI "TIOCGPKT const int *" argp
164b4016 365(since Linux 3.8)
a3bee85c 366Return the current packet mode setting in the integer pointed to by
164b4016 367.IR argp .
bd020450
MK
368.TP
369.BI "TIOCSPTLCK int *" argp
370Set (if
371.IR *argp
372is nonzero) or remove (if
373.IR *argp
374is zero) the pseudoterminal slave device.
375(See also
376.BR unlockpt (3).)
e757b911
MK
377.TP
378.BI "TIOCGPTLCK int *" argp
164b4016 379(since Linux 3.8)
e757b911
MK
380Place the current lock state of the pseudoterminal slave device
381in the location pointed to by
164b4016 382.IR argp .
0ec74af9
AS
383.TP
384.BI "TIOCGPTPEER int " flags
91e34595
MK
385.\" commit 54ebbfb1603415d9953c150535850d30609ef077
386(since Linux 4.13)
387Given a file descriptor in
388.I fd
389that refers to a pseudoterminal master,
390open (with the given
0ec74af9 391.BR open (2)-style
91e34595
MK
392.IR flags )
393and return a new file descriptor that refers to the peer
394pseudoterminal slave device.
395This operation can be performed
396regardless of whether the pathname of the slave device
5115e06c 397is accessible through the calling process's mount namespace.
efeece04 398.IP
0ec74af9 399Security-conscious programs interacting with namespaces may wish to use this
91e34595 400operation rather than
0ec74af9 401.BR open (2)
91e34595 402with the pathname returned by
0ec74af9 403.BR ptsname (3),
91e34595 404and similar library functions that have insecure APIs.
d754b76d
MK
405(For example, confusion can occur in some cases using
406.BR ptsname (3)
407with a pathname where a devpts filesystem
408has been mounted in a different mount namespace.)
e1215bb9 409.PP
097585ed
MK
410The BSD ioctls
411.BR TIOCSTOP ,
412.BR TIOCSTART ,
413.BR TIOCUCNTL ,
0daa9e92 414.B TIOCREMOTE
fea681da 415have not been implemented under Linux.
73d8cece 416.SS Modem control
fea681da
MK
417.TP
418.BI "TIOCMGET int *" argp
2096c4f3 419Get the status of modem bits.
fea681da
MK
420.TP
421.BI "TIOCMSET const int *" argp
2096c4f3 422Set the status of modem bits.
fea681da
MK
423.TP
424.BI "TIOCMBIC const int *" argp
2096c4f3 425Clear the indicated modem bits.
fea681da
MK
426.TP
427.BI "TIOCMBIS const int *" argp
2096c4f3 428Set the indicated modem bits.
dd3568a1 429.PP
d398da7b 430The following bits are used by the above ioctls:
efeece04 431.PP
fea681da
MK
432.nf
433TIOCM_LE DSR (data set ready/line enable)
434TIOCM_DTR DTR (data terminal ready)
435TIOCM_RTS RTS (request to send)
436TIOCM_ST Secondary TXD (transmit)
437TIOCM_SR Secondary RXD (receive)
438TIOCM_CTS CTS (clear to send)
439TIOCM_CAR DCD (data carrier detect)
440TIOCM_CD see TIOCM_CAR
441TIOCM_RNG RNG (ring)
442TIOCM_RI see TIOCM_RNG
443TIOCM_DSR DSR (data set ready)
444.fi
1dcf15dd 445.TP
41556382
MK
446.BI "TIOCMIWAIT int " arg
447Wait for any of the 4 modem bits (DCD, RI, DSR, CTS) to change.
448The bits of interest are specified as a bit mask in
449.IR arg ,
450by ORing together any of the bit values,
451.BR TIOCM_RNG ,
452.BR TIOCM_DSR ,
453.BR TIOCM_CD ,
454and
455.BR TIOCM_CTS .
456The caller should use
457.B TIOCGICOUNT
458to see which bit has changed.
459.TP
1dcf15dd 460.BI "TIOCGICOUNT struct serial_icounter_struct *" argp
452f300b 461Get counts of input serial line interrupts (DCD, RI, DSR, CTS).
5ffdc2fd 462The counts are written to the
452f300b
MK
463.I serial_icounter_struct
464structure pointed to by
465.IR argp .
efeece04 466.IP
452f300b
MK
467Note: both 1->0 and 0->1 transitions are counted, except for
468RI, where only 0->1 transitions are counted.
73d8cece 469.SS Marking a line as local
fea681da
MK
470.TP
471.BI "TIOCGSOFTCAR int *" argp
472("Get software carrier flag")
473Get the status of the CLOCAL flag in the c_cflag field of the
4961e71d
MK
474.I termios
475structure.
fea681da
MK
476.TP
477.BI "TIOCSSOFTCAR const int *" argp
478("Set software carrier flag")
4961e71d
MK
479Set the CLOCAL flag in the
480.I termios
481structure when
fea681da 482.RI * argp
c7094399 483is nonzero, and clear it otherwise.
dd3568a1 484.PP
097585ed
MK
485If the
486.B CLOCAL
487flag for a line is off, the hardware carrier detect (DCD)
fea681da
MK
488signal is significant, and an
489.BR open (2)
132249c4 490of the corresponding terminal will block until DCD is asserted,
097585ed
MK
491unless the
492.B O_NONBLOCK
493flag is given.
494If
495.B CLOCAL
496is set, the line behaves as if DCD is always asserted.
fea681da
MK
497The software carrier flag is usually turned on for local devices,
498and is off for lines with modems.
73d8cece 499.SS Linux-specific
097585ed
MK
500For the
501.B TIOCLINUX
502ioctl, see
d49a2220 503.BR ioctl_console (2).
73d8cece 504.SS Kernel debugging
0daa9e92 505.B "#include <linux/tty.h>"
fea681da
MK
506.TP
507.BI "TIOCTTYGSTRUCT struct tty_struct *" argp
f88233dd
MK
508Get the
509.I tty_struct
510corresponding to
fea681da 511.IR fd .
0e61ffa7
MK
512This command was removed in Linux 2.5.67.
513.\" commit b3506a09d15dc5aee6d4bb88d759b157016e1864
514.\" Author: Andries E. Brouwer <andries.brouwer@cwi.nl>
515.\" Date: Tue Apr 1 04:42:46 2003 -0800
516.\"
517.\" [PATCH] kill TIOCTTYGSTRUCT
518.\"
519.\" Only used for (dubious) debugging purposes, and exposes
520.\" internal kernel state.
c13182ef 521.\"
d282bb24 522.\" .SS Serial info
fea681da 523.\" .BR "#include <linux/serial.h>"
ba39b288 524.\" .PP
fea681da
MK
525.\" .TP
526.\" .BI "TIOCGSERIAL struct serial_struct *" argp
527.\" Get serial info.
528.\" .TP
529.\" .BI "TIOCSSERIAL const struct serial_struct *" argp
530.\" Set serial info.
47297adb 531.SH RETURN VALUE
fea681da 532The
0b80cf56 533.BR ioctl (2)
c13182ef 534system call returns 0 on success.
dec985f9 535On error, it returns \-1 and sets
fea681da
MK
536.I errno
537appropriately.
fea681da
MK
538.SH ERRORS
539.TP
fea681da
MK
540.B EINVAL
541Invalid command parameter.
542.TP
eab64696
MK
543.B ENOIOCTLCMD
544Unknown command.
fea681da
MK
545.TP
546.B ENOTTY
547Inappropriate
548.IR fd .
eab64696
MK
549.TP
550.B EPERM
551Insufficient permission.
fea681da
MK
552.SH EXAMPLE
553Check the condition of DTR on the serial port.
efeece04 554.PP
b76974c1 555.EX
fea681da
MK
556#include <termios.h>
557#include <fcntl.h>
558#include <sys/ioctl.h>
559
cf0a9ace 560int
c13182ef 561main(void)
cf0a9ace 562{
fea681da
MK
563 int fd, serial;
564
565 fd = open("/dev/ttyS0", O_RDONLY);
566 ioctl(fd, TIOCMGET, &serial);
567 if (serial & TIOCM_DTR)
fea681da 568 puts("TIOCM_DTR is set");
e3f16cf7
MK
569 else
570 puts("TIOCM_DTR is not set");
fea681da
MK
571 close(fd);
572}
b76974c1 573.EE
47297adb 574.SH SEE ALSO
c1a68b5e 575.BR ldattach (1),
fea681da 576.BR ioctl (2),
d49a2220 577.BR ioctl_console (2),
fea681da 578.BR termios (3),
88ab292b 579.BR pty (7)
25a46448 580.\"
fea681da
MK
581.\" FIONBIO const int *
582.\" FIONCLEX void
583.\" FIOCLEX void
584.\" FIOASYNC const int *
585.\" from serial.c:
586.\" TIOCSERCONFIG void
587.\" TIOCSERGWILD int *
588.\" TIOCSERSWILD const int *
589.\" TIOCSERGSTRUCT struct async_struct *
590.\" TIOCSERGETLSR int *
591.\" TIOCSERGETMULTI struct serial_multiport_struct *
592.\" TIOCSERSETMULTI const struct serial_multiport_struct *
593.\" TIOCGSERIAL, TIOCSSERIAL (see above)