-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a `rustix::pty` module, providing functions that wrap `posix_openpt`, `grantpt`, `unlockpt`, `ptsname`, and so on.
- Loading branch information
1 parent
94edb27
commit c2699e3
Showing
17 changed files
with
482 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) mod syscalls; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
//! libc syscalls supporting `rustix::pty`. | ||
|
||
use super::super::c; | ||
use super::super::conv::{borrowed_fd, ret}; | ||
use crate::fd::BorrowedFd; | ||
use crate::io; | ||
#[cfg(not(target_os = "android"))] | ||
use {super::super::conv::ret_owned_fd, crate::fd::OwnedFd, crate::pty::OpenptFlags}; | ||
#[cfg(any(apple, linux_like, target_os = "freebsd", target_os = "fuchsia"))] | ||
use { | ||
crate::ffi::{CStr, CString}, | ||
crate::path::SMALL_PATH_BUFFER_SIZE, | ||
alloc::borrow::ToOwned, | ||
alloc::vec::Vec, | ||
}; | ||
|
||
#[cfg(not(any(target_os = "android", target_os = "linux")))] | ||
#[inline] | ||
pub(crate) fn openpt(flags: OpenptFlags) -> io::Result<OwnedFd> { | ||
unsafe { ret_owned_fd(c::posix_openpt(flags.bits() as _)) } | ||
} | ||
|
||
#[cfg(any(apple, linux_like, target_os = "freebsd", target_os = "fuchsia"))] | ||
#[inline] | ||
pub(crate) fn ptsname(fd: BorrowedFd, mut buffer: Vec<u8>) -> io::Result<CString> { | ||
// This code would benefit from having a better way to read into | ||
// uninitialized memory, but that requires `unsafe`. | ||
buffer.clear(); | ||
buffer.reserve(SMALL_PATH_BUFFER_SIZE); | ||
buffer.resize(buffer.capacity(), 0_u8); | ||
|
||
loop { | ||
// On platforms with `ptsname_r`, use it. | ||
#[cfg(any(target_os = "freebsd", linux_like, target_os = "fuchsia"))] | ||
let r = | ||
unsafe { libc::ptsname_r(borrowed_fd(fd), buffer.as_mut_ptr().cast(), buffer.len()) }; | ||
|
||
// MacOS 10.13.4 has `ptsname_r`; use it if we have it, otherwise fall | ||
// back to calling the underlying ioctl directly. | ||
#[cfg(apple)] | ||
let r = unsafe { | ||
weak! { fn ptsname_r(c::c_int, *mut c::c_char, c::size_t) -> c::c_int } | ||
|
||
if let Some(libc_ptsname_r) = ptsname_r.get() { | ||
libc_ptsname_r(borrowed_fd(fd), buffer.as_mut_ptr().cast(), buffer.len()) | ||
} else { | ||
// The size declared in the `TIOCPTYGNAME` macro in sys/ttycom.h is 128. | ||
let mut name: [u8; 128] = [0_u8; 128]; | ||
match libc::ioctl(borrowed_fd(fd), libc::TIOCPTYGNAME as u64, &mut name) { | ||
0 => { | ||
let len = CStr::from_ptr(name.as_ptr().cast()).to_bytes().len(); | ||
std::ptr::copy_nonoverlapping(name.as_ptr(), buffer.as_mut_ptr(), len + 1); | ||
0 | ||
} | ||
_ => libc_errno::errno().0, | ||
} | ||
} | ||
}; | ||
|
||
if r == 0 { | ||
return Ok(unsafe { CStr::from_ptr(buffer.as_ptr().cast()).to_owned() }); | ||
} | ||
if r != libc::ERANGE { | ||
return Err(io::Errno::from_raw_os_error(r)); | ||
} | ||
|
||
buffer.reserve(1); // use `Vec` reallocation strategy to grow capacity exponentially | ||
buffer.resize(buffer.capacity(), 0_u8); | ||
} | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn unlockpt(fd: BorrowedFd) -> io::Result<()> { | ||
unsafe { ret(c::unlockpt(borrowed_fd(fd))) } | ||
} | ||
|
||
#[cfg(not(any(target_os = "android", target_os = "linux")))] | ||
#[inline] | ||
pub(crate) fn grantpt(fd: BorrowedFd) -> io::Result<()> { | ||
unsafe { ret(c::grantpt(borrowed_fd(fd))) } | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
#[inline] | ||
pub(crate) fn ioctl_tiocgptpeer(fd: BorrowedFd, flags: OpenptFlags) -> io::Result<OwnedFd> { | ||
unsafe { ret_owned_fd(c::ioctl(borrowed_fd(fd), c::TIOCGPTPEER, flags.bits())) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) mod syscalls; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//! linux_raw syscalls supporting `rustix::pty`. | ||
//! | ||
//! # Safety | ||
//! | ||
//! See the `rustix::backend` module documentation for details. | ||
#![allow(unsafe_code)] | ||
#![allow(clippy::undocumented_unsafe_blocks)] | ||
|
||
use super::super::c; | ||
use super::super::conv::{by_ref, c_uint, ret, ret_owned_fd}; | ||
use crate::fd::{BorrowedFd, OwnedFd}; | ||
use crate::ffi::CString; | ||
use crate::io; | ||
use crate::path::DecInt; | ||
use crate::pty::OpenptFlags; | ||
#[cfg(any(apple, freebsdlike, linux_like, target_os = "fuchsia"))] | ||
use alloc::vec::Vec; | ||
use core::mem::MaybeUninit; | ||
use linux_raw_sys::ioctl::{TIOCGPTN, TIOCGPTPEER, TIOCSPTLCK}; | ||
|
||
#[cfg(any(apple, freebsdlike, linux_like, target_os = "fuchsia"))] | ||
#[inline] | ||
pub(crate) fn ptsname(fd: BorrowedFd, mut buffer: Vec<u8>) -> io::Result<CString> { | ||
unsafe { | ||
let mut n = MaybeUninit::<c::c_int>::uninit(); | ||
ret(syscall!(__NR_ioctl, fd, c_uint(TIOCGPTN), &mut n))?; | ||
|
||
buffer.clear(); | ||
buffer.extend_from_slice(b"/dev/pts/"); | ||
buffer.extend_from_slice(DecInt::new(n.assume_init()).as_bytes()); | ||
// With Rust 1.58 we can append a '\0' ourselves and use | ||
// `from_vec_with_nul_unchecked`. | ||
Ok(CString::from_vec_unchecked(buffer)) | ||
} | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn unlockpt(fd: BorrowedFd) -> io::Result<()> { | ||
unsafe { | ||
ret(syscall_readonly!( | ||
__NR_ioctl, | ||
fd, | ||
c_uint(TIOCSPTLCK), | ||
by_ref(&0) | ||
)) | ||
} | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
#[inline] | ||
pub(crate) fn ioctl_tiocgptpeer(fd: BorrowedFd, flags: OpenptFlags) -> io::Result<OwnedFd> { | ||
unsafe { | ||
ret_owned_fd(syscall_readonly!( | ||
__NR_ioctl, | ||
fd, | ||
c_uint(TIOCGPTPEER), | ||
c_uint(flags.bits()) | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use crate::{backend, io}; | ||
use backend::fd::AsFd; | ||
|
||
/// `ioctl(fd, TIOCSCTTY, 0)`—Sets the controlling terminal for the processs. | ||
/// | ||
/// # References | ||
/// - [Linux] | ||
/// - [FreeBSD] | ||
/// - [NetBSD] | ||
/// - [OpenBSD] | ||
/// | ||
/// [Linux]: https://man7.org/linux/man-pages/man4/tty_ioctl.4.html | ||
/// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=tty&sektion=4 | ||
/// [NetBSD]: https://man.netbsd.org/tty.4 | ||
/// [OpenBSD]: https://man.openbsd.org/tty.4 | ||
#[cfg(not(any(windows, target_os = "haiku", target_os = "redox", target_os = "wasi")))] | ||
#[inline] | ||
#[doc(alias = "TIOCSCTTY")] | ||
pub fn ioctl_tiocsctty<Fd: AsFd>(fd: Fd) -> io::Result<()> { | ||
backend::process::syscalls::ioctl_tiocsctty(fd.as_fd()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.