Skip to content

Commit

Permalink
kill: use only least significant bits to identify signal with -l (uut…
Browse files Browse the repository at this point in the history
…ils#7225)

* kill: check the lower 5 bits when the input is a number

* test/kill: added testcase

* kill: check the last 7 bits

* kill: check only the last 8 bits and the signals in the range [128, 159]

---------

Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
  • Loading branch information
Felle33 and sylvestre authored Jan 28, 2025
1 parent 5e81358 commit 1595b6a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
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

0 comments on commit 1595b6a

Please sign in to comment.