summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/tests/unit/signal_test.ts8
-rw-r--r--runtime/ops/process.rs49
2 files changed, 11 insertions, 46 deletions
diff --git a/cli/tests/unit/signal_test.ts b/cli/tests/unit/signal_test.ts
index 4bb92bb31..b8b473e6d 100644
--- a/cli/tests/unit/signal_test.ts
+++ b/cli/tests/unit/signal_test.ts
@@ -204,10 +204,10 @@ Deno.test(
function windowsThrowsOnNegativeProcessIdTest() {
assertThrows(
() => {
- Deno.kill(-1, "SIGINT");
+ Deno.kill(-1, "SIGKILL");
},
TypeError,
- "Invalid process id (pid) -1 for signal SIGINT.",
+ "Invalid pid",
);
},
);
@@ -225,7 +225,7 @@ Deno.test(
Deno.kill(0, signal);
},
TypeError,
- `Cannot use ${signal} on PID 0`,
+ `Invalid pid`,
);
signal = "SIGTERM";
@@ -234,7 +234,7 @@ Deno.test(
Deno.kill(0, signal);
},
TypeError,
- `Cannot use ${signal} on PID 0`,
+ `Invalid pid`,
);
},
);
diff --git a/runtime/ops/process.rs b/runtime/ops/process.rs
index bd6328ae9..49f26ade4 100644
--- a/runtime/ops/process.rs
+++ b/runtime/ops/process.rs
@@ -301,6 +301,7 @@ pub fn kill(pid: i32, signal: &str) -> Result<(), AnyError> {
use deno_core::error::type_error;
use std::io::Error;
use std::io::ErrorKind::NotFound;
+ use winapi::shared::minwindef::DWORD;
use winapi::shared::minwindef::FALSE;
use winapi::shared::minwindef::TRUE;
use winapi::shared::winerror::ERROR_INVALID_PARAMETER;
@@ -308,45 +309,14 @@ pub fn kill(pid: i32, signal: &str) -> Result<(), AnyError> {
use winapi::um::handleapi::CloseHandle;
use winapi::um::processthreadsapi::OpenProcess;
use winapi::um::processthreadsapi::TerminateProcess;
- use winapi::um::wincon::GenerateConsoleCtrlEvent;
- use winapi::um::wincon::{CTRL_BREAK_EVENT, CTRL_CLOSE_EVENT, CTRL_C_EVENT};
use winapi::um::winnt::PROCESS_TERMINATE;
- if pid < 0 {
- return Err(type_error(format!(
- "Invalid process id (pid) {} for signal {}.",
- pid, signal
- )));
- }
-
- if matches!(signal, "SIGINT" | "SIGBREAK" | "SIGHUP") {
- let is_generated = unsafe {
- GenerateConsoleCtrlEvent(
- match signal {
- "SIGINT" => CTRL_C_EVENT,
- "SIGBREAK" => CTRL_BREAK_EVENT,
- // Need tokio::windows::signal::CtrlClose or equivalent
- // in signal.rs to get this working
- "SIGHUP" => CTRL_CLOSE_EVENT,
- _ => unreachable!(),
- },
- pid as u32,
- )
- };
- match is_generated {
- FALSE => {
- Err(Error::from_raw_os_error(unsafe { GetLastError() } as i32).into())
- }
- TRUE => Ok(()),
- _ => unreachable!(),
- }
- } else if matches!(signal, "SIGKILL" | "SIGTERM") {
- // PID 0 = System Idle Process and can't be opened.
- if pid == 0 {
- return Err(type_error(format!("Cannot use {} on PID 0", signal)));
- }
-
- let handle = unsafe { OpenProcess(PROCESS_TERMINATE, FALSE, pid as u32) };
+ if !matches!(signal, "SIGKILL" | "SIGTERM") {
+ Err(type_error(format!("Invalid signal: {}", signal)))
+ } else if pid <= 0 {
+ Err(type_error("Invalid pid"))
+ } else {
+ let handle = unsafe { OpenProcess(PROCESS_TERMINATE, FALSE, pid as DWORD) };
if handle.is_null() {
let err = match unsafe { GetLastError() } {
@@ -363,11 +333,6 @@ pub fn kill(pid: i32, signal: &str) -> Result<(), AnyError> {
_ => unreachable!(),
}
}
- } else {
- Err(type_error(format!(
- "Signal {} is unsupported on Windows.",
- signal
- )))
}
}