1 //! linux_raw syscalls supporting `rustix::termios`.
5 //! See the `rustix::backend` 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 Ok(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
))?
;
40 Ok(result
.assume_init())
47 target_arch
= "x86_64",
49 target_arch
= "riscv64",
50 target_arch
= "aarch64",
53 target_arch
= "mips64",
55 pub(crate) fn tcgetattr2(fd
: BorrowedFd
<'_
>) -> io
::Result
<crate::termios
::Termios2
> {
57 let mut result
= MaybeUninit
::<crate::termios
::Termios2
>::uninit();
61 c_uint(linux_raw_sys
::ioctl
::TCGETS2
),
64 Ok(result
.assume_init())
69 pub(crate) fn tcgetpgrp(fd
: BorrowedFd
<'_
>) -> io
::Result
<Pid
> {
71 let mut result
= MaybeUninit
::<__kernel_pid_t
>::uninit();
72 ret(syscall
!(__NR_ioctl
, fd
, c_uint(TIOCGPGRP
), &mut result
))?
;
73 let pid
= result
.assume_init();
74 debug_assert
!(pid
> 0);
75 Ok(Pid
::from_raw_nonzero(RawNonZeroPid
::new_unchecked(
82 pub(crate) fn tcsetattr(
84 optional_actions
: OptionalActions
,
87 // Translate from `optional_actions` into an ioctl request code. On MIPS,
88 // `optional_actions` already has `TCGETS` added to it.
89 let request
= if cfg
!(any(target_arch
= "mips", target_arch
= "mips64")) {
90 optional_actions
as u32
92 TCSETS
+ optional_actions
as u32
95 ret(syscall_readonly
!(
98 c_uint(request
as u32),
107 target_arch
= "x86_64",
109 target_arch
= "riscv64",
110 target_arch
= "aarch64",
112 target_arch
= "mips",
113 target_arch
= "mips64",
115 pub(crate) fn tcsetattr2(
117 optional_actions
: OptionalActions
,
118 termios
: &crate::termios
::Termios2
,
119 ) -> io
::Result
<()> {
121 ret(syscall_readonly
!(
124 c_uint(linux_raw_sys
::ioctl
::TCSETS2
+ optional_actions
as u32),
131 pub(crate) fn tcsendbreak(fd
: BorrowedFd
) -> io
::Result
<()> {
132 unsafe { ret(syscall_readonly!(__NR_ioctl, fd, c_uint(TCSBRK), c_uint(0))) }
136 pub(crate) fn tcdrain(fd
: BorrowedFd
) -> io
::Result
<()> {
137 unsafe { ret(syscall_readonly!(__NR_ioctl, fd, c_uint(TCSBRK), c_uint(1))) }
141 pub(crate) fn tcflush(fd
: BorrowedFd
, queue_selector
: QueueSelector
) -> io
::Result
<()> {
143 ret(syscall_readonly
!(
147 c_uint(queue_selector
as u32)
153 pub(crate) fn tcflow(fd
: BorrowedFd
, action
: Action
) -> io
::Result
<()> {
155 ret(syscall_readonly
!(
159 c_uint(action
as u32)
165 pub(crate) fn tcgetsid(fd
: BorrowedFd
) -> io
::Result
<Pid
> {
167 let mut result
= MaybeUninit
::<__kernel_pid_t
>::uninit();
168 ret(syscall
!(__NR_ioctl
, fd
, c_uint(TIOCGSID
), &mut result
))?
;
169 let pid
= result
.assume_init();
170 debug_assert
!(pid
> 0);
171 Ok(Pid
::from_raw_nonzero(RawNonZeroPid
::new_unchecked(
178 pub(crate) fn tcsetwinsize(fd
: BorrowedFd
, winsize
: Winsize
) -> io
::Result
<()> {
190 pub(crate) fn tcsetpgrp(fd
: BorrowedFd
<'_
>, pid
: Pid
) -> io
::Result
<()> {
191 unsafe { ret(syscall!(__NR_ioctl, fd, c_uint(TIOCSPGRP), pid)) }
196 #[allow(clippy::missing_const_for_fn)]
197 pub(crate) fn cfgetospeed(termios
: &Termios
) -> u32 {
198 termios
.c_cflag
& CBAUD
203 #[allow(clippy::missing_const_for_fn)]
204 pub(crate) fn cfgetispeed(termios
: &Termios
) -> u32 {
205 termios
.c_cflag
& CBAUD
209 pub(crate) fn cfmakeraw(termios
: &mut Termios
) {
210 // From the Linux [`cfmakeraw` man page]:
212 // [`cfmakeraw` man page]: https://man7.org/linux/man-pages/man3/cfmakeraw.3.html
213 termios
.c_iflag
&= !(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
| IGNCR
| ICRNL
| IXON
);
214 termios
.c_oflag
&= !OPOST
;
215 termios
.c_lflag
&= !(ECHO
| ECHONL
| ICANON
| ISIG
| IEXTEN
);
216 termios
.c_cflag
&= !(CSIZE
| PARENB
);
217 termios
.c_cflag
|= CS8
;
219 // Musl and glibc also do these:
220 termios
.c_cc
[VMIN
] = 1;
221 termios
.c_cc
[VTIME
] = 0;
225 pub(crate) fn cfsetospeed(termios
: &mut Termios
, speed
: u32) -> io
::Result
<()> {
226 if (speed
& !CBAUD
) != 0 {
227 return Err(io
::Errno
::INVAL
);
229 termios
.c_cflag
&= !CBAUD
;
230 termios
.c_cflag
|= speed
;
235 pub(crate) fn cfsetispeed(termios
: &mut Termios
, speed
: u32) -> io
::Result
<()> {
239 if (speed
& !CBAUD
) != 0 {
240 return Err(io
::Errno
::INVAL
);
242 termios
.c_cflag
&= !CBAUD
;
243 termios
.c_cflag
|= speed
;
248 pub(crate) fn cfsetspeed(termios
: &mut Termios
, speed
: u32) -> io
::Result
<()> {
249 if (speed
& !CBAUD
) != 0 {
250 return Err(io
::Errno
::INVAL
);
252 termios
.c_cflag
&= !CBAUD
;
253 termios
.c_cflag
|= speed
;
258 pub(crate) fn isatty(fd
: BorrowedFd
<'_
>) -> bool
{
259 // On error, Linux will return either `EINVAL` (2.6.32) or `ENOTTY`
260 // (otherwise), because we assume we're never passing an invalid
261 // file descriptor (which would get `EBADF`). Either way, an error
262 // means we don't have a tty.
263 tcgetwinsize(fd
).is_ok()
266 #[cfg(feature = "procfs")]
267 pub(crate) fn ttyname(fd
: BorrowedFd
<'_
>, buf
: &mut [u8]) -> io
::Result
<usize> {
268 let fd_stat
= super::super::fs
::syscalls
::fstat(fd
)?
;
270 // Quick check: if `fd` isn't a character device, it's not a tty.
271 if FileType
::from_raw_mode(fd_stat
.st_mode
) != FileType
::CharacterDevice
{
272 return Err(crate::io
::Errno
::NOTTY
);
275 // Check that `fd` is really a tty.
278 // Get a fd to '/proc/self/fd'.
279 let proc_self_fd
= io
::proc_self_fd()?
;
281 // Gather the ttyname by reading the 'fd' file inside 'proc_self_fd'.
283 super::super::fs
::syscalls
::readlinkat(proc_self_fd
, DecInt
::from_fd(fd
).as_c_str(), buf
)?
;
285 // If the number of bytes is equal to the buffer length, truncation may
286 // have occurred. This check also ensures that we have enough space for
287 // adding a NUL terminator.
289 return Err(io
::Errno
::RANGE
);
293 // Check that the path we read refers to the same file as `fd`.
294 let path
= CStr
::from_bytes_with_nul(&buf
[..=r
]).unwrap();
296 let path_stat
= super::super::fs
::syscalls
::stat(path
)?
;
297 if path_stat
.st_dev
!= fd_stat
.st_dev
|| path_stat
.st_ino
!= fd_stat
.st_ino
{
298 return Err(crate::io
::Errno
::NODEV
);