Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions crates/fspy_client_unix/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn get_fd_path<A: Allocator>(allocator: A, fd: BorrowedFd<'_>) -> nix::Result<Op
return Ok(Some(path));
}
let mut path = [0; PROC_FD_PATH_CAPACITY];
let path = proc_fd_path(fd, &mut path);
let path = proc_fd_path(fd.as_raw_fd(), &mut path);
match fspy_nostd_alloc::fs::readlinkat(allocator, CWD, path) {
Ok(path) => Ok(Some(path)),
Err(fspy_nostd::Error::BADF | fspy_nostd::Error::NOENT) => Ok(None),
Expand All @@ -24,18 +24,19 @@ fn get_fd_path<A: Allocator>(allocator: A, fd: BorrowedFd<'_>) -> nix::Result<Op
}

#[cfg(target_os = "linux")]
const PROC_FD_PATH_CAPACITY: usize =
pub const PROC_FD_PATH_CAPACITY: usize =
b"/proc/self/fd/".len() + <c_int as itoa::Integer>::MAX_STR_LEN + 1;

/// Formats `/proc/self/fd/{fd}` into `buf`, without allocating.
#[cfg(target_os = "linux")]
fn proc_fd_path<'buf>(
fd: BorrowedFd<'_>,
buf: &'buf mut [u8; PROC_FD_PATH_CAPACITY],
) -> fspy_nostd::CStr<'buf, fspy_nostd::Thin> {
pub fn proc_fd_path(
fd: c_int,
buf: &mut [u8; PROC_FD_PATH_CAPACITY],
) -> fspy_nostd::CStr<'_, fspy_nostd::Thin> {
const PREFIX: &[u8] = b"/proc/self/fd/";

let mut formatted = itoa::Buffer::new();
let fd = formatted.format(fd.as_raw_fd()).as_bytes();
let fd = formatted.format(fd).as_bytes();

buf[..PREFIX.len()].copy_from_slice(PREFIX);
let end = PREFIX.len() + fd.len();
Expand Down
8 changes: 4 additions & 4 deletions crates/fspy_preload_unix/src/interceptions/spawn/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ mod linux_only {
reason = "macro-generated code requires types from parent scope"
)]
use super::*;
use crate::client::convert::{PathAt, ToAbsolutePath};
use crate::client::convert::{PROC_FD_PATH_CAPACITY, PathAt, ToAbsolutePath, proc_fd_path};

intercept!(execvpe(64): unsafe extern "C" fn(
prog: *const libc::c_char,
Expand Down Expand Up @@ -286,12 +286,12 @@ mod linux_only {
reason = "suppresses unused warning on *::original"
)]
let _unused = fexecve::original;
let prog = format!("/proc/self/fd/{fd}\0");
let prog = prog.as_ptr();
let mut prog = [0; PROC_FD_PATH_CAPACITY];
let prog = proc_fd_path(fd, &mut prog);
handle_exec(
fspy_nostd_alloc::pooled_bump(),
ExecResolveConfig::search_path_disabled(),
prog.cast(),
prog.as_ptr().cast(),
argv,
envp,
)
Expand Down
Loading