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

Use posix_spawn_file_actions_addchdir_np when possible #58438

Merged
merged 1 commit into from
Feb 17, 2019
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
24 changes: 22 additions & 2 deletions src/libstd/sys/unix/process/process_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,7 @@ impl Command {
use mem;
use sys;

if self.get_cwd().is_some() ||
self.get_gid().is_some() ||
if self.get_gid().is_some() ||
self.get_uid().is_some() ||
self.env_saw_path() ||
self.get_closures().len() != 0 {
Expand All @@ -301,6 +300,24 @@ impl Command {
}
}

// Solaris and glibc 2.29+ can set a new working directory, and maybe
// others will gain this non-POSIX function too. We'll check for this
// weak symbol as soon as it's needed, so we can return early otherwise
// to do a manual chdir before exec.
weak! {
fn posix_spawn_file_actions_addchdir_np(
*mut libc::posix_spawn_file_actions_t,
*const libc::c_char
) -> libc::c_int
}
let addchdir = match self.get_cwd() {
Some(cwd) => match posix_spawn_file_actions_addchdir_np.get() {
Some(f) => Some((f, cwd)),
None => return Ok(None),
},
None => None,
};

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

struct PosixSpawnFileActions(libc::posix_spawn_file_actions_t);
Expand Down Expand Up @@ -345,6 +362,9 @@ impl Command {
fd,
libc::STDERR_FILENO))?;
}
if let Some((f, cwd)) = addchdir {
cvt(f(&mut file_actions.0, cwd.as_ptr()))?;
}

let mut set: libc::sigset_t = mem::uninitialized();
cvt(libc::sigemptyset(&mut set))?;
Expand Down