kill: added uresult

Related to #2464
This commit is contained in:
353fc443 2021-07-13 09:49:39 +00:00
parent 425de97650
commit 7d94121b95
No known key found for this signature in database
GPG key ID: D58B14ED3D42A937

View file

@ -7,20 +7,21 @@
// spell-checker:ignore (ToDO) signalname pids
// clippy bug https://github.com/rust-lang/rust-clippy/issues/7422
#![allow(clippy::nonstandard_macro_braces)]
#[macro_use]
extern crate uucore;
use clap::{crate_version, App, Arg};
use libc::{c_int, pid_t};
use std::io::Error;
use uucore::error::{UResult, USimpleError};
use uucore::signals::ALL_SIGNALS;
use uucore::InvalidEncodingHandling;
static ABOUT: &str = "Send signal to processes or list information about signals.";
static EXIT_OK: i32 = 0;
static EXIT_ERR: i32 = 1;
pub mod options {
pub static PIDS_OR_SIGNALS: &str = "pids_of_signals";
pub static LIST: &str = "list";
@ -36,7 +37,8 @@ pub enum Mode {
List,
}
pub fn uumain(args: impl uucore::Args) -> i32 {
#[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args = args
.collect_str(InvalidEncodingHandling::Ignore)
.accept_any();
@ -69,10 +71,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
return kill(&sig, &pids_or_signals);
}
Mode::Table => table(),
Mode::List => list(pids_or_signals.get(0).cloned()),
Mode::List => list(pids_or_signals.get(0).cloned())?,
}
EXIT_OK
Ok(())
}
pub fn uu_app() -> App<'static, 'static> {
@ -142,20 +144,23 @@ fn table() {
println!()
}
fn print_signal(signal_name_or_value: &str) {
fn print_signal(signal_name_or_value: &str) -> UResult<()> {
for (value, &signal) in ALL_SIGNALS.iter().enumerate() {
if signal == signal_name_or_value || (format!("SIG{}", signal)) == signal_name_or_value {
println!("{}", value);
exit!(EXIT_OK as i32)
return Ok(());
} else if signal_name_or_value == value.to_string() {
println!("{}", signal);
exit!(EXIT_OK as i32)
return Ok(());
}
}
crash!(EXIT_ERR, "unknown signal name {}", signal_name_or_value)
Err(USimpleError::new(
1,
format!("unknown signal name {}", signal_name_or_value),
))
}
fn print_signals() {
fn print_signals() -> UResult<()> {
let mut pos = 0;
for (idx, signal) in ALL_SIGNALS.iter().enumerate() {
pos += signal.len();
@ -168,32 +173,41 @@ fn print_signals() {
print!(" ");
}
}
Ok(())
}
fn list(arg: Option<String>) {
fn list(arg: Option<String>) -> UResult<()> {
match arg {
Some(ref x) => print_signal(x),
None => print_signals(),
};
Some(ref x) => Ok(print_signal(x)?),
None => Ok(print_signals()?),
}
}
fn kill(signalname: &str, pids: &[String]) -> i32 {
let mut status = 0;
fn kill(signalname: &str, pids: &[String]) -> UResult<()> {
let optional_signal_value = uucore::signals::signal_by_name_or_value(signalname);
let signal_value = match optional_signal_value {
Some(x) => x,
None => crash!(EXIT_ERR, "unknown signal name {}", signalname),
None => {
return Err(USimpleError::new(
1,
format!("unknown signal name {}", signalname),
));
}
};
for pid in pids {
match pid.parse::<usize>() {
Ok(x) => {
if unsafe { libc::kill(x as pid_t, signal_value as c_int) } != 0 {
show_error!("{}", Error::last_os_error());
status = 1;
return Err(USimpleError::new(1, format!("{}", Error::last_os_error())));
}
}
Err(e) => crash!(EXIT_ERR, "failed to parse argument {}: {}", pid, e),
Err(e) => {
return Err(USimpleError::new(
1,
format!("failed to parse argument {}: {}", pid, e),
));
}
};
}
status
Ok(())
}