Skip to content

Commit 81d0d37

Browse files
committed
pgrep/pkill/pidwait: Support --cgroup option
1 parent ece4a9a commit 81d0d37

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/uu/pgrep/src/process_matcher.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub struct Settings {
4444
pub gid: Option<HashSet<u32>>,
4545
pub pgroup: Option<HashSet<u64>>,
4646
pub session: Option<HashSet<u64>>,
47+
pub cgroup: Option<HashSet<String>>,
4748
pub threads: bool,
4849
}
4950

@@ -101,6 +102,9 @@ pub fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
101102
})
102103
.collect()
103104
}),
105+
cgroup: matches
106+
.get_many::<String>("cgroup")
107+
.map(|groups| groups.cloned().collect()),
104108
threads: false,
105109
};
106110

@@ -115,6 +119,7 @@ pub fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
115119
&& settings.gid.is_none()
116120
&& settings.pgroup.is_none()
117121
&& settings.session.is_none()
122+
&& settings.cgroup.is_none()
118123
&& !settings.require_handler
119124
&& pattern.is_empty()
120125
{
@@ -235,6 +240,10 @@ fn collect_matched_pids(settings: &Settings) -> Vec<ProcessInformation> {
235240
let parent_matched = any_matches(&settings.parent, pid.ppid().unwrap());
236241
let pgroup_matched = any_matches(&settings.pgroup, pid.pgid().unwrap());
237242
let session_matched = any_matches(&settings.session, pid.sid().unwrap());
243+
let cgroup_matched = any_matches(
244+
&settings.cgroup,
245+
pid.cgroup_v2_path().unwrap_or("/".to_string()),
246+
);
238247

239248
let ids_matched = any_matches(&settings.uid, pid.uid().unwrap())
240249
&& any_matches(&settings.euid, pid.euid().unwrap())
@@ -265,6 +274,7 @@ fn collect_matched_pids(settings: &Settings) -> Vec<ProcessInformation> {
265274
&& parent_matched
266275
&& pgroup_matched
267276
&& session_matched
277+
&& cgroup_matched
268278
&& ids_matched
269279
&& handler_matched)
270280
^ settings.inverse
@@ -413,8 +423,7 @@ pub fn clap_args(pattern_help: &'static str, enable_v_flag: bool) -> Vec<Arg> {
413423
// arg!(-L --logpidfile "fail if PID file is not locked"),
414424
arg!(-r --runstates <state> "match runstates [D,S,Z,...]"),
415425
// arg!(-A --"ignore-ancestors" "exclude our ancestors from results"),
416-
// arg!(--cgroup <grp> "match by cgroup v2 names")
417-
// .value_delimiter(','),
426+
arg!(--cgroup <grp> "match by cgroup v2 names").value_delimiter(','),
418427
// arg!(--ns <PID> "match the processes that belong to the same namespace as <pid>"),
419428
// arg!(--nslist <ns> "list which namespaces will be considered for the --ns option.")
420429
// .value_delimiter(',')

tests/by-util/test_pgrep.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,29 @@ fn test_nonexisting_session() {
501501
new_ucmd!().arg("--session=9999999999").fails();
502502
}
503503

504+
#[test]
505+
#[cfg(target_os = "linux")]
506+
fn test_nonexisting_cgroup() {
507+
new_ucmd!()
508+
.arg("--cgroup")
509+
.arg("NONEXISTING")
510+
.fails()
511+
.stdout_is("");
512+
}
513+
514+
#[test]
515+
#[cfg(target_os = "linux")]
516+
fn test_cgroup() {
517+
let init_is_systemd = new_ucmd!().arg("systemd").run().code() == 0;
518+
if init_is_systemd {
519+
new_ucmd!()
520+
.arg("--cgroup")
521+
.arg("/init.scope")
522+
.succeeds()
523+
.stdout_is("1\n");
524+
}
525+
}
526+
504527
#[test]
505528
#[cfg(target_os = "linux")]
506529
fn test_threads() {

0 commit comments

Comments
 (0)