Skip to content

Commit 76fb2a5

Browse files
Merge pull request #360 from dezgeg/require-handler-fixes
pgrep/pkill/pidwait: --require-handler fixes
2 parents d70f9a7 + eb0871a commit 76fb2a5

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

src/uu/pgrep/src/process_matcher.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pub fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
115115
&& settings.gid.is_none()
116116
&& settings.pgroup.is_none()
117117
&& settings.session.is_none()
118+
&& !settings.require_handler
118119
&& pattern.is_empty()
119120
{
120121
return Err(USimpleError::new(
@@ -138,14 +139,6 @@ pub fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
138139

139140
pub fn find_matching_pids(settings: &Settings) -> Vec<ProcessInformation> {
140141
let mut pids = collect_matched_pids(settings);
141-
#[cfg(unix)]
142-
if settings.require_handler {
143-
pids.retain(|pid| {
144-
let mask =
145-
u64::from_str_radix(pid.clone().status().get("SigCgt").unwrap(), 16).unwrap();
146-
mask & (1 << settings.signal) != 0
147-
});
148-
}
149142
if pids.is_empty() {
150143
uucore::error::set_exit_code(1);
151144
pids
@@ -247,14 +240,33 @@ fn collect_matched_pids(settings: &Settings) -> Vec<ProcessInformation> {
247240
&& any_matches(&settings.euid, pid.euid().unwrap())
248241
&& any_matches(&settings.gid, pid.gid().unwrap());
249242

243+
#[cfg(unix)]
244+
let handler_matched = if settings.require_handler {
245+
// Bits in SigCgt are off by one (ie. bit 0 represents signal 1, etc.)
246+
let mask_to_test = if settings.signal == 0 {
247+
// In original pgrep, testing for signal 0 seems to return results for signal 64 instead.
248+
1 << (64 - 1)
249+
} else {
250+
1 << (settings.signal - 1)
251+
};
252+
let mask =
253+
u64::from_str_radix(pid.clone().status().get("SigCgt").unwrap(), 16).unwrap();
254+
mask & mask_to_test != 0
255+
} else {
256+
true
257+
};
258+
#[cfg(not(unix))]
259+
let handler_matched = true;
260+
250261
if (run_state_matched
251262
&& pattern_matched
252263
&& tty_matched
253264
&& older_matched
254265
&& parent_matched
255266
&& pgroup_matched
256267
&& session_matched
257-
&& ids_matched)
268+
&& ids_matched
269+
&& handler_matched)
258270
^ settings.inverse
259271
{
260272
tmp_vec.push(pid);

tests/by-util/test_pgrep.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,26 @@ fn test_invalid_signal() {
369369
.stderr_contains("Unknown signal 'foo'");
370370
}
371371

372+
#[test]
373+
#[cfg(target_os = "linux")]
374+
fn test_signal_that_never_matches() {
375+
new_ucmd!()
376+
.arg("--require-handler")
377+
.arg("--signal=KILL")
378+
.arg(".*")
379+
.fails()
380+
.no_output();
381+
}
382+
383+
#[test]
384+
#[cfg(target_os = "linux")]
385+
fn test_lone_require_handler_is_allowed() {
386+
new_ucmd!()
387+
.arg("--require-handler")
388+
.run()
389+
.stderr_does_not_contain("no matching criteria specified");
390+
}
391+
372392
#[test]
373393
#[cfg(target_os = "linux")]
374394
fn test_does_not_match_pid() {

0 commit comments

Comments
 (0)