summaryrefslogtreecommitdiff
path: root/ext/node/ops
diff options
context:
space:
mode:
Diffstat (limited to 'ext/node/ops')
-rw-r--r--ext/node/ops/mod.rs1
-rw-r--r--ext/node/ops/os/mod.rs5
-rw-r--r--ext/node/ops/process.rs64
3 files changed, 65 insertions, 5 deletions
diff --git a/ext/node/ops/mod.rs b/ext/node/ops/mod.rs
index c14b63bf4..6381530dd 100644
--- a/ext/node/ops/mod.rs
+++ b/ext/node/ops/mod.rs
@@ -7,6 +7,7 @@ pub mod http2;
pub mod idna;
pub mod ipc;
pub mod os;
+pub mod process;
pub mod require;
pub mod util;
pub mod v8;
diff --git a/ext/node/ops/os/mod.rs b/ext/node/ops/os/mod.rs
index 1d3de797b..603f678e0 100644
--- a/ext/node/ops/os/mod.rs
+++ b/ext/node/ops/os/mod.rs
@@ -75,11 +75,6 @@ where
Ok(euid)
}
-#[op2(fast)]
-pub fn op_process_abort() {
- std::process::abort();
-}
-
#[op2]
#[serde]
pub fn op_cpus<P>(state: &mut OpState) -> Result<Vec<cpus::CpuInfo>, AnyError>
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();
+}