Skip to content

Commit c4df63f

Browse files
committed
Auto merge of #80537 - ehuss:macos-posix-spawn-chdir, r=dtolnay
Don't use posix_spawn_file_actions_addchdir_np on macOS. There is a bug on macOS where using `posix_spawn_file_actions_addchdir_np` with a relative executable path will cause `posix_spawnp` to return ENOENT, even though it successfully spawned the process in the given directory. `posix_spawn_file_actions_addchdir_np` was introduced in macOS 10.15 first released in Oct 2019. I have tested macOS 10.15.7 and 11.0.1. Example offending program: ```rust use std::fs; use std::os::unix::fs::PermissionsExt; use std::process::*; fn main() { fs::create_dir_all("bar").unwrap(); fs::create_dir_all("foo").unwrap(); fs::write("foo/foo.sh", "#!/bin/sh\necho hello ${PWD}\n").unwrap(); let perms = fs::Permissions::from_mode(0o755); fs::set_permissions("foo/foo.sh", perms).unwrap(); let c = Command::new("../foo/foo.sh").current_dir("bar").spawn(); eprintln!("{:?}", c); } ``` This prints: ``` Err(Os { code: 2, kind: NotFound, message: "No such file or directory" }) hello /Users/eric/Temp/bar ``` I wanted to open this PR to get some feedback on possible solutions. Alternatives: * Do nothing. * Document the bug. * Try to detect if the executable is a relative path on macOS, and avoid using `posix_spawn_file_actions_addchdir_np` only in that case. I looked at the [XNU source code](https://opensource.apple.com/source/xnu/xnu-6153.141.1/bsd/kern/kern_exec.c.auto.html), but I didn't see anything obvious that would explain the behavior. The actual chdir succeeds, it is something else further down that fails, but I couldn't see where. EDIT: I forgot to mention, relative exe paths with `current_dir` in general are discouraged (see #37868). I don't know if #37868 is fixable, since normalizing it would change the semantics for some platforms. Another option is to convert the executable to an absolute path with something like joining the cwd with the new cwd and the executable, but I'm uncertain about that.
2 parents 4253153 + 6e467b7 commit c4df63f

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

library/std/src/sys/unix/process/process_unix.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,20 @@ impl Command {
314314
) -> libc::c_int
315315
}
316316
let addchdir = match self.get_cwd() {
317-
Some(cwd) => match posix_spawn_file_actions_addchdir_np.get() {
318-
Some(f) => Some((f, cwd)),
319-
None => return Ok(None),
320-
},
317+
Some(cwd) => {
318+
if cfg!(target_os = "macos") {
319+
// There is a bug in macOS where a relative executable
320+
// path like "../myprogram" will cause `posix_spawn` to
321+
// successfully launch the program, but erroneously return
322+
// ENOENT when used with posix_spawn_file_actions_addchdir_np
323+
// which was introduced in macOS 10.15.
324+
return Ok(None);
325+
}
326+
match posix_spawn_file_actions_addchdir_np.get() {
327+
Some(f) => Some((f, cwd)),
328+
None => return Ok(None),
329+
}
330+
}
321331
None => None,
322332
};
323333

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// run-pass
2+
// ignore-emscripten no processes
3+
// ignore-sgx no processes
4+
5+
use std::env;
6+
use std::fs;
7+
use std::path::Path;
8+
use std::process::Command;
9+
10+
fn main() {
11+
// Checks the behavior of current_dir when used with a relative exe path.
12+
let me = env::current_exe().unwrap();
13+
if matches!(env::args().skip(1).next().as_deref(), Some("current-dir")) {
14+
let cwd = env::current_dir().unwrap();
15+
assert_eq!(cwd.file_name().unwrap(), "bar");
16+
std::process::exit(0);
17+
}
18+
let exe = me.file_name().unwrap();
19+
let cwd = me.parent().unwrap();
20+
eprintln!("cwd={:?}", cwd);
21+
// Change directory to where the exectuable is located, since this test
22+
// fundamentally needs to use relative paths. In some cases (like
23+
// remote-test-server), the current_dir can be somewhere else, so make
24+
// sure it is something we can use. We assume we can write to this
25+
// directory.
26+
env::set_current_dir(&cwd).unwrap();
27+
let foo = cwd.join("foo");
28+
let bar = cwd.join("bar");
29+
fs::create_dir_all(&foo).unwrap();
30+
fs::create_dir_all(&bar).unwrap();
31+
fs::copy(&me, foo.join(exe)).unwrap();
32+
33+
// Unfortunately this is inconsistent based on the platform, see
34+
// https://github.com/rust-lang/rust/issues/37868. On Windows,
35+
// it is relative *before* changing the directory, and on Unix
36+
// it is *after* changing the directory.
37+
let relative_exe = if cfg!(windows) {
38+
Path::new("foo").join(exe)
39+
} else {
40+
Path::new("../foo").join(exe)
41+
};
42+
43+
let status = Command::new(relative_exe)
44+
.arg("current-dir")
45+
.current_dir("bar")
46+
.status()
47+
.unwrap();
48+
assert!(status.success());
49+
}

0 commit comments

Comments
 (0)