summaryrefslogtreecommitdiff
path: root/cli/signal.rs
diff options
context:
space:
mode:
authorAli Hasani <a.hassssani@gmail.com>2020-05-17 21:41:24 +0430
committerGitHub <noreply@github.com>2020-05-17 19:11:24 +0200
commiteddb916883901385233bea24313e2920fcac5388 (patch)
tree0dbde6a34037b1c2b2459077731cb9afdc5b3a35 /cli/signal.rs
parenta054250a2cd709f74a3c9984af0cb74b7adde3bd (diff)
Implement Deno.kill for windows (#5347)
Diffstat (limited to 'cli/signal.rs')
-rw-r--r--cli/signal.rs43
1 files changed, 41 insertions, 2 deletions
diff --git a/cli/signal.rs b/cli/signal.rs
index 6be588b84..f74f9ab26 100644
--- a/cli/signal.rs
+++ b/cli/signal.rs
@@ -1,6 +1,23 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::op_error::OpError;
+#[cfg(not(unix))]
+const SIGINT: i32 = 2;
+#[cfg(not(unix))]
+const SIGKILL: i32 = 9;
+#[cfg(not(unix))]
+const SIGTERM: i32 = 15;
+
+#[cfg(not(unix))]
+use winapi::{
+ shared::minwindef::DWORD,
+ um::{
+ handleapi::CloseHandle,
+ processthreadsapi::{OpenProcess, TerminateProcess},
+ winnt::PROCESS_TERMINATE,
+ },
+};
+
#[cfg(unix)]
pub fn kill(pid: i32, signo: i32) -> Result<(), OpError> {
use nix::sys::signal::{kill as unix_kill, Signal};
@@ -11,7 +28,29 @@ pub fn kill(pid: i32, signo: i32) -> Result<(), OpError> {
}
#[cfg(not(unix))]
-pub fn kill(_pid: i32, _signal: i32) -> Result<(), OpError> {
- // TODO: implement this for windows
+pub fn kill(pid: i32, signal: i32) -> Result<(), OpError> {
+ match signal {
+ SIGINT | SIGKILL | SIGTERM => {
+ if pid <= 0 {
+ return Err(OpError::type_error("unsupported pid".to_string()));
+ }
+ unsafe {
+ let handle = OpenProcess(PROCESS_TERMINATE, 0, pid as DWORD);
+ if handle.is_null() {
+ return Err(OpError::from(std::io::Error::last_os_error()));
+ }
+ if TerminateProcess(handle, 1) == 0 {
+ CloseHandle(handle);
+ return Err(OpError::from(std::io::Error::last_os_error()));
+ }
+ if CloseHandle(handle) == 0 {
+ return Err(OpError::from(std::io::Error::last_os_error()));
+ }
+ }
+ }
+ _ => {
+ return Err(OpError::type_error("unsupported signal".to_string()));
+ }
+ }
Ok(())
}