Skip to content

Commit e5ac667

Browse files
CameronNemoCameron Nemo
and
Cameron Nemo
authored
fix: allow safer PosixSpawnFileActions usage (#2491)
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]>
1 parent 65d90b9 commit e5ac667

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

src/spawn.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
//! Safe wrappers around posix_spawn* functions found in the libc "spawn.h" header.
22
3-
use std::{
4-
ffi::CStr,
5-
mem,
6-
os::unix::io::{AsFd, AsRawFd},
7-
};
3+
use std::{ffi::CStr, mem, os::fd::RawFd};
84

95
#[cfg(any(feature = "fs", feature = "term"))]
106
use crate::fcntl::OFlag;
@@ -281,16 +277,12 @@ impl PosixSpawnFileActions {
281277
/// Add a [dup2](https://pubs.opengroup.org/onlinepubs/9699919799/functions/dup2.html) action. See
282278
/// [posix_spawn_file_actions_adddup2](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_adddup2.html).
283279
#[doc(alias("posix_spawn_file_actions_adddup2"))]
284-
pub fn add_dup2<Fd1: AsFd, Fd2: AsFd>(
285-
&mut self,
286-
fd: Fd1,
287-
newfd: Fd2,
288-
) -> Result<()> {
280+
pub fn add_dup2(&mut self, fd: RawFd, newfd: RawFd) -> Result<()> {
289281
let res = unsafe {
290282
libc::posix_spawn_file_actions_adddup2(
291283
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
292-
fd.as_fd().as_raw_fd(),
293-
newfd.as_fd().as_raw_fd(),
284+
fd,
285+
newfd,
294286
)
295287
};
296288
Errno::result(res)?;
@@ -303,17 +295,17 @@ impl PosixSpawnFileActions {
303295
/// Add an open action. See
304296
/// [posix_spawn_file_actions_addopen](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addopen.html).
305297
#[doc(alias("posix_spawn_file_actions_addopen"))]
306-
pub fn add_open<Fd: AsFd, P: ?Sized + NixPath>(
298+
pub fn add_open<P: ?Sized + NixPath>(
307299
&mut self,
308-
fd: Fd,
300+
fd: RawFd,
309301
path: &P,
310302
oflag: OFlag,
311303
mode: Mode,
312304
) -> Result<()> {
313305
let res = path.with_nix_path(|cstr| unsafe {
314306
libc::posix_spawn_file_actions_addopen(
315307
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
316-
fd.as_fd().as_raw_fd(),
308+
fd,
317309
cstr.as_ptr(),
318310
oflag.bits(),
319311
mode.bits(),
@@ -328,11 +320,11 @@ impl PosixSpawnFileActions {
328320
/// Add a close action. See
329321
/// [posix_spawn_file_actions_addclose](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addclose.html).
330322
#[doc(alias("posix_spawn_file_actions_addclose"))]
331-
pub fn add_close<Fd: AsFd>(&mut self, fd: Fd) -> Result<()> {
323+
pub fn add_close(&mut self, fd: RawFd) -> Result<()> {
332324
let res = unsafe {
333325
libc::posix_spawn_file_actions_addclose(
334326
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
335-
fd.as_fd().as_raw_fd(),
327+
fd,
336328
)
337329
};
338330
Errno::result(res)?;

0 commit comments

Comments
 (0)