Skip to content

Commit

Permalink
fix: allow safer PosixSpawnFileActions usage (#2491)
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.

Co-authored-by: Cameron Nemo <[email protected]>
  • Loading branch information
CameronNemo and Cameron Nemo authored Sep 10, 2024
1 parent 65d90b9 commit 89cc713
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions src/spawn.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
//! Safe wrappers around posix_spawn* functions found in the libc "spawn.h" header.
use std::{
ffi::CStr,
mem,
os::unix::io::{AsFd, AsRawFd},
};
use std::{ffi::CStr, mem, os::fd::RawFd};

#[cfg(any(feature = "fs", feature = "term"))]
use crate::fcntl::OFlag;
Expand Down Expand Up @@ -281,16 +277,12 @@ 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>(
&mut self,
fd: Fd1,
newfd: Fd2,
) -> Result<()> {
pub fn add_dup2(&mut self, fd: RawFd, newfd: RawFd) -> 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,
newfd,
)
};
Errno::result(res)?;
Expand All @@ -303,17 +295,17 @@ 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<P: ?Sized + NixPath>(
&mut self,
fd: Fd,
fd: RawFd,
path: &P,
oflag: OFlag,
mode: Mode,
) -> Result<()> {
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,
cstr.as_ptr(),
oflag.bits(),
mode.bits(),
Expand All @@ -328,11 +320,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(&mut self, fd: RawFd) -> 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,
)
};
Errno::result(res)?;
Expand Down

0 comments on commit 89cc713

Please sign in to comment.