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-03-22 "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 Get the session ID of the given terminal.
390 This fails with the error
392 if the terminal is not a master pseudoterminal
393 and not our controlling terminal.
401 Put the terminal into exclusive mode.
404 operations on the terminal are permitted.
407 except for a process with the
416 If the terminal is currently in exclusive mode,
417 place a nonzero value in the location pointed to by
419 otherwise, place zero in
426 Disable exclusive mode.
433 Get the line discipline of the terminal.
437 .BI "const int *" argp
439 Set the line discipline of the terminal.
440 .SS Pseudoterminal ioctls
444 .BI "const int *" argp
448 is nonzero) or disable packet mode.
449 Can be applied to the master side of a pseudoterminal only (and will return
452 In packet mode, each subsequent
454 will return a packet that either contains a single nonzero control byte,
455 or has a single byte containing zero (\(aq\e0\(aq) followed by data
456 written on the slave side of the pseudoterminal.
457 If the first byte is not
459 (0), it is an OR of one
460 or more of the following bits:
466 The read queue for the terminal is flushed.
468 TIOCPKT_FLUSHWRITE T{
469 The write queue for the terminal is flushed.
472 Output to the terminal is stopped.
475 Output to the terminal is restarted.
478 The start and stop characters are \fB\(haS\fP/\fB\(haQ\fP.
481 The start and stop characters are not \fB\(haS\fP/\fB\(haQ\fP.
486 While packet mode is in use, the presence
487 of control status information to be read
488 from the master side may be detected by a
490 for exceptional conditions or a
500 to implement a remote-echoed,
501 locally \fB\(haS\fP/\fB\(haQ\fP flow-controlled remote login.
505 .BI "const int *" argp
508 Return the current packet mode setting in the integer pointed to by
517 is nonzero) or remove (if
519 is zero) the lock on the pseudoterminal slave device.
528 Place the current lock state of the pseudoterminal slave device
529 in the location pointed to by
536 .\" commit 54ebbfb1603415d9953c150535850d30609ef077
538 Given a file descriptor in
540 that refers to a pseudoterminal master,
544 and return a new file descriptor that refers to the peer
545 pseudoterminal slave device.
546 This operation can be performed
547 regardless of whether the pathname of the slave device
548 is accessible through the calling process's mount namespace.
550 Security-conscious programs interacting with namespaces may wish to use this
551 operation rather than
553 with the pathname returned by
555 and similar library functions that have insecure APIs.
556 (For example, confusion can occur in some cases using
558 with a pathname where a devpts filesystem
559 has been mounted in a different mount namespace.)
567 have not been implemented under Linux.
574 Get the status of modem bits.
578 .BI "const int *" argp
580 Set the status of modem bits.
584 .BI "const int *" argp
586 Clear the indicated modem bits.
590 .BI "const int *" argp
592 Set the indicated modem bits.
594 The following bits are used by the above ioctls:
598 TIOCM_LE DSR (data set ready/line enable)
599 TIOCM_DTR DTR (data terminal ready)
600 TIOCM_RTS RTS (request to send)
601 TIOCM_ST Secondary TXD (transmit)
602 TIOCM_SR Secondary RXD (receive)
603 TIOCM_CTS CTS (clear to send)
604 TIOCM_CAR DCD (data carrier detect)
605 TIOCM_CD see TIOCM_CAR
607 TIOCM_RI see TIOCM_RNG
608 TIOCM_DSR DSR (data set ready)
615 Wait for any of the 4 modem bits (DCD, RI, DSR, CTS) to change.
616 The bits of interest are specified as a bit mask in
618 by ORing together any of the bit values,
624 The caller should use
626 to see which bit has changed.
630 .BI "struct serial_icounter_struct *" argp
632 Get counts of input serial line interrupts (DCD, RI, DSR, CTS).
633 The counts are written to the
634 .I serial_icounter_struct
635 structure pointed to by
638 Note: both 1->0 and 0->1 transitions are counted, except for
639 RI, where only 0->1 transitions are counted.
640 .SS Marking a line as local
646 ("Get software carrier flag")
647 Get the status of the CLOCAL flag in the c_cflag field of the
653 .BI "const int *" argp
655 ("Set software carrier flag")
656 Set the CLOCAL flag in the
660 is nonzero, and clear it otherwise.
664 flag for a line is off, the hardware carrier detect (DCD)
665 signal is significant, and an
667 of the corresponding terminal will block until DCD is asserted,
673 is set, the line behaves as if DCD is always asserted.
674 The software carrier flag is usually turned on for local devices,
675 and is off for lines with modems.
680 .BR ioctl_console (2).
682 .B "#include <linux/tty.h>"
686 .BI "struct tty_struct *" argp
692 This command was removed in Linux 2.5.67.
693 .\" commit b3506a09d15dc5aee6d4bb88d759b157016e1864
694 .\" Author: Andries E. Brouwer <andries.brouwer@cwi.nl>
695 .\" Date: Tue Apr 1 04:42:46 2003 -0800
697 .\" [PATCH] kill TIOCTTYGSTRUCT
699 .\" Only used for (dubious) debugging purposes, and exposes
700 .\" internal kernel state.
703 .\" .BR "#include <linux/serial.h>"
706 .\" .BI "TIOCGSERIAL struct serial_struct *" argp
709 .\" .BI "TIOCSSERIAL const struct serial_struct *" argp
714 system call returns 0 on success.
715 On error, it returns \-1 and sets
717 to indicate the error.
721 Invalid command parameter.
731 Insufficient permission.
733 Check the condition of DTR on the serial port.
739 #include <sys/ioctl.h>
746 fd = open("/dev/ttyS0", O_RDONLY);
747 ioctl(fd, TIOCMGET, &serial);
748 if (serial & TIOCM_DTR)
749 puts("TIOCM_DTR is set");
751 puts("TIOCM_DTR is not set");
758 .BR ioctl_console (2),
762 .\" FIONBIO const int *
765 .\" FIOASYNC const int *
767 .\" TIOCSERCONFIG void
768 .\" TIOCSERGWILD int *
769 .\" TIOCSERSWILD const int *
770 .\" TIOCSERGSTRUCT struct async_struct *
771 .\" TIOCSERGETLSR int *
772 .\" TIOCSERGETMULTI struct serial_multiport_struct *
773 .\" TIOCSERSETMULTI const struct serial_multiport_struct *
774 .\" TIOCGSERIAL, TIOCSSERIAL (see above)