Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/src/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ Our `coreutils` calls utility by `coreutils utility-name` and has `--list` to ru
Our `coreutils` is called as `utility-name` if its binary name ends with `utility-name` to support prefixed names.
Longer name is prioritized e.g. `sum` with the prefix `ck` is called as `cksum`.

On Linux with glibc, a symlink alias such as
`dummy -> true -> coreutils` selects `true` from the alias's immediate target.
On Linux with glibc, coreutils mimics individual binaries under some situations.
The symlink chain such as `false -> ... -> true` (hardlink or copy of `coreutils`)
selects `true` if `false` is not on `/usr/bin/`.

## `base32/base64/basenc`

Expand Down
24 changes: 14 additions & 10 deletions src/bin/coreutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ fn matched_utility<T>(name: &str, utils: &UtilityMap<T>) -> Option<&'static str>
.copied()
}

fn utility_from_symlink<T>(
binary: &std::path::Path,
utils: &UtilityMap<T>,
) -> Option<&'static str> {
let target = std::fs::read_link(binary).ok()?;
fn utility_from_symlink<T>(utils: &UtilityMap<T>) -> Option<&'static str> {
// resolve symlink chain to match with (path-based) AppArmor profile.
// Depending on /proc/self/exe is fine since this is an extension.
// GNUmakefile installs hardlinks by default.
let target = std::env::current_exe().ok()?;
let name = validation::name(&target)?;
matched_utility(name, utils)
}
Expand Down Expand Up @@ -75,7 +75,15 @@ fn main() -> ExitCode {

// binary name ends with util name?
let is_coreutils = binary_as_util.ends_with("utils");
let util_name = if let Some(util) = matched_utility(binary_as_util, &utils) {
let util_name = if cfg!(all(target_os = "linux", target_env = "gnu"))
// don't pay `readlink(2)` when called via PATH which is the most common usecase.
// package should not contain dummy `/usr/bin/false`.
&& !binary.starts_with("/usr/bin")
// People expect that `fake-true` -> `false` works as individual `false` binary.
&& let Some(util) = utility_from_symlink(&utils)
{
Some(OsString::from(util))
} else if let Some(util) = matched_utility(binary_as_util, &utils) {
Some(OsString::from(util))
} else if is_coreutils
&& let Some(util) = args
Expand All @@ -95,10 +103,6 @@ fn main() -> ExitCode {
// todo: Remove support of "*box" from binary
uucore::set_utility_is_second_arg();
args.next()
} else if cfg!(all(target_os = "linux", target_env = "gnu"))
&& let Some(util) = utility_from_symlink(&binary, &utils)
{
Some(OsString::from(util))
} else {
return validation::not_found(&OsString::from(binary_as_util));
};
Expand Down
41 changes: 28 additions & 13 deletions tests/test_util_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,46 +338,61 @@ fn test_sorted_utils() {
assert!(out.lines().filter(|s| !s.is_empty()).is_sorted());
}

// false is not specified. But `not-false` -> `echo` chain should work as `echo` in any cases.
#[cfg(all(target_os = "linux", target_env = "gnu", feature = "echo"))]
#[rstest::rstest]
#[case::relative_target_absolute_alias(false, false, false)]
#[case::relative_target_path_lookup(false, true, false)]
#[case::absolute_target_absolute_alias(true, false, false)]
#[case::absolute_target_path_lookup(true, true, false)]
#[case::relative_target_without_path(false, false, true)]
#[case::relative_target_absolute_alias(false, false, false, true)]
#[case::relative_target_path_lookup(false, true, false, true)]
#[case::absolute_target_absolute_alias(true, false, false, true)]
#[case::absolute_target_path_lookup(true, true, false, true)]
#[case::relative_target_without_path(false, false, true, true)]
#[case::reject_symlink_chain(false, false, false, false)]
fn test_utility_alias_symlink_chain(
#[case] absolute_target: bool,
#[case] path_lookup: bool,
#[case] unset_path: bool,
#[case] real_binary_target: bool,
) {
use std::os::unix::process::CommandExt;
use std::process::Command;
let scene = TestScenario::new("alias");
let at = &scene.fixtures;
at.mkdir("bin");
symlink_file(&scene.bin_path, at.plus("bin/echo")).unwrap();
if real_binary_target {
if std::fs::hard_link(&scene.bin_path, at.plus("bin/echo")).is_err() {
std::fs::copy(&scene.bin_path, at.plus("bin/echo")).unwrap();
}
} else {
symlink_file(&scene.bin_path, at.plus("bin/echo")).unwrap();
}
let target = if absolute_target {
at.plus("bin/echo")
} else {
std::path::PathBuf::from("bin/echo")
};
symlink_file(target, at.plus("dummy")).unwrap();
symlink_file(target, at.plus("not-false")).unwrap();
let binary = if path_lookup {
std::path::PathBuf::from("dummy")
std::path::PathBuf::from("not-false")
} else {
at.plus("dummy")
at.plus("not-false")
};
let mut command = Command::new(binary);
command.args(["hello", "world"]).current_dir(at.plus("bin"));
if unset_path {
command.arg0("dummy").env_remove("PATH");
command.arg0("not-false").env_remove("PATH");
} else {
command.env("PATH", at.root_dir_resolved());
}
let output = command.output().unwrap();
assert!(output.status.success(), "{output:?}");
assert_eq!(output.stdout, b"hello world\n");
assert!(output.stderr.is_empty());
if real_binary_target {
assert!(output.status.success(), "{output:?}");
assert_eq!(output.stdout, b"hello world\n");
assert!(output.stderr.is_empty());
} else {
assert!(!output.status.success(), "{output:?}");
assert!(output.stdout.is_empty());
// no check for stderr since we could improve error message
}
}

#[cfg(all(target_os = "linux", target_env = "gnu"))]
Expand Down
Loading