From 4970753b49328a72679b9d999887c46157b79ee9 Mon Sep 17 00:00:00 2001 From: tison Date: Mon, 27 Jan 2025 18:56:12 +0800 Subject: [PATCH 1/2] refactor: I/O safety for add_dup2 Signed-off-by: tison --- src/spawn.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/spawn.rs b/src/spawn.rs index eeb9b7871d..e6eb496f2e 100644 --- a/src/spawn.rs +++ b/src/spawn.rs @@ -1,7 +1,5 @@ //! 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")] @@ -9,6 +7,8 @@ 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)] @@ -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(&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, @@ -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( + pub fn add_open( &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, @@ -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(&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, From 7072b3f96a66a803fdd93d1551a703fb593f1e22 Mon Sep 17 00:00:00 2001 From: tison Date: Mon, 27 Jan 2025 21:56:54 +0800 Subject: [PATCH 2/2] add changelog Signed-off-by: tison --- changelog/2588.changed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/2588.changed.md diff --git a/changelog/2588.changed.md b/changelog/2588.changed.md new file mode 100644 index 0000000000..249adb4bfc --- /dev/null +++ b/changelog/2588.changed.md @@ -0,0 +1 @@ +Module sys/spawn now adopts I/O safety