Skip to content

Commit 3be01aa

Browse files
committed
Add error handling and some other improvements
Signed-off-by: Malhar Vora <mlvora.2010@gmail.com>
1 parent 41c2ffa commit 3be01aa

2 files changed

Lines changed: 69 additions & 15 deletions

File tree

src/uu/kill/src/errors.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 std::error::Error;
7+
8+
use uucore::error::UError;
9+
10+
#[derive(Debug)]
11+
pub enum KillError {
12+
/// Unsupported platform
13+
#[cfg(not(target_os = "linux"))]
14+
UnsupportedPlatform,
15+
OperationNotPermitted(i32),
16+
NoSuchProcess(i32),
17+
}
18+
19+
impl std::fmt::Display for KillError {
20+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21+
match self {
22+
#[cfg(not(target_os = "linux"))]
23+
Self::UnsupportedPlatform => write!(f, "kill is only supported on Linux for now"),
24+
Self::OperationNotPermitted(pid) => {
25+
write!(f, "bash: kill: ({pid}) - Operation not permitted")
26+
}
27+
Self::NoSuchProcess(pid) => write!(f, "bash: kill: ({pid}) - No such process"),
28+
}
29+
}
30+
}
31+
32+
impl UError for KillError {
33+
fn code(&self) -> i32 {
34+
1
35+
}
36+
37+
fn usage(&self) -> bool {
38+
false
39+
}
40+
}
41+
42+
impl Error for KillError {}

src/uu/kill/src/kill.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,46 @@
44
// file that was distributed with this source code.
55

66
// Remove this if the tool is ported to Non-UNIX platforms.
7+
#![cfg_attr(not(target_os = "linux"), allow(dead_code))]
78

9+
mod errors;
10+
11+
use crate::errors::KillError::{self, NoSuchProcess, OperationNotPermitted};
812
use clap::{Arg, ArgAction, Command, crate_version, value_parser};
913
use uucore::libc;
1014
use uucore::{error::UResult, format_usage, help_about, help_usage};
1115

1216
const ABOUT: &str = help_about!("kill.md");
1317
const USAGE: &str = help_usage!("kill.md");
1418

19+
#[cfg(not(target_os = "linux"))]
20+
fn kill(pid: i32, signal: i32) -> Result<(), KillError> {
21+
Err(UnsupportedPlatform)
22+
}
23+
24+
#[cfg(target_os = "linux")]
25+
fn kill(pid: i32, signal: i32) -> Result<(), KillError> {
26+
unsafe { libc::kill(pid, signal) };
27+
28+
let err = std::io::Error::last_os_error().raw_os_error();
29+
if let Some(err_no) = err {
30+
match err_no {
31+
libc::EPERM => return Err(OperationNotPermitted(pid)),
32+
libc::ESRCH => return Err(NoSuchProcess(pid)),
33+
_ => {}
34+
}
35+
}
36+
37+
Ok(())
38+
}
39+
1540
#[uucore::main]
1641
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
1742
let matches = uu_app().try_get_matches_from(args)?;
1843

1944
if let Some(pids) = matches.get_many::<i32>("pid") {
2045
for pid in pids {
21-
unsafe { libc::kill(*pid, libc::SIGTERM) };
22-
23-
let err = std::io::Error::last_os_error().raw_os_error();
24-
if let Some(err_no) = err {
25-
match err_no {
26-
libc::EPERM => {
27-
eprintln!("bash: kill: ({pid}) - Operation not permitted");
28-
}
29-
libc::ESRCH => {
30-
eprintln!("bash: kill: ({pid}) - No such process");
31-
}
32-
_ => {}
33-
}
34-
}
46+
kill(*pid, libc::SIGTERM)?;
3547
}
3648
}
3749

@@ -47,7 +59,7 @@ pub fn uu_app() -> Command {
4759
.arg(
4860
Arg::new("pid")
4961
.help("PID of the process to kill")
50-
.required(false)
62+
.required(true)
5163
.action(ArgAction::Append)
5264
.value_name("PID")
5365
.value_parser(value_parser!(i32)),

0 commit comments

Comments
 (0)