summaryrefslogtreecommitdiff
path: root/Utility
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2014-02-13 14:00:15 -0400
committerGravatar Joey Hess <joey@kitenet.net>2014-02-13 15:53:10 -0400
commit4f59f9439687cccfb7aac6aca62dbe97038179bf (patch)
tree94d8fa2e00989c183a20db782b8c5e9fa0e3ff9b /Utility
parent16405cbd31e2ee5a2ef021a1c178738709b087c3 (diff)
windows: Fix process termination code.
The ctrl-c hack used before didn't actually seem to work. No haskell libraries expose TerminateProcess. I tried just calling it via FFI, but got segfaults, probably to do with the wacky process handle not being managed correctly. Moving it all into one C function worked. This was hell. The EvilLinker hack was just final icing on the cake. We all know what the cake was made of.
Diffstat (limited to 'Utility')
-rw-r--r--Utility/Daemon.hs9
-rw-r--r--Utility/WinProcess.hs19
-rw-r--r--Utility/winprocess.c10
3 files changed, 33 insertions, 5 deletions
diff --git a/Utility/Daemon.hs b/Utility/Daemon.hs
index a3a8dbb51..c10e87192 100644
--- a/Utility/Daemon.hs
+++ b/Utility/Daemon.hs
@@ -13,14 +13,13 @@ import Common
import Utility.PID
#ifndef mingw32_HOST_OS
import Utility.LogFile
+#else
+import Utility.WinProcess
#endif
#ifndef mingw32_HOST_OS
import System.Posix
import Control.Concurrent.Async
-#else
-import System.PosixCompat.Types
-import System.Win32.Console (generateConsoleCtrlEvent, cTRL_C_EVENT)
#endif
#ifndef mingw32_HOST_OS
@@ -75,7 +74,7 @@ lockPidFile file = do
_ <- fdWrite fd' =<< show <$> getPID
closeFd fd
#else
- writeFile newfile "-1"
+ writeFile newfile . show =<< getPID
#endif
rename newfile file
where
@@ -121,5 +120,5 @@ stopDaemon pidfile = go =<< checkDaemon pidfile
#ifndef mingw32_HOST_OS
signalProcess sigTERM pid
#else
- generateConsoleCtrlEvent cTRL_C_EVENT pid
+ terminatePID pid
#endif
diff --git a/Utility/WinProcess.hs b/Utility/WinProcess.hs
new file mode 100644
index 000000000..5c6d4cfce
--- /dev/null
+++ b/Utility/WinProcess.hs
@@ -0,0 +1,19 @@
+{- Windows processes
+ -
+ - Copyright 2014 Joey Hess <joey@kitenet.net>
+ -
+ - Licensed under the GNU GPL version 3 or higher.
+ -}
+
+{-# LANGUAGE ForeignFunctionInterface #-}
+
+module Utility.WinProcess where
+
+import Utility.PID
+
+import System.Win32.Process
+import Foreign.C
+import Control.Exception
+
+foreign import ccall unsafe "terminatepid"
+ terminatePID :: PID -> IO ()
diff --git a/Utility/winprocess.c b/Utility/winprocess.c
new file mode 100644
index 000000000..b6e315573
--- /dev/null
+++ b/Utility/winprocess.c
@@ -0,0 +1,10 @@
+#include <windows.h>
+
+void terminatepid (DWORD pid) {
+ HANDLE h;
+ h = OpenProcess(PROCESS_TERMINATE, 0, pid);
+ if (h != NULL) {
+ TerminateProcess(h, 1);
+ }
+ CloseHandle(h);
+}