Skip to content

native: Fix a possible deadlock in spawn #13080

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

Merged
merged 1 commit into from
Mar 24, 2014
Merged
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
20 changes: 9 additions & 11 deletions src/libnative/io/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,17 @@ fn spawn_process_os(config: p::ProcessConfig,
assert_eq!(ret, 0);
}

let dirp = dir.map(|p| p.to_c_str());
let dirp = dirp.as_ref().map(|c| c.with_ref(|p| p)).unwrap_or(ptr::null());

with_envp(env, proc(envp) {
with_argv(config.program, config.args, proc(argv) unsafe {
let pipe = os::pipe();
let mut input = file::FileDesc::new(pipe.input, true);
let mut output = file::FileDesc::new(pipe.out, true);

unsafe { set_cloexec(output.fd()) };
set_cloexec(output.fd());

unsafe {
let pid = fork();
if pid < 0 {
fail!("failure in fork: {}", os::last_os_error());
Expand Down Expand Up @@ -551,27 +555,20 @@ fn spawn_process_os(config: p::ProcessConfig,
// error, but ignore it anyway.
let _ = libc::setsid();
}

with_dirp(dir, |dirp| {
if !dirp.is_null() && chdir(dirp) == -1 {
fail(&mut output);
}
});

with_envp(env, |envp| {
if !envp.is_null() {
set_environ(envp);
}
with_argv(config.program, config.args, |argv| {
let _ = execvp(*argv, argv);
fail(&mut output);
})
})
}
}

#[cfg(unix)]
fn with_argv<T>(prog: &str, args: &[~str], cb: |**libc::c_char| -> T) -> T {
fn with_argv<T>(prog: &str, args: &[~str], cb: proc:(**libc::c_char) -> T) -> T {
use std::slice;

// We can't directly convert `str`s into `*char`s, as someone needs to hold
Expand All @@ -597,7 +594,7 @@ fn with_argv<T>(prog: &str, args: &[~str], cb: |**libc::c_char| -> T) -> T {
}

#[cfg(unix)]
fn with_envp<T>(env: Option<~[(~str, ~str)]>, cb: |*c_void| -> T) -> T {
fn with_envp<T>(env: Option<~[(~str, ~str)]>, cb: proc:(*c_void) -> T) -> T {
use std::slice;

// On posixy systems we can pass a char** for envp, which is a
Expand Down Expand Up @@ -645,6 +642,7 @@ fn with_envp<T>(env: Option<~[(~str, ~str)]>, cb: |*mut c_void| -> T) -> T {
}
}

#[cfg(windows)]
fn with_dirp<T>(d: Option<&Path>, cb: |*libc::c_char| -> T) -> T {
match d {
Some(dir) => dir.with_c_str(|buf| cb(buf)),
Expand Down