Skip to content

Commit a7cf757

Browse files
tertsdiepraamsylvestre
authored andcommitted
kill: kill process group with negative id
1 parent e1d50da commit a7cf757

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

Cargo.lock

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/kill/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ path = "src/kill.rs"
1616

1717
[dependencies]
1818
clap = { version = "3.1", features = ["wrap_help", "cargo"] }
19-
libc = "0.2.125"
19+
nix = { version = "0.24.1", features = ["signal"] }
2020
uucore = { version=">=0.0.11", package="uucore", path="../../uucore", features=["signals"] }
2121

2222
[[bin]]

src/uu/kill/src/kill.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
// * For the full copyright and license information, please view the LICENSE file
66
// * that was distributed with this source code.
77

8-
// spell-checker:ignore (ToDO) signalname pids
8+
// spell-checker:ignore (ToDO) signalname pids killpg
99

1010
#[macro_use]
1111
extern crate uucore;
1212

1313
use clap::{crate_version, Arg, Command};
14-
use libc::{c_int, pid_t};
14+
use nix::sys::signal::{self, Signal};
15+
use nix::unistd::Pid;
1516
use std::io::Error;
1617
use uucore::display::Quotable;
17-
use uucore::error::{UResult, USimpleError};
18+
use uucore::error::{FromIo, UError, UResult, USimpleError};
1819
use uucore::signals::{signal_by_name_or_value, ALL_SIGNALS};
1920
use uucore::{format_usage, InvalidEncodingHandling};
2021

@@ -67,8 +68,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
6768
} else {
6869
15_usize //SIGTERM
6970
};
71+
let sig: Signal = (sig as i32)
72+
.try_into()
73+
.map_err(|e| std::io::Error::from_raw_os_error(e as i32))?;
7074
let pids = parse_pids(&pids_or_signals)?;
71-
kill(sig, &pids)
75+
kill(sig, &pids);
76+
Ok(())
7277
}
7378
Mode::Table => {
7479
table();
@@ -84,6 +89,7 @@ pub fn uu_app<'a>() -> Command<'a> {
8489
.about(ABOUT)
8590
.override_usage(format_usage(USAGE))
8691
.infer_long_args(true)
92+
.allow_negative_numbers(true)
8793
.arg(
8894
Arg::new(options::LIST)
8995
.short('l')
@@ -109,8 +115,7 @@ pub fn uu_app<'a>() -> Command<'a> {
109115
.arg(
110116
Arg::new(options::PIDS_OR_SIGNALS)
111117
.hide(true)
112-
.multiple_occurrences(true)
113-
.allow_hyphen_values(true)
118+
.multiple_occurrences(true),
114119
)
115120
}
116121

@@ -191,21 +196,21 @@ fn parse_signal_value(signal_name: &str) -> UResult<usize> {
191196
}
192197
}
193198

194-
fn parse_pids(pids: &[String]) -> UResult<Vec<usize>> {
199+
fn parse_pids(pids: &[String]) -> UResult<Vec<i32>> {
195200
pids.iter()
196201
.map(|x| {
197-
x.parse::<usize>().map_err(|e| {
202+
x.parse::<i32>().map_err(|e| {
198203
USimpleError::new(1, format!("failed to parse argument {}: {}", x.quote(), e))
199204
})
200205
})
201206
.collect()
202207
}
203208

204-
fn kill(signal_value: usize, pids: &[usize]) -> UResult<()> {
209+
fn kill(sig: Signal, pids: &[i32]) {
205210
for &pid in pids {
206-
if unsafe { libc::kill(pid as pid_t, signal_value as c_int) } != 0 {
207-
show!(USimpleError::new(1, format!("{}", Error::last_os_error())));
211+
if let Err(e) = signal::kill(Pid::from_raw(pid), sig) {
212+
show!(Error::from_raw_os_error(e as i32)
213+
.map_err_context(|| format!("sending signal to {} failed", pid)));
208214
}
209215
}
210-
Ok(())
211216
}

0 commit comments

Comments
 (0)