]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/ioctl_tty.2
Many pages: Use correct letter case in page titles (TH)
[thirdparty/man-pages.git] / man2 / ioctl_tty.2
1 .\" Copyright 2002 Walter Harms <walter.harms@informatik.uni-oldenburg.de>
2 .\" and Andries Brouwer <aeb@cwi.nl>.
3 .\"
4 .\" SPDX-License-Identifier: GPL-1.0-or-later
5 .\"
6 .TH ioctl_tty 2 (date) "Linux man-pages (unreleased)"
7 .SH NAME
8 ioctl_tty \- ioctls for terminals and serial lines
9 .SH LIBRARY
10 Standard C library
11 .RI ( libc ", " \-lc )
12 .SH SYNOPSIS
13 .nf
14 .B #include <sys/ioctl.h>
15 .BR "#include <asm/termbits.h>" " /* Definition of " "struct termios" ,
16 .BR " struct termios2" ", and"
17 .BR " Bnnn" ", " BOTHER ", " CBAUD ", " CLOCAL ,
18 .BR " TC*" { FLUSH , ON , OFF "} and other constants */"
19 .PP
20 .BI "int ioctl(int " fd ", int " cmd ", ...);"
21 .fi
22 .SH DESCRIPTION
23 The
24 .BR ioctl (2)
25 call for terminals and serial ports accepts many possible command arguments.
26 Most require a third argument, of varying type, here called
27 .I argp
28 or
29 .IR arg .
30 .PP
31 Use of
32 .BR ioctl ()
33 makes for nonportable programs.
34 Use the POSIX interface described in
35 .BR termios (3)
36 whenever possible.
37 .PP
38 Please note that
39 .B struct termios
40 from
41 .I <asm/termbits.h>
42 is different and incompatible with
43 .B struct termios
44 from
45 .IR <termios.h> .
46 These ioctl calls require
47 .B struct termios
48 from
49 .IR <asm/termbits.h> .
50 .SS Get and set terminal attributes
51 .TP
52 .B TCGETS
53 Argument:
54 .BI "struct termios\~*" argp
55 .IP
56 Equivalent to
57 .IR "tcgetattr(fd, argp)" .
58 .IP
59 Get the current serial port settings.
60 .TP
61 .B TCSETS
62 Argument:
63 .BI "const struct termios\~*" argp
64 .IP
65 Equivalent to
66 .IR "tcsetattr(fd, TCSANOW, argp)" .
67 .IP
68 Set the current serial port settings.
69 .TP
70 .B TCSETSW
71 Argument:
72 .BI "const struct termios\~*" argp
73 .IP
74 Equivalent to
75 .IR "tcsetattr(fd, TCSADRAIN, argp)" .
76 .IP
77 Allow the output buffer to drain, and
78 set the current serial port settings.
79 .TP
80 .B TCSETSF
81 Argument:
82 .BI "const struct termios\~*" argp
83 .IP
84 Equivalent to
85 .IR "tcsetattr(fd, TCSAFLUSH, argp)" .
86 .IP
87 Allow the output buffer to drain, discard pending input, and
88 set the current serial port settings.
89 .PP
90 The following four ioctls, added in Linux 2.6.20,
91 .\" commit 64bb6c5e1ddcd47c951740485026ef08975ee2e6
92 .\" commit 592ee3a5e5e2a981ef2829a0380093006d045661
93 are just like
94 .BR TCGETS ,
95 .BR TCSETS ,
96 .BR TCSETSW ,
97 .BR TCSETSF ,
98 except that they take a
99 .I "struct termios2\~*"
100 instead of a
101 .IR "struct termios\~*" .
102 If the structure member
103 .B c_cflag
104 contains the flag
105 .BR BOTHER ,
106 then the baud rate is stored in the structure members
107 .B c_ispeed
108 and
109 .B c_ospeed
110 as integer values.
111 These ioctls are not supported on all architectures.
112 .RS
113 .TS
114 lb l.
115 TCGETS2 \fBstruct termios2 *\fPargp
116 TCSETS2 \fBconst struct termios2 *\fPargp
117 TCSETSW2 \fBconst struct termios2 *\fPargp
118 TCSETSF2 \fBconst struct termios2 *\fPargp
119 .TE
120 .RE
121 .PP
122 The following four ioctls are just like
123 .BR TCGETS ,
124 .BR TCSETS ,
125 .BR TCSETSW ,
126 .BR TCSETSF ,
127 except that they take a
128 .I "struct termio\~*"
129 instead of a
130 .IR "struct termios\~*" .
131 .RS
132 .TS
133 lb l.
134 TCGETA \fBstruct termio *\fPargp
135 TCSETA \fBconst struct termio *\fPargp
136 TCSETAW \fBconst struct termio *\fPargp
137 TCSETAF \fBconst struct termio *\fPargp
138 .TE
139 .RE
140 .SS Locking the termios structure
141 The
142 .I termios
143 structure of a terminal can be locked.
144 The lock is itself a
145 .I termios
146 structure, with nonzero bits or fields indicating a
147 locked value.
148 .TP
149 .B TIOCGLCKTRMIOS
150 Argument:
151 .BI "struct termios\~*" argp
152 .IP
153 Gets the locking status of the
154 .I termios
155 structure of the terminal.
156 .TP
157 .B TIOCSLCKTRMIOS
158 Argument:
159 .BI "const struct termios\~*" argp
160 .IP
161 Sets the locking status of the
162 .I termios
163 structure of the terminal.
164 Only a process with the
165 .B CAP_SYS_ADMIN
166 capability can do this.
167 .SS Get and set window size
168 Window sizes are kept in the kernel, but not used by the kernel
169 (except in the case of virtual consoles, where the kernel will
170 update the window size when the size of the virtual console changes,
171 for example, by loading a new font).
172 .TP
173 .B TIOCGWINSZ
174 Argument:
175 .BI "struct winsize\~*" argp
176 .IP
177 Get window size.
178 .TP
179 .B TIOCSWINSZ
180 Argument:
181 .BI "const struct winsize\~*" argp
182 .IP
183 Set window size.
184 .PP
185 The struct used by these ioctls is defined as
186 .PP
187 .in +4n
188 .EX
189 struct winsize {
190 unsigned short ws_row;
191 unsigned short ws_col;
192 unsigned short ws_xpixel; /* unused */
193 unsigned short ws_ypixel; /* unused */
194 };
195 .EE
196 .in
197 .PP
198 When the window size changes, a
199 .B SIGWINCH
200 signal is sent to the
201 foreground process group.
202 .SS Sending a break
203 .TP
204 .B TCSBRK
205 Argument:
206 .BI "int " arg
207 .IP
208 Equivalent to
209 .IR "tcsendbreak(fd, arg)" .
210 .IP
211 If the terminal is using asynchronous serial data transmission, and
212 .I arg
213 is zero, then send a break (a stream of zero bits) for between
214 0.25 and 0.5 seconds.
215 If the terminal is not using asynchronous
216 serial data transmission, then either a break is sent, or the function
217 returns without doing anything.
218 When
219 .I arg
220 is nonzero, nobody knows what will happen.
221 .IP
222 (SVr4, UnixWare, Solaris, and Linux treat
223 .I "tcsendbreak(fd,arg)"
224 with nonzero
225 .I arg
226 like
227 .IR "tcdrain(fd)" .
228 SunOS treats
229 .I arg
230 as a multiplier, and sends a stream of bits
231 .I arg
232 times as long as done for zero
233 .IR arg .
234 DG/UX and AIX treat
235 .I arg
236 (when nonzero) as a time interval measured in milliseconds.
237 HP-UX ignores
238 .IR arg .)
239 .TP
240 .B TCSBRKP
241 Argument:
242 .BI "int " arg
243 .IP
244 So-called "POSIX version" of
245 .BR TCSBRK .
246 It treats nonzero
247 .I arg
248 as a time interval measured in deciseconds, and does nothing
249 when the driver does not support breaks.
250 .TP
251 .B TIOCSBRK
252 Argument:
253 .B void
254 .IP
255 Turn break on, that is, start sending zero bits.
256 .TP
257 .B TIOCCBRK
258 Argument:
259 .B void
260 .IP
261 Turn break off, that is, stop sending zero bits.
262 .SS Software flow control
263 .TP
264 .B TCXONC
265 Argument:
266 .BI "int " arg
267 .IP
268 Equivalent to
269 .IR "tcflow(fd, arg)" .
270 .IP
271 See
272 .BR tcflow (3)
273 for the argument values
274 .BR TCOOFF ,
275 .BR TCOON ,
276 .BR TCIOFF ,
277 .BR TCION .
278 .SS Buffer count and flushing
279 .TP
280 .B FIONREAD
281 Argument:
282 .BI "int\~*" argp
283 .IP
284 Get the number of bytes in the input buffer.
285 .TP
286 .B TIOCINQ
287 Argument:
288 .BI "int\~*" argp
289 .IP
290 Same as
291 .BR FIONREAD .
292 .TP
293 .B TIOCOUTQ
294 Argument:
295 .BI "int\~*" argp
296 .IP
297 Get the number of bytes in the output buffer.
298 .TP
299 .B TCFLSH
300 Argument:
301 .BI "int " arg
302 .IP
303 Equivalent to
304 .IR "tcflush(fd, arg)" .
305 .IP
306 See
307 .BR tcflush (3)
308 for the argument values
309 .BR TCIFLUSH ,
310 .BR TCOFLUSH ,
311 .BR TCIOFLUSH .
312 .TP
313 .B TIOCSERGETLSR
314 Argument:
315 .BI "int\~*" argp
316 .IP
317 Get line status register. Status register has
318 .B TIOCSER_TEMT
319 bit set when output buffer is empty and also hardware transmitter is physically empty.
320 .IP
321 Does not have to be supported by all serial tty drivers.
322 .IP
323 .BR tcdrain (3)
324 does not wait and returns immediately when
325 .B TIOCSER_TEMT
326 bit is set.
327 .SS Faking input
328 .TP
329 .B TIOCSTI
330 Argument:
331 .BI "const char\~*" argp
332 .IP
333 Insert the given byte in the input queue.
334 .SS Redirecting console output
335 .TP
336 .B TIOCCONS
337 Argument:
338 .B void
339 .IP
340 Redirect output that would have gone to
341 .I /dev/console
342 or
343 .I /dev/tty0
344 to the given terminal.
345 If that was a pseudoterminal master, send it to the slave.
346 In Linux before version 2.6.10,
347 anybody can do this as long as the output was not redirected yet;
348 since version 2.6.10, only a process with the
349 .B CAP_SYS_ADMIN
350 capability may do this.
351 If output was redirected already, then
352 .B EBUSY
353 is returned,
354 but redirection can be stopped by using this ioctl with
355 .I fd
356 pointing at
357 .I /dev/console
358 or
359 .IR /dev/tty0 .
360 .SS Controlling terminal
361 .TP
362 .B TIOCSCTTY
363 Argument:
364 .BI "int " arg
365 .IP
366 Make the given terminal the controlling terminal of the calling process.
367 The calling process must be a session leader and not have a
368 controlling terminal already.
369 For this case,
370 .I arg
371 should be specified as zero.
372 .IP
373 If this terminal is already the controlling terminal
374 of a different session group, then the ioctl fails with
375 .BR EPERM ,
376 unless the caller has the
377 .B CAP_SYS_ADMIN
378 capability and
379 .I arg
380 equals 1, in which case the terminal is stolen, and all processes that had
381 it as controlling terminal lose it.
382 .TP
383 .B TIOCNOTTY
384 Argument:
385 .B void
386 .IP
387 If the given terminal was the controlling terminal of the calling process,
388 give up this controlling terminal.
389 If the process was session leader,
390 then send
391 .B SIGHUP
392 and
393 .B SIGCONT
394 to the foreground process group
395 and all processes in the current session lose their controlling terminal.
396 .SS Process group and session ID
397 .TP
398 .B TIOCGPGRP
399 Argument:
400 .BI "pid_t\~*" argp
401 .IP
402 When successful, equivalent to
403 .IR "*argp = tcgetpgrp(fd)" .
404 .IP
405 Get the process group ID of the foreground process group on this terminal.
406 .TP
407 .B TIOCSPGRP
408 Argument:
409 .BI "const pid_t\~*" argp
410 .IP
411 Equivalent to
412 .IR "tcsetpgrp(fd, *argp)" .
413 .IP
414 Set the foreground process group ID of this terminal.
415 .TP
416 .B TIOCGSID
417 Argument:
418 .BI "pid_t\~*" argp
419 .IP
420 When successful, equivalent to
421 .IR "*argp = tcgetsid(fd)" .
422 .IP
423 Get the session ID of the given terminal.
424 This fails with the error
425 .B ENOTTY
426 if the terminal is not a master pseudoterminal
427 and not our controlling terminal.
428 Strange.
429 .SS Exclusive mode
430 .TP
431 .B TIOCEXCL
432 Argument:
433 .B void
434 .IP
435 Put the terminal into exclusive mode.
436 No further
437 .BR open (2)
438 operations on the terminal are permitted.
439 (They fail with
440 .BR EBUSY ,
441 except for a process with the
442 .B CAP_SYS_ADMIN
443 capability.)
444 .TP
445 .B TIOCGEXCL
446 Argument:
447 .BI "int\~*" argp
448 .IP
449 (since Linux 3.8)
450 If the terminal is currently in exclusive mode,
451 place a nonzero value in the location pointed to by
452 .IR argp ;
453 otherwise, place zero in
454 .IR *argp .
455 .TP
456 .B TIOCNXCL
457 Argument:
458 .B void
459 .IP
460 Disable exclusive mode.
461 .SS Line discipline
462 .TP
463 .B TIOCGETD
464 Argument:
465 .BI "int\~*" argp
466 .IP
467 Get the line discipline of the terminal.
468 .TP
469 .B TIOCSETD
470 Argument:
471 .BI "const int\~*" argp
472 .IP
473 Set the line discipline of the terminal.
474 .SS Pseudoterminal ioctls
475 .TP
476 .B TIOCPKT
477 Argument:
478 .BI "const int\~*" argp
479 .IP
480 Enable (when
481 .RI * argp
482 is nonzero) or disable packet mode.
483 Can be applied to the master side of a pseudoterminal only (and will return
484 .B ENOTTY
485 otherwise).
486 In packet mode, each subsequent
487 .BR read (2)
488 will return a packet that either contains a single nonzero control byte,
489 or has a single byte containing zero (\(aq\e0\(aq) followed by data
490 written on the slave side of the pseudoterminal.
491 If the first byte is not
492 .B TIOCPKT_DATA
493 (0), it is an OR of one
494 or more of the following bits:
495 .IP
496 .ad l
497 .TS
498 lb l.
499 TIOCPKT_FLUSHREAD T{
500 The read queue for the terminal is flushed.
501 T}
502 TIOCPKT_FLUSHWRITE T{
503 The write queue for the terminal is flushed.
504 T}
505 TIOCPKT_STOP T{
506 Output to the terminal is stopped.
507 T}
508 TIOCPKT_START T{
509 Output to the terminal is restarted.
510 T}
511 TIOCPKT_DOSTOP T{
512 The start and stop characters are \fB\(haS\fP/\fB\(haQ\fP.
513 T}
514 TIOCPKT_NOSTOP T{
515 The start and stop characters are not \fB\(haS\fP/\fB\(haQ\fP.
516 T}
517 .TE
518 .ad
519 .IP
520 While packet mode is in use, the presence
521 of control status information to be read
522 from the master side may be detected by a
523 .BR select (2)
524 for exceptional conditions or a
525 .BR poll (2)
526 for the
527 .B POLLPRI
528 event.
529 .IP
530 This mode is used by
531 .BR rlogin (1)
532 and
533 .BR rlogind (8)
534 to implement a remote-echoed,
535 locally \fB\(haS\fP/\fB\(haQ\fP flow-controlled remote login.
536 .TP
537 .B TIOCGPKT
538 Argument:
539 .BI "const int\~*" argp
540 .IP
541 (since Linux 3.8)
542 Return the current packet mode setting in the integer pointed to by
543 .IR argp .
544 .TP
545 .B TIOCSPTLCK
546 Argument:
547 .BI "int\~*" argp
548 .IP
549 Set (if
550 .I *argp
551 is nonzero) or remove (if
552 .I *argp
553 is zero) the lock on the pseudoterminal slave device.
554 (See also
555 .BR unlockpt (3).)
556 .TP
557 .B TIOCGPTLCK
558 Argument:
559 .BI "int\~*" argp
560 .IP
561 (since Linux 3.8)
562 Place the current lock state of the pseudoterminal slave device
563 in the location pointed to by
564 .IR argp .
565 .TP
566 .B TIOCGPTPEER
567 Argument:
568 .BI "int " flags
569 .IP
570 .\" commit 54ebbfb1603415d9953c150535850d30609ef077
571 (since Linux 4.13)
572 Given a file descriptor in
573 .I fd
574 that refers to a pseudoterminal master,
575 open (with the given
576 .BR open (2)-style
577 .IR flags )
578 and return a new file descriptor that refers to the peer
579 pseudoterminal slave device.
580 This operation can be performed
581 regardless of whether the pathname of the slave device
582 is accessible through the calling process's mount namespace.
583 .IP
584 Security-conscious programs interacting with namespaces may wish to use this
585 operation rather than
586 .BR open (2)
587 with the pathname returned by
588 .BR ptsname (3),
589 and similar library functions that have insecure APIs.
590 (For example, confusion can occur in some cases using
591 .BR ptsname (3)
592 with a pathname where a devpts filesystem
593 has been mounted in a different mount namespace.)
594 .PP
595 The BSD ioctls
596 .BR TIOCSTOP ,
597 .BR TIOCSTART ,
598 .BR TIOCUCNTL ,
599 and
600 .B TIOCREMOTE
601 have not been implemented under Linux.
602 .SS Modem control
603 .TP
604 .B TIOCMGET
605 Argument:
606 .BI "int\~*" argp
607 .IP
608 Get the status of modem bits.
609 .TP
610 .B TIOCMSET
611 Argument:
612 .BI "const int\~*" argp
613 .IP
614 Set the status of modem bits.
615 .TP
616 .B TIOCMBIC
617 Argument:
618 .BI "const int\~*" argp
619 .IP
620 Clear the indicated modem bits.
621 .TP
622 .B TIOCMBIS
623 Argument:
624 .BI "const int\~*" argp
625 .IP
626 Set the indicated modem bits.
627 .PP
628 The following bits are used by the above ioctls:
629 .PP
630 .TS
631 lb l.
632 TIOCM_LE DSR (data set ready/line enable)
633 TIOCM_DTR DTR (data terminal ready)
634 TIOCM_RTS RTS (request to send)
635 TIOCM_ST Secondary TXD (transmit)
636 TIOCM_SR Secondary RXD (receive)
637 TIOCM_CTS CTS (clear to send)
638 TIOCM_CAR DCD (data carrier detect)
639 TIOCM_CD see TIOCM_CAR
640 TIOCM_RNG RNG (ring)
641 TIOCM_RI see TIOCM_RNG
642 TIOCM_DSR DSR (data set ready)
643 .TE
644 .TP
645 .B TIOCMIWAIT
646 Argument:
647 .BI "int " arg
648 .IP
649 Wait for any of the 4 modem bits (DCD, RI, DSR, CTS) to change.
650 The bits of interest are specified as a bit mask in
651 .IR arg ,
652 by ORing together any of the bit values,
653 .BR TIOCM_RNG ,
654 .BR TIOCM_DSR ,
655 .BR TIOCM_CD ,
656 and
657 .BR TIOCM_CTS .
658 The caller should use
659 .B TIOCGICOUNT
660 to see which bit has changed.
661 .TP
662 .B TIOCGICOUNT
663 Argument:
664 .BI "struct serial_icounter_struct\~*" argp
665 .IP
666 Get counts of input serial line interrupts (DCD, RI, DSR, CTS).
667 The counts are written to the
668 .I serial_icounter_struct
669 structure pointed to by
670 .IR argp .
671 .IP
672 Note: both 1->0 and 0->1 transitions are counted, except for
673 RI, where only 0->1 transitions are counted.
674 .SS Marking a line as local
675 .TP
676 .B TIOCGSOFTCAR
677 Argument:
678 .BI "int\~*" argp
679 .IP
680 ("Get software carrier flag")
681 Get the status of the CLOCAL flag in the c_cflag field of the
682 .I termios
683 structure.
684 .TP
685 .B TIOCSSOFTCAR
686 Argument:
687 .BI "const int\~*" argp
688 .IP
689 ("Set software carrier flag")
690 Set the CLOCAL flag in the
691 .I termios
692 structure when
693 .RI * argp
694 is nonzero, and clear it otherwise.
695 .PP
696 If the
697 .B CLOCAL
698 flag for a line is off, the hardware carrier detect (DCD)
699 signal is significant, and an
700 .BR open (2)
701 of the corresponding terminal will block until DCD is asserted,
702 unless the
703 .B O_NONBLOCK
704 flag is given.
705 If
706 .B CLOCAL
707 is set, the line behaves as if DCD is always asserted.
708 The software carrier flag is usually turned on for local devices,
709 and is off for lines with modems.
710 .SS Linux-specific
711 For the
712 .B TIOCLINUX
713 ioctl, see
714 .BR ioctl_console (2).
715 .SS Kernel debugging
716 .B "#include <linux/tty.h>"
717 .TP
718 .B TIOCTTYGSTRUCT
719 Argument:
720 .BI "struct tty_struct\~*" argp
721 .IP
722 Get the
723 .I tty_struct
724 corresponding to
725 .IR fd .
726 This command was removed in Linux 2.5.67.
727 .\" commit b3506a09d15dc5aee6d4bb88d759b157016e1864
728 .\" Author: Andries E. Brouwer <andries.brouwer@cwi.nl>
729 .\" Date: Tue Apr 1 04:42:46 2003 -0800
730 .\"
731 .\" [PATCH] kill TIOCTTYGSTRUCT
732 .\"
733 .\" Only used for (dubious) debugging purposes, and exposes
734 .\" internal kernel state.
735 .\"
736 .\" .SS Serial info
737 .\" .BR "#include <linux/serial.h>"
738 .\" .PP
739 .\" .TP
740 .\" .BI "TIOCGSERIAL struct serial_struct *" argp
741 .\" Get serial info.
742 .\" .TP
743 .\" .BI "TIOCSSERIAL const struct serial_struct *" argp
744 .\" Set serial info.
745 .SH RETURN VALUE
746 The
747 .BR ioctl (2)
748 system call returns 0 on success.
749 On error, it returns \-1 and sets
750 .I errno
751 to indicate the error.
752 .SH ERRORS
753 .TP
754 .B EINVAL
755 Invalid command parameter.
756 .TP
757 .B ENOIOCTLCMD
758 Unknown command.
759 .TP
760 .B ENOTTY
761 Inappropriate
762 .IR fd .
763 .TP
764 .B EPERM
765 Insufficient permission.
766 .SH EXAMPLES
767 Check the condition of DTR on the serial port.
768 .PP
769 .\" SRC BEGIN (tiocmget.c)
770 .EX
771 #include <fcntl.h>
772 #include <stdio.h>
773 #include <sys/ioctl.h>
774 #include <unistd.h>
775
776 int
777 main(void)
778 {
779 int fd, serial;
780
781 fd = open("/dev/ttyS0", O_RDONLY);
782 ioctl(fd, TIOCMGET, &serial);
783 if (serial & TIOCM_DTR)
784 puts("TIOCM_DTR is set");
785 else
786 puts("TIOCM_DTR is not set");
787 close(fd);
788 }
789 .EE
790 .\" SRC END
791 .PP
792 Get or set arbitrary baudrate on the serial port.
793 .PP
794 .\" SRC BEGIN (tcgets.c)
795 .EX
796 /* SPDX-License-Identifier: GPL-2.0-or-later */
797
798 #include <asm/termbits.h>
799 #include <fcntl.h>
800 #include <stdio.h>
801 #include <stdlib.h>
802 #include <sys/ioctl.h>
803 #include <unistd.h>
804
805 int
806 main(int argc, char *argv[])
807 {
808 #if !defined BOTHER
809 fprintf(stderr, "BOTHER is unsupported\en");
810 /* Program may fallback to TCGETS/TCSETS with Bnnn constants */
811 exit(EXIT_FAILURE);
812 #else
813 /* Declare tio structure, its type depends on supported ioctl */
814 # if defined TCGETS2
815 struct termios2 tio;
816 # else
817 struct termios tio;
818 # endif
819 int fd, rc;
820
821 if (argc != 2 && argc != 3 && argc != 4) {
822 fprintf(stderr, "Usage: %s device [output [input] ]\en", argv[0]);
823 exit(EXIT_FAILURE);
824 }
825
826 fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY);
827 if (fd < 0) {
828 perror("open");
829 exit(EXIT_FAILURE);
830 }
831
832 /* Get the current serial port settings via supported ioctl */
833 # if defined TCGETS2
834 rc = ioctl(fd, TCGETS2, &tio);
835 # else
836 rc = ioctl(fd, TCGETS, &tio);
837 # endif
838 if (rc) {
839 perror("TCGETS");
840 close(fd);
841 exit(EXIT_FAILURE);
842 }
843
844 /* Change baud rate when more arguments were provided */
845 if (argc == 3 || argc == 4) {
846 /* Clear the current output baud rate and fill a new value */
847 tio.c_cflag &= \(tiCBAUD;
848 tio.c_cflag |= BOTHER;
849 tio.c_ospeed = atoi(argv[2]);
850
851 /* Clear the current input baud rate and fill a new value */
852 tio.c_cflag &= \(ti(CBAUD << IBSHIFT);
853 tio.c_cflag |= BOTHER << IBSHIFT;
854 /* When 4th argument is not provided reuse output baud rate */
855 tio.c_ispeed = (argc == 4) ? atoi(argv[3]) : atoi(argv[2]);
856
857 /* Set new serial port settings via supported ioctl */
858 # if defined TCSETS2
859 rc = ioctl(fd, TCSETS2, &tio);
860 # else
861 rc = ioctl(fd, TCSETS, &tio);
862 # endif
863 if (rc) {
864 perror("TCSETS");
865 close(fd);
866 exit(EXIT_FAILURE);
867 }
868
869 /* And get new values which were really configured */
870 # if defined TCGETS2
871 rc = ioctl(fd, TCGETS2, &tio);
872 # else
873 rc = ioctl(fd, TCGETS, &tio);
874 # endif
875 if (rc) {
876 perror("TCGETS");
877 close(fd);
878 exit(EXIT_FAILURE);
879 }
880 }
881
882 close(fd);
883
884 printf("output baud rate: %u\en", tio.c_ospeed);
885 printf("input baud rate: %u\en", tio.c_ispeed);
886
887 exit(EXIT_SUCCESS);
888 #endif
889 }
890 .EE
891 .\" SRC END
892 .SH SEE ALSO
893 .BR ldattach (8),
894 .BR ioctl (2),
895 .BR ioctl_console (2),
896 .BR termios (3),
897 .BR pty (7)
898 .\"
899 .\" FIONBIO const int *
900 .\" FIONCLEX void
901 .\" FIOCLEX void
902 .\" FIOASYNC const int *
903 .\" from serial.c:
904 .\" TIOCSERCONFIG void
905 .\" TIOCSERGWILD int *
906 .\" TIOCSERSWILD const int *
907 .\" TIOCSERGSTRUCT struct async_struct *
908 .\" TIOCSERGETMULTI struct serial_multiport_struct *
909 .\" TIOCSERSETMULTI const struct serial_multiport_struct *
910 .\" TIOCGSERIAL, TIOCSSERIAL (see above)