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 <termios.h>" " /* Definition of " CLOCAL ", and"
15 .BR " TC*" { FLUSH , ON , OFF "} constants */"
17 .BI "int ioctl(int " fd ", int " cmd ", ...);"
22 call for terminals and serial ports accepts many possible command arguments.
23 Most require a third argument, of varying type, here called
30 makes for nonportable programs.
31 Use the POSIX interface described in
34 .SS Get and set terminal attributes
38 .BI "struct termios *" argp
41 .IR "tcgetattr(fd, argp)" .
43 Get the current serial port settings.
47 .BI "const struct termios *" argp
50 .IR "tcsetattr(fd, TCSANOW, argp)" .
52 Set the current serial port settings.
56 .BI "const struct termios *" argp
59 .IR "tcsetattr(fd, TCSADRAIN, argp)" .
61 Allow the output buffer to drain, and
62 set the current serial port settings.
66 .BI "const struct termios *" argp
69 .IR "tcsetattr(fd, TCSAFLUSH, argp)" .
71 Allow the output buffer to drain, discard pending input, and
72 set the current serial port settings.
74 The following four ioctls, added in Linux 2.6.20,
75 .\" commit 64bb6c5e1ddcd47c951740485026ef08975ee2e6
76 .\" commit 592ee3a5e5e2a981ef2829a0380093006d045661
82 except that they take a
83 .I "struct termios2\ *"
85 .IR "struct termios\ *" .
86 If the structure member
90 then the baud rate is stored in the structure members
95 These ioctls are not supported on all architectures.
99 TCGETS2 \fBstruct termios2 *\fPargp
100 TCSETS2 \fBconst struct termios2 *\fPargp
101 TCSETSW2 \fBconst struct termios2 *\fPargp
102 TCSETSF2 \fBconst struct termios2 *\fPargp
106 The following four ioctls are just like
111 except that they take a
112 .I "struct termio\ *"
114 .IR "struct termios\ *" .
118 TCGETA \fBstruct termio *\fPargp
119 TCSETA \fBconst struct termio *\fPargp
120 TCSETAW \fBconst struct termio *\fPargp
121 TCSETAF \fBconst struct termio *\fPargp
124 .SS Locking the termios structure
127 structure of a terminal can be locked.
130 structure, with nonzero bits or fields indicating a
135 .BI "struct termios *" argp
137 Gets the locking status of the
139 structure of the terminal.
143 .BI "const struct termios *" argp
145 Sets the locking status of the
147 structure of the terminal.
148 Only a process with the
150 capability can do this.
151 .SS Get and set window size
152 Window sizes are kept in the kernel, but not used by the kernel
153 (except in the case of virtual consoles, where the kernel will
154 update the window size when the size of the virtual console changes,
155 for example, by loading a new font).
159 .BI "struct winsize *" argp
165 .BI "const struct winsize *" argp
169 The struct used by these ioctls is defined as
174 unsigned short ws_row;
175 unsigned short ws_col;
176 unsigned short ws_xpixel; /* unused */
177 unsigned short ws_ypixel; /* unused */
182 When the window size changes, a
184 signal is sent to the
185 foreground process group.
193 .IR "tcsendbreak(fd, arg)" .
195 If the terminal is using asynchronous serial data transmission, and
197 is zero, then send a break (a stream of zero bits) for between
198 0.25 and 0.5 seconds.
199 If the terminal is not using asynchronous
200 serial data transmission, then either a break is sent, or the function
201 returns without doing anything.
204 is nonzero, nobody knows what will happen.
206 (SVr4, UnixWare, Solaris, and Linux treat
207 .I "tcsendbreak(fd,arg)"
214 as a multiplier, and sends a stream of bits
216 times as long as done for zero
220 (when nonzero) as a time interval measured in milliseconds.
228 So-called "POSIX version" of
232 as a time interval measured in deciseconds, and does nothing
233 when the driver does not support breaks.
239 Turn break on, that is, start sending zero bits.
245 Turn break off, that is, stop sending zero bits.
246 .SS Software flow control
253 .IR "tcflow(fd, arg)" .
257 for the argument values
262 .SS Buffer count and flushing
268 Get the number of bytes in the input buffer.
281 Get the number of bytes in the output buffer.
288 .IR "tcflush(fd, arg)" .
292 for the argument values
300 .BI "const char *" argp
302 Insert the given byte in the input queue.
303 .SS Redirecting console output
309 Redirect output that would have gone to
313 to the given terminal.
314 If that was a pseudoterminal master, send it to the slave.
315 In Linux before version 2.6.10,
316 anybody can do this as long as the output was not redirected yet;
317 since version 2.6.10, only a process with the
319 capability may do this.
320 If output was redirected already, then
323 but redirection can be stopped by using this ioctl with
329 .SS Controlling terminal
335 Make the given terminal the controlling terminal of the calling process.
336 The calling process must be a session leader and not have a
337 controlling terminal already.
340 should be specified as zero.
342 If this terminal is already the controlling terminal
343 of a different session group, then the ioctl fails with
345 unless the caller has the
349 equals 1, in which case the terminal is stolen, and all processes that had
350 it as controlling terminal lose it.
356 If the given terminal was the controlling terminal of the calling process,
357 give up this controlling terminal.
358 If the process was session leader,
363 to the foreground process group
364 and all processes in the current session lose their controlling terminal.
365 .SS Process group and session ID
371 When successful, equivalent to
372 .IR "*argp = tcgetpgrp(fd)" .
374 Get the process group ID of the foreground process group on this terminal.
378 .BI "const pid_t *" argp
381 .IR "tcsetpgrp(fd, *argp)" .
383 Set the foreground process group ID of this terminal.
389 When successful, equivalent to
390 .IR "*argp = tcgetsid(fd)" .
392 Get the session ID of the given terminal.
393 This fails with the error
395 if the terminal is not a master pseudoterminal
396 and not our controlling terminal.
404 Put the terminal into exclusive mode.
407 operations on the terminal are permitted.
410 except for a process with the
419 If the terminal is currently in exclusive mode,
420 place a nonzero value in the location pointed to by
422 otherwise, place zero in
429 Disable exclusive mode.
436 Get the line discipline of the terminal.
440 .BI "const int *" argp
442 Set the line discipline of the terminal.
443 .SS Pseudoterminal ioctls
447 .BI "const int *" argp
451 is nonzero) or disable packet mode.
452 Can be applied to the master side of a pseudoterminal only (and will return
455 In packet mode, each subsequent
457 will return a packet that either contains a single nonzero control byte,
458 or has a single byte containing zero (\(aq\e0\(aq) followed by data
459 written on the slave side of the pseudoterminal.
460 If the first byte is not
462 (0), it is an OR of one
463 or more of the following bits:
469 The read queue for the terminal is flushed.
471 TIOCPKT_FLUSHWRITE T{
472 The write queue for the terminal is flushed.
475 Output to the terminal is stopped.
478 Output to the terminal is restarted.
481 The start and stop characters are \fB\(haS\fP/\fB\(haQ\fP.
484 The start and stop characters are not \fB\(haS\fP/\fB\(haQ\fP.
489 While packet mode is in use, the presence
490 of control status information to be read
491 from the master side may be detected by a
493 for exceptional conditions or a
503 to implement a remote-echoed,
504 locally \fB\(haS\fP/\fB\(haQ\fP flow-controlled remote login.
508 .BI "const int *" argp
511 Return the current packet mode setting in the integer pointed to by
520 is nonzero) or remove (if
522 is zero) the lock on the pseudoterminal slave device.
531 Place the current lock state of the pseudoterminal slave device
532 in the location pointed to by
539 .\" commit 54ebbfb1603415d9953c150535850d30609ef077
541 Given a file descriptor in
543 that refers to a pseudoterminal master,
547 and return a new file descriptor that refers to the peer
548 pseudoterminal slave device.
549 This operation can be performed
550 regardless of whether the pathname of the slave device
551 is accessible through the calling process's mount namespace.
553 Security-conscious programs interacting with namespaces may wish to use this
554 operation rather than
556 with the pathname returned by
558 and similar library functions that have insecure APIs.
559 (For example, confusion can occur in some cases using
561 with a pathname where a devpts filesystem
562 has been mounted in a different mount namespace.)
570 have not been implemented under Linux.
577 Get the status of modem bits.
581 .BI "const int *" argp
583 Set the status of modem bits.
587 .BI "const int *" argp
589 Clear the indicated modem bits.
593 .BI "const int *" argp
595 Set the indicated modem bits.
597 The following bits are used by the above ioctls:
601 TIOCM_LE DSR (data set ready/line enable)
602 TIOCM_DTR DTR (data terminal ready)
603 TIOCM_RTS RTS (request to send)
604 TIOCM_ST Secondary TXD (transmit)
605 TIOCM_SR Secondary RXD (receive)
606 TIOCM_CTS CTS (clear to send)
607 TIOCM_CAR DCD (data carrier detect)
608 TIOCM_CD see TIOCM_CAR
610 TIOCM_RI see TIOCM_RNG
611 TIOCM_DSR DSR (data set ready)
618 Wait for any of the 4 modem bits (DCD, RI, DSR, CTS) to change.
619 The bits of interest are specified as a bit mask in
621 by ORing together any of the bit values,
627 The caller should use
629 to see which bit has changed.
633 .BI "struct serial_icounter_struct *" argp
635 Get counts of input serial line interrupts (DCD, RI, DSR, CTS).
636 The counts are written to the
637 .I serial_icounter_struct
638 structure pointed to by
641 Note: both 1->0 and 0->1 transitions are counted, except for
642 RI, where only 0->1 transitions are counted.
643 .SS Marking a line as local
649 ("Get software carrier flag")
650 Get the status of the CLOCAL flag in the c_cflag field of the
656 .BI "const int *" argp
658 ("Set software carrier flag")
659 Set the CLOCAL flag in the
663 is nonzero, and clear it otherwise.
667 flag for a line is off, the hardware carrier detect (DCD)
668 signal is significant, and an
670 of the corresponding terminal will block until DCD is asserted,
676 is set, the line behaves as if DCD is always asserted.
677 The software carrier flag is usually turned on for local devices,
678 and is off for lines with modems.
683 .BR ioctl_console (2).
685 .B "#include <linux/tty.h>"
689 .BI "struct tty_struct *" argp
695 This command was removed in Linux 2.5.67.
696 .\" commit b3506a09d15dc5aee6d4bb88d759b157016e1864
697 .\" Author: Andries E. Brouwer <andries.brouwer@cwi.nl>
698 .\" Date: Tue Apr 1 04:42:46 2003 -0800
700 .\" [PATCH] kill TIOCTTYGSTRUCT
702 .\" Only used for (dubious) debugging purposes, and exposes
703 .\" internal kernel state.
706 .\" .BR "#include <linux/serial.h>"
709 .\" .BI "TIOCGSERIAL struct serial_struct *" argp
712 .\" .BI "TIOCSSERIAL const struct serial_struct *" argp
717 system call returns 0 on success.
718 On error, it returns \-1 and sets
720 to indicate the error.
724 Invalid command parameter.
734 Insufficient permission.
736 Check the condition of DTR on the serial port.
742 #include <sys/ioctl.h>
749 fd = open("/dev/ttyS0", O_RDONLY);
750 ioctl(fd, TIOCMGET, &serial);
751 if (serial & TIOCM_DTR)
752 puts("TIOCM_DTR is set");
754 puts("TIOCM_DTR is not set");
759 Get or set arbitrary baudrate on the serial port.
762 /* SPDX-License-Identifier: GPL-2.0-or-later */
764 #include <asm/termbits.h>
768 #include <sys/ioctl.h>
769 #include <sys/types.h>
773 main(int argc, char *argv[])
776 fprintf(stderr, "BOTHER is unsupported\en");
777 /* Program may fallback to TCGETS/TCSETS with Bnnn constants */
780 /* Declare tio structure, its type depends on supported ioctl */
788 if (argc != 2 && argc != 3 && argc != 4) {
789 fprintf(stderr, "Usage: %s device [output [input] ]\en", argv[0]);
793 fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY);
799 /* Get the current serial port settings via supported ioctl */
801 rc = ioctl(fd, TCGETS2, &tio);
803 rc = ioctl(fd, TCGETS, &tio);
811 /* Change baud rate when more arguments were provided */
812 if (argc == 3 || argc == 4) {
813 /* Clear the current output baud rate and fill a new value */
814 tio.c_cflag &= ~CBAUD;
815 tio.c_cflag |= BOTHER;
816 tio.c_ospeed = atoi(argv[2]);
818 /* Clear the current input baud rate and fill a new value */
819 tio.c_cflag &= ~(CBAUD << IBSHIFT);
820 tio.c_cflag |= BOTHER << IBSHIFT;
821 /* When 4th argument is not provided reuse output baud rate */
822 tio.c_ispeed = (argc == 4) ? atoi(argv[3]) : atoi(argv[2]);
824 /* Set new serial port settings via supported ioctl */
826 rc = ioctl(fd, TCSETS2, &tio);
828 rc = ioctl(fd, TCSETS, &tio);
836 /* And get new values which were really configured */
838 rc = ioctl(fd, TCGETS2, &tio);
840 rc = ioctl(fd, TCGETS, &tio);
851 printf("output baud rate: %u\en", tio.c_ospeed);
852 printf("input baud rate: %u\en", tio.c_ispeed);
861 .BR ioctl_console (2),
865 .\" FIONBIO const int *
868 .\" FIOASYNC const int *
870 .\" TIOCSERCONFIG void
871 .\" TIOCSERGWILD int *
872 .\" TIOCSERSWILD const int *
873 .\" TIOCSERGSTRUCT struct async_struct *
874 .\" TIOCSERGETLSR int *
875 .\" TIOCSERGETMULTI struct serial_multiport_struct *
876 .\" TIOCSERSETMULTI const struct serial_multiport_struct *
877 .\" TIOCGSERIAL, TIOCSSERIAL (see above)