diff --git a/src/uu/pidof/src/pidof.rs b/src/uu/pidof/src/pidof.rs index 4fbda42..30fa08a 100644 --- a/src/uu/pidof/src/pidof.rs +++ b/src/uu/pidof/src/pidof.rs @@ -25,6 +25,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { }; let mut collected = collect_matched_pids(&matches); + + if collected.is_empty() { + uucore::error::set_exit_code(1); + return Ok(()); + }; + collected.sort_by(|a, b| b.pid.cmp(&a.pid)); let output = collected diff --git a/tests/by-util/test_pidof.rs b/tests/by-util/test_pidof.rs index 373bf8d..4c15eb9 100644 --- a/tests/by-util/test_pidof.rs +++ b/tests/by-util/test_pidof.rs @@ -21,3 +21,15 @@ fn test_find_init() { fn test_find_kthreadd() { new_ucmd!().arg("kthreadd").succeeds(); } + +#[test] +#[cfg(target_os = "linux")] +fn test_no_program() { + new_ucmd!().fails().code_is(1); +} + +#[test] +#[cfg(target_os = "linux")] +fn test_no_pid_found() { + new_ucmd!().arg("NO_THIS_PROGRAM").fails().code_is(1); +}