Skip to content

Commit 89d00aa

Browse files
committed
Add return value handling and some tests
Signed-off-by: Malhar Vora <mlvora.2010@gmail.com>
1 parent 5c7a6c5 commit 89d00aa

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/uu/kill/src/kill.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,26 @@ use uucore::{error::UResult, format_usage, help_about, help_usage};
1414
const ABOUT: &str = help_about!("kill.md");
1515
const USAGE: &str = help_usage!("kill.md");
1616

17-
1817
#[uucore::main]
1918
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
2019
let matches = uu_app().try_get_matches_from(args)?;
2120

2221
if let Some(pids) = matches.get_many::<i32>("pid") {
2322
for pid in pids {
2423
unsafe { libc::kill(*pid, libc::SIGTERM) };
24+
25+
let err = std::io::Error::last_os_error().raw_os_error();
26+
if let Some(err_no) = err {
27+
match err_no {
28+
libc::EPERM => {
29+
eprintln!("bash: kill: ({pid}) - Operation not permitted");
30+
}
31+
libc::ESRCH => {
32+
eprintln!("bash: kill: ({pid}) - No such process");
33+
}
34+
_ => {}
35+
}
36+
}
2537
}
2638
}
2739

tests/by-util/test_kill.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn test_invalid_arg() {
1212

1313
#[test]
1414
#[cfg(target_os = "linux")]
15-
fn test_non_number_pid() {
15+
fn test_non_numerical_pid() {
1616
let res = new_ucmd!().arg("xyz").run();
1717

1818
let stdout = res.stdout_str();
@@ -21,3 +21,17 @@ fn test_non_number_pid() {
2121
assert!(stdout.trim().len() == 0);
2222
assert!(stderr.contains("invalid value 'xyz'"));
2323
}
24+
25+
#[test]
26+
#[cfg(target_os = "linux")]
27+
fn test_pid_doesnt_exist() {
28+
let non_existent_pid = "1234567890";
29+
let res = new_ucmd!().arg(non_existent_pid).run();
30+
31+
let stdout = res.stdout_str();
32+
let stderr = res.stderr_str();
33+
let error_msg = format!("bash: kill: ({non_existent_pid}) - No such process");
34+
35+
assert!(stdout.trim().len() == 0);
36+
assert!(stderr.contains(error_msg.as_str()));
37+
}

0 commit comments

Comments
 (0)