Skip to content

Commit

Permalink
fix: allow safer PosixSpawnFileActions usage
Browse files Browse the repository at this point in the history
Many functions used for PosixSpawnFileActions were demanding fds passed
implement the AsFd trait, but because these actions are meant to be
taken in the child process, that trait doesn't offer much benefit and
actually often leads to the caller needing to do an unsafe operation:
instantiating an OwnedFd from a RawFd. All of these functions need a
RawFd anyway, so just let the caller pass a RawFd directly rather than
have to unsafely create an OwnedFd first, which itself could have
unintended side effects like closing the FD in the parent when no
parent-side actions were intended.
  • Loading branch information
Cameron Nemo committed Sep 8, 2024
1 parent eb3209a commit cc7e859
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{
ffi::CStr,
mem,
os::unix::io::{AsFd, AsRawFd},
os::unix::io::AsRawFd,
};

#[cfg(any(feature = "fs", feature = "term"))]
Expand Down Expand Up @@ -281,16 +281,16 @@ impl PosixSpawnFileActions {
/// Add a [dup2](https://pubs.opengroup.org/onlinepubs/9699919799/functions/dup2.html) action. See
/// [posix_spawn_file_actions_adddup2](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_adddup2.html).
#[doc(alias("posix_spawn_file_actions_adddup2"))]
pub fn add_dup2<Fd1: AsFd, Fd2: AsFd>(
pub fn add_dup2<Fd1: AsRawFd, Fd2: AsRawFd>(
&mut self,
fd: Fd1,
newfd: Fd2,
) -> Result<()> {
let res = unsafe {
libc::posix_spawn_file_actions_adddup2(
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
fd.as_fd().as_raw_fd(),
newfd.as_fd().as_raw_fd(),
fd.as_raw_fd(),
newfd.as_raw_fd(),
)
};
Errno::result(res)?;
Expand All @@ -303,7 +303,7 @@ impl PosixSpawnFileActions {
/// Add an open action. See
/// [posix_spawn_file_actions_addopen](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addopen.html).
#[doc(alias("posix_spawn_file_actions_addopen"))]
pub fn add_open<Fd: AsFd, P: ?Sized + NixPath>(
pub fn add_open<Fd: AsRawFd, P: ?Sized + NixPath>(
&mut self,
fd: Fd,
path: &P,
Expand All @@ -313,7 +313,7 @@ impl PosixSpawnFileActions {
let res = path.with_nix_path(|cstr| unsafe {
libc::posix_spawn_file_actions_addopen(
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
fd.as_fd().as_raw_fd(),
fd.as_raw_fd(),
cstr.as_ptr(),
oflag.bits(),
mode.bits(),
Expand All @@ -328,11 +328,11 @@ impl PosixSpawnFileActions {
/// Add a close action. See
/// [posix_spawn_file_actions_addclose](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addclose.html).
#[doc(alias("posix_spawn_file_actions_addclose"))]
pub fn add_close<Fd: AsFd>(&mut self, fd: Fd) -> Result<()> {
pub fn add_close<Fd: AsRawFd>(&mut self, fd: Fd) -> Result<()> {
let res = unsafe {
libc::posix_spawn_file_actions_addclose(
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
fd.as_fd().as_raw_fd(),
fd.as_raw_fd(),
)
};
Errno::result(res)?;
Expand Down

0 comments on commit cc7e859

Please sign in to comment.