Skip to content

Commit

Permalink
Remove procfs. (#1275)
Browse files Browse the repository at this point in the history
Remove the procfs module, as most users don't need this functionality
and it can be implemented as a layer on top of rustix. This layer is now
available in the [rustix-linux-procfs] crate.

This requires changing `ttyname` to not use the `procfs` crate. It now
just uses a hard-coded "/proc/self/fd" path.

[rustix-linux-procfs]: https://crates.io/crates/rustix-linux-procfs
  • Loading branch information
sunfishcode authored Jan 23, 2025
1 parent 03ff108 commit f377e85
Show file tree
Hide file tree
Showing 15 changed files with 24 additions and 619 deletions.
11 changes: 1 addition & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ core = { version = "1.0.0", optional = true, package = "rustc-std-workspace-core
rustc-std-workspace-alloc = { version = "1.0.0", optional = true } # not aliased here but in lib.rs because of name collision with the alloc feature
compiler_builtins = { version = '0.1.49', optional = true }

# The procfs feature needs once_cell.
# With Rust 1.70.0, we can switch to `core::cell::OnceCell`.
[target.'cfg(any(target_os = "android", target_os = "linux"))'.dependencies]
once_cell = { version = "1.5.2", optional = true }

# Dependencies for platforms where linux_raw is supported, in addition to libc:
#
# On Linux on selected architectures, the linux_raw backend is supported, in
Expand Down Expand Up @@ -151,10 +146,7 @@ shm = ["fs"]
time = []

# Enable `rustix::param::*`.
param = ["fs"]

# Enable this to enable `rustix::io::proc_self_*` (on Linux) and `ttyname`.
procfs = ["once_cell", "fs"]
param = []

# Enable `rustix::pty::*`.
pty = ["fs"]
Expand Down Expand Up @@ -192,7 +184,6 @@ all-apis = [
"param",
"pipe",
"process",
"procfs",
"pty",
"rand",
"runtime",
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ default. The rest of the API is conditional with cargo feature flags:
| `param` | [`rustix::param`]—Process parameters. |
| `pipe` | [`rustix::pipe`]—Pipe operations. |
| `process` | [`rustix::process`]—Process-associated operations. |
| `procfs` | [`rustix::procfs`]—Utilities for reading `/proc` on Linux. |
| `pty` | [`rustix::pty`]—Pseudoterminal operations. |
| `rand` | [`rustix::rand`]—Random-related operations. |
| `shm` | [`rustix::shm`]—POSIX shared memory. |
Expand All @@ -87,7 +86,6 @@ default. The rest of the API is conditional with cargo feature flags:
[`rustix::param`]: https://docs.rs/rustix/*/rustix/param/index.html
[`rustix::pipe`]: https://docs.rs/rustix/*/rustix/pipe/index.html
[`rustix::process`]: https://docs.rs/rustix/*/rustix/process/index.html
[`rustix::procfs`]: https://docs.rs/rustix/*/rustix/procfs/index.html
[`rustix::pty`]: https://docs.rs/rustix/*/rustix/pty/index.html
[`rustix::rand`]: https://docs.rs/rustix/*/rustix/rand/index.html
[`rustix::shm`]: https://docs.rs/rustix/*/rustix/shm/index.html
Expand Down
3 changes: 1 addition & 2 deletions examples/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use rustix::termios::isatty;
#[cfg(all(
not(any(windows, target_os = "fuchsia")),
feature = "termios",
feature = "procfs",
feature = "alloc"
))]
use rustix::termios::ttyname;
Expand Down Expand Up @@ -62,7 +61,7 @@ fn show<Fd: AsFd>(fd: Fd) -> io::Result<()> {

#[cfg(feature = "termios")]
if isatty(fd) {
#[cfg(all(feature = "alloc", feature = "procfs"))]
#[cfg(feature = "alloc")]
#[cfg(not(target_os = "fuchsia"))]
println!(" - ttyname: {}", ttyname(fd, Vec::new())?.to_string_lossy());

Expand Down
6 changes: 1 addition & 5 deletions src/backend/libc/fs/syscalls.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
//! libc syscalls supporting `rustix::fs`.
use crate::backend::c;
#[cfg(any(
not(target_os = "redox"),
feature = "alloc",
all(linux_kernel, feature = "procfs")
))]
#[cfg(any(not(target_os = "redox"), feature = "alloc"))]
use crate::backend::conv::ret_usize;
use crate::backend::conv::{borrowed_fd, c_str, ret, ret_c_int, ret_off_t, ret_owned_fd};
use crate::fd::{BorrowedFd, OwnedFd};
Expand Down
2 changes: 1 addition & 1 deletion src/backend/libc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub(crate) fn if_glibc_is_less_than_2_25() -> bool {
}

// Private modules used by multiple public modules.
#[cfg(any(feature = "procfs", feature = "process", feature = "runtime"))]
#[cfg(any(feature = "process", feature = "runtime"))]
#[cfg(not(any(windows, target_os = "wasi")))]
pub(crate) mod pid;
#[cfg(any(feature = "process", feature = "thread"))]
Expand Down
9 changes: 3 additions & 6 deletions src/backend/libc/termios/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ use crate::backend::c;
use crate::backend::conv::ret_pid_t;
use crate::backend::conv::{borrowed_fd, ret};
use crate::fd::BorrowedFd;
#[cfg(all(feature = "alloc", feature = "procfs"))]
#[cfg(feature = "alloc")]
#[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
use crate::ffi::CStr;
#[cfg(any(
not(target_os = "espidf"),
all(
feature = "procfs",
not(any(target_os = "fuchsia", target_os = "wasi"))
)
not(any(target_os = "fuchsia", target_os = "wasi"))
))]
use core::mem::MaybeUninit;
#[cfg(not(target_os = "wasi"))]
Expand Down Expand Up @@ -508,7 +505,7 @@ pub(crate) fn isatty(fd: BorrowedFd<'_>) -> bool {
unsafe { c::isatty(borrowed_fd(fd)) != 0 }
}

#[cfg(all(feature = "alloc", feature = "procfs"))]
#[cfg(feature = "alloc")]
#[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
pub(crate) fn ttyname(dirfd: BorrowedFd<'_>, buf: &mut [MaybeUninit<u8>]) -> io::Result<usize> {
unsafe {
Expand Down
2 changes: 1 addition & 1 deletion src/backend/linux_raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub(crate) mod fd {
pub(crate) mod c;

// Private modules used by multiple public modules.
#[cfg(any(feature = "procfs", feature = "process", feature = "runtime"))]
#[cfg(any(feature = "process", feature = "runtime"))]
pub(crate) mod pid;
#[cfg(any(feature = "process", feature = "thread"))]
pub(crate) mod prctl;
Expand Down
23 changes: 12 additions & 11 deletions src/backend/linux_raw/termios/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ use crate::backend::conv::{by_ref, c_uint, ret};
use crate::fd::BorrowedFd;
use crate::io;
use crate::pid::Pid;
#[cfg(all(feature = "alloc", feature = "procfs"))]
use crate::procfs;
use crate::termios::{
speed, Action, ControlModes, InputModes, LocalModes, OptionalActions, OutputModes,
QueueSelector, SpecialCodeIndex, Termios, Winsize,
};
#[cfg(all(feature = "alloc", feature = "procfs"))]
#[cfg(feature = "alloc")]
use crate::{ffi::CStr, fs::FileType, path::DecInt};
use core::mem::MaybeUninit;

Expand Down Expand Up @@ -366,7 +364,7 @@ pub(crate) fn isatty(fd: BorrowedFd<'_>) -> bool {
tcgetwinsize(fd).is_ok()
}

#[cfg(all(feature = "alloc", feature = "procfs"))]
#[cfg(feature = "alloc")]
pub(crate) fn ttyname(fd: BorrowedFd<'_>, buf: &mut [MaybeUninit<u8>]) -> io::Result<usize> {
let fd_stat = crate::backend::fs::syscalls::fstat(fd)?;

Expand All @@ -378,15 +376,18 @@ pub(crate) fn ttyname(fd: BorrowedFd<'_>, buf: &mut [MaybeUninit<u8>]) -> io::Re
// Check that `fd` is really a tty.
tcgetwinsize(fd)?;

// Get a fd to "/proc/self/fd".
let proc_self_fd = procfs::proc_self_fd()?;
// Create the "/proc/self/fd/<fd>" string.
let mut proc_self_fd_buf: [u8; 25] = *b"/proc/self/fd/\0\0\0\0\0\0\0\0\0\0\0";
let dec_int = DecInt::from_fd(fd);
let bytes_with_nul = dec_int.as_bytes_with_nul();
proc_self_fd_buf[b"/proc/self/fd/".len()..][..bytes_with_nul.len()]
.copy_from_slice(bytes_with_nul);

// SAFETY: We just wrote a valid C String.
let proc_self_fd_path = unsafe { CStr::from_ptr(proc_self_fd_buf.as_ptr().cast()) };

// Gather the ttyname by reading the "fd" file inside `proc_self_fd`.
let r = crate::backend::fs::syscalls::readlinkat(
proc_self_fd,
DecInt::from_fd(fd).as_c_str(),
buf,
)?;
let r = crate::backend::fs::syscalls::readlinkat(crate::fs::CWD, proc_self_fd_path, buf)?;

// If the number of bytes is equal to the buffer length, truncation may
// have occurred. This check also ensures that we have enough space for
Expand Down
5 changes: 0 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,6 @@ pub mod pipe;
#[cfg(feature = "process")]
#[cfg_attr(docsrs, doc(cfg(feature = "process")))]
pub mod process;
#[cfg(feature = "procfs")]
#[cfg(linux_kernel)]
#[cfg_attr(docsrs, doc(cfg(feature = "procfs")))]
pub mod procfs;
#[cfg(not(windows))]
#[cfg(not(target_os = "wasi"))]
#[cfg(feature = "pty")]
Expand Down Expand Up @@ -346,7 +342,6 @@ pub(crate) mod path;
mod clockid;
#[cfg(not(any(windows, target_os = "wasi")))]
#[cfg(any(
feature = "procfs",
feature = "process",
feature = "runtime",
feature = "termios",
Expand Down
Loading

0 comments on commit f377e85

Please sign in to comment.