Merge pull request #126 from cakebaker/pgrep_fix_111

pgrep: handle empty /proc/<PID>/cmdline with -a
This commit is contained in:
Krysztal Huang
2024-07-16 00:55:08 +08:00
committed by GitHub
2 changed files with 32 additions and 1 deletions
+9 -1
View File
@@ -85,7 +85,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let formatted: Vec<_> = if matches.get_flag("list-full") {
pids.into_iter()
.map(|it| format!("{} {}", it.pid, it.cmdline))
.map(|it| {
// pgrep from procps-ng outputs the process name inside square brackets
// if /proc/<PID>/cmdline is empty
if it.cmdline.is_empty() {
format!("{} [{}]", it.pid, it.clone().status().get("Name").unwrap())
} else {
format!("{} {}", it.pid, it.cmdline)
}
})
.collect()
} else if matches.get_flag("list-name") {
pids.into_iter()
+23
View File
@@ -139,3 +139,26 @@ fn test_ignore_case() {
new_ucmd!().arg("SH").arg(arg).succeeds();
}
}
#[test]
#[cfg(target_os = "linux")]
fn test_list_full() {
for arg in ["-a", "--list-full"] {
new_ucmd!()
.arg("sh")
.arg(arg)
.succeeds()
// (?m) enables multi-line mode
.stdout_matches(&Regex::new("(?m)^[1-9][0-9]* .+$").unwrap());
}
}
#[test]
#[cfg(target_os = "linux")]
fn test_list_full_process_with_empty_cmdline() {
new_ucmd!()
.arg("kthreadd")
.arg("--list-full")
.succeeds()
.stdout_matches(&Regex::new(r"^[1-9][0-9]* \[kthreadd\]\n$").unwrap());
}