ioctl_tty (2) Linux Manual Page
NAME
ioctl_tty – ioctls for terminals and serial lines
SYNOPSIS
#include <termios.h>
int ioctl(int fd, int cmd, …);
DESCRIPTION
The ioctl(2) call for terminals and serial ports accepts many possible command arguments. Most require a third argument, of varying type, here called argp or arg.
Use of ioctl makes for nonportable programs. Use the POSIX interface described in termios(3) whenever possible.
Get and set terminal attributes
TCGETSargp struct termios *- Equivalent to
tcgetattr(fd, argp). - Get the current serial port settings.
TCSETS const struct termios *argp- Equivalent to
tcsetattr(fd, TCSANOW, argp). - Set the current serial port settings.
TCSETSWargp const struct termios *- Equivalent to
tcsetattr(fd, TCSADRAIN, argp). - Allow the output buffer to drain, and set the current serial port settings.
TCSETSFargp const struct termios *- Equivalent to
tcsetattr(fd, TCSAFLUSH, argp). - Allow the output buffer to drain, discard pending input, and set the current serial port settings.
The following four ioctls are just like TCGETS, TCSETS, TCSETSW, TCSETSF, except that they take a struct termio * instead of a struct termios *.
-
TCGETAargp struct termio * -
TCSETAargp const struct termio * -
TCSETAWargp const struct termio * -
TCSETAFargp const struct termio *
Locking the termios structure
The
termios structure of a terminal can be locked. The lock is itself a termios structure, with nonzero bits or fields indicating a locked value.
TIOCGLCKTRMIOS struct termios *argp- Gets the locking status of the
termios structure of the terminal. TIOCSLCKTRMIOS const struct termios *argp- Sets the locking status of the
termios structure of the terminal. Only a process with theCAP_SYS_ADMINcapability can do this.
Get and set window size
Window sizes are kept in the kernel, but not used by the kernel (except in the case of virtual consoles, where the kernel will update the window size when the size of the virtual console changes, for example, by loading a new font).
The following constants and structure are defined in <sys/ioctl.h>.
TIOCGWINSZargp struct winsize *- Get window size.
TIOCSWINSZargp const struct winsize *- Set window size.
The struct used by these ioctls is defined as
struct winsize {
When the window size changes, a SIGWINCH signal is sent to the foreground process group.
Sending a break
TCSBRKarg int- Equivalent to
tcsendbreak(fd, arg). - If the terminal is using asynchronous serial data transmission, and arg is zero, then send a break (a stream of zero bits) for between 0.25 and 0.5 seconds. If the terminal is not using asynchronous serial data transmission, then either a break is sent, or the function returns without doing anything. When arg is nonzero, nobody knows what will happen.
- (SVr4, UnixWare, Solaris, Linux treat tcsendbreak(fd,arg) with nonzero arg like tcdrain(fd). SunOS treats arg as a multiplier, and sends a stream of bits arg times as long as done for zero arg. DG/UX and AIX treat arg (when nonzero) as a time interval measured in milliseconds. HP-UX ignores arg.)
TCSBRKP intarg- So-called "POSIX version" of
TCSBRK. It treats nonzero arg as a time interval measured in deciseconds, and does nothing when the driver does not support breaks. TIOCSBRK void- Turn break on, that is, start sending zero bits.
TIOCCBRK void- Turn break off, that is, stop sending zero bits.
Software flow control
TCXONCarg int- Equivalent to
tcflow(fd, arg). - See
tcflow(3) for the argument valuesTCOOFF,TCOON,TCIOFF,TCION.
Buffer count and flushing
FIONREADargp int *- Get the number of bytes in the input buffer.
TIOCINQargp int *- Same as
FIONREAD. TIOCOUTQargp int *- Get the number of bytes in the output buffer.
TCFLSH intarg- Equivalent to
tcflush(fd, arg). - See
tcflush(3) for the argument valuesTCIFLUSH,TCOFLUSH,TCIOFLUSH.
Faking input
TIOCSTIargp const char *- Insert the given byte in the input queue.
Redirecting console output
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. In Linux before version 2.6.10, anybody can do this as long as the output was not redirected yet; since version 2.6.10, only a process with theCAP_SYS_ADMINcapability may do this. If output was redirected already, thenEBUSYis returned, but redirection can be stopped by using this ioctl with fd pointing at /dev/console or /dev/tty0.
Controlling terminal
TIOCSCTTYarg int- Make the given terminal the controlling terminal of the calling process.
The calling process must be a session leader and not have a controlling terminal already. For this case, arg should be specified as zero. - If this terminal is already the controlling terminal of a different session group, then the ioctl fails with
EPERM, unless the caller has theCAP_SYS_ADMINcapability and arg equals 1, in which case the terminal is stolen, and all processes that had it as controlling terminal lose it. TIOCNOTTY void- If the given terminal was the controlling terminal of the calling process,
give up this controlling terminal. If the process was session leader, then sendSIGHUPandSIGCONTto the foreground process group and all processes in the current session lose their controlling terminal.
Process group and session ID
TIOCGPGRPargp pid_t *- When successful, equivalent to
*argp = tcgetpgrp(fd). - Get the process group ID of the foreground process group on this terminal.
TIOCSPGRPargp const pid_t *- Equivalent to
tcsetpgrp(fd, *argp). - Set the foreground process group ID of this terminal.
TIOCGSIDargp pid_t *- Get the session ID of the given terminal.
This fails with the errorENOTTYif the terminal is not a master pseudoterminal and not our controlling terminal. Strange.
Exclusive mode
TIOCEXCL void- Put the terminal into exclusive mode.
No furtheropen(2) operations on the terminal are permitted. (They fail withEBUSY, except for a process with theCAP_SYS_ADMINcapability.) TIOCGEXCLargp int *- (since Linux 3.8)
If the terminal is currently in exclusive mode, place a nonzero value in the location pointed to by argp; otherwise, place zero in *argp. TIOCNXCL void- Disable exclusive mode.
Line discipline
TIOCGETDargp int *- Get the line discipline of the terminal.
TIOCSETDargp const int *- Set the line discipline of the terminal.
Pseudoterminal ioctls
TIOCPKTargp const int *- Enable (when
*argp is nonzero) or disable packet mode. Can be applied to the master side of a pseudoterminal only (and will returnENOTTYotherwise). In packet mode, each subsequentread(2) will return a packet that either contains a single nonzero control byte, or has a single byte containing zero (‘
