Skip to content

Commit 1595b6a

Browse files
Felle33sylvestre
andauthored
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 <[email protected]>
1 parent 5e81358 commit 1595b6a

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

src/uu/kill/src/kill.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ use uucore::{format_usage, help_about, help_usage, show};
1717
static ABOUT: &str = help_about!("kill.md");
1818
const USAGE: &str = help_usage!("kill.md");
1919

20+
// When the -l option is selected, the program displays the type of signal related to a certain
21+
// value or string. In case of a value, the program should control the lower 8 bits, but there is
22+
// a particular case in which if the value is in range [128, 159], it is translated to a signal
23+
const OFFSET: usize = 128;
24+
2025
pub mod options {
2126
pub static PIDS_OR_SIGNALS: &str = "pids_or_signals";
2227
pub static LIST: &str = "list";
@@ -164,13 +169,24 @@ fn table() {
164169
}
165170

166171
fn print_signal(signal_name_or_value: &str) -> UResult<()> {
172+
// Closure used to track the last 8 bits of the signal value
173+
// when the -l option is passed only the lower 8 bits are important
174+
// or the value is in range [128, 159]
175+
// Example: kill -l 143 => TERM because 143 = 15 + 128
176+
// Example: kill -l 2304 => EXIT
177+
let lower_8_bits = |x: usize| x & 0xff;
178+
let option_num_parse = signal_name_or_value.parse::<usize>().ok();
179+
167180
for (value, &signal) in ALL_SIGNALS.iter().enumerate() {
168181
if signal.eq_ignore_ascii_case(signal_name_or_value)
169182
|| format!("SIG{signal}").eq_ignore_ascii_case(signal_name_or_value)
170183
{
171184
println!("{value}");
172185
return Ok(());
173-
} else if signal_name_or_value == value.to_string() {
186+
} else if signal_name_or_value == value.to_string()
187+
|| option_num_parse.is_some_and(|signal_value| lower_8_bits(signal_value) == value)
188+
|| option_num_parse.is_some_and(|signal_value| signal_value == value + OFFSET)
189+
{
174190
println!("{signal}");
175191
return Ok(());
176192
}

tests/by-util/test_kill.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,39 @@ fn test_kill_with_signal_and_list() {
334334
.fails();
335335
}
336336

337+
#[test]
338+
fn test_kill_with_list_lower_bits() {
339+
new_ucmd!()
340+
.arg("-l")
341+
.arg("128")
342+
.succeeds()
343+
.stdout_contains("EXIT");
344+
345+
new_ucmd!()
346+
.arg("-l")
347+
.arg("143")
348+
.succeeds()
349+
.stdout_contains("TERM");
350+
351+
new_ucmd!()
352+
.arg("-l")
353+
.arg("256")
354+
.succeeds()
355+
.stdout_contains("EXIT");
356+
357+
new_ucmd!()
358+
.arg("-l")
359+
.arg("2304")
360+
.succeeds()
361+
.stdout_contains("EXIT");
362+
}
363+
364+
#[test]
365+
fn test_kill_with_list_lower_bits_unrecognized() {
366+
new_ucmd!().arg("-l").arg("111").fails();
367+
new_ucmd!().arg("-l").arg("384").fails();
368+
}
369+
337370
#[test]
338371
fn test_kill_with_signal_and_table() {
339372
let target = Target::new();

util/why-error.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ This file documents why some tests are failing:
2727
* gnu/tests/ls/stat-free-symlinks.sh
2828
* gnu/tests/misc/close-stdout.sh
2929
* gnu/tests/misc/comm.pl
30-
* gnu/tests/misc/kill.sh - https://github.com/uutils/coreutils/issues/7218
3130
* gnu/tests/misc/nohup.sh
3231
* gnu/tests/misc/numfmt.pl - https://github.com/uutils/coreutils/issues/7219 / https://github.com/uutils/coreutils/issues/7221
3332
* gnu/tests/misc/stdbuf.sh - https://github.com/uutils/coreutils/issues/7072

0 commit comments

Comments
 (0)