Skip to content

Commit ab063ca

Browse files
committed
coreutils: support --coreutils-prog utility selection
Support --coreutils-prog=UTIL when invoking the multicall binary, including under an unrecognized name or through a shebang wrapper. Help and diagnostics use the selected utility name. Add coverage for argument forwarding, exit status, invalid utilities, aliases, and shebang execution. All 20 multicall tests pass. Closes #9575.
1 parent aa74c61 commit ab063ca

3 files changed

Lines changed: 95 additions & 3 deletions

File tree

‎src/bin/coreutils.rs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Currently defined functions:
5151
#[allow(clippy::cognitive_complexity)]
5252
fn main() {
5353
let utils = util_map();
54-
let mut args = uucore::args_os();
54+
let mut args = uucore::args_os().peekable();
5555

5656
let binary = validation::binary_path(&mut args);
5757
let binary_as_util = validation::name(&binary).unwrap_or_else(|| {
@@ -68,6 +68,16 @@ fn main() {
6868

6969
let util_name = if let Some(&util) = matched_util {
7070
Some(OsString::from(util))
71+
} else if let Some(util) = args
72+
.peek()
73+
.and_then(|arg| arg.to_str())
74+
.and_then(|arg| arg.strip_prefix("--coreutils-prog="))
75+
{
76+
uucore::set_utility_name(util.to_owned());
77+
let util = OsString::from(util);
78+
uucore::set_utility_is_second_arg();
79+
args.next();
80+
Some(util)
7181
} else if is_coreutils || binary_as_util.ends_with("box") {
7282
// todo: Remove support of "*box" from binary
7383
uucore::set_utility_is_second_arg();

‎src/uucore/src/lib/lib.rs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ use std::os::unix::ffi::{OsStrExt, OsStringExt};
182182
use std::os::wasi::ffi::{OsStrExt, OsStringExt};
183183
use std::str;
184184
use std::str::Utf8Chunk;
185-
use std::sync::{LazyLock, atomic::Ordering};
185+
use std::sync::{LazyLock, OnceLock, atomic::Ordering};
186186

187187
/// Disables the custom signal handlers installed by Rust for stack-overflow handling. With those custom signal handlers processes ignore the first SIGBUS and SIGSEGV signal they receive.
188188
/// See <https://github.com/rust-lang/rust/blob/8ac1525e091d3db28e67adcbbd6db1e1deaa37fb/src/libstd/sys/unix/stack_overflow.rs#L71-L92> for details.
@@ -397,9 +397,19 @@ static UTIL_NAME: LazyLock<String> = LazyLock::new(|| {
397397
.into_owned()
398398
});
399399

400+
static UTILITY_NAME_OVERRIDE: OnceLock<String> = OnceLock::new();
401+
402+
/// Set the name used in diagnostics before dispatching a utility.
403+
/// The first explicit name is retained for the lifetime of the process.
404+
pub fn set_utility_name(name: String) {
405+
let _ = UTILITY_NAME_OVERRIDE.set(name);
406+
}
407+
400408
/// Derive the utility name.
401409
pub fn util_name() -> &'static str {
402-
&UTIL_NAME
410+
UTILITY_NAME_OVERRIDE
411+
.get()
412+
.map_or_else(|| &*UTIL_NAME, String::as_str)
403413
}
404414

405415
static EXECUTION_PHRASE: LazyLock<String> = LazyLock::new(|| {

‎tests/test_util_name.rs‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,75 @@ fn test_sorted_utils() {
341341
.unwrap();
342342
assert!(out.lines().filter(|s| !s.is_empty()).is_sorted());
343343
}
344+
345+
#[test]
346+
#[cfg(all(feature = "echo", feature = "true", feature = "false"))]
347+
fn coreutils_prog_option() {
348+
use std::process::Command;
349+
let scene = TestScenario::new("dispatch");
350+
let output = Command::new(&scene.bin_path)
351+
.args(["--coreutils-prog=echo", "hello", "world"])
352+
.output()
353+
.unwrap();
354+
assert!(output.status.success());
355+
assert_eq!(output.stdout, b"hello world\n");
356+
assert!(output.stderr.is_empty());
357+
for (name, expected) in [("true", 0), ("false", 1), ("missing_program", 1), ("", 1)] {
358+
let output = Command::new(&scene.bin_path)
359+
.arg(format!("--coreutils-prog={name}"))
360+
.output()
361+
.unwrap();
362+
assert_eq!(output.status.code(), Some(expected), "{name}: {output:?}");
363+
}
364+
}
365+
366+
#[test]
367+
#[cfg(all(unix, feature = "true", feature = "echo"))]
368+
fn coreutils_prog_option_under_unknown_name() {
369+
use std::process::Command;
370+
let scene = TestScenario::new("dispatch");
371+
let alias = scene.fixtures.plus("invalid_name");
372+
symlink_file(&scene.bin_path, &alias).unwrap();
373+
let output = Command::new(&alias)
374+
.arg("--coreutils-prog=true")
375+
.output()
376+
.unwrap();
377+
assert!(output.status.success(), "{output:?}");
378+
assert!(output.stdout.is_empty());
379+
assert!(output.stderr.is_empty());
380+
381+
let output = Command::new(&alias)
382+
.args(["--coreutils-prog=echo", "--help"])
383+
.output()
384+
.unwrap();
385+
assert!(output.status.success());
386+
assert!(
387+
String::from_utf8(output.stdout)
388+
.unwrap()
389+
.contains("Usage: echo")
390+
);
391+
}
392+
393+
#[test]
394+
#[cfg(all(unix, feature = "echo"))]
395+
fn coreutils_prog_shebang() {
396+
use std::os::unix::fs::PermissionsExt;
397+
use std::process::Command;
398+
// Exercise the kernel's shebang argument forwarding, including the script
399+
// filename, rather than only passing --coreutils-prog directly.
400+
let scene = TestScenario::new("dispatch");
401+
let script = scene.fixtures.plus("wrapper");
402+
std::fs::write(
403+
&script,
404+
format!("#!{} --coreutils-prog=echo\n", scene.bin_path.display()),
405+
)
406+
.unwrap();
407+
std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();
408+
let output = Command::new(&script).arg("hello").output().unwrap();
409+
assert!(output.status.success(), "{output:?}");
410+
assert_eq!(
411+
output.stdout,
412+
format!("{} hello\n", script.display()).as_bytes()
413+
);
414+
assert!(output.stderr.is_empty());
415+
}

0 commit comments

Comments
 (0)