Skip to content

Commit 5c7a6c5

Browse files
committed
Implement basic functional kill command with some tests
Signed-off-by: Malhar Vora <mlvora.2010@gmail.com>
1 parent 78d267d commit 5c7a6c5

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

src/uu/kill/src/kill.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,25 @@
55

66
// Remove this if the tool is ported to Non-UNIX platforms.
77

8-
use clap::{Command, crate_version};
8+
use clap::{Arg, ArgAction, Command, crate_version, value_parser};
9+
use uucore::libc;
910
#[cfg(target_os = "linux")]
1011
#[cfg(target_os = "linux")]
1112
use uucore::{error::UResult, format_usage, help_about, help_usage};
1213

1314
const ABOUT: &str = help_about!("kill.md");
1415
const USAGE: &str = help_usage!("kill.md");
1516

17+
1618
#[uucore::main]
1719
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
18-
let _matches = uu_app().try_get_matches_from(args)?;
20+
let matches = uu_app().try_get_matches_from(args)?;
21+
22+
if let Some(pids) = matches.get_many::<i32>("pid") {
23+
for pid in pids {
24+
unsafe { libc::kill(*pid, libc::SIGTERM) };
25+
}
26+
}
1927

2028
Ok(())
2129
}
@@ -26,4 +34,12 @@ pub fn uu_app() -> Command {
2634
.about(ABOUT)
2735
.override_usage(format_usage(USAGE))
2836
.infer_long_args(true)
37+
.arg(
38+
Arg::new("pid")
39+
.help("PID of the process to kill")
40+
.required(false)
41+
.action(ArgAction::Append)
42+
.value_name("PID")
43+
.value_parser(value_parser!(i32)),
44+
)
2945
}

tests/by-util/test_kill.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This file is part of the uutils util-linux package.
2+
//
3+
// For the full copyright and license information, please view the LICENSE
4+
// file that was distributed with this source code.
5+
6+
use uutests::new_ucmd;
7+
8+
#[test]
9+
fn test_invalid_arg() {
10+
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
11+
}
12+
13+
#[test]
14+
#[cfg(target_os = "linux")]
15+
fn test_non_number_pid() {
16+
let res = new_ucmd!().arg("xyz").run();
17+
18+
let stdout = res.stdout_str();
19+
let stderr = res.stderr_str();
20+
21+
assert!(stdout.trim().len() == 0);
22+
assert!(stderr.contains("invalid value 'xyz'"));
23+
}

tests/tests.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ fn init() {
1515
std::env::set_var("UUTESTS_BINARY_PATH", TESTS_BINARY);
1616
}
1717
}
18+
19+
#[cfg(feature = "kill")]
20+
#[path = "by-util/test_kill.rs"]
21+
mod test_kill;
22+
1823
#[cfg(feature = "lscpu")]
1924
#[path = "by-util/test_lscpu.rs"]
2025
mod test_lscpu;

0 commit comments

Comments
 (0)