Skip to content
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

Command: Support posix_spawn() on FreeBSD/OSX/GNU Linux #48624

Merged
merged 19 commits into from
Mar 23, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
102 changes: 102 additions & 0 deletions src/libstd/sys/unix/process/process_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl Command {
-> io::Result<(Process, StdioPipes)> {
use sys;


const CLOEXEC_MSG_FOOTER: &'static [u8] = b"NOEX";

let envp = self.capture_env();
Expand All @@ -34,6 +35,11 @@ impl Command {
}

let (ours, theirs) = self.setup_io(default, needs_stdin)?;

if let Some(ret) = self.posix_spawn(&theirs, envp.as_ref())? {
return Ok((ret, ours))
}

let (input, output) = sys::pipe::anon_pipe()?;

let pid = unsafe {
Expand Down Expand Up @@ -229,6 +235,102 @@ impl Command {
libc::execvp(self.get_argv()[0], self.get_argv().as_ptr());
io::Error::last_os_error()
}

#[cfg(not(any(target_os = "linux", target_os = "macos")))]
fn posix_spawn(&mut self, _stdio: &ChildPipes, _envp: Option<&CStringArray>)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change this to _?

Note that there is a subtle difference between using only _ and using a name that starts with an underscore. Something like _x still binds the value to the variable, whereas _ doesn’t bind at all.

(https://doc.rust-lang.org/book/second-edition/ch18-03-pattern-syntax.html)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed.

-> io::Result<Option<Process>>
{
Ok(None)
}

#[cfg(any(target_os = "linux", target_os = "macos"))]
fn posix_spawn(&mut self, stdio: &ChildPipes, envp: Option<&CStringArray>)
-> io::Result<Option<Process>>
{
use mem;
use sys;

if self.get_cwd().is_some() ||
self.get_gid().is_some() ||
self.get_uid().is_some() ||
self.get_closures().len() != 0 {
return Ok(None)
}

let mut p = Process { pid: 0, status: None };

struct PosixSpawnFileActions(libc::posix_spawn_file_actions_t);

impl Drop for PosixSpawnFileActions {
fn drop(&mut self) {
unsafe {
libc::posix_spawn_file_actions_destroy(&mut self.0);
}
}
}

struct PosixSpawnattr(libc::posix_spawnattr_t);

impl Drop for PosixSpawnattr {
fn drop(&mut self) {
unsafe {
libc::posix_spawnattr_destroy(&mut self.0);
}
}
}

unsafe {
let mut file_actions = PosixSpawnFileActions(mem::zeroed());
let mut attrs = PosixSpawnattr(mem::zeroed());

libc::posix_spawnattr_init(&mut attrs.0);
libc::posix_spawn_file_actions_init(&mut file_actions.0);

if let Some(fd) = stdio.stdin.fd() {
cvt(libc::posix_spawn_file_actions_adddup2(&mut file_actions.0,
fd,
libc::STDIN_FILENO))?;
}
if let Some(fd) = stdio.stdout.fd() {
cvt(libc::posix_spawn_file_actions_adddup2(&mut file_actions.0,
fd,
libc::STDOUT_FILENO))?;
}
if let Some(fd) = stdio.stderr.fd() {
cvt(libc::posix_spawn_file_actions_adddup2(&mut file_actions.0,
fd,
libc::STDERR_FILENO))?;
}

let mut set: libc::sigset_t = mem::zeroed();
cvt(libc::sigemptyset(&mut set))?;
cvt(libc::posix_spawnattr_setsigmask(&mut attrs.0,
&set))?;
cvt(libc::sigaddset(&mut set, libc::SIGPIPE))?;
cvt(libc::posix_spawnattr_setsigdefault(&mut attrs.0,
&set))?;

let flags = libc::POSIX_SPAWN_SETSIGDEF |
libc::POSIX_SPAWN_SETSIGMASK;
cvt(libc::posix_spawnattr_setflags(&mut attrs.0, flags as _))?;

let envp = envp.map(|c| c.as_ptr())
.unwrap_or(sys::os::environ() as *const _);
let ret = libc::posix_spawnp(
&mut p.pid,
self.get_argv()[0],
&file_actions.0,
&attrs.0,
self.get_argv().as_ptr() as *const _,
envp as *const _,
);
if ret == 0 {
Ok(Some(p))
} else {
Err(io::Error::last_os_error())
}
}
}
}

////////////////////////////////////////////////////////////////////////////////
Expand Down