1IOCTL_TTY(2) Linux Programmer's Manual IOCTL_TTY(2)
2
3
4
6 ioctl_tty - ioctls for terminals and serial lines
7
9 #include <sys/ioctl.h>
10 #include <termios.h> /* Definition of CLOCAL, and
11 TC*{FLUSH,ON,OFF} constants */
12
13 int ioctl(int fd, int cmd, ...);
14
16 The ioctl(2) call for terminals and serial ports accepts many possible
17 command arguments. Most require a third argument, of varying type,
18 here called argp or arg.
19
20 Use of ioctl() makes for nonportable programs. Use the POSIX interface
21 described in termios(3) whenever possible.
22
23 Get and set terminal attributes
24 TCGETS Argument: struct termios *argp
25
26 Equivalent to tcgetattr(fd, argp).
27
28 Get the current serial port settings.
29
30 TCSETS Argument: const struct termios *argp
31
32 Equivalent to tcsetattr(fd, TCSANOW, argp).
33
34 Set the current serial port settings.
35
36 TCSETSW
37 Argument: const struct termios *argp
38
39 Equivalent to tcsetattr(fd, TCSADRAIN, argp).
40
41 Allow the output buffer to drain, and set the current serial
42 port settings.
43
44 TCSETSF
45 Argument: const struct termios *argp
46
47 Equivalent to tcsetattr(fd, TCSAFLUSH, argp).
48
49 Allow the output buffer to drain, discard pending input, and set
50 the current serial port settings.
51
52 The following four ioctls, added in Linux 2.6.20, are just like TCGETS,
53 TCSETS, TCSETSW, TCSETSF, except that they take a struct termios2 * in‐
54 stead of a struct termios *. If the structure member c_cflag contains
55 the flag BOTHER, then the baud rate is stored in the structure members
56 c_ispeed and c_ospeed as integer values. These ioctls are not sup‐
57 ported on all architectures.
58
59 TCGETS2 struct termios2 *argp
60 TCSETS2 const struct termios2 *argp
61 TCSETSW2 const struct termios2 *argp
62 TCSETSF2 const struct termios2 *argp
63
64 The following four ioctls are just like TCGETS, TCSETS, TCSETSW, TC‐
65 SETSF, except that they take a struct termio * instead of a struct
66 termios *.
67
68 TCGETA struct termio *argp
69 TCSETA const struct termio *argp
70 TCSETAW const struct termio *argp
71 TCSETAF const struct termio *argp
72
73 Locking the termios structure
74 The termios structure of a terminal can be locked. The lock is itself
75 a termios structure, with nonzero bits or fields indicating a locked
76 value.
77
78 TIOCGLCKTRMIOS
79 Argument: struct termios *argp
80
81 Gets the locking status of the termios structure of the termi‐
82 nal.
83
84 TIOCSLCKTRMIOS
85 Argument: const struct termios *argp
86
87 Sets the locking status of the termios structure of the termi‐
88 nal. Only a process with the CAP_SYS_ADMIN capability can do
89 this.
90
91 Get and set window size
92 Window sizes are kept in the kernel, but not used by the kernel (except
93 in the case of virtual consoles, where the kernel will update the win‐
94 dow size when the size of the virtual console changes, for example, by
95 loading a new font).
96
97 TIOCGWINSZ
98 Argument: struct winsize *argp
99
100 Get window size.
101
102 TIOCSWINSZ
103 Argument: const struct winsize *argp
104
105 Set window size.
106
107 The struct used by these ioctls is defined as
108
109 struct winsize {
110 unsigned short ws_row;
111 unsigned short ws_col;
112 unsigned short ws_xpixel; /* unused */
113 unsigned short ws_ypixel; /* unused */
114 };
115
116 When the window size changes, a SIGWINCH signal is sent to the fore‐
117 ground process group.
118
119 Sending a break
120 TCSBRK Argument: int arg
121
122 Equivalent to tcsendbreak(fd, arg).
123
124 If the terminal is using asynchronous serial data transmission,
125 and arg is zero, then send a break (a stream of zero bits) for
126 between 0.25 and 0.5 seconds. If the terminal is not using
127 asynchronous serial data transmission, then either a break is
128 sent, or the function returns without doing anything. When arg
129 is nonzero, nobody knows what will happen.
130
131 (SVr4, UnixWare, Solaris, and Linux treat tcsendbreak(fd,arg)
132 with nonzero arg like tcdrain(fd). SunOS treats arg as a multi‐
133 plier, and sends a stream of bits arg times as long as done for
134 zero arg. DG/UX and AIX treat arg (when nonzero) as a time in‐
135 terval measured in milliseconds. HP-UX ignores arg.)
136
137 TCSBRKP
138 Argument: int arg
139
140 So-called "POSIX version" of TCSBRK. It treats nonzero arg as a
141 time interval measured in deciseconds, and does nothing when the
142 driver does not support breaks.
143
144 TIOCSBRK
145 Argument: void
146
147 Turn break on, that is, start sending zero bits.
148
149 TIOCCBRK
150 Argument: void
151
152 Turn break off, that is, stop sending zero bits.
153
154 Software flow control
155 TCXONC Argument: int arg
156
157 Equivalent to tcflow(fd, arg).
158
159 See tcflow(3) for the argument values TCOOFF, TCOON, TCIOFF,
160 TCION.
161
162 Buffer count and flushing
163 FIONREAD
164 Argument: int *argp
165
166 Get the number of bytes in the input buffer.
167
168 TIOCINQ
169 Argument: int *argp
170
171 Same as FIONREAD.
172
173 TIOCOUTQ
174 Argument: int *argp
175
176 Get the number of bytes in the output buffer.
177
178 TCFLSH Argument: int arg
179
180 Equivalent to tcflush(fd, arg).
181
182 See tcflush(3) for the argument values TCIFLUSH, TCOFLUSH,
183 TCIOFLUSH.
184
185 Faking input
186 TIOCSTI
187 Argument: const char *argp
188
189 Insert the given byte in the input queue.
190
191 Redirecting console output
192 TIOCCONS
193 Argument: void
194
195 Redirect output that would have gone to /dev/console or
196 /dev/tty0 to the given terminal. If that was a pseudoterminal
197 master, send it to the slave. In Linux before version 2.6.10,
198 anybody can do this as long as the output was not redirected
199 yet; since version 2.6.10, only a process with the CAP_SYS_ADMIN
200 capability may do this. If output was redirected already, then
201 EBUSY is returned, but redirection can be stopped by using this
202 ioctl with fd pointing at /dev/console or /dev/tty0.
203
204 Controlling terminal
205 TIOCSCTTY
206 Argument: int arg
207
208 Make the given terminal the controlling terminal of the calling
209 process. The calling process must be a session leader and not
210 have a controlling terminal already. For this case, arg should
211 be specified as zero.
212
213 If this terminal is already the controlling terminal of a dif‐
214 ferent session group, then the ioctl fails with EPERM, unless
215 the caller has the CAP_SYS_ADMIN capability and arg equals 1, in
216 which case the terminal is stolen, and all processes that had it
217 as controlling terminal lose it.
218
219 TIOCNOTTY
220 Argument: void
221
222 If the given terminal was the controlling terminal of the call‐
223 ing process, give up this controlling terminal. If the process
224 was session leader, then send SIGHUP and SIGCONT to the fore‐
225 ground process group and all processes in the current session
226 lose their controlling terminal.
227
228 Process group and session ID
229 TIOCGPGRP
230 Argument: pid_t *argp
231
232 When successful, equivalent to *argp = tcgetpgrp(fd).
233
234 Get the process group ID of the foreground process group on this
235 terminal.
236
237 TIOCSPGRP
238 Argument: const pid_t *argp
239
240 Equivalent to tcsetpgrp(fd, *argp).
241
242 Set the foreground process group ID of this terminal.
243
244 TIOCGSID
245 Argument: pid_t *argp
246
247 Get the session ID of the given terminal. This fails with the
248 error ENOTTY if the terminal is not a master pseudoterminal and
249 not our controlling terminal. Strange.
250
251 Exclusive mode
252 TIOCEXCL
253 Argument: void
254
255 Put the terminal into exclusive mode. No further open(2) opera‐
256 tions on the terminal are permitted. (They fail with EBUSY, ex‐
257 cept for a process with the CAP_SYS_ADMIN capability.)
258
259 TIOCGEXCL
260 Argument: int *argp
261
262 (since Linux 3.8) If the terminal is currently in exclusive
263 mode, place a nonzero value in the location pointed to by argp;
264 otherwise, place zero in *argp.
265
266 TIOCNXCL
267 Argument: void
268
269 Disable exclusive mode.
270
271 Line discipline
272 TIOCGETD
273 Argument: int *argp
274
275 Get the line discipline of the terminal.
276
277 TIOCSETD
278 Argument: const int *argp
279
280 Set the line discipline of the terminal.
281
282 Pseudoterminal ioctls
283 TIOCPKT
284 Argument: const int *argp
285
286 Enable (when *argp is nonzero) or disable packet mode. Can be
287 applied to the master side of a pseudoterminal only (and will
288 return ENOTTY otherwise). In packet mode, each subsequent
289 read(2) will return a packet that either contains a single non‐
290 zero control byte, or has a single byte containing zero ('\0')
291 followed by data written on the slave side of the pseudotermi‐
292 nal. If the first byte is not TIOCPKT_DATA (0), it is an OR of
293 one or more of the following bits:
294
295 TIOCPKT_FLUSHREAD The read queue for the
296 terminal is flushed.
297 TIOCPKT_FLUSHWRITE The write queue for the
298 terminal is flushed.
299 TIOCPKT_STOP Output to the terminal is
300 stopped.
301 TIOCPKT_START Output to the terminal is
302 restarted.
303 TIOCPKT_DOSTOP The start and stop charac‐
304 ters are ^S/^Q.
305 TIOCPKT_NOSTOP The start and stop charac‐
306 ters are not ^S/^Q.
307
308 While packet mode is in use, the presence of control status in‐
309 formation to be read from the master side may be detected by a
310 select(2) for exceptional conditions or a poll(2) for the POLL‐
311 PRI event.
312
313 This mode is used by rlogin(1) and rlogind(8) to implement a re‐
314 mote-echoed, locally ^S/^Q flow-controlled remote login.
315
316 TIOCGPKT
317 Argument: const int *argp
318
319 (since Linux 3.8) Return the current packet mode setting in the
320 integer pointed to by argp.
321
322 TIOCSPTLCK
323 Argument: int *argp
324
325 Set (if *argp is nonzero) or remove (if *argp is zero) the lock
326 on the pseudoterminal slave device. (See also unlockpt(3).)
327
328 TIOCGPTLCK
329 Argument: int *argp
330
331 (since Linux 3.8) Place the current lock state of the pseudoter‐
332 minal slave device in the location pointed to by argp.
333
334 TIOCGPTPEER
335 Argument: int flags
336
337 (since Linux 4.13) Given a file descriptor in fd that refers to
338 a pseudoterminal master, open (with the given open(2)-style
339 flags) and return a new file descriptor that refers to the peer
340 pseudoterminal slave device. This operation can be performed
341 regardless of whether the pathname of the slave device is acces‐
342 sible through the calling process's mount namespace.
343
344 Security-conscious programs interacting with namespaces may wish
345 to use this operation rather than open(2) with the pathname re‐
346 turned by ptsname(3), and similar library functions that have
347 insecure APIs. (For example, confusion can occur in some cases
348 using ptsname(3) with a pathname where a devpts filesystem has
349 been mounted in a different mount namespace.)
350
351 The BSD ioctls TIOCSTOP, TIOCSTART, TIOCUCNTL, and TIOCREMOTE have not
352 been implemented under Linux.
353
354 Modem control
355 TIOCMGET
356 Argument: int *argp
357
358 Get the status of modem bits.
359
360 TIOCMSET
361 Argument: const int *argp
362
363 Set the status of modem bits.
364
365 TIOCMBIC
366 Argument: const int *argp
367
368 Clear the indicated modem bits.
369
370 TIOCMBIS
371 Argument: const int *argp
372
373 Set the indicated modem bits.
374
375 The following bits are used by the above ioctls:
376
377 TIOCM_LE DSR (data set ready/line enable)
378 TIOCM_DTR DTR (data terminal ready)
379 TIOCM_RTS RTS (request to send)
380 TIOCM_ST Secondary TXD (transmit)
381 TIOCM_SR Secondary RXD (receive)
382 TIOCM_CTS CTS (clear to send)
383 TIOCM_CAR DCD (data carrier detect)
384 TIOCM_CD see TIOCM_CAR
385 TIOCM_RNG RNG (ring)
386 TIOCM_RI see TIOCM_RNG
387 TIOCM_DSR DSR (data set ready)
388
389 TIOCMIWAIT
390 Argument: int arg
391
392 Wait for any of the 4 modem bits (DCD, RI, DSR, CTS) to change.
393 The bits of interest are specified as a bit mask in arg, by OR‐
394 ing together any of the bit values, TIOCM_RNG, TIOCM_DSR, TI‐
395 OCM_CD, and TIOCM_CTS. The caller should use TIOCGICOUNT to see
396 which bit has changed.
397
398 TIOCGICOUNT
399 Argument: struct serial_icounter_struct *argp
400
401 Get counts of input serial line interrupts (DCD, RI, DSR, CTS).
402 The counts are written to the serial_icounter_struct structure
403 pointed to by argp.
404
405 Note: both 1->0 and 0->1 transitions are counted, except for RI,
406 where only 0->1 transitions are counted.
407
408 Marking a line as local
409 TIOCGSOFTCAR
410 Argument: int *argp
411
412 ("Get software carrier flag") Get the status of the CLOCAL flag
413 in the c_cflag field of the termios structure.
414
415 TIOCSSOFTCAR
416 Argument: const int *argp
417
418 ("Set software carrier flag") Set the CLOCAL flag in the termios
419 structure when *argp is nonzero, and clear it otherwise.
420
421 If the CLOCAL flag for a line is off, the hardware carrier detect (DCD)
422 signal is significant, and an open(2) of the corresponding terminal
423 will block until DCD is asserted, unless the O_NONBLOCK flag is given.
424 If CLOCAL is set, the line behaves as if DCD is always asserted. The
425 software carrier flag is usually turned on for local devices, and is
426 off for lines with modems.
427
428 Linux-specific
429 For the TIOCLINUX ioctl, see ioctl_console(2).
430
431 Kernel debugging
432 #include <linux/tty.h>
433
434 TIOCTTYGSTRUCT
435 Argument: struct tty_struct *argp
436
437 Get the tty_struct corresponding to fd. This command was re‐
438 moved in Linux 2.5.67.
439
441 The ioctl(2) system call returns 0 on success. On error, it returns -1
442 and sets errno to indicate the error.
443
445 EINVAL Invalid command parameter.
446
447 ENOIOCTLCMD
448 Unknown command.
449
450 ENOTTY Inappropriate fd.
451
452 EPERM Insufficient permission.
453
455 Check the condition of DTR on the serial port.
456
457 #include <stdio.h>
458 #include <unistd.h>
459 #include <fcntl.h>
460 #include <sys/ioctl.h>
461
462 int
463 main(void)
464 {
465 int fd, serial;
466
467 fd = open("/dev/ttyS0", O_RDONLY);
468 ioctl(fd, TIOCMGET, &serial);
469 if (serial & TIOCM_DTR)
470 puts("TIOCM_DTR is set");
471 else
472 puts("TIOCM_DTR is not set");
473 close(fd);
474 }
475
477 ldattach(1), ioctl(2), ioctl_console(2), termios(3), pty(7)
478
480 This page is part of release 5.13 of the Linux man-pages project. A
481 description of the project, information about reporting bugs, and the
482 latest version of this page, can be found at
483 https://www.kernel.org/doc/man-pages/.
484
485
486
487Linux 2021-08-27 IOCTL_TTY(2)