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