pgrep, pkill: Move signal to settings

This commit is contained in:
Tuomas Tynkkynen
2025-02-10 22:43:18 +02:00
parent bd6e1f25bd
commit 330d65bcae
2 changed files with 8 additions and 12 deletions
+3 -5
View File
@@ -32,6 +32,7 @@ struct Settings {
parent: Option<Vec<u64>>,
runstates: Option<String>,
terminal: Option<HashSet<Teletype>>,
signal: usize,
}
fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
@@ -56,6 +57,7 @@ fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
.flat_map(Teletype::try_from)
.collect::<HashSet<_>>()
}),
signal: parse_signal_value(matches.get_one::<String>("signal").unwrap())?,
};
if (!settings.newest
@@ -91,10 +93,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args)?;
let settings = get_match_settings(&matches)?;
// Parse signal
#[cfg(unix)]
let sig_num = parse_signal_value(matches.get_one::<String>("signal").unwrap())?;
// Collect pids
let pids = {
let mut pids = collect_matched_pids(&settings);
@@ -103,7 +101,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
pids.retain(|pid| {
let mask =
u64::from_str_radix(pid.clone().status().get("SigCgt").unwrap(), 16).unwrap();
mask & (1 << sig_num) != 0
mask & (1 << settings.signal) != 0
});
}
if pids.is_empty() {
+5 -7
View File
@@ -43,6 +43,7 @@ struct Settings {
parent: Option<Vec<u64>>,
runstates: Option<String>,
terminal: Option<HashSet<Teletype>>,
signal: usize,
}
fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
@@ -67,6 +68,7 @@ fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
.flat_map(Teletype::try_from)
.collect::<HashSet<_>>()
}),
signal: parse_signal_value(matches.get_one::<String>("signal").unwrap())?,
};
if (!settings.newest
@@ -97,19 +99,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(&args)?;
let settings = get_match_settings(&matches)?;
// Parse signal
#[cfg(unix)]
let sig_num = parse_signal_value(matches.get_one::<String>("signal").unwrap())?;
#[cfg(unix)]
let sig_name = signal_name_by_value(sig_num);
let sig_name = signal_name_by_value(settings.signal);
// Signal does not support converting from EXIT
// Instead, nix::signal::kill expects Option::None to properly handle EXIT
#[cfg(unix)]
let sig: Option<Signal> = if sig_name.is_some_and(|name| name == "EXIT") {
None
} else {
let sig = (sig_num as i32)
let sig = (settings.signal as i32)
.try_into()
.map_err(|e| std::io::Error::from_raw_os_error(e as i32))?;
Some(sig)
@@ -123,7 +121,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
pids.retain(|pid| {
let mask =
u64::from_str_radix(pid.clone().status().get("SigCgt").unwrap(), 16).unwrap();
mask & (1 << sig_num) != 0
mask & (1 << settings.signal) != 0
});
}
if pids.is_empty() {