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 " cmd ", ...);"
26 call for terminals and serial ports accepts many possible command 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.
337 .SS Redirecting console output
343 Redirect output that would have gone to
347 to the given terminal.
348 If that was a pseudoterminal master, send it to the slave.
350 anybody can do this as long as the output was not redirected yet;
351 since Linux 2.6.10, only a process with the
353 capability may do this.
354 If output was redirected already, then
357 but redirection can be stopped by using this ioctl with
363 .SS Controlling terminal
369 Make the given terminal the controlling terminal of the calling process.
370 The calling process must be a session leader and not have a
371 controlling terminal already.
374 should be specified as zero.
376 If this terminal is already the controlling terminal
377 of a different session group, then the ioctl fails with
379 unless the caller has the
383 equals 1, in which case the terminal is stolen, and all processes that had
384 it as controlling terminal lose it.
390 If the given terminal was the controlling terminal of the calling process,
391 give up this controlling terminal.
392 If the process was session leader,
397 to the foreground process group
398 and all processes in the current session lose their controlling terminal.
399 .SS Process group and session ID
405 When successful, equivalent to
406 .IR "*argp = tcgetpgrp(fd)" .
408 Get the process group ID of the foreground process group on this terminal.
412 .BI "const pid_t\~*" argp
415 .IR "tcsetpgrp(fd, *argp)" .
417 Set the foreground process group ID of this terminal.
423 When successful, equivalent to
424 .IR "*argp = tcgetsid(fd)" .
426 Get the session ID of the given terminal.
427 This fails with the error
429 if the terminal is not a master pseudoterminal
430 and not our controlling terminal.
438 Put the terminal into exclusive mode.
441 operations on the terminal are permitted.
444 except for a process with the
453 If the terminal is currently in exclusive mode,
454 place a nonzero value in the location pointed to by
456 otherwise, place zero in
463 Disable exclusive mode.
470 Get the line discipline of the terminal.
474 .BI "const int\~*" argp
476 Set the line discipline of the terminal.
477 .SS Pseudoterminal ioctls
481 .BI "const int\~*" argp
485 is nonzero) or disable packet mode.
486 Can be applied to the master side of a pseudoterminal only (and will return
489 In packet mode, each subsequent
491 will return a packet that either contains a single nonzero control byte,
492 or has a single byte containing zero (\(aq\e0\(aq) followed by data
493 written on the slave side of the pseudoterminal.
494 If the first byte is not
496 (0), it is an OR of one
497 or more of the following bits:
503 The read queue for the terminal is flushed.
505 TIOCPKT_FLUSHWRITE T{
506 The write queue for the terminal is flushed.
509 Output to the terminal is stopped.
512 Output to the terminal is restarted.
515 The start and stop characters are \fB\(haS\fP/\fB\(haQ\fP.
518 The start and stop characters are not \fB\(haS\fP/\fB\(haQ\fP.
523 While packet mode is in use, the presence
524 of control status information to be read
525 from the master side may be detected by a
527 for exceptional conditions or a
537 to implement a remote-echoed,
538 locally \fB\(haS\fP/\fB\(haQ\fP flow-controlled remote login.
542 .BI "const int\~*" argp
545 Return the current packet mode setting in the integer pointed to by
554 is nonzero) or remove (if
556 is zero) the lock on the pseudoterminal slave device.
565 Place the current lock state of the pseudoterminal slave device
566 in the location pointed to by
573 .\" commit 54ebbfb1603415d9953c150535850d30609ef077
575 Given a file descriptor in
577 that refers to a pseudoterminal master,
581 and return a new file descriptor that refers to the peer
582 pseudoterminal slave device.
583 This operation can be performed
584 regardless of whether the pathname of the slave device
585 is accessible through the calling process's mount namespace.
587 Security-conscious programs interacting with namespaces may wish to use this
588 operation rather than
590 with the pathname returned by
592 and similar library functions that have insecure APIs.
593 (For example, confusion can occur in some cases using
595 with a pathname where a devpts filesystem
596 has been mounted in a different mount namespace.)
604 have not been implemented under Linux.
611 Get the status of modem bits.
615 .BI "const int\~*" argp
617 Set the status of modem bits.
621 .BI "const int\~*" argp
623 Clear the indicated modem bits.
627 .BI "const int\~*" argp
629 Set the indicated modem bits.
631 The following bits are used by the above ioctls:
635 TIOCM_LE DSR (data set ready/line enable)
636 TIOCM_DTR DTR (data terminal ready)
637 TIOCM_RTS RTS (request to send)
638 TIOCM_ST Secondary TXD (transmit)
639 TIOCM_SR Secondary RXD (receive)
640 TIOCM_CTS CTS (clear to send)
641 TIOCM_CAR DCD (data carrier detect)
642 TIOCM_CD see TIOCM_CAR
644 TIOCM_RI see TIOCM_RNG
645 TIOCM_DSR DSR (data set ready)
652 Wait for any of the 4 modem bits (DCD, RI, DSR, CTS) to change.
653 The bits of interest are specified as a bit mask in
655 by ORing together any of the bit values,
661 The caller should use
663 to see which bit has changed.
667 .BI "struct serial_icounter_struct\~*" argp
669 Get counts of input serial line interrupts (DCD, RI, DSR, CTS).
670 The counts are written to the
671 .I serial_icounter_struct
672 structure pointed to by
675 Note: both 1->0 and 0->1 transitions are counted, except for
676 RI, where only 0->1 transitions are counted.
677 .SS Marking a line as local
683 ("Get software carrier flag")
684 Get the status of the CLOCAL flag in the c_cflag field of the
690 .BI "const int\~*" argp
692 ("Set software carrier flag")
693 Set the CLOCAL flag in the
697 is nonzero, and clear it otherwise.
701 flag for a line is off, the hardware carrier detect (DCD)
702 signal is significant, and an
704 of the corresponding terminal will block until DCD is asserted,
710 is set, the line behaves as if DCD is always asserted.
711 The software carrier flag is usually turned on for local devices,
712 and is off for lines with modems.
717 .BR ioctl_console (2).
719 .B "#include <linux/tty.h>"
723 .BI "struct tty_struct\~*" argp
729 This command was removed in Linux 2.5.67.
730 .\" commit b3506a09d15dc5aee6d4bb88d759b157016e1864
731 .\" Author: Andries E. Brouwer <andries.brouwer@cwi.nl>
732 .\" Date: Tue Apr 1 04:42:46 2003 -0800
734 .\" [PATCH] kill TIOCTTYGSTRUCT
736 .\" Only used for (dubious) debugging purposes, and exposes
737 .\" internal kernel state.
740 .\" .BR "#include <linux/serial.h>"
743 .\" .BI "TIOCGSERIAL struct serial_struct *" argp
746 .\" .BI "TIOCSSERIAL const struct serial_struct *" argp
751 system call returns 0 on success.
752 On error, it returns \-1 and sets
754 to indicate the error.
758 Invalid command parameter.
768 Insufficient permission.
770 Check the condition of DTR on the serial port.
772 .\" SRC BEGIN (tiocmget.c)
776 #include <sys/ioctl.h>
784 fd = open("/dev/ttyS0", O_RDONLY);
785 ioctl(fd, TIOCMGET, &serial);
786 if (serial & TIOCM_DTR)
787 puts("TIOCM_DTR is set");
789 puts("TIOCM_DTR is not set");
795 Get or set arbitrary baudrate on the serial port.
797 .\" SRC BEGIN (tcgets.c)
799 /* SPDX-License-Identifier: GPL-2.0-or-later */
801 #include <asm/termbits.h>
805 #include <sys/ioctl.h>
809 main(int argc, char *argv[])
812 fprintf(stderr, "BOTHER is unsupported\en");
813 /* Program may fallback to TCGETS/TCSETS with Bnnn constants */
816 /* Declare tio structure, its type depends on supported ioctl */
824 if (argc != 2 && argc != 3 && argc != 4) {
825 fprintf(stderr, "Usage: %s device [output [input] ]\en", argv[0]);
829 fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY);
835 /* Get the current serial port settings via supported ioctl */
837 rc = ioctl(fd, TCGETS2, &tio);
839 rc = ioctl(fd, TCGETS, &tio);
847 /* Change baud rate when more arguments were provided */
848 if (argc == 3 || argc == 4) {
849 /* Clear the current output baud rate and fill a new value */
850 tio.c_cflag &= \(tiCBAUD;
851 tio.c_cflag |= BOTHER;
852 tio.c_ospeed = atoi(argv[2]);
854 /* Clear the current input baud rate and fill a new value */
855 tio.c_cflag &= \(ti(CBAUD << IBSHIFT);
856 tio.c_cflag |= BOTHER << IBSHIFT;
857 /* When 4th argument is not provided reuse output baud rate */
858 tio.c_ispeed = (argc == 4) ? atoi(argv[3]) : atoi(argv[2]);
860 /* Set new serial port settings via supported ioctl */
862 rc = ioctl(fd, TCSETS2, &tio);
864 rc = ioctl(fd, TCSETS, &tio);
872 /* And get new values which were really configured */
874 rc = ioctl(fd, TCGETS2, &tio);
876 rc = ioctl(fd, TCGETS, &tio);
887 printf("output baud rate: %u\en", tio.c_ospeed);
888 printf("input baud rate: %u\en", tio.c_ispeed);
898 .BR ioctl_console (2),
902 .\" FIONBIO const int *
905 .\" FIOASYNC const int *
907 .\" TIOCSERCONFIG void
908 .\" TIOCSERGWILD int *
909 .\" TIOCSERSWILD const int *
910 .\" TIOCSERGSTRUCT struct async_struct *
911 .\" TIOCSERGETMULTI struct serial_multiport_struct *
912 .\" TIOCSERSETMULTI const struct serial_multiport_struct *
913 .\" TIOCGSERIAL, TIOCSSERIAL (see above)