kill: use only least significant bits to identify signal with -l (#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>
This commit is contained in:
Tommaso Fellegara 2025-01-28 10:21:19 +01:00 committed by GitHub
parent 5e81358c4a
commit 1595b6afaa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 50 additions and 2 deletions

View file

@ -17,6 +17,11 @@ use uucore::{format_usage, help_about, help_usage, show};
static ABOUT: &str = help_about!("kill.md"); static ABOUT: &str = help_about!("kill.md");
const USAGE: &str = help_usage!("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 mod options {
pub static PIDS_OR_SIGNALS: &str = "pids_or_signals"; pub static PIDS_OR_SIGNALS: &str = "pids_or_signals";
pub static LIST: &str = "list"; pub static LIST: &str = "list";
@ -164,13 +169,24 @@ fn table() {
} }
fn print_signal(signal_name_or_value: &str) -> UResult<()> { 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() { for (value, &signal) in ALL_SIGNALS.iter().enumerate() {
if signal.eq_ignore_ascii_case(signal_name_or_value) if signal.eq_ignore_ascii_case(signal_name_or_value)
|| format!("SIG{signal}").eq_ignore_ascii_case(signal_name_or_value) || format!("SIG{signal}").eq_ignore_ascii_case(signal_name_or_value)
{ {
println!("{value}"); println!("{value}");
return Ok(()); 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}"); println!("{signal}");
return Ok(()); return Ok(());
} }

View file

@ -334,6 +334,39 @@ fn test_kill_with_signal_and_list() {
.fails(); .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] #[test]
fn test_kill_with_signal_and_table() { fn test_kill_with_signal_and_table() {
let target = Target::new(); let target = Target::new();

View file

@ -27,7 +27,6 @@ This file documents why some tests are failing:
* gnu/tests/ls/stat-free-symlinks.sh * gnu/tests/ls/stat-free-symlinks.sh
* gnu/tests/misc/close-stdout.sh * gnu/tests/misc/close-stdout.sh
* gnu/tests/misc/comm.pl * 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/nohup.sh
* gnu/tests/misc/numfmt.pl - https://github.com/uutils/coreutils/issues/7219 / https://github.com/uutils/coreutils/issues/7221 * 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 * gnu/tests/misc/stdbuf.sh - https://github.com/uutils/coreutils/issues/7072