diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2024-04-20 18:55:07 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-20 18:55:07 +0530 |
commit | c0f40ed81a1c932f804ba62e635249cd43c31273 (patch) | |
tree | a8ce324f17f819eb62fe93199664886b769d9c5f /ext/node/ops/process.rs | |
parent | 04c6785faecdef37b3d4eefb61c0a18ff1cbdec0 (diff) |
fix(ext/node): implement process.kill in Rust (#23130)
Closes https://github.com/denoland/deno/issues/23056
Diffstat (limited to 'ext/node/ops/process.rs')
-rw-r--r-- | ext/node/ops/process.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/ext/node/ops/process.rs b/ext/node/ops/process.rs new file mode 100644 index 000000000..0992c46c6 --- /dev/null +++ b/ext/node/ops/process.rs @@ -0,0 +1,64 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +use deno_core::error::AnyError; +use deno_core::op2; +use deno_core::OpState; +use deno_permissions::PermissionsContainer; + +#[cfg(unix)] +fn kill(pid: i32, sig: i32) -> i32 { + // SAFETY: FFI call to libc + if unsafe { libc::kill(pid, sig) } < 0 { + std::io::Error::last_os_error().raw_os_error().unwrap() + } else { + 0 + } +} + +#[cfg(not(unix))] +fn kill(pid: i32, _sig: i32) -> i32 { + use winapi::shared::minwindef::DWORD; + use winapi::shared::minwindef::FALSE; + use winapi::shared::minwindef::TRUE; + use winapi::um::errhandlingapi::GetLastError; + use winapi::um::processthreadsapi::GetCurrentProcess; + use winapi::um::processthreadsapi::OpenProcess; + use winapi::um::processthreadsapi::TerminateProcess; + use winapi::um::winnt::PROCESS_TERMINATE; + + // SAFETY: FFI call to winapi + unsafe { + let p_hnd = if pid == 0 { + GetCurrentProcess() + } else { + OpenProcess(PROCESS_TERMINATE, FALSE, pid as DWORD) + }; + + if p_hnd.is_null() { + return GetLastError() as i32; + } + + if TerminateProcess(p_hnd, 1) == TRUE { + return 0; + } + + GetLastError() as i32 + } +} + +#[op2(fast)] +pub fn op_node_process_kill( + state: &mut OpState, + #[smi] pid: i32, + #[smi] sig: i32, +) -> Result<i32, AnyError> { + state + .borrow_mut::<PermissionsContainer>() + .check_run_all("process.kill")?; + Ok(kill(pid, sig)) +} + +#[op2(fast)] +pub fn op_process_abort() { + std::process::abort(); +} |