1TTY_IOCTL(4)               Linux Programmer's Manual              TTY_IOCTL(4)
2
3
4

NAME

6       tty_ioctl - ioctls for terminals and serial lines
7

SYNOPSIS

9       #include <termios.h>
10
11       int ioctl(int fd, int cmd, ...);
12

DESCRIPTION

14       The  ioctl(2) call for terminals and serial ports accepts many possible
15       command arguments.  Most require a third  argument,  of  varying  type,
16       here called argp or arg.
17
18       Use  of  ioctl makes for nonportable programs.  Use the POSIX interface
19       described in termios(3) whenever possible.
20
21   Get and set terminal attributes
22       TCGETS    struct termios *argp
23              Equivalent to tcgetattr(fd, argp).
24              Get the current serial port settings.
25
26       TCSETS    const struct termios *argp
27              Equivalent to tcsetattr(fd, TCSANOW, argp).
28              Set the current serial port settings.
29
30       TCSETSW   const struct termios *argp
31              Equivalent to tcsetattr(fd, TCSADRAIN, argp).
32              Allow the output buffer to drain, and  set  the  current  serial
33              port settings.
34
35       TCSETSF   const struct termios *argp
36              Equivalent to tcsetattr(fd, TCSAFLUSH, argp).
37              Allow the output buffer to drain, discard pending input, and set
38              the current serial port settings.
39
40       The following four  ioctls  are  just  like  TCGETS,  TCSETS,  TCSETSW,
41       TCSETSF,  except  that  they take a struct termio * instead of a struct
42       termios *.
43
44              TCGETA    struct termio *argp
45
46              TCSETA    const struct termio *argp
47
48              TCSETAW   const struct termio *argp
49
50              TCSETAF   const struct termio *argp
51
52   Locking the termios structure
53       The termios structure of a terminal can be locked.  The lock is  itself
54       a  termios  structure,  with nonzero bits or fields indicating a locked
55       value.
56
57       TIOCGLCKTRMIOS struct termios *argp
58              Gets the locking status of the termios structure of  the  termi‐
59              nal.
60
61       TIOCSLCKTRMIOS const struct termios *argp
62              Sets  the  locking status of the termios structure of the termi‐
63              nal.   Only  root  (more   precisely:   a   process   with   the
64              CAP_SYS_ADMIN capability) can do this.
65
66   Get and set window size
67       Window sizes are kept in the kernel, but not used by the kernel (except
68       in the case of virtual consoles, where the kernel will update the  win‐
69       dow  size when the size of the virtual console changes, for example, by
70       loading a new font).
71
72       The following constants and structure are defined in <sys/ioctl.h>.
73
74       TIOCGWINSZ     struct winsize *argp
75              Get window size.
76
77       TIOCSWINSZ     const struct winsize *argp
78              Set window size.
79
80       The struct used by these ioctls is defined as
81
82           struct winsize {
83               unsigned short ws_row;
84               unsigned short ws_col;
85               unsigned short ws_xpixel;   /* unused */
86               unsigned short ws_ypixel;   /* unused */
87           };
88
89       When the window size changes, a SIGWINCH signal is sent  to  the  fore‐
90       ground process group.
91
92   Sending a break
93       TCSBRK    int arg
94              Equivalent to tcsendbreak(fd, arg).
95              If  the terminal is using asynchronous serial data transmission,
96              and arg is zero, then send a break (a stream of zero  bits)  for
97              between  0.25  and  0.5  seconds.   If the terminal is not using
98              asynchronous serial data transmission, then either  a  break  is
99              sent,  or the function returns without doing anything.  When arg
100              is nonzero, nobody knows what will happen.
101
102              (SVr4, UnixWare, Solaris, Linux treat  tcsendbreak(fd,arg)  with
103              nonzero arg like tcdrain(fd).  SunOS treats arg as a multiplier,
104              and sends a stream of bits arg times as long as  done  for  zero
105              arg.   DG/UX and AIX treat arg (when nonzero) as a time interval
106              measured in milliseconds.  HP-UX ignores arg.)
107
108       TCSBRKP   int arg
109              So-called "POSIX version" of TCSBRK.  It treats nonzero arg as a
110              timeinterval  measured in deciseconds, and does nothing when the
111              driver does not support breaks.
112
113       TIOCSBRK  void
114              Turn break on, that is, start sending zero bits.
115
116       TIOCCBRK  void
117              Turn break off, that is, stop sending zero bits.
118
119   Software flow control
120       TCXONC    int arg
121              Equivalent to tcflow(fd, arg).
122              See tcflow(3) for the argument  values  TCOOFF,  TCOON,  TCIOFF,
123              TCION.
124
125   Buffer count and flushing
126       FIONREAD  int *argp
127              Get the number of bytes in the input buffer.
128
129       TIOCINQ   int *argp
130              Same as FIONREAD.
131
132       TIOCOUTQ  int *argp
133              Get the number of bytes in the output buffer.
134
135       TCFLSH    int arg
136              Equivalent to tcflush(fd, arg).
137              See  tcflush(3)  for  the  argument  values  TCIFLUSH, TCOFLUSH,
138              TCIOFLUSH.
139
140   Faking input
141       TIOCSTI   const char *argp
142              Insert the given byte in the input queue.
143
144   Redirecting console output
145       TIOCCONS  void
146              Redirect  output  that  would  have  gone  to  /dev/console   or
147              /dev/tty0  to  the given terminal.  If that was a pseudoterminal
148              master, send it to the slave.  In Linux before  version  2.6.10,
149              anybody  can  do  this  as long as the output was not redirected
150              yet; since  version  2.6.10,  only  root  (a  process  with  the
151              CAP_SYS_ADMIN capability) may do this.  If output was redirected
152              already EBUSY is returned, but redirection  can  be  stopped  by
153              using this ioctl with fd pointing at /dev/console or /dev/tty0.
154
155   Controlling terminal
156       TIOCSCTTY int arg
157              Make  the given terminal the controlling terminal of the calling
158              process.  The calling process must be a session leader  and  not
159              have  a  controlling  terminal  already.   If  this  terminal is
160              already the controlling terminal of a  different  session  group
161              then the ioctl fails with EPERM, unless the caller is root (more
162              precisely: has the CAP_SYS_ADMIN capability) and arg  equals  1,
163              in which case the terminal is stolen, and all processes that had
164              it as controlling terminal lose it.
165
166       TIOCNOTTY void
167              If the given terminal was the controlling terminal of the  call‐
168              ing  process, give up this controlling terminal.  If the process
169              was session leader, then send SIGHUP and SIGCONT  to  the  fore‐
170              ground  process  group  and all processes in the current session
171              lose their controlling terminal.
172
173   Process group and session ID
174       TIOCGPGRP pid_t *argp
175              When successful, equivalent to *argp = tcgetpgrp(fd).
176              Get the process group ID of the foreground process group on this
177              terminal.
178
179       TIOCSPGRP const pid_t *argp
180              Equivalent to tcsetpgrp(fd, *argp).
181              Set the foreground process group ID of this terminal.
182
183       TIOCGSID  pid_t *argp
184              Get  the  session ID of the given terminal.  This will fail with
185              ENOTTY in case the terminal is not a master  pseudoterminal  and
186              not our controlling terminal.  Strange.
187
188   Exclusive mode
189       TIOCEXCL  void
190              Put the terminal into exclusive mode.  No further open(2) opera‐
191              tions on the terminal  are  permitted.   (They  will  fail  with
192              EBUSY,   except   for   root,   that  is,  a  process  with  the
193              CAP_SYS_ADMIN capability.)
194
195       TIOCNXCL  void
196              Disable exclusive mode.
197
198   Line discipline
199       TIOCGETD  int *argp
200              Get the line discipline of the terminal.
201
202       TIOCSETD  const int *argp
203              Set the line discipline of the terminal.
204
205   Pseudoterminal ioctls
206       TIOCPKT   const int *argp
207              Enable (when *argp is nonzero) or disable packet mode.   Can  be
208              applied  to  the  master side of a pseudoterminal only (and will
209              return ENOTTY  otherwise).   In  packet  mode,  each  subsequent
210              read(2)  will  return  a  packet  that  either contains a single
211              nonzero control byte, or has a single byte containing zero (' ')
212              followed  by  data written on the slave side of the pseudotermi‐
213              nal.  If the first byte is not TIOCPKT_DATA (0), it is an OR  of
214              one or more of the following bits:
215
216              TIOCPKT_FLUSHREAD   The read queue for the terminal is flushed.
217              TIOCPKT_FLUSHWRITE  The write queue for the terminal is flushed.
218              TIOCPKT_STOP        Output to the terminal is stopped.
219              TIOCPKT_START       Output to the terminal is restarted.
220              TIOCPKT_DOSTOP      The start and stop characters are ^S/^Q.
221              TIOCPKT_NOSTOP      The start and stop characters are not ^S/^Q.
222
223              While this mode is in use, the presence of control status infor‐
224              mation to be read from the master side  may  be  detected  by  a
225              select(2) for exceptional conditions.
226
227              This  mode  is  used  by rlogin(1) and rlogind(8) to implement a
228              remote-echoed, locally ^S/^Q flow-controlled remote login.
229
230              The BSD ioctls TIOCSTOP, TIOCSTART, TIOCUCNTL,  TIOCREMOTE  have
231              not been implemented under Linux.
232
233   Modem control
234       TIOCMGET  int *argp
235              get the status of modem bits.
236
237       TIOCMSET  const int *argp
238              set the status of modem bits.
239
240       TIOCMBIC  const int *argp
241              clear the indicated modem bits.
242
243       TIOCMBIS  const int *argp
244              set the indicated modem bits.
245
246       Bits used by these four ioctls:
247
248       TIOCM_LE        DSR (data set ready/line enable)
249       TIOCM_DTR       DTR (data terminal ready)
250       TIOCM_RTS       RTS (request to send)
251       TIOCM_ST        Secondary TXD (transmit)
252       TIOCM_SR        Secondary RXD (receive)
253       TIOCM_CTS       CTS (clear to send)
254       TIOCM_CAR       DCD (data carrier detect)
255       TIOCM_CD         see TIOCM_CAR
256       TIOCM_RNG       RNG (ring)
257       TIOCM_RI         see TIOCM_RNG
258       TIOCM_DSR       DSR (data set ready)
259
260   Marking a line as local
261       TIOCGSOFTCAR   int *argp
262              ("Get  software carrier flag") Get the status of the CLOCAL flag
263              in the c_cflag field of the termios structure.
264
265       TIOCSSOFTCAR   const int *argp
266              ("Set software carrier flag") Set the CLOCAL flag in the termios
267              structure when *argp is nonzero, and clear it otherwise.
268
269       If the CLOCAL flag for a line is off, the hardware carrier detect (DCD)
270       signal is significant, and an open(2)  of  the  corresponding  terminal
271       will  block until DCD is asserted, unless the O_NONBLOCK flag is given.
272       If CLOCAL is set, the line behaves as if DCD is always  asserted.   The
273       software  carrier  flag  is usually turned on for local devices, and is
274       off for lines with modems.
275
276   Linux-specific
277       For the TIOCLINUX ioctl, see console_ioctl(4).
278
279   Kernel debugging
280       #include <linux/tty.h>
281
282       TIOCTTYGSTRUCT struct tty_struct *argp
283              Get the tty_struct corresponding to fd.
284

RETURN VALUE

286       The ioctl(2) system call returns 0 on success.  On error it returns  -1
287       and sets errno appropriately.
288

ERRORS

290       EINVAL Invalid command parameter.
291
292       ENOIOCTLCMD
293              Unknown command.
294
295       ENOTTY Inappropriate fd.
296
297       EPERM  Insufficient permission.
298

EXAMPLE

300       Check the condition of DTR on the serial port.
301
302       #include <termios.h>
303       #include <fcntl.h>
304       #include <sys/ioctl.h>
305
306       int
307       main(void)
308       {
309           int fd, serial;
310
311           fd = open("/dev/ttyS0", O_RDONLY);
312           ioctl(fd, TIOCMGET, &serial);
313           if (serial & TIOCM_DTR)
314               puts("TIOCM_DTR is not set");
315           else
316               puts("TIOCM_DTR is set");
317           close(fd);
318       }
319

SEE ALSO

321       ioctl(2), termios(3), console_ioctl(4), pty(7)
322

COLOPHON

324       This  page  is  part of release 3.53 of the Linux man-pages project.  A
325       description of the project, and information about reporting  bugs,  can
326       be found at http://www.kernel.org/doc/man-pages/.
327
328
329
330Linux                             2008-10-29                      TTY_IOCTL(4)
Impressum