tty_ioctl (4) - Linux Manuals
tty_ioctl: ioctls for terminals and serial lines
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
- TCGETS
-
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.
- TCSETSW
-
Equivalent to
tcsetattr(fd, TCSADRAIN, argp). - Allow the output buffer to drain, and set the current serial port settings.
- TCSETSF
-
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 *.
-
TCGETA
-
TCSETA
-
TCSETAW
-
TCSETAF
Locking the termios structure
Thetermios 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 the CAP_SYS_ADMIN capability 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>.
- TIOCGWINSZ
-
Get window size.
- TIOCSWINSZ
-
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.
The BSD ioctls
TIOCSTOP,
TIOCSTART,
TIOCUCNTL,
TIOCREMOTE
have not been implemented under Linux.
The following bits are used by the above ioctls:
If the
CLOCAL
flag for a line is off, the hardware carrier detect (DCD)
signal is significant, and an
open(2)
of the corresponding terminal will block until DCD is asserted,
unless the
O_NONBLOCK
flag is given.
If
CLOCAL
is set, the line behaves as if DCD is always asserted.
The software carrier flag is usually turned on for local devices,
and is off for lines with modems.
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>
int
main(void)
{
Sending a break
tcsendbreak(fd, arg).
TCSBRK.
It treats nonzero
arg
as a time interval measured in deciseconds, and does nothing
when the driver does not support breaks.
Software flow control
tcflow(fd, arg).
Buffer count and flushing
FIONREAD.
tcflush(fd, arg).
Faking input
Redirecting console output
/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 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.
Controlling terminal
The calling process must be a session leader and not have a
controlling terminal already.
For this case,
arg
should be specified as zero.
give up this controlling terminal.
If the process was session leader,
then send
SIGHUP
and
SIGCONT
to the foreground process group
and all processes in the current session lose their controlling terminal.
Process group and session ID
*argp = tcgetpgrp(fd).
tcsetpgrp(fd, *argp).
This fails with the error
ENOTTY
if the terminal is not a master pseudoterminal
and not our controlling terminal.
Strange.
Exclusive mode
No further
open(2)
operations on the terminal are permitted.
(They fail with
EBUSY,
except for a process with the
CAP_SYS_ADMIN
capability.)
If the terminal is currently in exclusive mode,
place a nonzero value in the location pointed to by
argp;
otherwise, place zero in
*argp.
Line discipline
Pseudoterminal ioctls
*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 haS/haQ. TIOCPKT_NOSTOP The start and stop characters are not haS/haQ.
Return the current packet mode setting in the integer pointed to by
argp.
*argp
is nonzero) or remove (if
*argp
is zero) the lock on the pseudoterminal slave device.
(See also
unlockpt(3).)
Place the current lock state of the pseudoterminal slave device
in the location pointed to by
argp.
Given a file descriptor in
fd
that refers to a pseudoterminal master,
open (with the given
open(2)-style
flags)
and return a new file descriptor that refers to the peer
pseudoterminal slave device.
This operation can be performed
regardless of whether the pathname of the slave device
is accessible through the calling process's mount namespace.
Modem control
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 see TIOCM_CAR TIOCM_RNG RNG (ring) TIOCM_RI see TIOCM_RNG TIOCM_DSR DSR (data set ready)
The bits of interest are specified as a bit mask in
arg,
by ORing together any of the bit values,
TIOCM_RNG,
TIOCM_DSR,
TIOCM_CD,
and
TIOCM_CTS.
The caller should use
TIOCGICOUNT
to see which bit has changed.
The counts are written to the
serial_icounter_struct
structure pointed to by
argp.
Marking a line as local
Get the status of the CLOCAL flag in the c_cflag field of the
termios
structure.
Set the CLOCAL flag in the
termios
structure when
*argp
is nonzero, and clear it otherwise.
Linux-specific
For the
TIOCLINUX
ioctl, see
ioctl_console(2).
Kernel debugging
#include <linux/tty.h>
tty_struct
corresponding to
fd.
This command was removed in Linux 2.5.67.
RETURN VALUE
The
ioctl(2)
system call returns 0 on success.
On error, it returns -1 and sets
errno
appropriately.
ERRORS
EXAMPLES
Check the condition of DTR on the serial port.
COLOPHON
This page is part of release 5.10 of the Linux
man-pages
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
https://www.kernel.org/doc/man-pages/.