diff --git a/library/Cargo.lock b/library/Cargo.lock index 59b76d8d4427d..268367cd9c144 100644 --- a/library/Cargo.lock +++ b/library/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -158,9 +158,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.159" +version = "0.2.161" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" +checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" dependencies = [ "rustc-std-workspace-core", ] diff --git a/library/std/Cargo.toml b/library/std/Cargo.toml index 358bd25ff1bc6..5f8dcde189bcf 100644 --- a/library/std/Cargo.toml +++ b/library/std/Cargo.toml @@ -39,7 +39,7 @@ miniz_oxide = { version = "0.7.0", optional = true, default-features = false } addr2line = { version = "0.22.0", optional = true, default-features = false } [target.'cfg(not(all(windows, target_env = "msvc")))'.dependencies] -libc = { version = "0.2.159", default-features = false, features = [ +libc = { version = "0.2.161", default-features = false, features = [ 'rustc-dep-of-std', ], public = true } diff --git a/library/std/src/sys/pal/unix/process/process_unix.rs b/library/std/src/sys/pal/unix/process/process_unix.rs index 4551d49e841f7..b490cad1b32cd 100644 --- a/library/std/src/sys/pal/unix/process/process_unix.rs +++ b/library/std/src/sys/pal/unix/process/process_unix.rs @@ -575,16 +575,6 @@ impl Command { } } - // Solaris, glibc 2.29+, and musl 1.24+ 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) => { if cfg!(target_vendor = "apple") { @@ -597,7 +587,7 @@ impl Command { return Ok(None); } } - match posix_spawn_file_actions_addchdir_np.get() { + match get_posix_spawn_addchdir() { Some(f) => Some((f, cwd)), None => return Ok(None), } @@ -871,6 +861,40 @@ impl Command { } } +type PosixSpawnAddChdirFn = + unsafe extern "C" fn(*mut libc::posix_spawn_file_actions_t, *const libc::c_char) -> libc::c_int; + +/// Get the function pointer for adding a chdir action to a `posix_spawn_file_actions_t`, if +/// available, assuming a dynamic libc. +#[cfg(not(all(target_os = "linux", target_env = "musl")))] +fn get_posix_spawn_addchdir() -> Option { + use crate::sys::weak::weak; + + // 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 + } + + posix_spawn_file_actions_addchdir_np.get() +} + +/// Get the function pointer for adding a chdir action to a `posix_spawn_file_actions_t`, if +/// available, on platforms where the function is known to exist. +/// +/// Weak symbol lookup doesn't work with statically linked libcs, so in cases where static linking +/// is possible we need to either check for the presence of the symbol at compile time or know about +/// it upfront. +#[cfg(all(target_os = "linux", target_env = "musl"))] +fn get_posix_spawn_addchdir() -> Option { + // Our minimum required musl supports this function, so we can just use it. + Some(libc::posix_spawn_file_actions_addchdir_np) +} + //////////////////////////////////////////////////////////////////////////////// // Processes ////////////////////////////////////////////////////////////////////////////////