Skip to content

Commit e26abe3

Browse files
author
Cameron Nemo
committed
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.
1 parent eb3209a commit e26abe3

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/spawn.rs

Lines changed: 8 additions & 12 deletions
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::unix::io::AsRawFd};
84

95
#[cfg(any(feature = "fs", feature = "term"))]
106
use crate::fcntl::OFlag;
@@ -281,16 +277,16 @@ 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>(
280+
pub fn add_dup2<Fd1: AsRawFd, Fd2: AsRawFd>(
285281
&mut self,
286282
fd: Fd1,
287283
newfd: Fd2,
288284
) -> Result<()> {
289285
let res = unsafe {
290286
libc::posix_spawn_file_actions_adddup2(
291287
&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(),
288+
fd.as_raw_fd(),
289+
newfd.as_raw_fd(),
294290
)
295291
};
296292
Errno::result(res)?;
@@ -303,7 +299,7 @@ impl PosixSpawnFileActions {
303299
/// Add an open action. See
304300
/// [posix_spawn_file_actions_addopen](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addopen.html).
305301
#[doc(alias("posix_spawn_file_actions_addopen"))]
306-
pub fn add_open<Fd: AsFd, P: ?Sized + NixPath>(
302+
pub fn add_open<Fd: AsRawFd, P: ?Sized + NixPath>(
307303
&mut self,
308304
fd: Fd,
309305
path: &P,
@@ -313,7 +309,7 @@ impl PosixSpawnFileActions {
313309
let res = path.with_nix_path(|cstr| unsafe {
314310
libc::posix_spawn_file_actions_addopen(
315311
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
316-
fd.as_fd().as_raw_fd(),
312+
fd.as_raw_fd(),
317313
cstr.as_ptr(),
318314
oflag.bits(),
319315
mode.bits(),
@@ -328,11 +324,11 @@ impl PosixSpawnFileActions {
328324
/// Add a close action. See
329325
/// [posix_spawn_file_actions_addclose](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addclose.html).
330326
#[doc(alias("posix_spawn_file_actions_addclose"))]
331-
pub fn add_close<Fd: AsFd>(&mut self, fd: Fd) -> Result<()> {
327+
pub fn add_close<Fd: AsRawFd>(&mut self, fd: Fd) -> Result<()> {
332328
let res = unsafe {
333329
libc::posix_spawn_file_actions_addclose(
334330
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
335-
fd.as_fd().as_raw_fd(),
331+
fd.as_raw_fd(),
336332
)
337333
};
338334
Errno::result(res)?;

0 commit comments

Comments
 (0)