From 7f8a59cd594065ef1a2bee1cd27e7e81193d2947 Mon Sep 17 00:00:00 2001 From: Cameron Nemo Date: Sun, 8 Sep 2024 12:43:07 -0700 Subject: [PATCH] fix: allow safer PosixSpawnFileActions usage 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. --- src/spawn.rs | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/spawn.rs b/src/spawn.rs index 48686757dd..eeb9b7871d 100644 --- a/src/spawn.rs +++ b/src/spawn.rs @@ -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; @@ -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( - &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)?; @@ -303,9 +295,9 @@ 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( + pub fn add_open( &mut self, - fd: Fd, + fd: RawFd, path: &P, oflag: OFlag, mode: Mode, @@ -313,7 +305,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, cstr.as_ptr(), oflag.bits(), mode.bits(), @@ -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(&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)?;