Skip to content

Commit 4265f33

Browse files
committed
pidof: Support --check-root option
1 parent 27dd036 commit 4265f33

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

src/uu/pidof/src/pidof.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::path::PathBuf;
77

88
use clap::{crate_version, Arg, ArgAction, ArgMatches, Command};
99
use uu_pgrep::process::{walk_process, ProcessInformation};
10-
use uucore::{error::UResult, format_usage, help_about, help_usage};
10+
use uucore::{error::UResult, format_usage, help_about, help_usage, process::geteuid};
1111

1212
const ABOUT: &str = help_about!("pidof.md");
1313
const USAGE: &str = help_usage!("pidof.md");
@@ -94,21 +94,32 @@ fn collect_matched_pids(matches: &ArgMatches) -> Vec<usize> {
9494
.copied()
9595
.collect::<Vec<_>>();
9696

97+
// Original pidof silently ignores the check-root option if the user is not root.
98+
let check_root = matches.get_flag("check-root") && geteuid() == 0;
99+
let our_root = ProcessInformation::current_process_info()
100+
.unwrap()
101+
.root()
102+
.unwrap();
103+
97104
program_names
98105
.into_iter()
99106
.flat_map(|program| {
100107
let mut processed = Vec::new();
101108
for mut process in collected.clone() {
102-
let contains =
103-
match_process_name(&mut process, &program, with_workers, match_scripts);
104-
let should_omit = arg_omit_pid.contains(&process.pid);
105-
106-
if contains && !should_omit {
107-
if matches.get_flag("t") {
108-
processed.extend_from_slice(&process.thread_ids());
109-
} else {
110-
processed.push(process.pid);
111-
}
109+
if !match_process_name(&mut process, &program, with_workers, match_scripts) {
110+
continue;
111+
}
112+
if arg_omit_pid.contains(&process.pid) {
113+
continue;
114+
}
115+
if check_root && process.root().unwrap() != our_root {
116+
continue;
117+
}
118+
119+
if matches.get_flag("t") {
120+
processed.extend_from_slice(&process.thread_ids());
121+
} else {
122+
processed.push(process.pid);
112123
}
113124
}
114125

@@ -140,12 +151,13 @@ pub fn uu_app() -> Command {
140151
.index(1)
141152
.action(ArgAction::Append),
142153
)
143-
// .arg(
144-
// Arg::new("c")
145-
// .short('c')
146-
// .help("Return PIDs with the same root directory")
147-
// .action(ArgAction::SetTrue),
148-
// )
154+
.arg(
155+
Arg::new("check-root")
156+
.short('c')
157+
.long("check-root")
158+
.help("Only return PIDs with the same root directory")
159+
.action(ArgAction::SetTrue),
160+
)
149161
.arg(
150162
Arg::new("S")
151163
.short('S')

tests/by-util/test_pidof.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ fn test_quiet() {
4646
.no_output();
4747
}
4848

49+
#[test]
50+
#[cfg(target_os = "linux")]
51+
fn test_check_root_accepted() {
52+
new_ucmd!()
53+
.arg("-w")
54+
.arg("--check-root")
55+
.arg("kthreadd")
56+
.succeeds();
57+
}
58+
4959
#[test]
5060
#[cfg(target_os = "linux")]
5161
fn test_single_shot() {

0 commit comments

Comments
 (0)