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: fix the fail to use only least significant bits to identify signal with -l #7225

Merged
merged 6 commits into from
Jan 28, 2025
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
18 changes: 17 additions & 1 deletion src/uu/kill/src/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ use uucore::{format_usage, help_about, help_usage, show};
static ABOUT: &str = help_about!("kill.md");
const USAGE: &str = help_usage!("kill.md");

// When the -l option is selected, the program displays the type of signal related to a certain
// value or string. In case of a value, the program should control the lower 8 bits, but there is
// a particular case in which if the value is in range [128, 159], it is translated to a signal
const OFFSET: usize = 128;

pub mod options {
pub static PIDS_OR_SIGNALS: &str = "pids_or_signals";
pub static LIST: &str = "list";
Expand Down Expand Up @@ -164,13 +169,24 @@ fn table() {
}

fn print_signal(signal_name_or_value: &str) -> UResult<()> {
// Closure used to track the last 8 bits of the signal value
// when the -l option is passed only the lower 8 bits are important
// or the value is in range [128, 159]
// Example: kill -l 143 => TERM because 143 = 15 + 128
// Example: kill -l 2304 => EXIT
let lower_8_bits = |x: usize| x & 0xff;
let option_num_parse = signal_name_or_value.parse::<usize>().ok();

for (value, &signal) in ALL_SIGNALS.iter().enumerate() {
if signal.eq_ignore_ascii_case(signal_name_or_value)
|| format!("SIG{signal}").eq_ignore_ascii_case(signal_name_or_value)
{
println!("{value}");
return Ok(());
} else if signal_name_or_value == value.to_string() {
} else if signal_name_or_value == value.to_string()
|| option_num_parse.is_some_and(|signal_value| lower_8_bits(signal_value) == value)
|| option_num_parse.is_some_and(|signal_value| signal_value == value + OFFSET)
{
println!("{signal}");
return Ok(());
}
Expand Down
33 changes: 33 additions & 0 deletions tests/by-util/test_kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,39 @@ fn test_kill_with_signal_and_list() {
.fails();
}

#[test]
fn test_kill_with_list_lower_bits() {
new_ucmd!()
.arg("-l")
.arg("128")
.succeeds()
.stdout_contains("EXIT");

new_ucmd!()
.arg("-l")
.arg("143")
.succeeds()
.stdout_contains("TERM");

new_ucmd!()
.arg("-l")
.arg("256")
.succeeds()
.stdout_contains("EXIT");

new_ucmd!()
.arg("-l")
.arg("2304")
.succeeds()
.stdout_contains("EXIT");
}

#[test]
fn test_kill_with_list_lower_bits_unrecognized() {
new_ucmd!().arg("-l").arg("111").fails();
new_ucmd!().arg("-l").arg("384").fails();
}

#[test]
fn test_kill_with_signal_and_table() {
let target = Target::new();
Expand Down
1 change: 0 additions & 1 deletion util/why-error.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ This file documents why some tests are failing:
* gnu/tests/ls/stat-free-symlinks.sh
* gnu/tests/misc/close-stdout.sh
* gnu/tests/misc/comm.pl
* gnu/tests/misc/kill.sh - https://github.com/uutils/coreutils/issues/7218
* gnu/tests/misc/nohup.sh
* gnu/tests/misc/numfmt.pl - https://github.com/uutils/coreutils/issues/7219 / https://github.com/uutils/coreutils/issues/7221
* gnu/tests/misc/stdbuf.sh - https://github.com/uutils/coreutils/issues/7072
Expand Down
Loading