Skip to content

Commit

Permalink
refactor: I/O safety for add_dup2
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun committed Jan 27, 2025
1 parent bcbcb50 commit 2ed0f4c
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/spawn.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! Safe wrappers around posix_spawn* functions found in the libc "spawn.h" header.
use std::{ffi::CStr, mem, os::fd::RawFd};

#[cfg(any(feature = "fs", feature = "term"))]
use crate::fcntl::OFlag;
#[cfg(feature = "signal")]
use crate::sys::signal::SigSet;
#[cfg(feature = "fs")]
use crate::sys::stat::Mode;
use crate::{errno::Errno, unistd::Pid, NixPath, Result};
use std::os::fd::AsRawFd;
use std::{ffi::CStr, mem, os::fd::RawFd};

/// A spawn attributes object. See [posix_spawnattr_t](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_init.html).
#[repr(transparent)]
Expand Down Expand Up @@ -277,7 +277,14 @@ 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: RawFd, newfd: RawFd) -> Result<()> {
pub fn add_dup2<Fd, NewFd>(&mut self, fd: Fd, newfd: NewFd) -> Result<()>
where
Fd: std::os::fd::AsFd,
NewFd: std::os::fd::AsFd,
{
let fd = fd.as_fd().as_raw_fd();
let newfd = newfd.as_fd().as_raw_fd();

let res = unsafe {
libc::posix_spawn_file_actions_adddup2(
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
Expand Down

0 comments on commit 2ed0f4c

Please sign in to comment.