1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
.\" Copyright 2002 Walter Harms <walter.harms@informatik.uni-oldenburg.de>
.\" and Andries Brouwer <aeb@cwi.nl>.
.\" Distributed under GPL.
.\"
.TH TTY_IOCTL 4 2002-12-29 "Linux" "Linux Programmer's Manual"
.SH NAME
tty ioctl \- ioctls for terminals and serial lines
.SH SYNOPSIS
.sp
.B "#include <termios.h>"
.sp
.BI "int ioctl(int " fd ", int " cmd ", ...);"
.SH DESCRIPTION
The
.BR ioctl ()
call for terminals and serial ports accepts many possible command arguments.
Most require a third argument, of varying type, here called \fIargp\fP
or \fIarg\fP.
.LP
Use of
.I ioctl
makes for non-portable programs.
Use the POSIX interface described in
.BR termios (3)
whenever possible.
.SS "Get and Set Terminal Attributes"
.TP
.BI "TCGETS struct termios *" argp
Equivalent to
.IR "tcgetattr(fd, argp)" .
.br
Get the current serial port settings.
.TP
.BI "TCSETS const struct termios *" argp
Equivalent to
.IR "tcsetattr(fd, TCSANOW, argp)" .
.br
Set the current serial port settings.
.TP
.BI "TCSETSW const struct termios *" argp
Equivalent to
.IR "tcsetattr(fd, TCSADRAIN, argp)" .
.br
Allow the output buffer to drain, and
set the current serial port settings.
.TP
.BI "TCSETSF const struct termios *" argp
Equivalent to
.IR "tcsetattr(fd, TCSAFLUSH, argp)" .
.br
Allow the output buffer to drain, discard pending input, and
set the current serial port settings.
.LP
The following four ioctls are just like
.BR TCGETS ,
.BR TCSETS ,
.BR TCSETSW ,
.BR TCSETSF ,
except that they take a
.I "struct termio *"
instead of a
.IR "struct termios *" .
.TP
.BI "TCGETA struct termio *" argp
.TP
.BI "TCSETA const struct termio *" argp
.TP
.BI "TCSETAW const struct termio *" argp
.TP
.BI "TCSETAF const struct termio *" argp
.SS "Locking the termios structure"
The termios structure of a tty can be locked.
The lock is itself
a termios structure, with nonzero bits or fields indicating a
locked value.
.TP
.BI "TIOCGLCKTRMIOS struct termios *" argp
Gets the locking status of the termios structure of
the terminal.
.TP
.BI "TIOCSLCKTRMIOS const struct termios *" argp
Sets the locking status of the termios structure of
the terminal.
Only root can do this.
.SS "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).
.TP
.BI "TIOCGWINSZ struct winsize *" argp
Get window size.
.TP
.BI "TIOCSWINSZ const struct winsize *" argp
Set window size.
.LP
The struct used by these ioctls is defined as
.in +4n
.nf
struct winsize {
unsigned short ws_row;
unsigned short ws_col;
unsigned short ws_xpixel; /* unused */
unsigned short ws_ypixel; /* unused */
};
.fi
.in
When the window size changes, a
.B SIGWINCH
signal is sent to the
foreground process group.
.SS "Sending a Break"
.TP
.BI "TCSBRK int " arg
Equivalent to
.IR "tcsendbreak(fd, arg)" .
.br
If the terminal is using asynchronous serial data transmission, and
.I 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
.I arg
is nonzero, nobody knows what will happen.
(SVr4, UnixWare, Solaris, Linux treat
.I "tcsendbreak(fd,arg)"
with nonzero
.I arg
like
.IR "tcdrain(fd)" .
SunOS treats
.I arg
as a multiplier, and sends a stream of bits
.I arg
times as long as done for zero
.IR arg .
DG/UX and AIX treat
.I arg
(when nonzero) as a timeinterval measured in milliseconds.
HP-UX ignores
.IR arg .)
.TP
.BI "TCSBRKP int " arg
So-called "POSIX version" of
.BR TCSBRK .
It treats nonzero
.I arg
as a timeinterval measured in deciseconds, and does nothing
when the driver does not support breaks.
.TP
.B "TIOCSBRK void"
Turn break on, that is, start sending zero bits.
.TP
.B "TIOCCBRK void"
Turn break off, that is, stop sending zero bits.
.SS "Software flow control"
.TP
.BI "TCXONC int " arg
Equivalent to
.IR "tcflow(fd, arg)" .
.br
See
.BR tcflow (3)
for the argument values
.BR TCOOFF ,
.BR TCOON ,
.BR TCIOFF ,
.BR TCION .
.SS "Buffer count and flushing"
.TP
.BI "FIONREAD int *" argp
Get the number of bytes in the input buffer.
.TP
.BI "TIOCINQ int *" argp
Same as
.BR FIONREAD .
.TP
.BI "TIOCOUTQ int *" argp
Get the number of bytes in the output buffer.
.TP
.BI "TCFLSH int " arg
Equivalent to
.IR "tcflush(fd, arg)" .
.br
See
.BR tcflush (3)
for the argument values
.BR TCIFLUSH ,
.BR TCOFLUSH ,
.BR TCIOFLUSH .
.SS "Faking input"
.TP
.BI "TIOCSTI const char *" argp
Insert the given byte in the input queue.
.SS "Redirecting console output"
.TP
.B "TIOCCONS void"
Redirect output that would have gone to
.I /dev/console
or
.I /dev/tty0
to the given tty.
If that was a pty master, send it to the slave.
Anybody can do this as long as the output was not redirected yet.
If it was redirected already
.B EBUSY
is returned,
but root may stop redirection by using this ioctl with
.I fd
pointing at
.I /dev/console
or
.IR /dev/tty0 .
.SS "Controlling tty"
.TP
.BI "TIOCSCTTY int " arg
Make the given tty the controlling tty of the calling process.
The calling process must be a session leader and not have a
controlling tty already.
If this tty is already the controlling tty
of a different session group then the ioctl fails with
.BR EPERM ,
unless the caller is root and
.I arg
equals 1, in which case the tty is stolen, and all processes that had
it as controlling tty lose it.
.TP
.B TIOCNOTTY void
If the given tty was the controlling tty of the calling process,
give up this controlling tty.
If the process was session leader,
then send
.B SIGHUP
and
.B SIGCONT
to the foreground process group
and all processes in the current session lose their controlling tty.
.SS "Process group and session ID"
.TP
.BI "TIOCGPGRP pid_t *" argp
When successful, equivalent to
.IR "*argp = tcgetpgrp(fd)" .
.br
Get the process group ID of the foreground process group on this tty.
.TP
.BI "TIOCSPGRP const pid_t *" argp
Equivalent to
.IR "tcsetpgrp(fd, *argp)" .
.br
Set the foreground process group ID of this tty.
.TP
.BI "TIOCGSID pid_t *" argp
Get the session ID of the given tty.
This will fail with
.B ENOTTY
in case the tty is not a master pty and not our controlling tty.
Strange.
.SS "Exclusive mode"
.TP
.B "TIOCEXCL void"
Put the tty into exclusive mode.
No further
.BR open (2)
operations on the terminal are permitted.
(They will fail with
.BR EBUSY ,
except for root.)
.TP
.B "TIOCNXCL void"
Disable exclusive mode.
.SS "Line discipline"
.TP
.BI "TIOCGETD int *" argp
Get the line discipline of the tty.
.TP
.BI "TIOCSETD const int *" argp
Set the line discipline of the tty.
.SS "Pseudo-tty ioctls"
.TP
.BI "TIOCPKT const int *" argp
Enable (when
.RI * argp
is nonzero) or disable packet mode.
Can be applied to the master side of a pseudo-terminal only (and will return
.B ENOTTY
otherwise).
In packet mode, each subsequent
.BR 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 pty.
If the first byte is not
.B TIOCPKT_DATA
(0), it is an OR of one
or more of the following bits:
.nf
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 t_stopc is `^S' and t_startc is `^Q'.
TIOCPKT_NOSTOP The start and stop characters are not
`^S/^Q'.
.fi
While this mode is in use, the presence
of control status information to be read
from the master side may be detected by a
.BR select (2)
for exceptional conditions.
This mode is used by
.BR rlogin (1)
and
.BR rlogind (8)
to implement a remote-echoed, locally `^S/^Q' flow-controlled remote login.
The BSD ioctls
.BR TIOCSTOP ,
.BR TIOCSTART ,
.BR TIOCUCNTL ,
.B TIOCREMOTE
have not been implemented under Linux.
.SS "Modem control"
.TP
.BI "TIOCMGET int *" argp
get the status of modem bits.
.TP
.BI "TIOCMSET const int *" argp
set the status of modem bits.
.TP
.BI "TIOCMBIC const int *" argp
clear the indicated modem bits.
.TP
.BI "TIOCMBIS const int *" argp
set the indicated modem bits.
.LP
Bits used by these four ioctls:
.nf
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)
.fi
.SS "Marking a line as local"
.TP
.BI "TIOCGSOFTCAR int *" argp
("Get software carrier flag")
Get the status of the CLOCAL flag in the c_cflag field of the
termios structure.
.TP
.BI "TIOCSSOFTCAR const int *" argp
("Set software carrier flag")
Set the CLOCAL flag in the termios structure when
.RI * argp
is nonzero, and clear it otherwise.
.LP
If the
.B CLOCAL
flag for a line is off, the hardware carrier detect (DCD)
signal is significant, and an
.BR open (2)
of the corresponding tty will block until DCD is asserted,
unless the
.B O_NONBLOCK
flag is given.
If
.B 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.
.SS "Linux-specific"
For the
.B TIOCLINUX
ioctl, see
.BR console_ioctl (4).
.SS "Kernel debugging"
.sp
.B "#include <linux/tty.h>"
.TP
.BI "TIOCTTYGSTRUCT struct tty_struct *" argp
Get the
.I tty_struct
corresponding to
.IR fd .
.\"
.\" .SS "Serial info"
.\" .sp
.\" .BR "#include <linux/serial.h>"
.\" .sp
.\" .TP
.\" .BI "TIOCGSERIAL struct serial_struct *" argp
.\" Get serial info.
.\" .TP
.\" .BI "TIOCSSERIAL const struct serial_struct *" argp
.\" Set serial info.
.SH "RETURN VALUE"
The
.BR ioctl ()
system call returns 0 on success.
On error it returns \-1 and sets
.I errno
appropriately.
.SH ERRORS
.TP
.B EINVAL
Invalid command parameter.
.TP
.B ENOIOCTLCMD
Unknown command.
.TP
.B ENOTTY
Inappropriate
.IR fd .
.TP
.B EPERM
Insufficient permission.
.SH EXAMPLE
Check the condition of DTR on the serial port.
.nf
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>
int
main(void)
{
int fd, serial;
fd = open("/dev/ttyS0", O_RDONLY);
ioctl(fd, TIOCMGET, &serial);
if (serial & TIOCM_DTR)
puts("TIOCM_DTR is not set");
else
puts("TIOCM_DTR is set");
close(fd);
}
.fi
.SH "SEE ALSO"
.BR ioctl (2),
.BR termios (3),
.BR console_ioctl (4),
.BR pty (7)
.\"
.\" FIONBIO const int *
.\" FIONCLEX void
.\" FIOCLEX void
.\" FIOASYNC const int *
.\" from serial.c:
.\" TIOCSERCONFIG void
.\" TIOCSERGWILD int *
.\" TIOCSERSWILD const int *
.\" TIOCSERGSTRUCT struct async_struct *
.\" TIOCSERGETLSR int *
.\" TIOCSERGETMULTI struct serial_multiport_struct *
.\" TIOCSERSETMULTI const struct serial_multiport_struct *
.\" TIOCGSERIAL, TIOCSSERIAL (see above)
|