2 .\" Copyright 2002 Walter Harms <walter.harms@informatik.uni-oldenburg.de>
3 .\" and Andries Brouwer <aeb@cwi.nl>.
5 .\" SPDX-License-Identifier: GPL-1.0-or-later
7 .TH ioctl_tty 2 (date) "Linux man-pages (unreleased)"
9 ioctl_tty \- ioctls for terminals and serial lines
12 .RI ( libc ", " \-lc )
15 .B #include <sys/ioctl.h>
16 .BR "#include <asm/termbits.h>" " /* Definition of " "struct termios" ,
17 .BR " struct termios2" ", and"
18 .BR " Bnnn" ", " BOTHER ", " CBAUD ", " CLOCAL ,
19 .BR " TC*" { FLUSH , ON , OFF "} and other constants */"
21 .BI "int ioctl(int " fd ", int " op ", ...);"
26 call for terminals and serial ports accepts many possible operation arguments.
27 Most require a third argument, of varying type, here called
34 makes for nonportable programs.
35 Use the POSIX interface described in
43 is different and incompatible with
47 These ioctl calls require
50 .IR <asm/termbits.h> .
51 .SS Get and set terminal attributes
55 .BI "struct termios\~*" argp
58 .IR "tcgetattr(fd, argp)" .
60 Get the current serial port settings.
64 .BI "const struct termios\~*" argp
67 .IR "tcsetattr(fd, TCSANOW, argp)" .
69 Set the current serial port settings.
73 .BI "const struct termios\~*" argp
76 .IR "tcsetattr(fd, TCSADRAIN, argp)" .
78 Allow the output buffer to drain, and
79 set the current serial port settings.
83 .BI "const struct termios\~*" argp
86 .IR "tcsetattr(fd, TCSAFLUSH, argp)" .
88 Allow the output buffer to drain, discard pending input, and
89 set the current serial port settings.
91 The following four ioctls, added in Linux 2.6.20,
92 .\" commit 64bb6c5e1ddcd47c951740485026ef08975ee2e6
93 .\" commit 592ee3a5e5e2a981ef2829a0380093006d045661
99 except that they take a
100 .I "struct termios2\~*"
102 .IR "struct termios\~*" .
103 If the structure member
107 then the baud rate is stored in the structure members
112 These ioctls are not supported on all architectures.
116 TCGETS2 \fBstruct termios2 *\fPargp
117 TCSETS2 \fBconst struct termios2 *\fPargp
118 TCSETSW2 \fBconst struct termios2 *\fPargp
119 TCSETSF2 \fBconst struct termios2 *\fPargp
123 The following four ioctls are just like
128 except that they take a
129 .I "struct termio\~*"
131 .IR "struct termios\~*" .
135 TCGETA \fBstruct termio *\fPargp
136 TCSETA \fBconst struct termio *\fPargp
137 TCSETAW \fBconst struct termio *\fPargp
138 TCSETAF \fBconst struct termio *\fPargp
141 .SS Locking the termios structure
144 structure of a terminal can be locked.
147 structure, with nonzero bits or fields indicating a
152 .BI "struct termios\~*" argp
154 Gets the locking status of the
156 structure of the terminal.
160 .BI "const struct termios\~*" argp
162 Sets the locking status of the
164 structure of the terminal.
165 Only a process with the
167 capability can do this.
168 .SS Get and set window size
169 Window sizes are kept in the kernel, but not used by the kernel
170 (except in the case of virtual consoles, where the kernel will
171 update the window size when the size of the virtual console changes,
172 for example, by loading a new font).
176 .BI "struct winsize\~*" argp
182 .BI "const struct winsize\~*" argp
186 The struct used by these ioctls is defined as
191 unsigned short ws_row;
192 unsigned short ws_col;
193 unsigned short ws_xpixel; /* unused */
194 unsigned short ws_ypixel; /* unused */
199 When the window size changes, a
201 signal is sent to the
202 foreground process group.
210 .IR "tcsendbreak(fd, arg)" .
212 If the terminal is using asynchronous serial data transmission, and
214 is zero, then send a break (a stream of zero bits) for between
215 0.25 and 0.5 seconds.
216 If the terminal is not using asynchronous
217 serial data transmission, then either a break is sent, or the function
218 returns without doing anything.
221 is nonzero, nobody knows what will happen.
223 (SVr4, UnixWare, Solaris, and Linux treat
224 .I "tcsendbreak(fd,arg)"
231 as a multiplier, and sends a stream of bits
233 times as long as done for zero
237 (when nonzero) as a time interval measured in milliseconds.
245 So-called "POSIX version" of
249 as a time interval measured in deciseconds, and does nothing
250 when the driver does not support breaks.
256 Turn break on, that is, start sending zero bits.
262 Turn break off, that is, stop sending zero bits.
263 .SS Software flow control
270 .IR "tcflow(fd, arg)" .
274 for the argument values
279 .SS Buffer count and flushing
285 Get the number of bytes in the input buffer.
298 Get the number of bytes in the output buffer.
305 .IR "tcflush(fd, arg)" .
309 for the argument values
318 Get line status register.
322 output buffer is empty and also hardware transmitter is physically empty.
324 Does not have to be supported by all serial tty drivers.
327 does not wait and returns immediately when
334 .BI "const char\~*" argp
336 Insert the given byte in the input queue.
339 .\" commit 690c8b804ad2eafbd35da5d3c95ad325ca7d5061
340 .\" commit 83efeeeb3d04b22aaed1df99bc70a48fe9d22c4d
341 this operation may require the
344 .I dev.tty.legacy_tiocsti
345 sysctl variable is set to false).
346 .SS Redirecting console output
352 Redirect output that would have gone to
356 to the given terminal.
357 If that was a pseudoterminal master, send it to the slave.
359 anybody can do this as long as the output was not redirected yet;
360 since Linux 2.6.10, only a process with the
362 capability may do this.
363 If output was redirected already, then
366 but redirection can be stopped by using this ioctl with
372 .SS Controlling terminal
378 Make the given terminal the controlling terminal of the calling process.
379 The calling process must be a session leader and not have a
380 controlling terminal already.
383 should be specified as zero.
385 If this terminal is already the controlling terminal
386 of a different session group, then the ioctl fails with
388 unless the caller has the
392 equals 1, in which case the terminal is stolen, and all processes that had
393 it as controlling terminal lose it.
399 If the given terminal was the controlling terminal of the calling process,
400 give up this controlling terminal.
401 If the process was session leader,
406 to the foreground process group
407 and all processes in the current session lose their controlling terminal.
408 .SS Process group and session ID
414 When successful, equivalent to
415 .IR "*argp = tcgetpgrp(fd)" .
417 Get the process group ID of the foreground process group on this terminal.
421 .BI "const pid_t\~*" argp
424 .IR "tcsetpgrp(fd, *argp)" .
426 Set the foreground process group ID of this terminal.
432 When successful, equivalent to
433 .IR "*argp = tcgetsid(fd)" .
435 Get the session ID of the given terminal.
436 This fails with the error
438 if the terminal is not a master pseudoterminal
439 and not our controlling terminal.
447 Put the terminal into exclusive mode.
450 operations on the terminal are permitted.
453 except for a process with the
462 If the terminal is currently in exclusive mode,
463 place a nonzero value in the location pointed to by
465 otherwise, place zero in
472 Disable exclusive mode.
479 Get the line discipline of the terminal.
483 .BI "const int\~*" argp
485 Set the line discipline of the terminal.
486 .SS Pseudoterminal ioctls
490 .BI "const int\~*" argp
494 is nonzero) or disable packet mode.
495 Can be applied to the master side of a pseudoterminal only (and will return
498 In packet mode, each subsequent
500 will return a packet that either contains a single nonzero control byte,
501 or has a single byte containing zero (\[aq]\e0\[aq]) followed by data
502 written on the slave side of the pseudoterminal.
503 If the first byte is not
505 (0), it is an OR of one
506 or more of the following bits:
512 The read queue for the terminal is flushed.
514 TIOCPKT_FLUSHWRITE T{
515 The write queue for the terminal is flushed.
518 Output to the terminal is stopped.
521 Output to the terminal is restarted.
524 The start and stop characters are \fB\[ha]S\fP/\fB\[ha]Q\fP.
527 The start and stop characters are not \fB\[ha]S\fP/\fB\[ha]Q\fP.
532 While packet mode is in use, the presence
533 of control status information to be read
534 from the master side may be detected by a
536 for exceptional conditions or a
546 to implement a remote-echoed,
547 locally \fB\[ha]S\fP/\fB\[ha]Q\fP flow-controlled remote login.
551 .BI "const int\~*" argp
554 Return the current packet mode setting in the integer pointed to by
563 is nonzero) or remove (if
565 is zero) the lock on the pseudoterminal slave device.
574 Place the current lock state of the pseudoterminal slave device
575 in the location pointed to by
582 .\" commit 54ebbfb1603415d9953c150535850d30609ef077
584 Given a file descriptor in
586 that refers to a pseudoterminal master,
590 and return a new file descriptor that refers to the peer
591 pseudoterminal slave device.
592 This operation can be performed
593 regardless of whether the pathname of the slave device
594 is accessible through the calling process's mount namespace.
596 Security-conscious programs interacting with namespaces may wish to use this
597 operation rather than
599 with the pathname returned by
601 and similar library functions that have insecure APIs.
602 (For example, confusion can occur in some cases using
604 with a pathname where a devpts filesystem
605 has been mounted in a different mount namespace.)
613 have not been implemented under Linux.
620 Get the status of modem bits.
624 .BI "const int\~*" argp
626 Set the status of modem bits.
630 .BI "const int\~*" argp
632 Clear the indicated modem bits.
636 .BI "const int\~*" argp
638 Set the indicated modem bits.
640 The following bits are used by the above ioctls:
644 TIOCM_LE DSR (data set ready/line enable)
645 TIOCM_DTR DTR (data terminal ready)
646 TIOCM_RTS RTS (request to send)
647 TIOCM_ST Secondary TXD (transmit)
648 TIOCM_SR Secondary RXD (receive)
649 TIOCM_CTS CTS (clear to send)
650 TIOCM_CAR DCD (data carrier detect)
651 TIOCM_CD see TIOCM_CAR
653 TIOCM_RI see TIOCM_RNG
654 TIOCM_DSR DSR (data set ready)
661 Wait for any of the 4 modem bits (DCD, RI, DSR, CTS) to change.
662 The bits of interest are specified as a bit mask in
664 by ORing together any of the bit values,
670 The caller should use
672 to see which bit has changed.
676 .BI "struct serial_icounter_struct\~*" argp
678 Get counts of input serial line interrupts (DCD, RI, DSR, CTS).
679 The counts are written to the
680 .I serial_icounter_struct
681 structure pointed to by
684 Note: both 1->0 and 0->1 transitions are counted, except for
685 RI, where only 0->1 transitions are counted.
686 .SS Marking a line as local
692 ("Get software carrier flag")
693 Get the status of the CLOCAL flag in the c_cflag field of the
699 .BI "const int\~*" argp
701 ("Set software carrier flag")
702 Set the CLOCAL flag in the
706 is nonzero, and clear it otherwise.
710 flag for a line is off, the hardware carrier detect (DCD)
711 signal is significant, and an
713 of the corresponding terminal will block until DCD is asserted,
719 is set, the line behaves as if DCD is always asserted.
720 The software carrier flag is usually turned on for local devices,
721 and is off for lines with modems.
726 .BR ioctl_console (2).
728 .B "#include <linux/tty.h>"
732 .BI "struct tty_struct\~*" argp
738 This operation was removed in Linux 2.5.67.
739 .\" commit b3506a09d15dc5aee6d4bb88d759b157016e1864
740 .\" Author: Andries E. Brouwer <andries.brouwer@cwi.nl>
741 .\" Date: Tue Apr 1 04:42:46 2003 -0800
743 .\" [PATCH] kill TIOCTTYGSTRUCT
745 .\" Only used for (dubious) debugging purposes, and exposes
746 .\" internal kernel state.
749 .\" .BR "#include <linux/serial.h>"
752 .\" .BI "TIOCGSERIAL struct serial_struct *" argp
755 .\" .BI "TIOCSSERIAL const struct serial_struct *" argp
760 system call returns 0 on success.
761 On error, it returns \-1 and sets
763 to indicate the error.
767 Invalid operation parameter.
777 Insufficient permission.
779 Check the condition of DTR on the serial port.
781 .\" SRC BEGIN (tiocmget.c)
785 #include <sys/ioctl.h>
793 fd = open("/dev/ttyS0", O_RDONLY);
794 ioctl(fd, TIOCMGET, &serial);
795 if (serial & TIOCM_DTR)
796 puts("TIOCM_DTR is set");
798 puts("TIOCM_DTR is not set");
804 Get or set arbitrary baudrate on the serial port.
806 .\" SRC BEGIN (tcgets.c)
808 /* SPDX-License-Identifier: GPL-2.0-or-later */
810 #include <asm/termbits.h>
814 #include <sys/ioctl.h>
818 main(int argc, char *argv[])
821 fprintf(stderr, "BOTHER is unsupported\en");
822 /* Program may fallback to TCGETS/TCSETS with Bnnn constants */
825 /* Declare tio structure, its type depends on supported ioctl */
833 if (argc != 2 && argc != 3 && argc != 4) {
834 fprintf(stderr, "Usage: %s device [output [input] ]\en", argv[0]);
838 fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY);
844 /* Get the current serial port settings via supported ioctl */
846 rc = ioctl(fd, TCGETS2, &tio);
848 rc = ioctl(fd, TCGETS, &tio);
856 /* Change baud rate when more arguments were provided */
857 if (argc == 3 || argc == 4) {
858 /* Clear the current output baud rate and fill a new value */
859 tio.c_cflag &= \[ti]CBAUD;
860 tio.c_cflag |= BOTHER;
861 tio.c_ospeed = atoi(argv[2]);
863 /* Clear the current input baud rate and fill a new value */
864 tio.c_cflag &= \[ti](CBAUD << IBSHIFT);
865 tio.c_cflag |= BOTHER << IBSHIFT;
866 /* When 4th argument is not provided reuse output baud rate */
867 tio.c_ispeed = (argc == 4) ? atoi(argv[3]) : atoi(argv[2]);
869 /* Set new serial port settings via supported ioctl */
871 rc = ioctl(fd, TCSETS2, &tio);
873 rc = ioctl(fd, TCSETS, &tio);
881 /* And get new values which were really configured */
883 rc = ioctl(fd, TCGETS2, &tio);
885 rc = ioctl(fd, TCGETS, &tio);
896 printf("output baud rate: %u\en", tio.c_ospeed);
897 printf("input baud rate: %u\en", tio.c_ispeed);
907 .BR ioctl_console (2),
911 .\" FIONBIO const int *
914 .\" FIOASYNC const int *
916 .\" TIOCSERCONFIG void
917 .\" TIOCSERGWILD int *
918 .\" TIOCSERSWILD const int *
919 .\" TIOCSERGSTRUCT struct async_struct *
920 .\" TIOCSERGETMULTI struct serial_multiport_struct *
921 .\" TIOCSERSETMULTI const struct serial_multiport_struct *
922 .\" TIOCGSERIAL, TIOCSSERIAL (see above)