Skip to content

Commit 831878a

Browse files
Merge pull request #455 from dezgeg/ps_sig_fields
ps: support signal mask related fields
2 parents 7795a04 + e8c56a5 commit 831878a

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

src/uu/pgrep/src/process.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,46 @@ impl ProcessInformation {
417417
self.get_uid_or_gid_field("Gid", 2)
418418
}
419419

420+
/// Helper function to get a hex field from status and parse it as u64
421+
fn get_hex_status_field(&mut self, field_name: &str) -> Result<u64, io::Error> {
422+
self.status()
423+
.get(field_name)
424+
.ok_or_else(|| {
425+
io::Error::new(
426+
io::ErrorKind::InvalidData,
427+
format!("{field_name} field not found"),
428+
)
429+
})
430+
.and_then(|value| {
431+
u64::from_str_radix(value.trim(), 16).map_err(|_| {
432+
io::Error::new(
433+
io::ErrorKind::InvalidData,
434+
format!("Invalid {field_name} value"),
435+
)
436+
})
437+
})
438+
}
439+
440+
/// Returns the signal caught mask for the process
441+
pub fn signals_caught_mask(&mut self) -> Result<u64, io::Error> {
442+
self.get_hex_status_field("SigCgt")
443+
}
444+
445+
/// Returns the pending signals mask for the process
446+
pub fn signals_pending_mask(&mut self) -> Result<u64, io::Error> {
447+
self.get_hex_status_field("SigPnd")
448+
}
449+
450+
/// Returns the blocked signals mask for the process
451+
pub fn signals_blocked_mask(&mut self) -> Result<u64, io::Error> {
452+
self.get_hex_status_field("SigBlk")
453+
}
454+
455+
/// Returns the ignored signals mask for the process
456+
pub fn signals_ignored_mask(&mut self) -> Result<u64, io::Error> {
457+
self.get_hex_status_field("SigIgn")
458+
}
459+
420460
// Root directory of the process (which can be changed by chroot)
421461
pub fn root(&mut self) -> Result<PathBuf, io::Error> {
422462
read_link(format!("/proc/{}/root", self.pid))

src/uu/pgrep/src/process_matcher.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,7 @@ fn collect_matched_pids(settings: &Settings) -> UResult<Vec<ProcessInformation>>
313313
} else {
314314
1 << (settings.signal - 1)
315315
};
316-
let mask =
317-
u64::from_str_radix(pid.clone().status().get("SigCgt").unwrap(), 16).unwrap();
316+
let mask = pid.clone().signals_caught_mask().unwrap();
318317
mask & mask_to_test != 0
319318
} else {
320319
true

src/uu/pidwait/src/pidwait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
3737
}
3838
} else {
3939
for ele in proc_infos.iter_mut() {
40-
println!("waiting for {} (pid {})", ele.status()["Name"], ele.pid);
40+
println!("waiting for {} (pid {})", ele.name().unwrap(), ele.pid);
4141
}
4242
}
4343
}

src/uu/ps/src/picker.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ pub(crate) fn collect_pickers(
4242
"group" | "egroup" => pickers.push(helper(egroup)),
4343
"rgroup" => pickers.push(helper(rgroup)),
4444
"sgroup" => pickers.push(helper(sgroup)),
45+
"pending" => pickers.push(helper(pending)),
46+
"blocked" => pickers.push(helper(blocked)),
47+
"ignored" => pickers.push(helper(ignored)),
48+
"caught" => pickers.push(helper(caught)),
4549
"tname" | "tt" | "tty" => pickers.push(helper(tty)),
4650
"time" | "cputime" => pickers.push(helper(time)),
4751
"ucmd" | "comm" => pickers.push(helper(ucmd)),
@@ -179,6 +183,38 @@ fn ucmd(proc_info: RefCell<ProcessInformation>) -> String {
179183
proc_info.borrow_mut().name().unwrap()
180184
}
181185

186+
fn pending(proc_info: RefCell<ProcessInformation>) -> String {
187+
proc_info
188+
.borrow_mut()
189+
.signals_pending_mask()
190+
.map(|mask| format!("{mask:016x}"))
191+
.unwrap_or_else(|_| "?".to_string())
192+
}
193+
194+
fn blocked(proc_info: RefCell<ProcessInformation>) -> String {
195+
proc_info
196+
.borrow_mut()
197+
.signals_blocked_mask()
198+
.map(|mask| format!("{mask:016x}"))
199+
.unwrap_or_else(|_| "?".to_string())
200+
}
201+
202+
fn ignored(proc_info: RefCell<ProcessInformation>) -> String {
203+
proc_info
204+
.borrow_mut()
205+
.signals_ignored_mask()
206+
.map(|mask| format!("{mask:016x}"))
207+
.unwrap_or_else(|_| "?".to_string())
208+
}
209+
210+
fn caught(proc_info: RefCell<ProcessInformation>) -> String {
211+
proc_info
212+
.borrow_mut()
213+
.signals_caught_mask()
214+
.map(|mask| format!("{mask:016x}"))
215+
.unwrap_or_else(|_| "?".to_string())
216+
}
217+
182218
#[test]
183219
fn test_time() {
184220
let formatted = {

0 commit comments

Comments
 (0)