Skip to content

refactor: I/O safety for add_open and add_dup2 #2588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2588.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Module sys/spawn now adopts I/O safety
22 changes: 16 additions & 6 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};

/// 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 All @@ -295,13 +302,14 @@ impl PosixSpawnFileActions {
/// Add an open action. See
/// [posix_spawn_file_actions_addopen](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addopen.html).
#[doc(alias("posix_spawn_file_actions_addopen"))]
pub fn add_open<P: ?Sized + NixPath>(
pub fn add_open<Fd: std::os::fd::AsFd, P: ?Sized + NixPath>(
&mut self,
fd: RawFd,
fd: Fd,
path: &P,
oflag: OFlag,
mode: Mode,
) -> Result<()> {
let fd = fd.as_fd().as_raw_fd();
let res = path.with_nix_path(|cstr| unsafe {
libc::posix_spawn_file_actions_addopen(
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
Expand All @@ -320,7 +328,9 @@ impl PosixSpawnFileActions {
/// Add a close action. See
/// [posix_spawn_file_actions_addclose](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addclose.html).
#[doc(alias("posix_spawn_file_actions_addclose"))]
pub fn add_close(&mut self, fd: RawFd) -> Result<()> {
pub fn add_close<Fd: std::os::fd::AsFd>(&mut self, fd: Fd) -> Result<()> {
let fd = fd.as_fd().as_raw_fd();

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