-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
add std::os::unix::process::CommandExt::fd #145687
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ use libc::{gid_t, uid_t}; | |
| use super::common::*; | ||
| use crate::io::{self, Error, ErrorKind}; | ||
| use crate::num::NonZero; | ||
| use crate::os::fd::AsRawFd; | ||
| use crate::process::StdioPipes; | ||
| use crate::sys::cvt; | ||
| #[cfg(target_os = "linux")] | ||
|
|
@@ -71,8 +72,12 @@ impl Command { | |
| let (ours, theirs) = self.setup_io(default, needs_stdin)?; | ||
|
|
||
| if let Some(ret) = self.posix_spawn(&theirs, envp.as_ref())? { | ||
| self.last_spawn_was_posix_spawn(true); | ||
| // Close fds in the parent that have been duplicated in the child | ||
| self.close_owned_fds(); | ||
Qelxiros marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return Ok((ret, ours)); | ||
| } | ||
| self.last_spawn_was_posix_spawn(false); | ||
|
|
||
| #[cfg(target_os = "linux")] | ||
| let (input, output) = sys::net::Socket::new_pair(libc::AF_UNIX, libc::SOCK_SEQPACKET)?; | ||
|
|
@@ -124,6 +129,9 @@ impl Command { | |
| drop(env_lock); | ||
| drop(output); | ||
|
|
||
| // Close fds in the parent that have been duplicated in the child | ||
| self.close_owned_fds(); | ||
|
|
||
| #[cfg(target_os = "linux")] | ||
| let pidfd = if self.get_create_pidfd() { self.recv_pidfd(&input) } else { -1 }; | ||
|
|
||
|
|
@@ -292,6 +300,11 @@ impl Command { | |
| cvt_r(|| libc::dup2(fd, libc::STDERR_FILENO))?; | ||
| } | ||
|
|
||
| for &(ref old_fd, new_fd) in self.get_fds() { | ||
| cvt_r(|| libc::dup2(old_fd.as_raw_fd(), new_fd))?; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is problematic and probably unsound (as it violates I/O safety) if
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to check for that? If not, it seems impossible to avoid.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @joboet friendly nudge :)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use |
||
| cvt_r(|| libc::close(old_fd.as_raw_fd()))?; | ||
| } | ||
|
|
||
| #[cfg(not(target_os = "l4re"))] | ||
| { | ||
| if let Some(_g) = self.get_groups() { | ||
|
|
@@ -455,6 +468,7 @@ impl Command { | |
| use core::sync::atomic::{Atomic, AtomicU8, Ordering}; | ||
|
|
||
| use crate::mem::MaybeUninit; | ||
| use crate::os::fd::AsRawFd; | ||
| use crate::sys::{self, cvt_nz, on_broken_pipe_used}; | ||
|
|
||
| if self.get_gid().is_some() | ||
|
|
@@ -715,6 +729,17 @@ impl Command { | |
| libc::STDERR_FILENO, | ||
| ))?; | ||
| } | ||
| for &(ref old_fd, new_fd) in self.get_fds() { | ||
| cvt_nz(libc::posix_spawn_file_actions_adddup2( | ||
| file_actions.0.as_mut_ptr(), | ||
| old_fd.as_raw_fd(), | ||
| new_fd, | ||
| ))?; | ||
| cvt_nz(libc::posix_spawn_file_actions_addclose( | ||
| file_actions.0.as_mut_ptr(), | ||
| old_fd.as_raw_fd(), | ||
| ))?; | ||
| } | ||
| if let Some((f, cwd)) = addchdir { | ||
| cvt_nz(f(file_actions.0.as_mut_ptr(), cwd.as_ptr()))?; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.