Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ feat_common_core = [
"dmesg",
"fsfreeze",
"hexdump",
"kill",
"last",
"lscpu",
"lsipc",
Expand Down Expand Up @@ -105,6 +106,7 @@ chcpu = { optional = true, version = "0.0.1", package = "uu_chcpu", path = "src/
ctrlaltdel = { optional = true, version = "0.0.1", package = "uu_ctrlaltdel", path = "src/uu/ctrlaltdel" }
dmesg = { optional = true, version = "0.0.1", package = "uu_dmesg", path = "src/uu/dmesg" }
fsfreeze = { optional = true, version = "0.0.1", package = "uu_fsfreeze", path = "src/uu/fsfreeze" }
kill = { optional = true, version = "0.0.1", package = "uu_kill", path = "src/uu/kill" }
hexdump = { optional = true, version = "0.0.1", package = "uu_hexdump", path = "src/uu/hexdump" }
last = { optional = true, version = "0.0.1", package = "uu_last", path = "src/uu/last" }
lscpu = { optional = true, version = "0.0.1", package = "uu_lscpu", path = "src/uu/lscpu" }
Expand Down
15 changes: 15 additions & 0 deletions src/uu/kill/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "uu_kill"
version = "0.0.1"
edition = "2024"

[lib]
path = "src/kill.rs"

[[bin]]
name = "kill"
path = "src/main.rs"

[dependencies]
uucore = { workspace = true, features = ["entries"] }
clap = { workspace = true }
7 changes: 7 additions & 0 deletions src/uu/kill/kill.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# lsns

```
kill [OPTION]...
```

Send a signal to a job.
41 changes: 41 additions & 0 deletions src/uu/kill/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// This file is part of the uutils util-linux package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use std::error::Error;

use uucore::error::UError;

#[derive(Debug)]
pub enum KillError {
#[cfg(not(target_os = "linux"))]
UnsupportedPlatform,
OperationNotPermitted(i32),
NoSuchProcess(i32),
}

impl std::fmt::Display for KillError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
#[cfg(not(target_os = "linux"))]
Self::UnsupportedPlatform => write!(f, "kill is only supported on Linux for now"),
Self::OperationNotPermitted(pid) => {
write!(f, "bash: kill: ({pid}) - Operation not permitted")
}
Self::NoSuchProcess(pid) => write!(f, "bash: kill: ({pid}) - No such process"),
}
}
}

impl UError for KillError {
fn code(&self) -> i32 {
1
}

fn usage(&self) -> bool {
false
}
}

impl Error for KillError {}
69 changes: 69 additions & 0 deletions src/uu/kill/src/kill.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// This file is part of the uutils util-linux package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

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

mod errors;

use crate::errors::KillError;
#[cfg(target_os = "linux")]
use crate::errors::KillError::{NoSuchProcess, OperationNotPermitted};
use clap::{Arg, ArgAction, Command, crate_version, value_parser};
use uucore::libc;
use uucore::{error::UResult, format_usage, help_about, help_usage};

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

#[cfg(not(target_os = "linux"))]
fn kill(_pid: i32, _signal: i32) -> Result<(), KillError> {
Err(KillError::UnsupportedPlatform)
}

#[cfg(target_os = "linux")]
fn kill(pid: i32, signal: i32) -> Result<(), KillError> {
unsafe { libc::kill(pid, signal) };

let err = std::io::Error::last_os_error().raw_os_error();
if let Some(err_no) = err {
match err_no {
libc::EPERM => return Err(OperationNotPermitted(pid)),
libc::ESRCH => return Err(NoSuchProcess(pid)),
_ => {}
}
}

Ok(())
}

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

if let Some(pids) = matches.get_many::<i32>("pid") {
for pid in pids {
kill(*pid, libc::SIGTERM)?;
}
}

Ok(())
}

pub fn uu_app() -> Command {
Command::new(uucore::util_name())
.version(crate_version!())
.about(ABOUT)
.override_usage(format_usage(USAGE))
.infer_long_args(true)
.arg(
Arg::new("pid")
.help("PID of the process to kill")
.required(true)
.action(ArgAction::Append)
.value_name("PID")
.value_parser(value_parser!(i32)),
)
}
1 change: 1 addition & 0 deletions src/uu/kill/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uucore::bin!(uu_kill);
37 changes: 37 additions & 0 deletions tests/by-util/test_kill.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// This file is part of the uutils util-linux package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use uutests::new_ucmd;

#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}

#[test]
#[cfg(target_os = "linux")]
fn test_non_numerical_pid() {
let res = new_ucmd!().arg("xyz").run();

let stdout = res.stdout_str();
let stderr = res.stderr_str();

assert!(stdout.trim().is_empty());
assert!(stderr.contains("invalid value 'xyz'"));
}

#[test]
#[cfg(target_os = "linux")]
fn test_pid_doesnt_exist() {
let non_existent_pid = "1234567890";
let res = new_ucmd!().arg(non_existent_pid).run();

let stdout = res.stdout_str();
let stderr = res.stderr_str();
let error_msg = format!("bash: kill: ({non_existent_pid}) - No such process");

assert!(stdout.trim().is_empty());
assert!(stderr.contains(error_msg.as_str()));
}
5 changes: 5 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ fn init() {
std::env::set_var("UUTESTS_BINARY_PATH", TESTS_BINARY);
}
}

#[cfg(feature = "kill")]
#[path = "by-util/test_kill.rs"]
mod test_kill;

#[cfg(feature = "lscpu")]
#[path = "by-util/test_lscpu.rs"]
mod test_lscpu;
Expand Down
Loading