ioctl_tty.2: Update DTR example
[man-pages.git] / man2 / ioctl_tty.2
blobde6d1c839ffab2eec92d81d8e679a4c8aa63743a
1 .\" Copyright 2002 Walter Harms <walter.harms@informatik.uni-oldenburg.de>
2 .\" and Andries Brouwer <aeb@cwi.nl>.
3 .\"
4 .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
5 .\" Distributed under GPL
6 .\" %%%LICENSE_END
7 .\"
8 .TH IOCTL_TTY 2 2021-03-22 "Linux" "Linux Programmer's Manual"
9 .SH NAME
10 ioctl_tty \- ioctls for terminals and serial lines
11 .SH SYNOPSIS
12 .nf
13 .B #include <sys/ioctl.h>
14 .BR "#include <termios.h>" "      /* Definition of " CLOCAL ", and"
15 .BR    "                             TC*" { FLUSH , ON , OFF "} constants */"
16 .PP
17 .BI "int ioctl(int " fd ", int " cmd ", ...);"
18 .fi
19 .SH DESCRIPTION
20 The
21 .BR ioctl (2)
22 call for terminals and serial ports accepts many possible command arguments.
23 Most require a third argument, of varying type, here called
24 .I argp
26 .IR arg .
27 .PP
28 Use of
29 .BR ioctl ()
30 makes for nonportable programs.
31 Use the POSIX interface described in
32 .BR termios (3)
33 whenever possible.
34 .SS Get and set terminal attributes
35 .TP
36 .B TCGETS
37 Argument:
38 .BI "struct termios *" argp
39 .IP
40 Equivalent to
41 .IR "tcgetattr(fd, argp)" .
42 .IP
43 Get the current serial port settings.
44 .TP
45 .B TCSETS
46 Argument:
47 .BI "const struct termios *" argp
48 .IP
49 Equivalent to
50 .IR "tcsetattr(fd, TCSANOW, argp)" .
51 .IP
52 Set the current serial port settings.
53 .TP
54 .B TCSETSW
55 Argument:
56 .BI "const struct termios *" argp
57 .IP
58 Equivalent to
59 .IR "tcsetattr(fd, TCSADRAIN, argp)" .
60 .IP
61 Allow the output buffer to drain, and
62 set the current serial port settings.
63 .TP
64 .B TCSETSF
65 Argument:
66 .BI "const struct termios *" argp
67 .IP
68 Equivalent to
69 .IR "tcsetattr(fd, TCSAFLUSH, argp)" .
70 .IP
71 Allow the output buffer to drain, discard pending input, and
72 set the current serial port settings.
73 .PP
74 The following four ioctls are just like
75 .BR TCGETS ,
76 .BR TCSETS ,
77 .BR TCSETSW ,
78 .BR TCSETSF ,
79 except that they take a
80 .I "struct termio\ *"
81 instead of a
82 .IR "struct termios\ *" .
83 .RS
84 .TS
85 lb l.
86 TCGETA  \fBstruct termio *\fPargp
87 TCSETA  \fBconst struct termio *\fPargp
88 TCSETAW \fBconst struct termio *\fPargp
89 TCSETAF \fBconst struct termio *\fPargp
90 .TE
91 .RE
92 .SS Locking the termios structure
93 The
94 .I termios
95 structure of a terminal can be locked.
96 The lock is itself a
97 .I termios
98 structure, with nonzero bits or fields indicating a
99 locked value.
101 .B TIOCGLCKTRMIOS
102 Argument:
103 .BI "struct termios *" argp
105 Gets the locking status of the
106 .I termios
107 structure of the terminal.
109 .B TIOCSLCKTRMIOS
110 Argument:
111 .BI "const struct termios *" argp
113 Sets the locking status of the
114 .I termios
115 structure of the terminal.
116 Only a process with the
117 .BR CAP_SYS_ADMIN
118 capability can do this.
119 .SS Get and set window size
120 Window sizes are kept in the kernel, but not used by the kernel
121 (except in the case of virtual consoles, where the kernel will
122 update the window size when the size of the virtual console changes,
123 for example, by loading a new font).
125 .B TIOCGWINSZ
126 Argument:
127 .BI "struct winsize *" argp
129 Get window size.
131 .B TIOCSWINSZ
132 Argument:
133 .BI "const struct winsize *" argp
135 Set window size.
137 The struct used by these ioctls is defined as
139 .in +4n
141 struct winsize {
142     unsigned short ws_row;
143     unsigned short ws_col;
144     unsigned short ws_xpixel;   /* unused */
145     unsigned short ws_ypixel;   /* unused */
150 When the window size changes, a
151 .B SIGWINCH
152 signal is sent to the
153 foreground process group.
154 .SS Sending a break
156 .B TCSBRK
157 Argument:
158 .BI "int " arg
160 Equivalent to
161 .IR "tcsendbreak(fd, arg)" .
163 If the terminal is using asynchronous serial data transmission, and
164 .I arg
165 is zero, then send a break (a stream of zero bits) for between
166 0.25 and 0.5 seconds.
167 If the terminal is not using asynchronous
168 serial data transmission, then either a break is sent, or the function
169 returns without doing anything.
170 When
171 .I arg
172 is nonzero, nobody knows what will happen.
174 (SVr4, UnixWare, Solaris, and Linux treat
175 .I "tcsendbreak(fd,arg)"
176 with nonzero
177 .I arg
178 like
179 .IR "tcdrain(fd)" .
180 SunOS treats
181 .I arg
182 as a multiplier, and sends a stream of bits
183 .I arg
184 times as long as done for zero
185 .IR arg .
186 DG/UX and AIX treat
187 .I arg
188 (when nonzero) as a time interval measured in milliseconds.
189 HP-UX ignores
190 .IR arg .)
192 .B TCSBRKP
193 Argument:
194 .BI "int " arg
196 So-called "POSIX version" of
197 .BR TCSBRK .
198 It treats nonzero
199 .I arg
200 as a time interval measured in deciseconds, and does nothing
201 when the driver does not support breaks.
203 .B TIOCSBRK
204 Argument:
205 .BI "void"
207 Turn break on, that is, start sending zero bits.
209 .B TIOCCBRK
210 Argument:
211 .BI "void"
213 Turn break off, that is, stop sending zero bits.
214 .SS Software flow control
216 .B TCXONC
217 Argument:
218 .BI "int " arg
220 Equivalent to
221 .IR "tcflow(fd, arg)" .
224 .BR tcflow (3)
225 for the argument values
226 .BR TCOOFF ,
227 .BR TCOON ,
228 .BR TCIOFF ,
229 .BR TCION .
230 .SS Buffer count and flushing
232 .BI FIONREAD
233 Argument:
234 .BI "int *" argp
236 Get the number of bytes in the input buffer.
238 .B TIOCINQ
239 Argument:
240 .BI "int *" argp
242 Same as
243 .BR FIONREAD .
245 .B TIOCOUTQ
246 Argument:
247 .BI "int *" argp
249 Get the number of bytes in the output buffer.
251 .B TCFLSH
252 Argument:
253 .BI "int " arg
255 Equivalent to
256 .IR "tcflush(fd, arg)" .
259 .BR tcflush (3)
260 for the argument values
261 .BR TCIFLUSH ,
262 .BR TCOFLUSH ,
263 .BR TCIOFLUSH .
264 .SS Faking input
266 .B TIOCSTI
267 Argument:
268 .BI "const char *" argp
270 Insert the given byte in the input queue.
271 .SS Redirecting console output
273 .B TIOCCONS
274 Argument:
275 .BI "void"
277 Redirect output that would have gone to
278 .I /dev/console
280 .I /dev/tty0
281 to the given terminal.
282 If that was a pseudoterminal master, send it to the slave.
283 In Linux before version 2.6.10,
284 anybody can do this as long as the output was not redirected yet;
285 since version 2.6.10, only a process with the
286 .BR CAP_SYS_ADMIN
287 capability may do this.
288 If output was redirected already, then
289 .B EBUSY
290 is returned,
291 but redirection can be stopped by using this ioctl with
292 .I fd
293 pointing at
294 .I /dev/console
296 .IR /dev/tty0 .
297 .SS Controlling terminal
299 .B TIOCSCTTY
300 Argument:
301 .BI "int " arg
303 Make the given terminal the controlling terminal of the calling process.
304 The calling process must be a session leader and not have a
305 controlling terminal already.
306 For this case,
307 .I arg
308 should be specified as zero.
310 If this terminal is already the controlling terminal
311 of a different session group, then the ioctl fails with
312 .BR EPERM ,
313 unless the caller has the
314 .BR CAP_SYS_ADMIN
315 capability and
316 .I arg
317 equals 1, in which case the terminal is stolen, and all processes that had
318 it as controlling terminal lose it.
320 .B TIOCNOTTY
321 Argument:
322 .BI "void"
324 If the given terminal was the controlling terminal of the calling process,
325 give up this controlling terminal.
326 If the process was session leader,
327 then send
328 .B SIGHUP
330 .B SIGCONT
331 to the foreground process group
332 and all processes in the current session lose their controlling terminal.
333 .SS Process group and session ID
335 .B TIOCGPGRP
336 Argument:
337 .BI "pid_t *" argp
339 When successful, equivalent to
340 .IR "*argp = tcgetpgrp(fd)" .
342 Get the process group ID of the foreground process group on this terminal.
344 .B TIOCSPGRP
345 Argument:
346 .BI "const pid_t *" argp
348 Equivalent to
349 .IR "tcsetpgrp(fd, *argp)" .
351 Set the foreground process group ID of this terminal.
353 .B TIOCGSID
354 Argument:
355 .BI "pid_t *" argp
357 Get the session ID of the given terminal.
358 This fails with the error
359 .B ENOTTY
360 if the terminal is not a master pseudoterminal
361 and not our controlling terminal.
362 Strange.
363 .SS Exclusive mode
365 .B TIOCEXCL
366 Argument:
367 .BI "void"
369 Put the terminal into exclusive mode.
370 No further
371 .BR open (2)
372 operations on the terminal are permitted.
373 (They fail with
374 .BR EBUSY ,
375 except for a process with the
376 .BR CAP_SYS_ADMIN
377 capability.)
379 .B TIOCGEXCL
380 Argument:
381 .BI "int *" argp
383 (since Linux 3.8)
384 If the terminal is currently in exclusive mode,
385 place a nonzero value in the location pointed to by
386 .IR argp ;
387 otherwise, place zero in
388 .IR *argp .
390 .B TIOCNXCL
391 Argument:
392 .BI "void"
394 Disable exclusive mode.
395 .SS Line discipline
397 .B TIOCGETD
398 Argument:
399 .BI "int *" argp
401 Get the line discipline of the terminal.
403 .B TIOCSETD
404 Argument:
405 .BI "const int *" argp
407 Set the line discipline of the terminal.
408 .SS Pseudoterminal ioctls
410 .B TIOCPKT
411 Argument:
412 .BI "const int *" argp
414 Enable (when
415 .RI * argp
416 is nonzero) or disable packet mode.
417 Can be applied to the master side of a pseudoterminal only (and will return
418 .B ENOTTY
419 otherwise).
420 In packet mode, each subsequent
421 .BR read (2)
422 will return a packet that either contains a single nonzero control byte,
423 or has a single byte containing zero (\(aq\e0\(aq) followed by data
424 written on the slave side of the pseudoterminal.
425 If the first byte is not
426 .B TIOCPKT_DATA
427 (0), it is an OR of one
428 or more of the following bits:
430 .ad l
432 lb l.
433 TIOCPKT_FLUSHREAD       T{
434 The read queue for the terminal is flushed.
436 TIOCPKT_FLUSHWRITE      T{
437 The write queue for the terminal is flushed.
439 TIOCPKT_STOP    T{
440 Output to the terminal is stopped.
442 TIOCPKT_START   T{
443 Output to the terminal is restarted.
445 TIOCPKT_DOSTOP  T{
446 The start and stop characters are \fB\(haS\fP/\fB\(haQ\fP.
448 TIOCPKT_NOSTOP  T{
449 The start and stop characters are not \fB\(haS\fP/\fB\(haQ\fP.
454 While packet mode is in use, the presence
455 of control status information to be read
456 from the master side may be detected by a
457 .BR select (2)
458 for exceptional conditions or a
459 .BR poll (2)
460 for the
461 .B POLLPRI
462 event.
464 This mode is used by
465 .BR rlogin (1)
467 .BR rlogind (8)
468 to implement a remote-echoed,
469 locally \fB\(haS\fP/\fB\(haQ\fP flow-controlled remote login.
471 .B TIOCGPKT
472 Argument:
473 .BI "const int *" argp
475 (since Linux 3.8)
476 Return the current packet mode setting in the integer pointed to by
477 .IR argp .
479 .B TIOCSPTLCK
480 Argument:
481 .BI "int *" argp
483 Set (if
484 .IR *argp
485 is nonzero) or remove (if
486 .IR *argp
487 is zero) the lock on the pseudoterminal slave device.
488 (See also
489 .BR unlockpt (3).)
491 .B TIOCGPTLCK
492 Argument:
493 .BI "int *" argp
495 (since Linux 3.8)
496 Place the current lock state of the pseudoterminal slave device
497 in the location pointed to by
498 .IR argp .
500 .B TIOCGPTPEER
501 Argument:
502 .BI "int " flags
504 .\" commit 54ebbfb1603415d9953c150535850d30609ef077
505 (since Linux 4.13)
506 Given a file descriptor in
507 .I fd
508 that refers to a pseudoterminal master,
509 open (with the given
510 .BR open (2)-style
511 .IR flags )
512 and return a new file descriptor that refers to the peer
513 pseudoterminal slave device.
514 This operation can be performed
515 regardless of whether the pathname of the slave device
516 is accessible through the calling process's mount namespace.
518 Security-conscious programs interacting with namespaces may wish to use this
519 operation rather than
520 .BR open (2)
521 with the pathname returned by
522 .BR ptsname (3),
523 and similar library functions that have insecure APIs.
524 (For example, confusion can occur in some cases using
525 .BR ptsname (3)
526 with a pathname where a devpts filesystem
527 has been mounted in a different mount namespace.)
529 The BSD ioctls
530 .BR TIOCSTOP ,
531 .BR TIOCSTART ,
532 .BR TIOCUCNTL ,
534 .B TIOCREMOTE
535 have not been implemented under Linux.
536 .SS Modem control
538 .B TIOCMGET
539 Argument:
540 .BI "int *" argp
542 Get the status of modem bits.
544 .B TIOCMSET
545 Argument:
546 .BI "const int *" argp
548 Set the status of modem bits.
550 .B TIOCMBIC
551 Argument:
552 .BI "const int *" argp
554 Clear the indicated modem bits.
556 .B TIOCMBIS
557 Argument:
558 .BI "const int *" argp
560 Set the indicated modem bits.
562 The following bits are used by the above ioctls:
565 lb l.
566 TIOCM_LE        DSR (data set ready/line enable)
567 TIOCM_DTR       DTR (data terminal ready)
568 TIOCM_RTS       RTS (request to send)
569 TIOCM_ST        Secondary TXD (transmit)
570 TIOCM_SR        Secondary RXD (receive)
571 TIOCM_CTS       CTS (clear to send)
572 TIOCM_CAR       DCD (data carrier detect)
573 TIOCM_CD        see TIOCM_CAR
574 TIOCM_RNG       RNG (ring)
575 TIOCM_RI        see TIOCM_RNG
576 TIOCM_DSR       DSR (data set ready)
579 .B TIOCMIWAIT
580 Argument:
581 .BI "int " arg
583 Wait for any of the 4 modem bits (DCD, RI, DSR, CTS) to change.
584 The bits of interest are specified as a bit mask in
585 .IR arg ,
586 by ORing together any of the bit values,
587 .BR TIOCM_RNG ,
588 .BR TIOCM_DSR ,
589 .BR TIOCM_CD ,
591 .BR TIOCM_CTS .
592 The caller should use
593 .B TIOCGICOUNT
594 to see which bit has changed.
596 .B TIOCGICOUNT
597 Argument:
598 .BI "struct serial_icounter_struct *" argp
600 Get counts of input serial line interrupts (DCD, RI, DSR, CTS).
601 The counts are written to the
602 .I serial_icounter_struct
603 structure pointed to by
604 .IR argp .
606 Note: both 1->0 and 0->1 transitions are counted, except for
607 RI, where only 0->1 transitions are counted.
608 .SS Marking a line as local
610 .B TIOCGSOFTCAR
611 Argument:
612 .BI "int *" argp
614 ("Get software carrier flag")
615 Get the status of the CLOCAL flag in the c_cflag field of the
616 .I termios
617 structure.
619 .B TIOCSSOFTCAR
620 Argument:
621 .BI "const int *" argp
623 ("Set software carrier flag")
624 Set the CLOCAL flag in the
625 .I termios
626 structure when
627 .RI * argp
628 is nonzero, and clear it otherwise.
630 If the
631 .B CLOCAL
632 flag for a line is off, the hardware carrier detect (DCD)
633 signal is significant, and an
634 .BR open (2)
635 of the corresponding terminal will block until DCD is asserted,
636 unless the
637 .B O_NONBLOCK
638 flag is given.
640 .B CLOCAL
641 is set, the line behaves as if DCD is always asserted.
642 The software carrier flag is usually turned on for local devices,
643 and is off for lines with modems.
644 .SS Linux-specific
645 For the
646 .B TIOCLINUX
647 ioctl, see
648 .BR ioctl_console (2).
649 .SS Kernel debugging
650 .B "#include <linux/tty.h>"
652 .B TIOCTTYGSTRUCT
653 Argument:
654 .BI "struct tty_struct *" argp
656 Get the
657 .I tty_struct
658 corresponding to
659 .IR fd .
660 This command was removed in Linux 2.5.67.
661 .\"     commit b3506a09d15dc5aee6d4bb88d759b157016e1864
662 .\"     Author: Andries E. Brouwer <andries.brouwer@cwi.nl>
663 .\"     Date:   Tue Apr 1 04:42:46 2003 -0800
665 .\"     [PATCH] kill TIOCTTYGSTRUCT
667 .\"     Only used for (dubious) debugging purposes, and exposes
668 .\"     internal kernel state.
670 .\" .SS Serial info
671 .\" .BR "#include <linux/serial.h>"
672 .\" .PP
673 .\" .TP
674 .\" .BI "TIOCGSERIAL    struct serial_struct *" argp
675 .\" Get serial info.
676 .\" .TP
677 .\" .BI "TIOCSSERIAL    const struct serial_struct *" argp
678 .\" Set serial info.
679 .SH RETURN VALUE
681 .BR ioctl (2)
682 system call returns 0 on success.
683 On error, it returns \-1 and sets
684 .I errno
685 to indicate the error.
686 .SH ERRORS
688 .B EINVAL
689 Invalid command parameter.
691 .B ENOIOCTLCMD
692 Unknown command.
694 .B ENOTTY
695 Inappropriate
696 .IR fd .
698 .B EPERM
699 Insufficient permission.
700 .SH EXAMPLES
701 Check the condition of DTR on the serial port.
704 #include <stdio.h>
705 #include <unistd.h>
706 #include <fcntl.h>
707 #include <sys/ioctl.h>
710 main(void)
712     int fd, serial;
714     fd = open("/dev/ttyS0", O_RDONLY);
715     ioctl(fd, TIOCMGET, &serial);
716     if (serial & TIOCM_DTR)
717         puts("TIOCM_DTR is set");
718     else
719         puts("TIOCM_DTR is not set");
720     close(fd);
723 .SH SEE ALSO
724 .BR ldattach (1),
725 .BR ioctl (2),
726 .BR ioctl_console (2),
727 .BR termios (3),
728 .BR pty (7)
730 .\" FIONBIO                     const int *
731 .\" FIONCLEX                    void
732 .\" FIOCLEX                     void
733 .\" FIOASYNC                    const int *
734 .\" from serial.c:
735 .\" TIOCSERCONFIG               void
736 .\" TIOCSERGWILD                int *
737 .\" TIOCSERSWILD                const int *
738 .\" TIOCSERGSTRUCT              struct async_struct *
739 .\" TIOCSERGETLSR               int *
740 .\" TIOCSERGETMULTI             struct serial_multiport_struct *
741 .\" TIOCSERSETMULTI             const struct serial_multiport_struct *
742 .\" TIOCGSERIAL, TIOCSSERIAL (see above)