Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/uu/pgrep/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,46 @@ impl ProcessInformation {
self.get_uid_or_gid_field("Gid", 2)
}

/// Helper function to get a hex field from status and parse it as u64
fn get_hex_status_field(&mut self, field_name: &str) -> Result<u64, io::Error> {
self.status()
.get(field_name)
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("{field_name} field not found"),
)
})
.and_then(|value| {
u64::from_str_radix(value.trim(), 16).map_err(|_| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("Invalid {field_name} value"),
)
})
})
}

/// Returns the signal caught mask for the process
pub fn signals_caught_mask(&mut self) -> Result<u64, io::Error> {
self.get_hex_status_field("SigCgt")
}

/// Returns the pending signals mask for the process
pub fn signals_pending_mask(&mut self) -> Result<u64, io::Error> {
self.get_hex_status_field("SigPnd")
}

/// Returns the blocked signals mask for the process
pub fn signals_blocked_mask(&mut self) -> Result<u64, io::Error> {
self.get_hex_status_field("SigBlk")
}

/// Returns the ignored signals mask for the process
pub fn signals_ignored_mask(&mut self) -> Result<u64, io::Error> {
self.get_hex_status_field("SigIgn")
}

// Root directory of the process (which can be changed by chroot)
pub fn root(&mut self) -> Result<PathBuf, io::Error> {
read_link(format!("/proc/{}/root", self.pid))
Expand Down
3 changes: 1 addition & 2 deletions src/uu/pgrep/src/process_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,7 @@ fn collect_matched_pids(settings: &Settings) -> UResult<Vec<ProcessInformation>>
} else {
1 << (settings.signal - 1)
};
let mask =
u64::from_str_radix(pid.clone().status().get("SigCgt").unwrap(), 16).unwrap();
let mask = pid.clone().signals_caught_mask().unwrap();
mask & mask_to_test != 0
} else {
true
Expand Down
2 changes: 1 addition & 1 deletion src/uu/pidwait/src/pidwait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}
} else {
for ele in proc_infos.iter_mut() {
println!("waiting for {} (pid {})", ele.status()["Name"], ele.pid);
println!("waiting for {} (pid {})", ele.name().unwrap(), ele.pid);
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions src/uu/ps/src/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ pub(crate) fn collect_pickers(
"group" | "egroup" => pickers.push(helper(egroup)),
"rgroup" => pickers.push(helper(rgroup)),
"sgroup" => pickers.push(helper(sgroup)),
"pending" => pickers.push(helper(pending)),
"blocked" => pickers.push(helper(blocked)),
"ignored" => pickers.push(helper(ignored)),
"caught" => pickers.push(helper(caught)),
"tname" | "tt" | "tty" => pickers.push(helper(tty)),
"time" | "cputime" => pickers.push(helper(time)),
"ucmd" | "comm" => pickers.push(helper(ucmd)),
Expand Down Expand Up @@ -179,6 +183,38 @@ fn ucmd(proc_info: RefCell<ProcessInformation>) -> String {
proc_info.borrow_mut().name().unwrap()
}

fn pending(proc_info: RefCell<ProcessInformation>) -> String {
proc_info
.borrow_mut()
.signals_pending_mask()
.map(|mask| format!("{mask:016x}"))
.unwrap_or_else(|_| "?".to_string())
}

fn blocked(proc_info: RefCell<ProcessInformation>) -> String {
proc_info
.borrow_mut()
.signals_blocked_mask()
.map(|mask| format!("{mask:016x}"))
.unwrap_or_else(|_| "?".to_string())
}

fn ignored(proc_info: RefCell<ProcessInformation>) -> String {
proc_info
.borrow_mut()
.signals_ignored_mask()
.map(|mask| format!("{mask:016x}"))
.unwrap_or_else(|_| "?".to_string())
}

fn caught(proc_info: RefCell<ProcessInformation>) -> String {
proc_info
.borrow_mut()
.signals_caught_mask()
.map(|mask| format!("{mask:016x}"))
.unwrap_or_else(|_| "?".to_string())
}

#[test]
fn test_time() {
let formatted = {
Expand Down
Loading