Skip to content

Commit

Permalink
kill: adding support for handling SIGEXIT (#6269)
Browse files Browse the repository at this point in the history
kill: convert SIGEXT (0) to None so nix takes correct action
  • Loading branch information
dcarrier committed Apr 27, 2024
1 parent 65b25c7 commit 5ee9c69
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/uu/kill/src/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use nix::unistd::Pid;
use std::io::Error;
use uucore::display::Quotable;
use uucore::error::{FromIo, UResult, USimpleError};
use uucore::signals::{signal_by_name_or_value, ALL_SIGNALS};
use uucore::signals::{signal_by_name_or_value, signal_name_by_value, ALL_SIGNALS};
use uucore::{format_usage, help_about, help_usage, show};

static ABOUT: &str = help_about!("kill.md");
Expand Down Expand Up @@ -60,9 +60,19 @@ 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 sig_name = signal_name_by_value(sig);
// Signal does not support converting from EXIT
// Instead, nix::signal::kill expects Option::None to properly handle EXIT
let sig: Option<Signal> = if sig_name.is_some_and(|name| name == "EXIT") {
None
} else {
let sig = (sig as i32)
.try_into()
.map_err(|e| std::io::Error::from_raw_os_error(e as i32))?;
Some(sig)
};

let pids = parse_pids(&pids_or_signals)?;
if pids.is_empty() {
Err(USimpleError::new(
Expand Down Expand Up @@ -211,7 +221,7 @@ fn parse_pids(pids: &[String]) -> UResult<Vec<i32>> {
.collect()
}

fn kill(sig: Signal, pids: &[i32]) {
fn kill(sig: Option<Signal>, pids: &[i32]) {
for &pid in pids {
if let Err(e) = signal::kill(Pid::from_raw(pid), sig) {
show!(Error::from_raw_os_error(e as i32)
Expand Down
10 changes: 10 additions & 0 deletions tests/by-util/test_kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,13 @@ fn test_kill_no_pid_provided() {
.fails()
.stderr_contains("no process ID specified");
}

#[test]
fn test_kill_with_signal_exit_new_form() {
let target = Target::new();
new_ucmd!()
.arg("-s")
.arg("EXIT")
.arg(format!("{}", target.pid()))
.succeeds();
}

0 comments on commit 5ee9c69

Please sign in to comment.