Skip to content

Commit 8f6c633

Browse files
Merge pull request #365 from dezgeg/pidof-w
pidof: Implement --with-workers
2 parents e721d85 + 9fb6aa3 commit 8f6c633

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

src/uu/pidof/src/pidof.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,22 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4545
Ok(())
4646
}
4747

48-
fn get_executable_name(process: &mut ProcessInformation) -> String {
48+
fn match_process_name(
49+
process: &mut ProcessInformation,
50+
name_to_match: &str,
51+
with_workers: bool,
52+
) -> bool {
4953
let binding = process.cmdline.split(' ').collect::<Vec<_>>();
5054
let mut path = binding.first().unwrap().to_string();
5155

5256
if path.is_empty() {
57+
if !with_workers {
58+
return false;
59+
}
5360
path.clone_from(&process.status()["Name"]);
5461
};
5562

56-
PathBuf::from(path)
57-
.file_name()
58-
.unwrap()
59-
.to_str()
60-
.unwrap()
61-
.to_string()
63+
PathBuf::from(path).file_name().unwrap().to_str().unwrap() == name_to_match
6264
}
6365

6466
fn collect_matched_pids(matches: &ArgMatches) -> Vec<usize> {
@@ -67,6 +69,7 @@ fn collect_matched_pids(matches: &ArgMatches) -> Vec<usize> {
6769
.unwrap()
6870
.cloned()
6971
.collect();
72+
let with_workers = matches.get_flag("with-workers");
7073

7174
let collected = walk_process().collect::<Vec<_>>();
7275
let arg_omit_pid = matches
@@ -80,7 +83,7 @@ fn collect_matched_pids(matches: &ArgMatches) -> Vec<usize> {
8083
.flat_map(|program| {
8184
let mut processed = Vec::new();
8285
for mut process in collected.clone() {
83-
let contains = program == get_executable_name(&mut process);
86+
let contains = match_process_name(&mut process, &program, with_workers);
8487
let should_omit = arg_omit_pid.contains(&process.pid);
8588

8689
if contains && !should_omit {
@@ -174,6 +177,13 @@ pub fn uu_app() -> Command {
174177
.help("Show thread ids instead of process ids")
175178
.action(ArgAction::SetTrue),
176179
)
180+
.arg(
181+
Arg::new("with-workers")
182+
.short('w')
183+
.long("with-workers")
184+
.help("Show kernel worker threads as well")
185+
.action(ArgAction::SetTrue),
186+
)
177187
// .arg(
178188
// Arg::new("x")
179189
// .short('x')

tests/by-util/test_pidof.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ fn test_find_init() {
2424

2525
#[test]
2626
#[cfg(target_os = "linux")]
27-
fn test_find_kthreadd() {
28-
new_ucmd!().arg("kthreadd").succeeds();
27+
fn test_find_kthreadd_only_with_w_flag() {
28+
new_ucmd!().arg("kthreadd").fails();
29+
new_ucmd!().arg("-w").arg("kthreadd").succeeds();
2930
}
3031

3132
#[test]
@@ -37,15 +38,20 @@ fn test_no_pid_found() {
3738
#[test]
3839
#[cfg(target_os = "linux")]
3940
fn test_quiet() {
40-
new_ucmd!().arg("kthreadd").arg("-q").succeeds().no_output();
41+
new_ucmd!()
42+
.arg("kthreadd")
43+
.arg("-q")
44+
.arg("-w")
45+
.succeeds()
46+
.no_output();
4147
}
4248

4349
#[test]
4450
#[cfg(target_os = "linux")]
4551
fn test_single_shot() {
4652
for arg in ["-s", "--single-shot"] {
4753
let binding = new_ucmd!()
48-
.args(&[arg, "kthreadd", "kthreadd", "kthreadd"])
54+
.args(&[arg, "-w", "kthreadd", "kthreadd", "kthreadd"])
4955
.succeeds();
5056
let output = binding.stdout_str().trim_end();
5157

@@ -63,7 +69,7 @@ fn test_single_shot() {
6369
#[cfg(target_os = "linux")]
6470
fn test_omit_pid() {
6571
for arg in ["-o=1000", "--omit-pid=1000"] {
66-
new_ucmd!().arg(arg).arg("kthreadd").succeeds();
72+
new_ucmd!().arg(arg).arg("-w").arg("kthreadd").succeeds();
6773
}
6874
}
6975

@@ -76,7 +82,7 @@ fn test_separator() {
7682

7783
for arg in ["-S", "-d", "--separator"] {
7884
new_ucmd!()
79-
.args(&[arg, "separator", "kthreadd", "kthreadd"])
85+
.args(&[arg, "separator", "-w", "kthreadd", "kthreadd"])
8086
.succeeds()
8187
.stdout_matches(re);
8288
}

0 commit comments

Comments
 (0)