Skip to content

Commit a9016c8

Browse files
committed
[musl] use posix_spawn if a directory change was requested
Currently, not all libcs have the `posix_spawn_file_actions_addchdir_np` symbol available to them. So we attempt to do a weak symbol lookup for that function. But that only works if libc is a dynamic library -- with statically linked musl binaries the symbol lookup would never work, so we would never be able to use it even if the musl in use supported the symbol. Now that Rust has a minimum musl version of 1.2.3, all supported musl versions now include this symbol, so we can unconditionally expect it to be there. This symbol was added to libc in rust-lang/libc#3949 -- use it here. I couldn't find any tests for whether the posix_spawn path is used, but I've verified with cargo-nextest that this change works. This is a substantial improvement to nextest's performance with musl. On my workstation with a Ryzen 7950x, against https://github.com/clap-rs/clap at 61f5ee514f8f60ed8f04c6494bdf36c19e7a8126: Before: ``` Summary [ 1.071s] 879 tests run: 879 passed, 0 skipped ``` After: ``` Summary [ 0.392s] 879 tests run: 879 passed, 0 skipped ``` Fixes rust-lang#99740.
1 parent b0c2d2e commit a9016c8

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

library/std/src/process/tests.rs

+14
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,23 @@ fn stdout_works() {
9696
#[test]
9797
#[cfg_attr(any(windows, target_os = "vxworks"), ignore)]
9898
fn set_current_dir_works() {
99+
// On many Unix platforms this will use the posix_spawn path.
99100
let mut cmd = shell_cmd();
100101
cmd.arg("-c").arg("pwd").current_dir("/").stdout(Stdio::piped());
101102
assert_eq!(run_output(cmd), "/\n");
103+
104+
// Also test the fork/exec path by setting a pre_exec function.
105+
#[cfg(unix)]
106+
{
107+
use crate::os::unix::process::CommandExt;
108+
109+
let mut cmd = shell_cmd();
110+
cmd.arg("-c").arg("pwd").current_dir("/").stdout(Stdio::piped());
111+
unsafe {
112+
cmd.pre_exec(|| Ok(()));
113+
}
114+
assert_eq!(run_output(cmd), "/\n");
115+
}
102116
}
103117

104118
#[test]

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

+42-12
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,6 @@ impl Command {
448448
use core::sync::atomic::{AtomicU8, Ordering};
449449

450450
use crate::mem::MaybeUninit;
451-
use crate::sys::weak::weak;
452451
use crate::sys::{self, cvt_nz, on_broken_pipe_flag_used};
453452

454453
if self.get_gid().is_some()
@@ -462,6 +461,8 @@ impl Command {
462461

463462
cfg_if::cfg_if! {
464463
if #[cfg(target_os = "linux")] {
464+
use crate::sys::weak::weak;
465+
465466
weak! {
466467
fn pidfd_spawnp(
467468
*mut libc::c_int,
@@ -575,16 +576,6 @@ impl Command {
575576
}
576577
}
577578

578-
// Solaris, glibc 2.29+, and musl 1.24+ can set a new working directory,
579-
// and maybe others will gain this non-POSIX function too. We'll check
580-
// for this weak symbol as soon as it's needed, so we can return early
581-
// otherwise to do a manual chdir before exec.
582-
weak! {
583-
fn posix_spawn_file_actions_addchdir_np(
584-
*mut libc::posix_spawn_file_actions_t,
585-
*const libc::c_char
586-
) -> libc::c_int
587-
}
588579
let addchdir = match self.get_cwd() {
589580
Some(cwd) => {
590581
if cfg!(target_vendor = "apple") {
@@ -597,7 +588,10 @@ impl Command {
597588
return Ok(None);
598589
}
599590
}
600-
match posix_spawn_file_actions_addchdir_np.get() {
591+
// Check for the availability of the posix_spawn addchdir
592+
// function now. If it isn't available, bail and use the
593+
// fork/exec path.
594+
match get_posix_spawn_addchdir() {
601595
Some(f) => Some((f, cwd)),
602596
None => return Ok(None),
603597
}
@@ -871,6 +865,42 @@ impl Command {
871865
}
872866
}
873867

868+
type PosixSpawnAddChdirFn =
869+
unsafe extern "C" fn(*mut libc::posix_spawn_file_actions_t, *const libc::c_char) -> libc::c_int;
870+
871+
/// Get the function pointer for adding a chdir action to a
872+
/// `posix_spawn_file_actions_t`, if available, assuming a dynamic libc.
873+
///
874+
/// Some platforms can set a new working directory for a spawned process in the
875+
/// `posix_spawn` path. This function looks up the function pointer for adding
876+
/// such an action to a `posix_spawn_file_actions_t` struct.
877+
#[cfg(not(all(target_os = "linux", target_env = "musl")))]
878+
fn get_posix_spawn_addchdir() -> Option<PosixSpawnAddChdirFn> {
879+
use crate::sys::weak::weak;
880+
881+
weak! {
882+
fn posix_spawn_file_actions_addchdir_np(
883+
*mut libc::posix_spawn_file_actions_t,
884+
*const libc::c_char
885+
) -> libc::c_int
886+
}
887+
888+
posix_spawn_file_actions_addchdir_np.get()
889+
}
890+
891+
/// Get the function pointer for adding a chdir action to a
892+
/// `posix_spawn_file_actions_t`, if available, on platforms where the function
893+
/// is known to exist.
894+
///
895+
/// Weak symbol lookup doesn't work with statically linked libcs, so in cases
896+
/// where static linking is possible we need to either check for the presence
897+
/// of the symbol at compile time or know about it upfront.
898+
#[cfg(all(target_os = "linux", target_env = "musl"))]
899+
fn get_posix_spawn_addchdir() -> Option<PosixSpawnAddChdirFn> {
900+
// Our minimum required musl supports this function, so we can just use it.
901+
Some(libc::posix_spawn_file_actions_addchdir_np)
902+
}
903+
874904
////////////////////////////////////////////////////////////////////////////////
875905
// Processes
876906
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)