Skip to content

Commit 021a2b7

Browse files
committed
pgrep, pkill: Extract function for finding all the processes
Requires adding require_handler to Settings.
1 parent 330d65b commit 021a2b7

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

src/uu/pgrep/src/pgrep.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct Settings {
3333
runstates: Option<String>,
3434
terminal: Option<HashSet<Teletype>>,
3535
signal: usize,
36+
require_handler: bool,
3637
}
3738

3839
fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
@@ -58,6 +59,7 @@ fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
5859
.collect::<HashSet<_>>()
5960
}),
6061
signal: parse_signal_value(matches.get_one::<String>("signal").unwrap())?,
62+
require_handler: matches.get_flag("require-handler"),
6163
};
6264

6365
if (!settings.newest
@@ -76,6 +78,24 @@ fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
7678
Ok(settings)
7779
}
7880

81+
fn find_matching_pids(settings: &Settings) -> Vec<ProcessInformation> {
82+
let mut pids = collect_matched_pids(settings);
83+
#[cfg(unix)]
84+
if settings.require_handler {
85+
pids.retain(|pid| {
86+
let mask =
87+
u64::from_str_radix(pid.clone().status().get("SigCgt").unwrap(), 16).unwrap();
88+
mask & (1 << settings.signal) != 0
89+
});
90+
}
91+
if pids.is_empty() {
92+
uucore::error::set_exit_code(1);
93+
pids
94+
} else {
95+
process_flag_o_n(settings, &mut pids)
96+
}
97+
}
98+
7999
/// # Conceptual model of `pgrep`
80100
///
81101
/// At first, `pgrep` command will check the patterns is legal.
@@ -94,23 +114,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
94114
let settings = get_match_settings(&matches)?;
95115

96116
// Collect pids
97-
let pids = {
98-
let mut pids = collect_matched_pids(&settings);
99-
#[cfg(unix)]
100-
if matches.get_flag("require-handler") {
101-
pids.retain(|pid| {
102-
let mask =
103-
u64::from_str_radix(pid.clone().status().get("SigCgt").unwrap(), 16).unwrap();
104-
mask & (1 << settings.signal) != 0
105-
});
106-
}
107-
if pids.is_empty() {
108-
uucore::error::set_exit_code(1);
109-
pids
110-
} else {
111-
process_flag_o_n(&settings, &mut pids)
112-
}
113-
};
117+
let pids = find_matching_pids(&settings);
114118

115119
// Processing output
116120
let output = if matches.get_flag("count") {

src/uu/pkill/src/pkill.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct Settings {
4444
runstates: Option<String>,
4545
terminal: Option<HashSet<Teletype>>,
4646
signal: usize,
47+
require_handler: bool,
4748
}
4849

4950
fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
@@ -69,6 +70,7 @@ fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
6970
.collect::<HashSet<_>>()
7071
}),
7172
signal: parse_signal_value(matches.get_one::<String>("signal").unwrap())?,
73+
require_handler: matches.get_flag("require-handler"),
7274
};
7375

7476
if (!settings.newest
@@ -87,6 +89,24 @@ fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
8789
Ok(settings)
8890
}
8991

92+
fn find_matching_pids(settings: &Settings) -> Vec<ProcessInformation> {
93+
let mut pids = collect_matched_pids(settings);
94+
#[cfg(unix)]
95+
if settings.require_handler {
96+
pids.retain(|pid| {
97+
let mask =
98+
u64::from_str_radix(pid.clone().status().get("SigCgt").unwrap(), 16).unwrap();
99+
mask & (1 << settings.signal) != 0
100+
});
101+
}
102+
if pids.is_empty() {
103+
uucore::error::set_exit_code(1);
104+
pids
105+
} else {
106+
process_flag_o_n(settings, &mut pids)
107+
}
108+
}
109+
90110
#[uucore::main]
91111
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
92112
#[cfg(unix)]
@@ -114,23 +134,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
114134
};
115135

116136
// Collect pids
117-
let pids = {
118-
let mut pids = collect_matched_pids(&settings);
119-
#[cfg(unix)]
120-
if matches.get_flag("require-handler") {
121-
pids.retain(|pid| {
122-
let mask =
123-
u64::from_str_radix(pid.clone().status().get("SigCgt").unwrap(), 16).unwrap();
124-
mask & (1 << settings.signal) != 0
125-
});
126-
}
127-
if pids.is_empty() {
128-
uucore::error::set_exit_code(1);
129-
pids
130-
} else {
131-
process_flag_o_n(&settings, &mut pids)
132-
}
133-
};
137+
let pids = find_matching_pids(&settings);
134138

135139
// Send signal
136140
// TODO: Implement -q

0 commit comments

Comments
 (0)