From f2b139f23d574ca1d2c764b581a3b993e9af1570 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 2 Aug 2023 21:20:51 +0200 Subject: Command: also print removed env vars --- library/std/src/sys/unix/process/process_common.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'library/std/src/sys') diff --git a/library/std/src/sys/unix/process/process_common.rs b/library/std/src/sys/unix/process/process_common.rs index 640648e8707..9362fc7f205 100644 --- a/library/std/src/sys/unix/process/process_common.rs +++ b/library/std/src/sys/unix/process/process_common.rs @@ -561,6 +561,8 @@ impl fmt::Debug for Command { for (key, value_opt) in self.get_envs() { if let Some(value) = value_opt { write!(f, "{}={value:?} ", key.to_string_lossy())?; + } else { + write!(f, "unset({}) ", key.to_string_lossy())?; } } if self.program != self.args[0] { -- cgit 1.4.1-3-g733a5 From 396cbe66397ba77184e4ed896f4ba17f623c00f7 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 3 Aug 2023 09:22:54 +0200 Subject: make unsetting env vars print as executable command --- library/std/src/process/tests.rs | 5 +++-- library/std/src/sys/unix/process/process_common.rs | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) (limited to 'library/std/src/sys') diff --git a/library/std/src/process/tests.rs b/library/std/src/process/tests.rs index 2c331eb0104..0d5321c2eea 100644 --- a/library/std/src/process/tests.rs +++ b/library/std/src/process/tests.rs @@ -543,8 +543,8 @@ fn debug_print() { ); let mut command_with_removed_env = Command::new("boring-name"); - command_with_removed_env.env_remove("BAR"); - assert_eq!(format!("{command_with_removed_env:?}"), r#"unset(BAR) "boring-name""#); + command_with_removed_env.env_remove("FOO").env_remove("BAR"); + assert_eq!(format!("{command_with_removed_env:?}"), r#"unset BAR FOO && "boring-name""#); assert_eq!( format!("{command_with_removed_env:#?}"), format!( @@ -557,6 +557,7 @@ fn debug_print() { clear: false, vars: {{ "BAR": None, + "FOO": None, }}, }}, {PIDFD}}}"# diff --git a/library/std/src/sys/unix/process/process_common.rs b/library/std/src/sys/unix/process/process_common.rs index 9362fc7f205..3a02a6c20d9 100644 --- a/library/std/src/sys/unix/process/process_common.rs +++ b/library/std/src/sys/unix/process/process_common.rs @@ -558,11 +558,25 @@ impl fmt::Debug for Command { if let Some(ref cwd) = self.cwd { write!(f, "cd {cwd:?} && ")?; } + // Removed env vars need a separate command. + // We use a single `unset` command for all of them. + let mut any_removed = false; + for (key, value_opt) in self.get_envs() { + if value_opt.is_none() { + if !any_removed { + write!(f, "unset ")?; + any_removed = true; + } + write!(f, "{} ", key.to_string_lossy())?; + } + } + if any_removed { + write!(f, "&& ")?; + } + // Altered env vars can just be added in front of the program. for (key, value_opt) in self.get_envs() { if let Some(value) = value_opt { write!(f, "{}={value:?} ", key.to_string_lossy())?; - } else { - write!(f, "unset({}) ", key.to_string_lossy())?; } } if self.program != self.args[0] { -- cgit 1.4.1-3-g733a5 From 53a29e0e60b14486fbe06d0d68f311989d693816 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 3 Aug 2023 12:07:42 +0200 Subject: also print clearing the environment entirely --- library/std/src/process/tests.rs | 23 +++++++++++++++++ library/std/src/sys/unix/process/process_common.rs | 29 +++++++++++++--------- library/std/src/sys_common/process.rs | 4 +++ 3 files changed, 44 insertions(+), 12 deletions(-) (limited to 'library/std/src/sys') diff --git a/library/std/src/process/tests.rs b/library/std/src/process/tests.rs index 0d5321c2eea..b50dea2e737 100644 --- a/library/std/src/process/tests.rs +++ b/library/std/src/process/tests.rs @@ -560,6 +560,29 @@ fn debug_print() { "FOO": None, }}, }}, +{PIDFD}}}"# + ) + ); + + let mut command_with_cleared_env = Command::new("boring-name"); + command_with_cleared_env.env_clear().env("BAR", "val").env_remove("FOO"); + assert_eq!(format!("{command_with_cleared_env:?}"), r#"env -i BAR="val" "boring-name""#); + assert_eq!( + format!("{command_with_cleared_env:#?}"), + format!( + r#"Command {{ + program: "boring-name", + args: [ + "boring-name", + ], + env: CommandEnv {{ + clear: true, + vars: {{ + "BAR": Some( + "val", + ), + }}, + }}, {PIDFD}}}"# ) ); diff --git a/library/std/src/sys/unix/process/process_common.rs b/library/std/src/sys/unix/process/process_common.rs index 3a02a6c20d9..23d9f3b78ee 100644 --- a/library/std/src/sys/unix/process/process_common.rs +++ b/library/std/src/sys/unix/process/process_common.rs @@ -558,20 +558,25 @@ impl fmt::Debug for Command { if let Some(ref cwd) = self.cwd { write!(f, "cd {cwd:?} && ")?; } - // Removed env vars need a separate command. - // We use a single `unset` command for all of them. - let mut any_removed = false; - for (key, value_opt) in self.get_envs() { - if value_opt.is_none() { - if !any_removed { - write!(f, "unset ")?; - any_removed = true; + if self.env.does_clear() { + write!(f, "env -i ")?; + // Altered env vars will be printed next, that should exactly work as expected. + } else { + // Removed env vars need a separate command. + // We use a single `unset` command for all of them. + let mut any_removed = false; + for (key, value_opt) in self.get_envs() { + if value_opt.is_none() { + if !any_removed { + write!(f, "unset ")?; + any_removed = true; + } + write!(f, "{} ", key.to_string_lossy())?; } - write!(f, "{} ", key.to_string_lossy())?; } - } - if any_removed { - write!(f, "&& ")?; + if any_removed { + write!(f, "&& ")?; + } } // Altered env vars can just be added in front of the program. for (key, value_opt) in self.get_envs() { diff --git a/library/std/src/sys_common/process.rs b/library/std/src/sys_common/process.rs index 18883048dae..4d295cf0f09 100644 --- a/library/std/src/sys_common/process.rs +++ b/library/std/src/sys_common/process.rs @@ -80,6 +80,10 @@ impl CommandEnv { self.vars.clear(); } + pub fn does_clear(&self) -> bool { + self.clear + } + pub fn have_changed_path(&self) -> bool { self.saw_path || self.clear } -- cgit 1.4.1-3-g733a5 From fc75f723f681dc2cb1b0be7395c936adfb2f6f63 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 3 Aug 2023 12:14:58 +0200 Subject: also use 'env' for printing unsetting --- library/std/src/process/tests.rs | 2 +- library/std/src/sys/unix/process/process_common.rs | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) (limited to 'library/std/src/sys') diff --git a/library/std/src/process/tests.rs b/library/std/src/process/tests.rs index b50dea2e737..72894142d17 100644 --- a/library/std/src/process/tests.rs +++ b/library/std/src/process/tests.rs @@ -544,7 +544,7 @@ fn debug_print() { let mut command_with_removed_env = Command::new("boring-name"); command_with_removed_env.env_remove("FOO").env_remove("BAR"); - assert_eq!(format!("{command_with_removed_env:?}"), r#"unset BAR FOO && "boring-name""#); + assert_eq!(format!("{command_with_removed_env:?}"), r#"env -u BAR -u FOO "boring-name""#); assert_eq!( format!("{command_with_removed_env:#?}"), format!( diff --git a/library/std/src/sys/unix/process/process_common.rs b/library/std/src/sys/unix/process/process_common.rs index 23d9f3b78ee..957947a674a 100644 --- a/library/std/src/sys/unix/process/process_common.rs +++ b/library/std/src/sys/unix/process/process_common.rs @@ -562,21 +562,17 @@ impl fmt::Debug for Command { write!(f, "env -i ")?; // Altered env vars will be printed next, that should exactly work as expected. } else { - // Removed env vars need a separate command. - // We use a single `unset` command for all of them. + // Removed env vars need the command to be wrappen in `env`. let mut any_removed = false; for (key, value_opt) in self.get_envs() { if value_opt.is_none() { if !any_removed { - write!(f, "unset ")?; + write!(f, "env ")?; any_removed = true; } - write!(f, "{} ", key.to_string_lossy())?; + write!(f, "-u {} ", key.to_string_lossy())?; } } - if any_removed { - write!(f, "&& ")?; - } } // Altered env vars can just be added in front of the program. for (key, value_opt) in self.get_envs() { -- cgit 1.4.1-3-g733a5 From 98c94ec72f5c9319b9192d688c5abd851dcd2d15 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 6 Sep 2023 17:58:21 +0200 Subject: fix typo Co-authored-by: Marcin S. --- library/std/src/sys/unix/process/process_common.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'library/std/src/sys') diff --git a/library/std/src/sys/unix/process/process_common.rs b/library/std/src/sys/unix/process/process_common.rs index 957947a674a..644d32b6459 100644 --- a/library/std/src/sys/unix/process/process_common.rs +++ b/library/std/src/sys/unix/process/process_common.rs @@ -562,7 +562,7 @@ impl fmt::Debug for Command { write!(f, "env -i ")?; // Altered env vars will be printed next, that should exactly work as expected. } else { - // Removed env vars need the command to be wrappen in `env`. + // Removed env vars need the command to be wrapped in `env`. let mut any_removed = false; for (key, value_opt) in self.get_envs() { if value_opt.is_none() { -- cgit 1.4.1-3-g733a5 From 31cfa4a95611b1151bcdee213d39e2edbeee6c19 Mon Sep 17 00:00:00 2001 From: The 8472 Date: Thu, 21 Sep 2023 19:29:36 +0200 Subject: Fall back to _SC_NPROCESSORS_ONLN if sched_getaffinity returns an empty mask --- library/std/src/sys/unix/thread.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'library/std/src/sys') diff --git a/library/std/src/sys/unix/thread.rs b/library/std/src/sys/unix/thread.rs index 2afec897a88..0a3936ea8d5 100644 --- a/library/std/src/sys/unix/thread.rs +++ b/library/std/src/sys/unix/thread.rs @@ -316,25 +316,38 @@ pub fn available_parallelism() -> io::Result { target_os = "solaris", target_os = "illumos", ))] { + #[allow(unused_assignments)] + #[allow(unused_mut)] + let mut quota = usize::MAX; + #[cfg(any(target_os = "android", target_os = "linux"))] { - let quota = cgroups::quota().max(1); + quota = cgroups::quota().max(1); let mut set: libc::cpu_set_t = unsafe { mem::zeroed() }; unsafe { if libc::sched_getaffinity(0, mem::size_of::(), &mut set) == 0 { let count = libc::CPU_COUNT(&set) as usize; let count = count.min(quota); - // reported to occur on MIPS kernels older than our minimum supported kernel version for those targets - let count = NonZeroUsize::new(count) - .expect("CPU count must be > 0. This may be a bug in sched_getaffinity(); try upgrading the kernel."); - return Ok(count); + + // According to sched_getaffinity's API it should always be non-zero, but + // some old MIPS kernels were buggy and zero-initialized the mask if + // none was explicitly set. + // In that case we use the sysconf fallback. + if let Some(count) = NonZeroUsize::new(count) { + return Ok(count) + } } } } match unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) } { -1 => Err(io::Error::last_os_error()), 0 => Err(io::const_io_error!(io::ErrorKind::NotFound, "The number of hardware threads is not known for the target platform")), - cpus => Ok(unsafe { NonZeroUsize::new_unchecked(cpus as usize) }), + cpus => { + let count = cpus as usize; + // Cover the unusual situation where we were able to get the quota but not the affinity mask + let count = count.min(quota); + Ok(unsafe { NonZeroUsize::new_unchecked(count) }) + } } } else if #[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd"))] { use crate::ptr; -- cgit 1.4.1-3-g733a5