Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kill: adding support for handling SIGEXIT #6269

Merged
merged 4 commits into from
Apr 27, 2024
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
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 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 @@
} 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") {
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please add a comment, it isn't super obvious :)

Copy link
Contributor Author

@dcarrier dcarrier Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, very good point! Added.

None
} else {
let sig = (sig as i32)
.try_into()
.map_err(|e| std::io::Error::from_raw_os_error(e as i32))?;

Check warning on line 72 in src/uu/kill/src/kill.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/kill/src/kill.rs#L72

Added line #L72 was not covered by tests
Some(sig)
};

let pids = parse_pids(&pids_or_signals)?;
if pids.is_empty() {
Err(USimpleError::new(
Expand Down Expand Up @@ -211,7 +221,7 @@
.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();
}
Loading