1 .\" Copyright 2002 Walter Harms <walter.harms@informatik.uni-oldenburg.de>
2 .\" and Andries Brouwer <aeb@cwi.nl>.
4 .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
5 .\" Distributed under GPL
8 .TH IOCTL_TTY 2 2021-08-27 "Linux" "Linux Programmer's Manual"
10 ioctl_tty \- ioctls for terminals and serial lines
13 .B #include <sys/ioctl.h>
14 .BR "#include <asm/termbits.h>" " /* Definition of " "struct termios" ,
15 .BR " struct termios2" ", and"
16 .BR " Bnnn" ", " BOTHER ", " CBAUD ", " CLOCAL ,
17 .BR " TC*" { FLUSH , ON , OFF "} and other constants */"
19 .BI "int ioctl(int " fd ", int " cmd ", ...);"
24 call for terminals and serial ports accepts many possible command arguments.
25 Most require a third argument, of varying type, here called
32 makes for nonportable programs.
33 Use the POSIX interface described in
41 is different and incompatible with
45 These ioctl calls require
48 .IR <asm/termbits.h> .
49 .SS Get and set terminal attributes
53 .BI "struct termios *" argp
56 .IR "tcgetattr(fd, argp)" .
58 Get the current serial port settings.
62 .BI "const struct termios *" argp
65 .IR "tcsetattr(fd, TCSANOW, argp)" .
67 Set the current serial port settings.
71 .BI "const struct termios *" argp
74 .IR "tcsetattr(fd, TCSADRAIN, argp)" .
76 Allow the output buffer to drain, and
77 set the current serial port settings.
81 .BI "const struct termios *" argp
84 .IR "tcsetattr(fd, TCSAFLUSH, argp)" .
86 Allow the output buffer to drain, discard pending input, and
87 set the current serial port settings.
89 The following four ioctls, added in Linux 2.6.20,
90 .\" commit 64bb6c5e1ddcd47c951740485026ef08975ee2e6
91 .\" commit 592ee3a5e5e2a981ef2829a0380093006d045661
97 except that they take a
98 .I "struct termios2\ *"
100 .IR "struct termios\ *" .
101 If the structure member
105 then the baud rate is stored in the structure members
110 These ioctls are not supported on all architectures.
114 TCGETS2 \fBstruct termios2 *\fPargp
115 TCSETS2 \fBconst struct termios2 *\fPargp
116 TCSETSW2 \fBconst struct termios2 *\fPargp
117 TCSETSF2 \fBconst struct termios2 *\fPargp
121 The following four ioctls are just like
126 except that they take a
127 .I "struct termio\ *"
129 .IR "struct termios\ *" .
133 TCGETA \fBstruct termio *\fPargp
134 TCSETA \fBconst struct termio *\fPargp
135 TCSETAW \fBconst struct termio *\fPargp
136 TCSETAF \fBconst struct termio *\fPargp
139 .SS Locking the termios structure
142 structure of a terminal can be locked.
145 structure, with nonzero bits or fields indicating a
150 .BI "struct termios *" argp
152 Gets the locking status of the
154 structure of the terminal.
158 .BI "const struct termios *" argp
160 Sets the locking status of the
162 structure of the terminal.
163 Only a process with the
165 capability can do this.
166 .SS Get and set window size
167 Window sizes are kept in the kernel, but not used by the kernel
168 (except in the case of virtual consoles, where the kernel will
169 update the window size when the size of the virtual console changes,
170 for example, by loading a new font).
174 .BI "struct winsize *" argp
180 .BI "const struct winsize *" argp
184 The struct used by these ioctls is defined as
189 unsigned short ws_row;
190 unsigned short ws_col;
191 unsigned short ws_xpixel; /* unused */
192 unsigned short ws_ypixel; /* unused */
197 When the window size changes, a
199 signal is sent to the
200 foreground process group.
208 .IR "tcsendbreak(fd, arg)" .
210 If the terminal is using asynchronous serial data transmission, and
212 is zero, then send a break (a stream of zero bits) for between
213 0.25 and 0.5 seconds.
214 If the terminal is not using asynchronous
215 serial data transmission, then either a break is sent, or the function
216 returns without doing anything.
219 is nonzero, nobody knows what will happen.
221 (SVr4, UnixWare, Solaris, and Linux treat
222 .I "tcsendbreak(fd,arg)"
229 as a multiplier, and sends a stream of bits
231 times as long as done for zero
235 (when nonzero) as a time interval measured in milliseconds.
243 So-called "POSIX version" of
247 as a time interval measured in deciseconds, and does nothing
248 when the driver does not support breaks.
254 Turn break on, that is, start sending zero bits.
260 Turn break off, that is, stop sending zero bits.
261 .SS Software flow control
268 .IR "tcflow(fd, arg)" .
272 for the argument values
277 .SS Buffer count and flushing
283 Get the number of bytes in the input buffer.
296 Get the number of bytes in the output buffer.
303 .IR "tcflush(fd, arg)" .
307 for the argument values
315 .BI "const char *" argp
317 Insert the given byte in the input queue.
318 .SS Redirecting console output
324 Redirect output that would have gone to
328 to the given terminal.
329 If that was a pseudoterminal master, send it to the slave.
330 In Linux before version 2.6.10,
331 anybody can do this as long as the output was not redirected yet;
332 since version 2.6.10, only a process with the
334 capability may do this.
335 If output was redirected already, then
338 but redirection can be stopped by using this ioctl with
344 .SS Controlling terminal
350 Make the given terminal the controlling terminal of the calling process.
351 The calling process must be a session leader and not have a
352 controlling terminal already.
355 should be specified as zero.
357 If this terminal is already the controlling terminal
358 of a different session group, then the ioctl fails with
360 unless the caller has the
364 equals 1, in which case the terminal is stolen, and all processes that had
365 it as controlling terminal lose it.
371 If the given terminal was the controlling terminal of the calling process,
372 give up this controlling terminal.
373 If the process was session leader,
378 to the foreground process group
379 and all processes in the current session lose their controlling terminal.
380 .SS Process group and session ID
386 When successful, equivalent to
387 .IR "*argp = tcgetpgrp(fd)" .
389 Get the process group ID of the foreground process group on this terminal.
393 .BI "const pid_t *" argp
396 .IR "tcsetpgrp(fd, *argp)" .
398 Set the foreground process group ID of this terminal.
404 When successful, equivalent to
405 .IR "*argp = tcgetsid(fd)" .
407 Get the session ID of the given terminal.
408 This fails with the error
410 if the terminal is not a master pseudoterminal
411 and not our controlling terminal.
419 Put the terminal into exclusive mode.
422 operations on the terminal are permitted.
425 except for a process with the
434 If the terminal is currently in exclusive mode,
435 place a nonzero value in the location pointed to by
437 otherwise, place zero in
444 Disable exclusive mode.
451 Get the line discipline of the terminal.
455 .BI "const int *" argp
457 Set the line discipline of the terminal.
458 .SS Pseudoterminal ioctls
462 .BI "const int *" argp
466 is nonzero) or disable packet mode.
467 Can be applied to the master side of a pseudoterminal only (and will return
470 In packet mode, each subsequent
472 will return a packet that either contains a single nonzero control byte,
473 or has a single byte containing zero (\(aq\e0\(aq) followed by data
474 written on the slave side of the pseudoterminal.
475 If the first byte is not
477 (0), it is an OR of one
478 or more of the following bits:
484 The read queue for the terminal is flushed.
486 TIOCPKT_FLUSHWRITE T{
487 The write queue for the terminal is flushed.
490 Output to the terminal is stopped.
493 Output to the terminal is restarted.
496 The start and stop characters are \fB\(haS\fP/\fB\(haQ\fP.
499 The start and stop characters are not \fB\(haS\fP/\fB\(haQ\fP.
504 While packet mode is in use, the presence
505 of control status information to be read
506 from the master side may be detected by a
508 for exceptional conditions or a
518 to implement a remote-echoed,
519 locally \fB\(haS\fP/\fB\(haQ\fP flow-controlled remote login.
523 .BI "const int *" argp
526 Return the current packet mode setting in the integer pointed to by
535 is nonzero) or remove (if
537 is zero) the lock on the pseudoterminal slave device.
546 Place the current lock state of the pseudoterminal slave device
547 in the location pointed to by
554 .\" commit 54ebbfb1603415d9953c150535850d30609ef077
556 Given a file descriptor in
558 that refers to a pseudoterminal master,
562 and return a new file descriptor that refers to the peer
563 pseudoterminal slave device.
564 This operation can be performed
565 regardless of whether the pathname of the slave device
566 is accessible through the calling process's mount namespace.
568 Security-conscious programs interacting with namespaces may wish to use this
569 operation rather than
571 with the pathname returned by
573 and similar library functions that have insecure APIs.
574 (For example, confusion can occur in some cases using
576 with a pathname where a devpts filesystem
577 has been mounted in a different mount namespace.)
585 have not been implemented under Linux.
592 Get the status of modem bits.
596 .BI "const int *" argp
598 Set the status of modem bits.
602 .BI "const int *" argp
604 Clear the indicated modem bits.
608 .BI "const int *" argp
610 Set the indicated modem bits.
612 The following bits are used by the above ioctls:
616 TIOCM_LE DSR (data set ready/line enable)
617 TIOCM_DTR DTR (data terminal ready)
618 TIOCM_RTS RTS (request to send)
619 TIOCM_ST Secondary TXD (transmit)
620 TIOCM_SR Secondary RXD (receive)
621 TIOCM_CTS CTS (clear to send)
622 TIOCM_CAR DCD (data carrier detect)
623 TIOCM_CD see TIOCM_CAR
625 TIOCM_RI see TIOCM_RNG
626 TIOCM_DSR DSR (data set ready)
633 Wait for any of the 4 modem bits (DCD, RI, DSR, CTS) to change.
634 The bits of interest are specified as a bit mask in
636 by ORing together any of the bit values,
642 The caller should use
644 to see which bit has changed.
648 .BI "struct serial_icounter_struct *" argp
650 Get counts of input serial line interrupts (DCD, RI, DSR, CTS).
651 The counts are written to the
652 .I serial_icounter_struct
653 structure pointed to by
656 Note: both 1->0 and 0->1 transitions are counted, except for
657 RI, where only 0->1 transitions are counted.
658 .SS Marking a line as local
664 ("Get software carrier flag")
665 Get the status of the CLOCAL flag in the c_cflag field of the
671 .BI "const int *" argp
673 ("Set software carrier flag")
674 Set the CLOCAL flag in the
678 is nonzero, and clear it otherwise.
682 flag for a line is off, the hardware carrier detect (DCD)
683 signal is significant, and an
685 of the corresponding terminal will block until DCD is asserted,
691 is set, the line behaves as if DCD is always asserted.
692 The software carrier flag is usually turned on for local devices,
693 and is off for lines with modems.
698 .BR ioctl_console (2).
700 .B "#include <linux/tty.h>"
704 .BI "struct tty_struct *" argp
710 This command was removed in Linux 2.5.67.
711 .\" commit b3506a09d15dc5aee6d4bb88d759b157016e1864
712 .\" Author: Andries E. Brouwer <andries.brouwer@cwi.nl>
713 .\" Date: Tue Apr 1 04:42:46 2003 -0800
715 .\" [PATCH] kill TIOCTTYGSTRUCT
717 .\" Only used for (dubious) debugging purposes, and exposes
718 .\" internal kernel state.
721 .\" .BR "#include <linux/serial.h>"
724 .\" .BI "TIOCGSERIAL struct serial_struct *" argp
727 .\" .BI "TIOCSSERIAL const struct serial_struct *" argp
732 system call returns 0 on success.
733 On error, it returns \-1 and sets
735 to indicate the error.
739 Invalid command parameter.
749 Insufficient permission.
751 Check the condition of DTR on the serial port.
757 #include <sys/ioctl.h>
764 fd = open("/dev/ttyS0", O_RDONLY);
765 ioctl(fd, TIOCMGET, &serial);
766 if (serial & TIOCM_DTR)
767 puts("TIOCM_DTR is set");
769 puts("TIOCM_DTR is not set");
774 Get or set arbitrary baudrate on the serial port.
777 /* SPDX-License-Identifier: GPL-2.0-or-later */
779 #include <asm/termbits.h>
783 #include <sys/ioctl.h>
784 #include <sys/types.h>
788 main(int argc, char *argv[])
791 fprintf(stderr, "BOTHER is unsupported\en");
792 /* Program may fallback to TCGETS/TCSETS with Bnnn constants */
795 /* Declare tio structure, its type depends on supported ioctl */
803 if (argc != 2 && argc != 3 && argc != 4) {
804 fprintf(stderr, "Usage: %s device [output [input] ]\en", argv[0]);
808 fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY);
814 /* Get the current serial port settings via supported ioctl */
816 rc = ioctl(fd, TCGETS2, &tio);
818 rc = ioctl(fd, TCGETS, &tio);
826 /* Change baud rate when more arguments were provided */
827 if (argc == 3 || argc == 4) {
828 /* Clear the current output baud rate and fill a new value */
829 tio.c_cflag &= ~CBAUD;
830 tio.c_cflag |= BOTHER;
831 tio.c_ospeed = atoi(argv[2]);
833 /* Clear the current input baud rate and fill a new value */
834 tio.c_cflag &= ~(CBAUD << IBSHIFT);
835 tio.c_cflag |= BOTHER << IBSHIFT;
836 /* When 4th argument is not provided reuse output baud rate */
837 tio.c_ispeed = (argc == 4) ? atoi(argv[3]) : atoi(argv[2]);
839 /* Set new serial port settings via supported ioctl */
841 rc = ioctl(fd, TCSETS2, &tio);
843 rc = ioctl(fd, TCSETS, &tio);
851 /* And get new values which were really configured */
853 rc = ioctl(fd, TCGETS2, &tio);
855 rc = ioctl(fd, TCGETS, &tio);
866 printf("output baud rate: %u\en", tio.c_ospeed);
867 printf("input baud rate: %u\en", tio.c_ispeed);
876 .BR ioctl_console (2),
880 .\" FIONBIO const int *
883 .\" FIOASYNC const int *
885 .\" TIOCSERCONFIG void
886 .\" TIOCSERGWILD int *
887 .\" TIOCSERSWILD const int *
888 .\" TIOCSERGSTRUCT struct async_struct *
889 .\" TIOCSERGETLSR int *
890 .\" TIOCSERGETMULTI struct serial_multiport_struct *
891 .\" TIOCSERSETMULTI const struct serial_multiport_struct *
892 .\" TIOCGSERIAL, TIOCSSERIAL (see above)