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 2 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
8 changes: 7 additions & 1 deletion src/uu/kill/src/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,19 @@ fn table() {
}

fn print_signal(signal_name_or_value: &str) -> UResult<()> {
let lower_5_bits = |x: usize| x & 0b11111;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please document this magic number :)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is not entirely correct, because it recognizes, for example, 111 (= b1101111) as a TERM signal whereas GNU kill doesn't recognize it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes you are right. I have done some tests and the range of number accepted goes from 0 to 64 and from 128 to 192 (both endings included). It is like they are managing the same signals but mapped 2 times.
I noticed also that we don't manage the name of the signals from 32 to 64 (and therefore also from 160 to 192).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could add this feature in this pull request or should I open a new one?

Copy link
Contributor

Choose a reason for hiding this comment

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

I would create a new PR. There is also a ticket for it: #6218

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_5_bits(signal_value) == lower_5_bits(value))
{
println!("{signal}");
return Ok(());
}
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,15 @@ fn test_kill_with_signal_and_list() {
.fails();
}

#[test]
fn test_kill_with_list_lower_bits() {
new_ucmd!()
.arg("-l")
.arg("143")
.succeeds()
.stdout_matches(&Regex::new("TERM").unwrap());
Copy link
Contributor

Choose a reason for hiding this comment

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

While this works, I think using a regex is a bit overkill and you can simplify it by using either stdout_is or stdout_contains.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, I have just seen the function stdout_only, what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that's also fine.

}

#[test]
fn test_kill_with_signal_and_table() {
let target = Target::new();
Expand Down
Loading