mirror of
https://github.com/uutils/procps.git
synced 2026-06-10 16:14:00 -07:00
Merge pull request #126 from cakebaker/pgrep_fix_111
pgrep: handle empty /proc/<PID>/cmdline with -a
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user