1 .\" Copyright 2002 Walter Harms <walter.harms@informatik.uni-oldenburg.de>
2 .\" and Andries Brouwer <aeb@cwi.nl>.
4 .\" SPDX-License-Identifier: GPL-1.0-or-later
6 .TH ioctl_tty 2 (date) "Linux man-pages (unreleased)"
8 ioctl_tty \- ioctls for terminals and serial lines
11 .RI ( libc ", " \-lc )
14 .B #include <sys/ioctl.h>
15 .BR "#include <asm/termbits.h>" " /* Definition of " "struct termios" ,
16 .BR " struct termios2" ", and"
17 .BR " Bnnn" ", " BOTHER ", " CBAUD ", " CLOCAL ,
18 .BR " TC*" { FLUSH , ON , OFF "} and other constants */"
20 .BI "int ioctl(int " fd ", int " cmd ", ...);"
25 call for terminals and serial ports accepts many possible command arguments.
26 Most require a third argument, of varying type, here called
33 makes for nonportable programs.
34 Use the POSIX interface described in
42 is different and incompatible with
46 These ioctl calls require
49 .IR <asm/termbits.h> .
50 .SS Get and set terminal attributes
54 .BI "struct termios\~*" argp
57 .IR "tcgetattr(fd, argp)" .
59 Get the current serial port settings.
63 .BI "const struct termios\~*" argp
66 .IR "tcsetattr(fd, TCSANOW, argp)" .
68 Set the current serial port settings.
72 .BI "const struct termios\~*" argp
75 .IR "tcsetattr(fd, TCSADRAIN, argp)" .
77 Allow the output buffer to drain, and
78 set the current serial port settings.
82 .BI "const struct termios\~*" argp
85 .IR "tcsetattr(fd, TCSAFLUSH, argp)" .
87 Allow the output buffer to drain, discard pending input, and
88 set the current serial port settings.
90 The following four ioctls, added in Linux 2.6.20,
91 .\" commit 64bb6c5e1ddcd47c951740485026ef08975ee2e6
92 .\" commit 592ee3a5e5e2a981ef2829a0380093006d045661
98 except that they take a
99 .I "struct termios2\~*"
101 .IR "struct termios\~*" .
102 If the structure member
106 then the baud rate is stored in the structure members
111 These ioctls are not supported on all architectures.
115 TCGETS2 \fBstruct termios2 *\fPargp
116 TCSETS2 \fBconst struct termios2 *\fPargp
117 TCSETSW2 \fBconst struct termios2 *\fPargp
118 TCSETSF2 \fBconst struct termios2 *\fPargp
122 The following four ioctls are just like
127 except that they take a
128 .I "struct termio\~*"
130 .IR "struct termios\~*" .
134 TCGETA \fBstruct termio *\fPargp
135 TCSETA \fBconst struct termio *\fPargp
136 TCSETAW \fBconst struct termio *\fPargp
137 TCSETAF \fBconst struct termio *\fPargp
140 .SS Locking the termios structure
143 structure of a terminal can be locked.
146 structure, with nonzero bits or fields indicating a
151 .BI "struct termios\~*" argp
153 Gets the locking status of the
155 structure of the terminal.
159 .BI "const struct termios\~*" argp
161 Sets the locking status of the
163 structure of the terminal.
164 Only a process with the
166 capability can do this.
167 .SS Get and set window size
168 Window sizes are kept in the kernel, but not used by the kernel
169 (except in the case of virtual consoles, where the kernel will
170 update the window size when the size of the virtual console changes,
171 for example, by loading a new font).
175 .BI "struct winsize\~*" argp
181 .BI "const struct winsize\~*" argp
185 The struct used by these ioctls is defined as
190 unsigned short ws_row;
191 unsigned short ws_col;
192 unsigned short ws_xpixel; /* unused */
193 unsigned short ws_ypixel; /* unused */
198 When the window size changes, a
200 signal is sent to the
201 foreground process group.
209 .IR "tcsendbreak(fd, arg)" .
211 If the terminal is using asynchronous serial data transmission, and
213 is zero, then send a break (a stream of zero bits) for between
214 0.25 and 0.5 seconds.
215 If the terminal is not using asynchronous
216 serial data transmission, then either a break is sent, or the function
217 returns without doing anything.
220 is nonzero, nobody knows what will happen.
222 (SVr4, UnixWare, Solaris, and Linux treat
223 .I "tcsendbreak(fd,arg)"
230 as a multiplier, and sends a stream of bits
232 times as long as done for zero
236 (when nonzero) as a time interval measured in milliseconds.
244 So-called "POSIX version" of
248 as a time interval measured in deciseconds, and does nothing
249 when the driver does not support breaks.
255 Turn break on, that is, start sending zero bits.
261 Turn break off, that is, stop sending zero bits.
262 .SS Software flow control
269 .IR "tcflow(fd, arg)" .
273 for the argument values
278 .SS Buffer count and flushing
284 Get the number of bytes in the input buffer.
297 Get the number of bytes in the output buffer.
304 .IR "tcflush(fd, arg)" .
308 for the argument values
317 Get line status register. Status register has
319 bit set when output buffer is empty and also hardware transmitter is physically empty.
321 Does not have to be supported by all serial tty drivers.
324 does not wait and returns immediately when
331 .BI "const char\~*" argp
333 Insert the given byte in the input queue.
334 .SS Redirecting console output
340 Redirect output that would have gone to
344 to the given terminal.
345 If that was a pseudoterminal master, send it to the slave.
346 In Linux before version 2.6.10,
347 anybody can do this as long as the output was not redirected yet;
348 since version 2.6.10, only a process with the
350 capability may do this.
351 If output was redirected already, then
354 but redirection can be stopped by using this ioctl with
360 .SS Controlling terminal
366 Make the given terminal the controlling terminal of the calling process.
367 The calling process must be a session leader and not have a
368 controlling terminal already.
371 should be specified as zero.
373 If this terminal is already the controlling terminal
374 of a different session group, then the ioctl fails with
376 unless the caller has the
380 equals 1, in which case the terminal is stolen, and all processes that had
381 it as controlling terminal lose it.
387 If the given terminal was the controlling terminal of the calling process,
388 give up this controlling terminal.
389 If the process was session leader,
394 to the foreground process group
395 and all processes in the current session lose their controlling terminal.
396 .SS Process group and session ID
402 When successful, equivalent to
403 .IR "*argp = tcgetpgrp(fd)" .
405 Get the process group ID of the foreground process group on this terminal.
409 .BI "const pid_t\~*" argp
412 .IR "tcsetpgrp(fd, *argp)" .
414 Set the foreground process group ID of this terminal.
420 When successful, equivalent to
421 .IR "*argp = tcgetsid(fd)" .
423 Get the session ID of the given terminal.
424 This fails with the error
426 if the terminal is not a master pseudoterminal
427 and not our controlling terminal.
435 Put the terminal into exclusive mode.
438 operations on the terminal are permitted.
441 except for a process with the
450 If the terminal is currently in exclusive mode,
451 place a nonzero value in the location pointed to by
453 otherwise, place zero in
460 Disable exclusive mode.
467 Get the line discipline of the terminal.
471 .BI "const int\~*" argp
473 Set the line discipline of the terminal.
474 .SS Pseudoterminal ioctls
478 .BI "const int\~*" argp
482 is nonzero) or disable packet mode.
483 Can be applied to the master side of a pseudoterminal only (and will return
486 In packet mode, each subsequent
488 will return a packet that either contains a single nonzero control byte,
489 or has a single byte containing zero (\(aq\e0\(aq) followed by data
490 written on the slave side of the pseudoterminal.
491 If the first byte is not
493 (0), it is an OR of one
494 or more of the following bits:
500 The read queue for the terminal is flushed.
502 TIOCPKT_FLUSHWRITE T{
503 The write queue for the terminal is flushed.
506 Output to the terminal is stopped.
509 Output to the terminal is restarted.
512 The start and stop characters are \fB\(haS\fP/\fB\(haQ\fP.
515 The start and stop characters are not \fB\(haS\fP/\fB\(haQ\fP.
520 While packet mode is in use, the presence
521 of control status information to be read
522 from the master side may be detected by a
524 for exceptional conditions or a
534 to implement a remote-echoed,
535 locally \fB\(haS\fP/\fB\(haQ\fP flow-controlled remote login.
539 .BI "const int\~*" argp
542 Return the current packet mode setting in the integer pointed to by
551 is nonzero) or remove (if
553 is zero) the lock on the pseudoterminal slave device.
562 Place the current lock state of the pseudoterminal slave device
563 in the location pointed to by
570 .\" commit 54ebbfb1603415d9953c150535850d30609ef077
572 Given a file descriptor in
574 that refers to a pseudoterminal master,
578 and return a new file descriptor that refers to the peer
579 pseudoterminal slave device.
580 This operation can be performed
581 regardless of whether the pathname of the slave device
582 is accessible through the calling process's mount namespace.
584 Security-conscious programs interacting with namespaces may wish to use this
585 operation rather than
587 with the pathname returned by
589 and similar library functions that have insecure APIs.
590 (For example, confusion can occur in some cases using
592 with a pathname where a devpts filesystem
593 has been mounted in a different mount namespace.)
601 have not been implemented under Linux.
608 Get the status of modem bits.
612 .BI "const int\~*" argp
614 Set the status of modem bits.
618 .BI "const int\~*" argp
620 Clear the indicated modem bits.
624 .BI "const int\~*" argp
626 Set the indicated modem bits.
628 The following bits are used by the above ioctls:
632 TIOCM_LE DSR (data set ready/line enable)
633 TIOCM_DTR DTR (data terminal ready)
634 TIOCM_RTS RTS (request to send)
635 TIOCM_ST Secondary TXD (transmit)
636 TIOCM_SR Secondary RXD (receive)
637 TIOCM_CTS CTS (clear to send)
638 TIOCM_CAR DCD (data carrier detect)
639 TIOCM_CD see TIOCM_CAR
641 TIOCM_RI see TIOCM_RNG
642 TIOCM_DSR DSR (data set ready)
649 Wait for any of the 4 modem bits (DCD, RI, DSR, CTS) to change.
650 The bits of interest are specified as a bit mask in
652 by ORing together any of the bit values,
658 The caller should use
660 to see which bit has changed.
664 .BI "struct serial_icounter_struct\~*" argp
666 Get counts of input serial line interrupts (DCD, RI, DSR, CTS).
667 The counts are written to the
668 .I serial_icounter_struct
669 structure pointed to by
672 Note: both 1->0 and 0->1 transitions are counted, except for
673 RI, where only 0->1 transitions are counted.
674 .SS Marking a line as local
680 ("Get software carrier flag")
681 Get the status of the CLOCAL flag in the c_cflag field of the
687 .BI "const int\~*" argp
689 ("Set software carrier flag")
690 Set the CLOCAL flag in the
694 is nonzero, and clear it otherwise.
698 flag for a line is off, the hardware carrier detect (DCD)
699 signal is significant, and an
701 of the corresponding terminal will block until DCD is asserted,
707 is set, the line behaves as if DCD is always asserted.
708 The software carrier flag is usually turned on for local devices,
709 and is off for lines with modems.
714 .BR ioctl_console (2).
716 .B "#include <linux/tty.h>"
720 .BI "struct tty_struct\~*" argp
726 This command was removed in Linux 2.5.67.
727 .\" commit b3506a09d15dc5aee6d4bb88d759b157016e1864
728 .\" Author: Andries E. Brouwer <andries.brouwer@cwi.nl>
729 .\" Date: Tue Apr 1 04:42:46 2003 -0800
731 .\" [PATCH] kill TIOCTTYGSTRUCT
733 .\" Only used for (dubious) debugging purposes, and exposes
734 .\" internal kernel state.
737 .\" .BR "#include <linux/serial.h>"
740 .\" .BI "TIOCGSERIAL struct serial_struct *" argp
743 .\" .BI "TIOCSSERIAL const struct serial_struct *" argp
748 system call returns 0 on success.
749 On error, it returns \-1 and sets
751 to indicate the error.
755 Invalid command parameter.
765 Insufficient permission.
767 Check the condition of DTR on the serial port.
769 .\" SRC BEGIN (tiocmget.c)
773 #include <sys/ioctl.h>
781 fd = open("/dev/ttyS0", O_RDONLY);
782 ioctl(fd, TIOCMGET, &serial);
783 if (serial & TIOCM_DTR)
784 puts("TIOCM_DTR is set");
786 puts("TIOCM_DTR is not set");
792 Get or set arbitrary baudrate on the serial port.
794 .\" SRC BEGIN (tcgets.c)
796 /* SPDX-License-Identifier: GPL-2.0-or-later */
798 #include <asm/termbits.h>
802 #include <sys/ioctl.h>
806 main(int argc, char *argv[])
809 fprintf(stderr, "BOTHER is unsupported\en");
810 /* Program may fallback to TCGETS/TCSETS with Bnnn constants */
813 /* Declare tio structure, its type depends on supported ioctl */
821 if (argc != 2 && argc != 3 && argc != 4) {
822 fprintf(stderr, "Usage: %s device [output [input] ]\en", argv[0]);
826 fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY);
832 /* Get the current serial port settings via supported ioctl */
834 rc = ioctl(fd, TCGETS2, &tio);
836 rc = ioctl(fd, TCGETS, &tio);
844 /* Change baud rate when more arguments were provided */
845 if (argc == 3 || argc == 4) {
846 /* Clear the current output baud rate and fill a new value */
847 tio.c_cflag &= \(tiCBAUD;
848 tio.c_cflag |= BOTHER;
849 tio.c_ospeed = atoi(argv[2]);
851 /* Clear the current input baud rate and fill a new value */
852 tio.c_cflag &= \(ti(CBAUD << IBSHIFT);
853 tio.c_cflag |= BOTHER << IBSHIFT;
854 /* When 4th argument is not provided reuse output baud rate */
855 tio.c_ispeed = (argc == 4) ? atoi(argv[3]) : atoi(argv[2]);
857 /* Set new serial port settings via supported ioctl */
859 rc = ioctl(fd, TCSETS2, &tio);
861 rc = ioctl(fd, TCSETS, &tio);
869 /* And get new values which were really configured */
871 rc = ioctl(fd, TCGETS2, &tio);
873 rc = ioctl(fd, TCGETS, &tio);
884 printf("output baud rate: %u\en", tio.c_ospeed);
885 printf("input baud rate: %u\en", tio.c_ispeed);
895 .BR ioctl_console (2),
899 .\" FIONBIO const int *
902 .\" FIOASYNC const int *
904 .\" TIOCSERCONFIG void
905 .\" TIOCSERGWILD int *
906 .\" TIOCSERSWILD const int *
907 .\" TIOCSERGSTRUCT struct async_struct *
908 .\" TIOCSERGETMULTI struct serial_multiport_struct *
909 .\" TIOCSERSETMULTI const struct serial_multiport_struct *
910 .\" TIOCGSERIAL, TIOCSSERIAL (see above)