Skip to content

Commit aea9c33

Browse files
leogrekoops
authored andcommitted
refactor(ctl): use rustix::process for kill and process-alive checks
Co-authored-by: Leonardo Di Giovanna <leonardodigiovanna1@gmail.com> Signed-off-by: Leonardo Grasso <me@leonardograsso.com>
1 parent 966ac7b commit aea9c33

4 files changed

Lines changed: 21 additions & 14 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/premptictl/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ serde_json = "1"
1414

1515
[target.'cfg(unix)'.dependencies]
1616
libc = "0.2"
17+
# `process` enables `kill_process` / `test_kill_process` (safe wrappers
18+
# around `kill(pid, sig)` and `kill(pid, 0)`).
19+
rustix = { version = "1", features = ["process"] }
1720

1821
[target.'cfg(windows)'.dependencies]
1922
uds_windows = "1.2"

tools/premptictl/src/daemon/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,15 @@ fn spawn_signal_watcher(
333333
#[cfg(unix)]
334334
#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
335335
pub(crate) fn process_alive(pid: u32) -> bool {
336-
let rc = unsafe { libc::kill(pid as i32, 0) };
337-
if rc == 0 {
338-
return true;
336+
let Some(pid) = rustix::process::Pid::from_raw(pid as i32) else {
337+
return false;
338+
};
339+
match rustix::process::test_kill_process(pid) {
340+
Ok(()) => true,
341+
// ESRCH = no such process. Anything else (e.g. EPERM: process exists
342+
// but we can't signal it) still counts as "alive".
343+
Err(e) => e != rustix::io::Errno::SRCH,
339344
}
340-
let err = std::io::Error::last_os_error();
341-
err.raw_os_error() != Some(libc::ESRCH)
342345
}
343346

344347
#[cfg(windows)]

tools/premptictl/src/daemon/stop.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ pub fn graceful_stop(child: &mut Child, timeout: Duration) -> io::Result<ExitSta
2424

2525
#[cfg(unix)]
2626
fn request_graceful_stop(child: &mut Child) -> io::Result<()> {
27-
let pid = child.id() as i32;
28-
let rc = unsafe { libc::kill(pid, libc::SIGTERM) };
29-
if rc != 0 {
30-
let err = io::Error::last_os_error();
27+
let Some(pid) = rustix::process::Pid::from_raw(child.id() as i32) else {
28+
// PID 0 / invalid: a spawned Child should never have this; treat as
29+
// "already gone" for safety.
30+
return Ok(());
31+
};
32+
match rustix::process::kill_process(pid, rustix::process::Signal::TERM) {
33+
Ok(()) => Ok(()),
3134
// ESRCH means the process is already gone; that's fine.
32-
if err.raw_os_error() == Some(libc::ESRCH) {
33-
return Ok(());
34-
}
35-
return Err(err);
35+
Err(e) if e == rustix::io::Errno::SRCH => Ok(()),
36+
Err(e) => Err(e.into()),
3637
}
37-
Ok(())
3838
}
3939

4040
#[cfg(windows)]

0 commit comments

Comments
 (0)