]> git.proxmox.com Git - rustc.git/blob - vendor/rustix/src/imp/linux_raw/termios/syscalls.rs
New upstream version 1.64.0+dfsg1
[rustc.git] / vendor / rustix / src / imp / linux_raw / termios / syscalls.rs
1 //! linux_raw syscalls supporting `rustix::termios`.
2 //!
3 //! # Safety
4 //!
5 //! See the `rustix::imp` module documentation for details.
6 #![allow(unsafe_code)]
7 #![allow(clippy::undocumented_unsafe_blocks)]
8
9 use super::super::conv::{by_ref, c_uint, ret};
10 use crate::fd::BorrowedFd;
11 use crate::io;
12 use crate::process::{Pid, RawNonZeroPid};
13 use crate::termios::{
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,
16 VMIN, VTIME,
17 };
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,
24 };
25
26 #[inline]
27 pub(crate) fn tcgetwinsize(fd: BorrowedFd<'_>) -> io::Result<Winsize> {
28 unsafe {
29 let mut result = MaybeUninit::<Winsize>::uninit();
30 ret(syscall!(__NR_ioctl, fd, c_uint(TIOCGWINSZ), &mut result))
31 .map(|()| result.assume_init())
32 }
33 }
34
35 #[inline]
36 pub(crate) fn tcgetattr(fd: BorrowedFd<'_>) -> io::Result<Termios> {
37 unsafe {
38 let mut result = MaybeUninit::<Termios>::uninit();
39 ret(syscall!(__NR_ioctl, fd, c_uint(TCGETS), &mut result)).map(|()| result.assume_init())
40 }
41 }
42
43 #[inline]
44 pub(crate) fn tcgetpgrp(fd: BorrowedFd<'_>) -> io::Result<Pid> {
45 unsafe {
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))
51 })
52 }
53 }
54
55 #[inline]
56 pub(crate) fn tcsetattr(
57 fd: BorrowedFd,
58 optional_actions: OptionalActions,
59 termios: &Termios,
60 ) -> io::Result<()> {
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
65 } else {
66 TCSETS + optional_actions as u32
67 };
68 unsafe {
69 ret(syscall_readonly!(
70 __NR_ioctl,
71 fd,
72 c_uint(request as u32),
73 by_ref(termios)
74 ))
75 }
76 }
77
78 #[inline]
79 pub(crate) fn tcsendbreak(fd: BorrowedFd) -> io::Result<()> {
80 unsafe { ret(syscall_readonly!(__NR_ioctl, fd, c_uint(TCSBRK), c_uint(0))) }
81 }
82
83 #[inline]
84 pub(crate) fn tcdrain(fd: BorrowedFd) -> io::Result<()> {
85 unsafe { ret(syscall_readonly!(__NR_ioctl, fd, c_uint(TCSBRK), c_uint(1))) }
86 }
87
88 #[inline]
89 pub(crate) fn tcflush(fd: BorrowedFd, queue_selector: QueueSelector) -> io::Result<()> {
90 unsafe {
91 ret(syscall_readonly!(
92 __NR_ioctl,
93 fd,
94 c_uint(TCFLSH),
95 c_uint(queue_selector as u32)
96 ))
97 }
98 }
99
100 #[inline]
101 pub(crate) fn tcflow(fd: BorrowedFd, action: Action) -> io::Result<()> {
102 unsafe {
103 ret(syscall_readonly!(
104 __NR_ioctl,
105 fd,
106 c_uint(TCXONC),
107 c_uint(action as u32)
108 ))
109 }
110 }
111
112 #[inline]
113 pub(crate) fn tcgetsid(fd: BorrowedFd) -> io::Result<Pid> {
114 unsafe {
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))
120 })
121 }
122 }
123
124 #[inline]
125 pub(crate) fn tcsetwinsize(fd: BorrowedFd, winsize: Winsize) -> io::Result<()> {
126 unsafe {
127 ret(syscall!(
128 __NR_ioctl,
129 fd,
130 c_uint(TIOCSWINSZ),
131 by_ref(&winsize)
132 ))
133 }
134 }
135
136 #[inline]
137 pub(crate) fn tcsetpgrp(fd: BorrowedFd<'_>, pid: Pid) -> io::Result<()> {
138 unsafe { ret(syscall!(__NR_ioctl, fd, c_uint(TIOCSPGRP), pid)) }
139 }
140
141 #[inline]
142 #[must_use]
143 #[allow(clippy::missing_const_for_fn)]
144 pub(crate) fn cfgetospeed(termios: &Termios) -> u32 {
145 termios.c_cflag & CBAUD
146 }
147
148 #[inline]
149 #[must_use]
150 #[allow(clippy::missing_const_for_fn)]
151 pub(crate) fn cfgetispeed(termios: &Termios) -> u32 {
152 termios.c_cflag & CBAUD
153 }
154
155 #[inline]
156 pub(crate) fn cfmakeraw(termios: &mut Termios) {
157 // From the Linux [`cfmakeraw` man page]:
158 //
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;
165
166 // Musl and glibc also do these:
167 termios.c_cc[VMIN] = 1;
168 termios.c_cc[VTIME] = 0;
169 }
170
171 #[inline]
172 pub(crate) fn cfsetospeed(termios: &mut Termios, speed: u32) -> io::Result<()> {
173 if (speed & !CBAUD) != 0 {
174 return Err(io::Errno::INVAL);
175 }
176 termios.c_cflag &= !CBAUD;
177 termios.c_cflag |= speed;
178 Ok(())
179 }
180
181 #[inline]
182 pub(crate) fn cfsetispeed(termios: &mut Termios, speed: u32) -> io::Result<()> {
183 if speed == 0 {
184 return Ok(());
185 }
186 if (speed & !CBAUD) != 0 {
187 return Err(io::Errno::INVAL);
188 }
189 termios.c_cflag &= !CBAUD;
190 termios.c_cflag |= speed;
191 Ok(())
192 }
193
194 #[inline]
195 pub(crate) fn cfsetspeed(termios: &mut Termios, speed: u32) -> io::Result<()> {
196 if (speed & !CBAUD) != 0 {
197 return Err(io::Errno::INVAL);
198 }
199 termios.c_cflag &= !CBAUD;
200 termios.c_cflag |= speed;
201 Ok(())
202 }
203
204 #[inline]
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()
211 }
212
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)?;
216
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);
220 }
221
222 // Check that `fd` is really a tty.
223 tcgetwinsize(fd)?;
224
225 // Get a fd to '/proc/self/fd'.
226 let proc_self_fd = io::proc_self_fd()?;
227
228 // Gather the ttyname by reading the 'fd' file inside 'proc_self_fd'.
229 let r =
230 super::super::fs::syscalls::readlinkat(proc_self_fd, DecInt::from_fd(&fd).as_c_str(), buf)?;
231
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.
235 if r == buf.len() {
236 return Err(io::Errno::RANGE);
237 }
238 buf[r] = b'\0';
239
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();
242
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);
246 }
247
248 Ok(r)
249 }