Skip to content

Commit 4970753

Browse files
committed
refactor: I/O safety for add_dup2
Signed-off-by: tison <[email protected]>
1 parent bcbcb50 commit 4970753

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/spawn.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! Safe wrappers around posix_spawn* functions found in the libc "spawn.h" header.
22
3-
use std::{ffi::CStr, mem, os::fd::RawFd};
4-
53
#[cfg(any(feature = "fs", feature = "term"))]
64
use crate::fcntl::OFlag;
75
#[cfg(feature = "signal")]
86
use crate::sys::signal::SigSet;
97
#[cfg(feature = "fs")]
108
use crate::sys::stat::Mode;
119
use crate::{errno::Errno, unistd::Pid, NixPath, Result};
10+
use std::os::fd::AsRawFd;
11+
use std::{ffi::CStr, mem};
1212

1313
/// A spawn attributes object. See [posix_spawnattr_t](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_init.html).
1414
#[repr(transparent)]
@@ -277,7 +277,14 @@ impl PosixSpawnFileActions {
277277
/// Add a [dup2](https://pubs.opengroup.org/onlinepubs/9699919799/functions/dup2.html) action. See
278278
/// [posix_spawn_file_actions_adddup2](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_adddup2.html).
279279
#[doc(alias("posix_spawn_file_actions_adddup2"))]
280-
pub fn add_dup2(&mut self, fd: RawFd, newfd: RawFd) -> Result<()> {
280+
pub fn add_dup2<Fd, NewFd>(&mut self, fd: Fd, newfd: NewFd) -> Result<()>
281+
where
282+
Fd: std::os::fd::AsFd,
283+
NewFd: std::os::fd::AsFd,
284+
{
285+
let fd = fd.as_fd().as_raw_fd();
286+
let newfd = newfd.as_fd().as_raw_fd();
287+
281288
let res = unsafe {
282289
libc::posix_spawn_file_actions_adddup2(
283290
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
@@ -295,13 +302,14 @@ impl PosixSpawnFileActions {
295302
/// Add an open action. See
296303
/// [posix_spawn_file_actions_addopen](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addopen.html).
297304
#[doc(alias("posix_spawn_file_actions_addopen"))]
298-
pub fn add_open<P: ?Sized + NixPath>(
305+
pub fn add_open<Fd: std::os::fd::AsFd, P: ?Sized + NixPath>(
299306
&mut self,
300-
fd: RawFd,
307+
fd: Fd,
301308
path: &P,
302309
oflag: OFlag,
303310
mode: Mode,
304311
) -> Result<()> {
312+
let fd = fd.as_fd().as_raw_fd();
305313
let res = path.with_nix_path(|cstr| unsafe {
306314
libc::posix_spawn_file_actions_addopen(
307315
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
@@ -320,7 +328,9 @@ impl PosixSpawnFileActions {
320328
/// Add a close action. See
321329
/// [posix_spawn_file_actions_addclose](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addclose.html).
322330
#[doc(alias("posix_spawn_file_actions_addclose"))]
323-
pub fn add_close(&mut self, fd: RawFd) -> Result<()> {
331+
pub fn add_close<Fd: std::os::fd::AsFd>(&mut self, fd: Fd) -> Result<()> {
332+
let fd = fd.as_fd().as_raw_fd();
333+
324334
let res = unsafe {
325335
libc::posix_spawn_file_actions_addclose(
326336
&mut self.fa as *mut libc::posix_spawn_file_actions_t,

0 commit comments

Comments
 (0)