ioctl_tty(2) System Calls Manual ioctl_tty(2) ioctl_tty - ioctl LIBRARY Standard C library (libc, -lc) #include #include /* Definition of struct termios, struct termios2, and Bnnn, BOTHER, CBAUD, CLOCAL, TC*{FLUSH,ON,OFF} and other constants */ int ioctl(int fd, int cmd, ...); ioctl(2) . , argp arg. ioctl() . POSIX, termios(3). Please note that struct termios from is different and incompatible with struct termios from . These ioctl calls require struct termios from . TCGETS : struct termios *argp tcgetattr(fd, argp). . TCSETS : const struct termios *argp tcsetattr(fd, TCSANOW, argp). . TCSETSW : const struct termios *argp tcsetattr(fd, TCSADRAIN, argp). . TCSETSF : const struct termios *argp tcsetattr(fd, TCSAFLUSH, argp). , . The following four ioctls, added in Linux 2.6.20, are just like TCGETS, TCSETS, TCSETSW, TCSETSF, except that they take a struct termios2 * instead of a struct termios *. If the structure member c_cflag contains the flag BOTHER, then the baud rate is stored in the structure members c_ispeed and c_ospeed as integer values. These ioctls are not supported on all architectures. TCGETS2 struct termios2 *argp TCSETS2 const struct termios2 *argp TCSETSW2 const struct termios2 *argp TCSETSF2 const struct termios2 *argp ioctl TCGETS, TCSETS, TCSETSW, TCSETSF, , struct termio *, struct termios *. TCGETA struct termio *argp TCSETA const struct termio *argp TCSETAW const struct termio *argp TCSETAF const struct termio *argp termios termios . termios, , . TIOCGLCKTRMIOS : struct termios *argp termios . TIOCSLCKTRMIOS : const struct termios *argp termios . CAP_SYS_ADMIN. , ( , , - ). TIOCGWINSZ : struct winsize *argp . TIOCSWINSZ : const struct winsize *argp . , ioctl, : struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; /* */ unsigned short ws_ypixel; /* */ }; SIGWINCH (foreground) . Break TCSBRK : int arg tcsendbreak(fd, arg). arg , break ( ) 0.25 - 0.5 . , break , , . arg , , . ( SVr4, UnixWare, Solaris Linux tcsendbreak(fd,arg) arg tcdrain(fd). SunOS arg arg , arg. DG/UX AIX arg ( ) . HP-UX arg .) TCSBRKP : int arg <>> TCSBRK. arg (1/10 ), , break. TIOCSBRK : void break, . TIOCCBRK : void break, . TCXONC : int arg tcflow(fd, arg). tcflow(3) TCOOFF, TCOON, TCIOFF, TCION. FIONREAD : int *argp . TIOCINQ : int *argp FIONREAD. TIOCOUTQ : int *argp . TCFLSH : int arg tcflush(fd, arg). tcflush(3) TCIFLUSH, TCOFLUSH, TCIOFLUSH. TIOCSERGETLSR : int *argp Get line status register. Status register has TIOCSER_TEMT bit set when output buffer is empty and also hardware transmitter is physically empty. Does not have to be supported by all serial tty drivers. tcdrain(3) does not wait and returns immediately when TIOCSER_TEMT bit is set. TIOCSTI : const char *argp . Since Linux 6.2, this operation may require the CAP_SYS_ADMIN capability (if the dev.tty.legacy_tiocsti sysctl variable is set to false). TIOCCONS : void Redirect output that would have gone to /dev/console or /dev/tty0 to the given terminal. If that was a pseudoterminal master, send it to the slave. Before Linux 2.6.10, anybody can do this as long as the output was not redirected yet; since Linux 2.6.10, only a process with the CAP_SYS_ADMIN capability may do this. If output was redirected already, then EBUSY is returned, but redirection can be stopped by using this ioctl with fd pointing at /dev/console or /dev/tty0. TIOCSCTTY : int arg . . arg 0. , ioctl EPERM, CAP_SYS_ADMIN arg 1 -- , , . TIOCNOTTY : void , . , SIGHUP SIGCONT, . TIOCGPGRP : pid_t *argp *argp = tcgetpgrp(fd). . TIOCSPGRP : const pid_t *argp tcsetpgrp(fd, *argp). . TIOCGSID : pid_t *argp *argp = tcgetsid(fd). . ENOTTY, - . . (Exclusive) TIOCEXCL : void . open(2) ( EBUSY, CAP_SYS_ADMIN). TIOCGEXCL : int *argp ( Linux 3.8) , , argp; , *argp. TIOCNXCL : void . TIOCGETD : int *argp . TIOCSETD : const int *argp . ioctl - TIOCPKT : const int *argp Enable (when *argp is nonzero) or disable packet mode. Can be applied to the master side of a pseudoterminal only (and will return ENOTTY otherwise). In packet mode, each subsequent read(2) will return a packet that either contains a single nonzero control byte, or has a single byte containing zero ('\0') followed by data written on the slave side of the pseudoterminal. If the first byte is not TIOCPKT_DATA (0), it is an OR of one or more of the following bits: TIOCPKT_FLUSHREAD The read queue for the terminal is flushed. TIOCPKT_FLUSHWRITE The write queue for the terminal is flushed. TIOCPKT_STOP Output to the terminal is stopped. TIOCPKT_START Output to the terminal is restarted. TIOCPKT_DOSTOP The start and stop characters are ^S/^Q. TIOCPKT_NOSTOP The start and stop characters are not ^S/^Q. While packet mode is in use, the presence of control status information to be read from the master side may be detected by a select(2) for exceptional conditions or a poll(2) for the POLLPRI event. This mode is used by rlogin(1) and rlogind(8) to implement a remote-echoed, locally ^S/^Q flow-controlled remote login. TIOCGPKT : const int *argp ( Linux 3.8) , argp. TIOCSPTLCK : int *argp Set (if *argp is nonzero) or remove (if *argp is zero) the lock on the pseudoterminal slave device. (See also unlockpt(3).) TIOCGPTLCK : int *argp ( Linux 3.8) - , argp. TIOCGPTPEER : int flags ( Linux 4.13) ( flags open(2)) fd , -, , -. . , , open(2) , ptsname(3) , (, ptsname(3) , devpts ). ioctl BSD -- TIOCSTOP, TIOCSTART, TIOCUCNTL TIOCREMOTE -- Linux. TIOCMGET : int *argp . TIOCMSET : const int *argp . TIOCMBIC : const int *argp . TIOCMBIS : const int *argp . ioctl : TIOCM_LE DSR (data set ready/line enable) TIOCM_DTR DTR (data terminal ready) TIOCM_RTS RTS (request to send) TIOCM_ST Secondary TXD (transmit) TIOCM_SR Secondary RXD (receive) TIOCM_CTS CTS (clear to send) TIOCM_CAR DCD (data carrier detect) TIOCM_CD TIOCM_CAR TIOCM_RNG RNG (ring) TIOCM_RI TIOCM_RNG TIOCM_DSR DSR (data set ready) TIOCMIWAIT : int arg 4 (DCD, RI, DSR, CTS). arg OR TIOCM_RNG, TIOCM_DSR, TIOCM_CD TIOCM_CTS. TIOCGICOUNT. TIOCGICOUNT : struct serial_icounter_struct *argp (DCD, RI, DSR, CTS). serial_icounter_struct, argp. : 1->0 0->1, RI, 0->1. TIOCGSOFTCAR : int *argp (<< >>) CLOCAL c_cflag termios. TIOCSSOFTCAR : const int *argp (<< >>) CLOCAL c_cflag termios *argp . CLOCAL , DCD, open(2) , DCD ( O_NONBLOCK). CLOCAL , , DCD . . , Linux ioctl TIOCLINUX ioctl_console(2). #include TIOCTTYGSTRUCT : struct tty_struct *argp tty_struct, fd. Linux 2.5.67. The ioctl(2) system call returns 0 on success. On error, it returns -1 and sets errno to indicate the error. EINVAL . ENOIOCTLCMD . ENOTTY fd. EPERM . DTR . #include #include #include #include int main(void) { int fd, serial; fd = open("/dev/ttyS0", O_RDONLY); ioctl(fd, TIOCMGET, &serial); if (serial & TIOCM_DTR) puts("TIOCM_DTR is set"); else puts("TIOCM_DTR is not set"); close(fd); } Get or set arbitrary baudrate on the serial port. /* SPDX-License-Identifier: GPL-2.0-or-later */ #include #include #include #include #include #include int main(int argc, char *argv[]) { #if !defined BOTHER fprintf(stderr, "BOTHER is unsupported\n"); /* Program may fallback to TCGETS/TCSETS with Bnnn constants */ exit(EXIT_FAILURE); #else /* Declare tio structure, its type depends on supported ioctl */ # if defined TCGETS2 struct termios2 tio; # else struct termios tio; # endif int fd, rc; if (argc != 2 && argc != 3 && argc != 4) { fprintf(stderr, "Usage: %s device [output [input] ]\n", argv[0]); exit(EXIT_FAILURE); } fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY); if (fd < 0) { perror("open"); exit(EXIT_FAILURE); } /* Get the current serial port settings via supported ioctl */ # if defined TCGETS2 rc = ioctl(fd, TCGETS2, &tio); # else rc = ioctl(fd, TCGETS, &tio); # endif if (rc) { perror("TCGETS"); close(fd); exit(EXIT_FAILURE); } /* Change baud rate when more arguments were provided */ if (argc == 3 || argc == 4) { /* Clear the current output baud rate and fill a new value */ tio.c_cflag &= ~CBAUD; tio.c_cflag |= BOTHER; tio.c_ospeed = atoi(argv[2]); /* Clear the current input baud rate and fill a new value */ tio.c_cflag &= ~(CBAUD << IBSHIFT); tio.c_cflag |= BOTHER << IBSHIFT; /* When 4th argument is not provided reuse output baud rate */ tio.c_ispeed = (argc == 4) ? atoi(argv[3]) : atoi(argv[2]); /* Set new serial port settings via supported ioctl */ # if defined TCSETS2 rc = ioctl(fd, TCSETS2, &tio); # else rc = ioctl(fd, TCSETS, &tio); # endif if (rc) { perror("TCSETS"); close(fd); exit(EXIT_FAILURE); } /* And get new values which were really configured */ # if defined TCGETS2 rc = ioctl(fd, TCGETS2, &tio); # else rc = ioctl(fd, TCGETS, &tio); # endif if (rc) { perror("TCGETS"); close(fd); exit(EXIT_FAILURE); } } close(fd); printf("output baud rate: %u\n", tio.c_ospeed); printf("input baud rate: %u\n", tio.c_ispeed); exit(EXIT_SUCCESS); #endif } . ldattach(8), ioctl(2), ioctl_console(2), termios(3), pty(7) Azamat Hackimov , Dmitriy S. Seregin , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . ioctl_tty(2)