Debian Codegraph @

third_party/rust/rustix/src/pty.rs

raw
1 //! Pseudoterminal operations.
2 //!
3 //! For the `openpty` and `login_tty` functions, see the
4 //! [rustix-openpty crate].
5 //!
6 //! [rustix-openpty crate]: https://crates.io/crates/rustix-openpty
7
8 #![allow(unsafe_code)]
9
10 use crate::backend::c;
11 use crate::fd::{AsFd, OwnedFd};
12 use crate::fs::OFlags;
13 use crate::{backend, io};
14 #[cfg(all(
15 feature = "alloc",
16 any(
17 apple,
18 linux_like,
19 target_os = "freebsd",
20 target_os = "fuchsia",
21 target_os = "illumos"
22 )
23 ))]
24 use {crate::ffi::CString, alloc::vec::Vec};
25
26 #[cfg(target_os = "linux")]
27 use crate::{fd::FromRawFd, ioctl};
28
29 bitflags::bitflags! {
30 /// `O_*` flags for use with [`openpt`] and [`ioctl_tiocgptpeer`].
31 ///
32 /// [`ioctl_tiocgptpeer`]: https://docs.rs/rustix/*/x86_64-unknown-linux-gnu/rustix/pty/fn.ioctl_tiocgptpeer.html
33 #[repr(transparent)]
34 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
35 pub struct OpenptFlags: u32 {
36 /// `O_RDWR`
37 const RDWR = c::O_RDWR as c::c_uint;
38
39 /// `O_NOCTTY`
40 #[cfg(not(any(target_os = "espidf", target_os = "l4re", target_os = "redox", target_os = "vita")))]
41 const NOCTTY = c::O_NOCTTY as c::c_uint;
42
43 /// `O_CLOEXEC`
44 ///
45 /// The standard `posix_openpt` function doesn't support `CLOEXEC`, but
46 /// rustix supports it on Linux, and FreeBSD and NetBSD support it.
47 #[cfg(any(linux_kernel, target_os = "freebsd", target_os = "netbsd"))]
48 const CLOEXEC = c::O_CLOEXEC as c::c_uint;
49
50 /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
51 const _ = !0;
52 }
53 }
54
55 impl From<OpenptFlags> for OFlags {
56 #[inline]
57 fn from(flags: OpenptFlags) -> Self {
58 // `OpenptFlags` is a subset of `OFlags`.
59 Self::from_bits_retain(flags.bits() as _)
60 }
61 }
62
63 /// `posix_openpt(flags)`—Open a pseudoterminal device.
64 ///
65 /// On Linux, an additional `CLOEXEC` flag value may be passed to request the
66 /// close-on-exec flag be set.
67 ///
68 /// On Linux, if the system has no free pseudoterminals available, the
69 /// underlying system call fails with [`io::Errno::NOSPC`], however this rustix
70 /// function translates that to [`io::Errno::AGAIN`], so that the linux_raw and
71 /// libc backends have the same behavior.
72 ///
73 /// # References
74 /// - [POSIX]
75 /// - [Linux]
76 /// - [Apple]
77 /// - [FreeBSD]
78 /// - [DragonFly BSD]
79 /// - [NetBSD]
80 /// - [OpenBSD]
81 /// - [illumos]
82 ///
83 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/posix_openpt.html
84 /// [Linux]: https://man7.org/linux/man-pages/man3/posix_openpt.3.html
85 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/posix_openpt.3.html
86 /// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=posix_openpt&sektion=2
87 /// [DragonFly BSD]: https://man.dragonflybsd.org/?command=posix_openpt&section=3
88 /// [NetBSD]: https://man.netbsd.org/posix_openpt.3
89 /// [OpenBSD]: https://man.openbsd.org/posix_openpt
90 /// [illumos]: https://illumos.org/man/3C/posix_openpt
91 #[inline]
92 #[doc(alias = "posix_openpt")]
93 pub fn openpt(flags: OpenptFlags) -> io::Result<OwnedFd> {
94 // On Linux, open the device ourselves so that we can support `CLOEXEC`.
95 #[cfg(linux_kernel)]
96 {
97 use crate::fs::{open, Mode};
98 match open(cstr!("/dev/ptmx"), flags.into(), Mode::empty()) {
99 // Match libc `openat` behavior with `ENOSPC`.
100 Err(io::Errno::NOSPC) => Err(io::Errno::AGAIN),
101 otherwise => otherwise,
102 }
103 }
104
105 // On all other platforms, use `openpt`.
106 #[cfg(not(linux_kernel))]
107 {
108 backend::pty::syscalls::openpt(flags)
109 }
110 }
111
112 /// `ptsname(fd)`—Return the name of a pseudoterminal.
113 ///
114 /// # References
115 /// - [POSIX]
116 /// - [Linux]
117 /// - [glibc]
118 ///
119 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/ptsname.html
120 /// [Linux]: https://man7.org/linux/man-pages/man3/ptsname.3.html
121 /// [glibc]: https://sourceware.org/glibc/manual/latest/html_node/Allocation.html#index-ptsname
122 #[cfg(all(
123 feature = "alloc",
124 any(
125 apple,
126 linux_like,
127 target_os = "freebsd",
128 target_os = "fuchsia",
129 target_os = "illumos"
130 )
131 ))]
132 #[inline]
133 #[doc(alias = "ptsname_r")]
134 pub fn ptsname<Fd: AsFd, B: Into<Vec<u8>>>(fd: Fd, reuse: B) -> io::Result<CString> {
135 backend::pty::syscalls::ptsname(fd.as_fd(), reuse.into())
136 }
137
138 /// `unlockpt(fd)`—Unlock a pseudoterminal.
139 ///
140 /// # References
141 /// - [POSIX]
142 /// - [Linux]
143 /// - [glibc]
144 ///
145 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/unlockpt.html
146 /// [Linux]: https://man7.org/linux/man-pages/man3/unlockpt.3.html
147 /// [glibc]: https://sourceware.org/glibc/manual/latest/html_node/Allocation.html#index-unlockpt
148 #[inline]
149 pub fn unlockpt<Fd: AsFd>(fd: Fd) -> io::Result<()> {
150 backend::pty::syscalls::unlockpt(fd.as_fd())
151 }
152
153 /// `grantpt(fd)`—Grant access to the user side of a pseudoterminal.
154 ///
155 /// On Linux, calling this function has no effect, as the kernel is expected to
156 /// grant the appropriate access. On all other platforms, this function has
157 /// unspecified behavior if the calling process has a [`Signal::Child`] signal
158 /// handler installed.
159 ///
160 /// # References
161 /// - [POSIX]
162 /// - [Linux]
163 /// - [glibc]
164 ///
165 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/grantpt.html
166 /// [Linux]: https://man7.org/linux/man-pages/man3/grantpt.3.html
167 /// [glibc]: https://sourceware.org/glibc/manual/latest/html_node/Allocation.html#index-grantpt
168 /// [`Signal::Child`]: crate::process::Signal::Child
169 #[inline]
170 pub fn grantpt<Fd: AsFd>(fd: Fd) -> io::Result<()> {
171 #[cfg(not(linux_kernel))]
172 {
173 backend::pty::syscalls::grantpt(fd.as_fd())
174 }
175
176 // On Linux, we assume the kernel has already granted the needed
177 // permissions to the user side of the pseudoterminal.
178 #[cfg(linux_kernel)]
179 {
180 let _ = fd;
181 Ok(())
182 }
183 }
184
185 /// `ioctl(fd, TIOCGPTPEER)`—Open the user side of a pseudoterminal.
186 ///
187 /// This function is currently only implemented on Linux.
188 ///
189 /// # References
190 /// - [Linux]
191 ///
192 /// [Linux]: https://man7.org/linux/man-pages/man2/ioctl_tty.2.html
193 #[cfg(target_os = "linux")]
194 #[inline]
195 pub fn ioctl_tiocgptpeer<Fd: AsFd>(fd: Fd, flags: OpenptFlags) -> io::Result<OwnedFd> {
196 unsafe { ioctl::ioctl(fd, Tiocgptpeer(flags)) }
197 }
198
199 #[cfg(target_os = "linux")]
200 struct Tiocgptpeer(OpenptFlags);
201
202 #[cfg(target_os = "linux")]
203 unsafe impl ioctl::Ioctl for Tiocgptpeer {
204 type Output = OwnedFd;
205
206 const IS_MUTATING: bool = false;
207 const OPCODE: ioctl::Opcode = ioctl::Opcode::old(c::TIOCGPTPEER as ioctl::RawOpcode);
208
209 fn as_ptr(&mut self) -> *mut c::c_void {
210 self.0.bits() as *mut c::c_void
211 }
212
213 unsafe fn output_from_ptr(
214 ret: ioctl::IoctlOutput,
215 _arg: *mut c::c_void,
216 ) -> io::Result<Self::Output> {
217 Ok(OwnedFd::from_raw_fd(ret))
218 }
219 }