From e1d50dae87036186e9b4e1f21042218e2eb813eb Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Fri, 6 May 2022 13:58:05 +0200 Subject: [PATCH 1/3] kill: fix typo --- src/uu/kill/src/kill.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/uu/kill/src/kill.rs b/src/uu/kill/src/kill.rs index df868e418..e77d0c666 100644 --- a/src/uu/kill/src/kill.rs +++ b/src/uu/kill/src/kill.rs @@ -22,7 +22,7 @@ static ABOUT: &str = "Send signal to processes or list information about signals const USAGE: &str = "{} [OPTIONS]... PID..."; pub mod options { - pub static PIDS_OR_SIGNALS: &str = "pids_of_signals"; + pub static PIDS_OR_SIGNALS: &str = "pids_or_signals"; pub static LIST: &str = "list"; pub static TABLE: &str = "table"; pub static TABLE_OLD: &str = "table_old"; @@ -109,7 +109,8 @@ pub fn uu_app<'a>() -> Command<'a> { .arg( Arg::new(options::PIDS_OR_SIGNALS) .hide(true) - .multiple_occurrences(true), + .multiple_occurrences(true) + .allow_hyphen_values(true) ) } From a7cf757127c38e9e8b4d9892419110f75c48d9da Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Fri, 6 May 2022 23:55:54 +0200 Subject: [PATCH 2/3] kill: kill process group with negative id --- Cargo.lock | 3 ++- src/uu/kill/Cargo.toml | 2 +- src/uu/kill/src/kill.rs | 29 +++++++++++++++++------------ 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1079f8a65..4c858b83f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1158,6 +1158,7 @@ dependencies = [ "bitflags", "cfg-if 1.0.0", "libc", + "memoffset", ] [[package]] @@ -2452,7 +2453,7 @@ name = "uu_kill" version = "0.0.13" dependencies = [ "clap 3.1.15", - "libc", + "nix", "uucore", ] diff --git a/src/uu/kill/Cargo.toml b/src/uu/kill/Cargo.toml index 24347a90a..4ab76deea 100644 --- a/src/uu/kill/Cargo.toml +++ b/src/uu/kill/Cargo.toml @@ -16,7 +16,7 @@ path = "src/kill.rs" [dependencies] clap = { version = "3.1", features = ["wrap_help", "cargo"] } -libc = "0.2.125" +nix = { version = "0.24.1", features = ["signal"] } uucore = { version=">=0.0.11", package="uucore", path="../../uucore", features=["signals"] } [[bin]] diff --git a/src/uu/kill/src/kill.rs b/src/uu/kill/src/kill.rs index e77d0c666..c966c7fac 100644 --- a/src/uu/kill/src/kill.rs +++ b/src/uu/kill/src/kill.rs @@ -5,16 +5,17 @@ // * For the full copyright and license information, please view the LICENSE file // * that was distributed with this source code. -// spell-checker:ignore (ToDO) signalname pids +// spell-checker:ignore (ToDO) signalname pids killpg #[macro_use] extern crate uucore; use clap::{crate_version, Arg, Command}; -use libc::{c_int, pid_t}; +use nix::sys::signal::{self, Signal}; +use nix::unistd::Pid; use std::io::Error; use uucore::display::Quotable; -use uucore::error::{UResult, USimpleError}; +use uucore::error::{FromIo, UError, UResult, USimpleError}; use uucore::signals::{signal_by_name_or_value, ALL_SIGNALS}; use uucore::{format_usage, InvalidEncodingHandling}; @@ -67,8 +68,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } else { 15_usize //SIGTERM }; + let sig: Signal = (sig as i32) + .try_into() + .map_err(|e| std::io::Error::from_raw_os_error(e as i32))?; let pids = parse_pids(&pids_or_signals)?; - kill(sig, &pids) + kill(sig, &pids); + Ok(()) } Mode::Table => { table(); @@ -84,6 +89,7 @@ pub fn uu_app<'a>() -> Command<'a> { .about(ABOUT) .override_usage(format_usage(USAGE)) .infer_long_args(true) + .allow_negative_numbers(true) .arg( Arg::new(options::LIST) .short('l') @@ -109,8 +115,7 @@ pub fn uu_app<'a>() -> Command<'a> { .arg( Arg::new(options::PIDS_OR_SIGNALS) .hide(true) - .multiple_occurrences(true) - .allow_hyphen_values(true) + .multiple_occurrences(true), ) } @@ -191,21 +196,21 @@ fn parse_signal_value(signal_name: &str) -> UResult { } } -fn parse_pids(pids: &[String]) -> UResult> { +fn parse_pids(pids: &[String]) -> UResult> { pids.iter() .map(|x| { - x.parse::().map_err(|e| { + x.parse::().map_err(|e| { USimpleError::new(1, format!("failed to parse argument {}: {}", x.quote(), e)) }) }) .collect() } -fn kill(signal_value: usize, pids: &[usize]) -> UResult<()> { +fn kill(sig: Signal, pids: &[i32]) { for &pid in pids { - if unsafe { libc::kill(pid as pid_t, signal_value as c_int) } != 0 { - show!(USimpleError::new(1, format!("{}", Error::last_os_error()))); + if let Err(e) = signal::kill(Pid::from_raw(pid), sig) { + show!(Error::from_raw_os_error(e as i32) + .map_err_context(|| format!("sending signal to {} failed", pid))); } } - Ok(()) } From 53c3efecd898d04623fb374ddfcdd19ac76197bd Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Sat, 7 May 2022 12:08:06 +0200 Subject: [PATCH 3/3] kill: remove table_old arg in favor of a short alias --- src/uu/kill/src/kill.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/uu/kill/src/kill.rs b/src/uu/kill/src/kill.rs index c966c7fac..5ae836818 100644 --- a/src/uu/kill/src/kill.rs +++ b/src/uu/kill/src/kill.rs @@ -26,7 +26,6 @@ pub mod options { pub static PIDS_OR_SIGNALS: &str = "pids_or_signals"; pub static LIST: &str = "list"; pub static TABLE: &str = "table"; - pub static TABLE_OLD: &str = "table_old"; pub static SIGNAL: &str = "signal"; } @@ -46,7 +45,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app().get_matches_from(args); - let mode = if matches.is_present(options::TABLE) || matches.is_present(options::TABLE_OLD) { + let mode = if matches.is_present(options::TABLE) { Mode::Table } else if matches.is_present(options::LIST) { Mode::List @@ -95,16 +94,15 @@ pub fn uu_app<'a>() -> Command<'a> { .short('l') .long(options::LIST) .help("Lists signals") - .conflicts_with(options::TABLE) - .conflicts_with(options::TABLE_OLD), + .conflicts_with(options::TABLE), ) .arg( Arg::new(options::TABLE) .short('t') + .short_alias('L') .long(options::TABLE) .help("Lists table of signals"), ) - .arg(Arg::new(options::TABLE_OLD).short('L').hide(true)) .arg( Arg::new(options::SIGNAL) .short('s')