1 //! linux_raw syscalls supporting `rustix::termios`.
5 //! See the `rustix::imp` module documentation for details.
7 #![allow(clippy::undocumented_unsafe_blocks)]
9 use super::super::conv
::{by_ref, c_uint, ret}
;
10 use crate::fd
::BorrowedFd
;
12 use crate::process
::{Pid, RawNonZeroPid}
;
14 Action
, OptionalActions
, QueueSelector
, Termios
, Winsize
, BRKINT
, CBAUD
, CS8
, CSIZE
, ECHO
,
15 ECHONL
, ICANON
, ICRNL
, IEXTEN
, IGNBRK
, IGNCR
, INLCR
, ISIG
, ISTRIP
, IXON
, OPOST
, PARENB
, PARMRK
,
18 #[cfg(feature = "procfs")]
19 use crate::{ffi::CStr, fs::FileType, path::DecInt}
;
20 use core
::mem
::MaybeUninit
;
21 use linux_raw_sys
::general
::__kernel_pid_t
;
22 use linux_raw_sys
::ioctl
::{
23 TCFLSH
, TCGETS
, TCSBRK
, TCSETS
, TCXONC
, TIOCGPGRP
, TIOCGSID
, TIOCGWINSZ
, TIOCSPGRP
, TIOCSWINSZ
,
27 pub(crate) fn tcgetwinsize(fd
: BorrowedFd
<'_
>) -> io
::Result
<Winsize
> {
29 let mut result
= MaybeUninit
::<Winsize
>::uninit();
30 ret(syscall
!(__NR_ioctl
, fd
, c_uint(TIOCGWINSZ
), &mut result
))
31 .map(|()| result
.assume_init())
36 pub(crate) fn tcgetattr(fd
: BorrowedFd
<'_
>) -> io
::Result
<Termios
> {
38 let mut result
= MaybeUninit
::<Termios
>::uninit();
39 ret(syscall
!(__NR_ioctl
, fd
, c_uint(TCGETS
), &mut result
)).map(|()| result
.assume_init())
44 pub(crate) fn tcgetpgrp(fd
: BorrowedFd
<'_
>) -> io
::Result
<Pid
> {
46 let mut result
= MaybeUninit
::<__kernel_pid_t
>::uninit();
47 ret(syscall
!(__NR_ioctl
, fd
, c_uint(TIOCGPGRP
), &mut result
)).map(|()| {
48 let pid
= result
.assume_init();
49 debug_assert
!(pid
> 0);
50 Pid
::from_raw_nonzero(RawNonZeroPid
::new_unchecked(pid
as u32))
56 pub(crate) fn tcsetattr(
58 optional_actions
: OptionalActions
,
61 // Translate from `optional_actions` into an ioctl request code. On MIPS,
62 // `optional_actions` already has `TCGETS` added to it.
63 let request
= if cfg
!(any(target_arch
= "mips", target_arch
= "mips64")) {
64 optional_actions
as u32
66 TCSETS
+ optional_actions
as u32
69 ret(syscall_readonly
!(
72 c_uint(request
as u32),
79 pub(crate) fn tcsendbreak(fd
: BorrowedFd
) -> io
::Result
<()> {
80 unsafe { ret(syscall_readonly!(__NR_ioctl, fd, c_uint(TCSBRK), c_uint(0))) }
84 pub(crate) fn tcdrain(fd
: BorrowedFd
) -> io
::Result
<()> {
85 unsafe { ret(syscall_readonly!(__NR_ioctl, fd, c_uint(TCSBRK), c_uint(1))) }
89 pub(crate) fn tcflush(fd
: BorrowedFd
, queue_selector
: QueueSelector
) -> io
::Result
<()> {
91 ret(syscall_readonly
!(
95 c_uint(queue_selector
as u32)
101 pub(crate) fn tcflow(fd
: BorrowedFd
, action
: Action
) -> io
::Result
<()> {
103 ret(syscall_readonly
!(
107 c_uint(action
as u32)
113 pub(crate) fn tcgetsid(fd
: BorrowedFd
) -> io
::Result
<Pid
> {
115 let mut result
= MaybeUninit
::<__kernel_pid_t
>::uninit();
116 ret(syscall
!(__NR_ioctl
, fd
, c_uint(TIOCGSID
), &mut result
)).map(|()| {
117 let pid
= result
.assume_init();
118 debug_assert
!(pid
> 0);
119 Pid
::from_raw_nonzero(RawNonZeroPid
::new_unchecked(pid
as u32))
125 pub(crate) fn tcsetwinsize(fd
: BorrowedFd
, winsize
: Winsize
) -> io
::Result
<()> {
137 pub(crate) fn tcsetpgrp(fd
: BorrowedFd
<'_
>, pid
: Pid
) -> io
::Result
<()> {
138 unsafe { ret(syscall!(__NR_ioctl, fd, c_uint(TIOCSPGRP), pid)) }
143 #[allow(clippy::missing_const_for_fn)]
144 pub(crate) fn cfgetospeed(termios
: &Termios
) -> u32 {
145 termios
.c_cflag
& CBAUD
150 #[allow(clippy::missing_const_for_fn)]
151 pub(crate) fn cfgetispeed(termios
: &Termios
) -> u32 {
152 termios
.c_cflag
& CBAUD
156 pub(crate) fn cfmakeraw(termios
: &mut Termios
) {
157 // From the Linux [`cfmakeraw` man page]:
159 // [`cfmakeraw` man page]: https://man7.org/linux/man-pages/man3/cfmakeraw.3.html
160 termios
.c_iflag
&= !(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
| IGNCR
| ICRNL
| IXON
);
161 termios
.c_oflag
&= !OPOST
;
162 termios
.c_lflag
&= !(ECHO
| ECHONL
| ICANON
| ISIG
| IEXTEN
);
163 termios
.c_cflag
&= !(CSIZE
| PARENB
);
164 termios
.c_cflag
|= CS8
;
166 // Musl and glibc also do these:
167 termios
.c_cc
[VMIN
] = 1;
168 termios
.c_cc
[VTIME
] = 0;
172 pub(crate) fn cfsetospeed(termios
: &mut Termios
, speed
: u32) -> io
::Result
<()> {
173 if (speed
& !CBAUD
) != 0 {
174 return Err(io
::Errno
::INVAL
);
176 termios
.c_cflag
&= !CBAUD
;
177 termios
.c_cflag
|= speed
;
182 pub(crate) fn cfsetispeed(termios
: &mut Termios
, speed
: u32) -> io
::Result
<()> {
186 if (speed
& !CBAUD
) != 0 {
187 return Err(io
::Errno
::INVAL
);
189 termios
.c_cflag
&= !CBAUD
;
190 termios
.c_cflag
|= speed
;
195 pub(crate) fn cfsetspeed(termios
: &mut Termios
, speed
: u32) -> io
::Result
<()> {
196 if (speed
& !CBAUD
) != 0 {
197 return Err(io
::Errno
::INVAL
);
199 termios
.c_cflag
&= !CBAUD
;
200 termios
.c_cflag
|= speed
;
205 pub(crate) fn isatty(fd
: BorrowedFd
<'_
>) -> bool
{
206 // On error, Linux will return either `EINVAL` (2.6.32) or `ENOTTY`
207 // (otherwise), because we assume we're never passing an invalid
208 // file descriptor (which would get `EBADF`). Either way, an error
209 // means we don't have a tty.
210 tcgetwinsize(fd
).is_ok()
213 #[cfg(feature = "procfs")]
214 pub(crate) fn ttyname(fd
: BorrowedFd
<'_
>, buf
: &mut [u8]) -> io
::Result
<usize> {
215 let fd_stat
= super::super::fs
::syscalls
::fstat(fd
)?
;
217 // Quick check: if `fd` isn't a character device, it's not a tty.
218 if FileType
::from_raw_mode(fd_stat
.st_mode
) != FileType
::CharacterDevice
{
219 return Err(crate::io
::Errno
::NOTTY
);
222 // Check that `fd` is really a tty.
225 // Get a fd to '/proc/self/fd'.
226 let proc_self_fd
= io
::proc_self_fd()?
;
228 // Gather the ttyname by reading the 'fd' file inside 'proc_self_fd'.
230 super::super::fs
::syscalls
::readlinkat(proc_self_fd
, DecInt
::from_fd(&fd
).as_c_str(), buf
)?
;
232 // If the number of bytes is equal to the buffer length, truncation may
233 // have occurred. This check also ensures that we have enough space for
234 // adding a NUL terminator.
236 return Err(io
::Errno
::RANGE
);
240 // Check that the path we read refers to the same file as `fd`.
241 let path
= CStr
::from_bytes_with_nul(&buf
[..=r
]).unwrap();
243 let path_stat
= super::super::fs
::syscalls
::stat(path
)?
;
244 if path_stat
.st_dev
!= fd_stat
.st_dev
|| path_stat
.st_ino
!= fd_stat
.st_ino
{
245 return Err(crate::io
::Errno
::NODEV
);