rust/library/std/src
bors c4df63f47f 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.
2021-01-17 23:44:46 +00:00
..
backtrace use Once instead of Mutex to manage capture resolution 2021-01-06 10:44:06 +10:00
collections Clarify what the effects of a 'logic error' are 2021-01-16 09:36:28 +00:00
env std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
error std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
f32 std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
f64 std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
ffi Update tests for extern block linting 2021-01-13 07:49:16 -05:00
fs std: Fix test got_symlink_permission on Windows 2020-11-16 21:09:26 +03:00
io Add benchmark and fast path for BufReader::read_exact 2021-01-17 12:10:39 +10:00
lazy std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
memchr std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
net Auto merge of #79342 - CDirkx:ipaddr-const, r=oli-obk 2020-12-19 13:13:41 +00:00
num rustc_expand: Mark inner #![test] attributes as soft-unstable 2020-11-20 19:35:03 +03:00
os Take sys/vxworks/{fd,fs,io} from sys/unix instead. 2020-10-16 06:19:00 +02:00
panic review: fix nits and move panic safety tests to the correct place 2020-09-25 23:10:24 +02:00
path Refactor parse_prefix on Windows 2020-11-07 16:15:48 +01:00
prelude Style nit: replace for_each & return with for & continue 2021-01-06 15:13:38 +01:00
process std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
sync Fix documentation typo 2020-12-22 12:19:46 +01:00
sys Don't use posix_spawn_file_actions_addchdir_np on macOS. 2021-01-17 09:51:02 -08:00
sys_common Optimize away some path lookups in the generic fs::copy implementation. 2021-01-06 08:36:31 -08:00
thread Recommend panic::resume_unwind instead of panicking. 2020-12-18 17:03:45 -05:00
time Duration::zero() -> Duration::ZERO 2020-10-21 20:44:03 -07:00
alloc.rs Fix safety comment 2021-01-07 09:13:21 +01:00
ascii.rs Convert many files to intra-doc links 2020-09-02 17:37:40 -04:00
backtrace.rs use Once instead of Mutex to manage capture resolution 2021-01-06 10:44:06 +10:00
env.rs Improve docs for std::env::args() 2020-09-04 14:00:09 -07:00
error.rs Remove many unnecessary manual link resolves from library 2020-12-31 11:54:32 -08:00
f32.rs Auto merge of #79473 - m-ou-se:clamp-in-core, r=m-ou-se 2020-12-19 21:57:38 +00:00
f64.rs Auto merge of #79473 - m-ou-se:clamp-in-core, r=m-ou-se 2020-12-19 21:57:38 +00:00
fs.rs Drop support for cloudabi targets 2020-11-22 17:11:41 -05:00
future.rs Fix stabilization marker for future_readiness_fns 2020-09-15 23:12:08 +02:00
keyword_docs.rs Update code to account for extern ABI requirement 2021-01-13 07:49:45 -05:00
lazy.rs Add (internal-only) SyncOnceCell::get_or_init_pin. 2020-12-08 22:57:50 +01:00
lib.rs Auto merge of #77853 - ijackson:slice-strip-stab, r=Amanieu 2021-01-07 15:21:30 +00:00
macros.rs Bump bootstrap compiler to 1.50 beta 2020-12-30 09:27:19 -05:00
memchr.rs std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
num.rs std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
panic.rs Add tracking issue number for panic_any. 2020-10-28 21:23:45 +01:00
panicking.rs fix another comment, and make __rust_start_panic code a bit more semantically clear 2020-12-25 23:37:27 +01:00
path.rs Remove many unnecessary manual link resolves from library 2020-12-31 11:54:32 -08:00
primitive_docs.rs Auto merge of #80590 - camelid:bool-never-docs, r=nagisa 2021-01-03 12:21:12 +00:00
process.rs Auto merge of #78439 - lzutao:rm-clouldabi, r=Mark-Simulacrum 2020-11-23 19:01:19 +00:00
rt.rs Prevent __rust_begin_short_backtrace frames from being tail-call optimised away 2020-08-07 19:31:25 +01:00
time.rs Drop support for cloudabi targets 2020-11-22 17:11:41 -05:00