diff --git a/src/uu/pidof/src/pidof.rs b/src/uu/pidof/src/pidof.rs index e570679..24834fd 100644 --- a/src/uu/pidof/src/pidof.rs +++ b/src/uu/pidof/src/pidof.rs @@ -3,6 +3,8 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. +use std::path::PathBuf; + use clap::{crate_version, Arg, ArgAction, ArgMatches, Command}; use uu_pgrep::process::{walk_process, ProcessInformation}; use uucore::{error::UResult, format_usage, help_about, help_usage}; @@ -36,6 +38,22 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { Ok(()) } +fn get_executable_name(process: &mut ProcessInformation) -> String { + let binding = process.cmdline.split(' ').collect::>(); + let mut path = binding.first().unwrap().to_string(); + + if path.is_empty() { + path.clone_from(&process.status()["Name"]); + }; + + PathBuf::from(path) + .file_name() + .unwrap() + .to_str() + .unwrap() + .to_string() +} + fn collect_matched_pids(matches: &ArgMatches) -> Vec { let program_name: Vec<_> = matches .get_many::("program-name") @@ -52,7 +70,7 @@ fn collect_matched_pids(matches: &ArgMatches) -> Vec { let mut processed = Vec::new(); for mut process in collected { - let contains = program_name.contains(process.status().get("Name").unwrap()); + let contains = program_name.contains(&get_executable_name(&mut process)); let should_omit = arg_omit_pid.contains(&process.pid); if contains && !should_omit { diff --git a/tests/by-util/test_pidof.rs b/tests/by-util/test_pidof.rs index 85ca6f4..b1f6766 100644 --- a/tests/by-util/test_pidof.rs +++ b/tests/by-util/test_pidof.rs @@ -9,3 +9,16 @@ use crate::common::util::TestScenario; fn test_invalid_arg() { new_ucmd!().arg("--definitely-invalid").fails().code_is(1); } + +#[test] +#[cfg(target_os = "linux")] +fn test_find_init() { + new_ucmd!().arg("init").succeeds(); +} + + +#[test] +#[cfg(target_os = "linux")] +fn test_find_kthreadd() { + new_ucmd!().arg("kthreadd").succeeds(); +}