Skip to content

Commit cc7e859

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 cc7e859

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/spawn.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::{
44
ffi::CStr,
55
mem,
6-
os::unix::io::{AsFd, AsRawFd},
6+
os::unix::io::AsRawFd,
77
};
88

99
#[cfg(any(feature = "fs", feature = "term"))]
@@ -281,16 +281,16 @@ impl PosixSpawnFileActions {
281281
/// Add a [dup2](https://pubs.opengroup.org/onlinepubs/9699919799/functions/dup2.html) action. See
282282
/// [posix_spawn_file_actions_adddup2](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_adddup2.html).
283283
#[doc(alias("posix_spawn_file_actions_adddup2"))]
284-
pub fn add_dup2<Fd1: AsFd, Fd2: AsFd>(
284+
pub fn add_dup2<Fd1: AsRawFd, Fd2: AsRawFd>(
285285
&mut self,
286286
fd: Fd1,
287287
newfd: Fd2,
288288
) -> Result<()> {
289289
let res = unsafe {
290290
libc::posix_spawn_file_actions_adddup2(
291291
&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(),
292+
fd.as_raw_fd(),
293+
newfd.as_raw_fd(),
294294
)
295295
};
296296
Errno::result(res)?;
@@ -303,7 +303,7 @@ impl PosixSpawnFileActions {
303303
/// Add an open action. See
304304
/// [posix_spawn_file_actions_addopen](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addopen.html).
305305
#[doc(alias("posix_spawn_file_actions_addopen"))]
306-
pub fn add_open<Fd: AsFd, P: ?Sized + NixPath>(
306+
pub fn add_open<Fd: AsRawFd, P: ?Sized + NixPath>(
307307
&mut self,
308308
fd: Fd,
309309
path: &P,
@@ -313,7 +313,7 @@ impl PosixSpawnFileActions {
313313
let res = path.with_nix_path(|cstr| unsafe {
314314
libc::posix_spawn_file_actions_addopen(
315315
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
316-
fd.as_fd().as_raw_fd(),
316+
fd.as_raw_fd(),
317317
cstr.as_ptr(),
318318
oflag.bits(),
319319
mode.bits(),
@@ -328,11 +328,11 @@ impl PosixSpawnFileActions {
328328
/// Add a close action. See
329329
/// [posix_spawn_file_actions_addclose](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addclose.html).
330330
#[doc(alias("posix_spawn_file_actions_addclose"))]
331-
pub fn add_close<Fd: AsFd>(&mut self, fd: Fd) -> Result<()> {
331+
pub fn add_close<Fd: AsRawFd>(&mut self, fd: Fd) -> Result<()> {
332332
let res = unsafe {
333333
libc::posix_spawn_file_actions_addclose(
334334
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
335-
fd.as_fd().as_raw_fd(),
335+
fd.as_raw_fd(),
336336
)
337337
};
338338
Errno::result(res)?;

0 commit comments

Comments
 (0)