kill function fow MinGW

Started by kosh, October 08, 2012, 10:47:26 AM

Previous topic - Next topic

kosh

Hello Lutz.



This patch is implementation 'kill' function for MinGW (Windows).

and modified 'plainProcess' and 'p_destroy' functions.


int kill(pid_t pid, int sig);

'kill' function terminates specified process by process ID.

However, second argument (int sig) will be ignored.


diff -wru newlisp-10.4.4-orig/nl-filesys.c newlisp-10.4.4/nl-filesys.c
--- newlisp-10.4.4-orig/nl-filesys.c 2012-09-14 00:00:27 +0900
+++ newlisp-10.4.4/nl-filesys.c 2012-10-05 14:34:46 +0900
@@ -72,6 +72,8 @@
 #define pclose _pclose
 #define pipe _pipe
 
+int kill(pid_t pid, int sig);
+
 /*
 Set binary as default file mode for Windows.
 See also http://www.mingw.org/MinGWiki/index.php/binary
@@ -1142,22 +1144,25 @@
 {
 char * cPtr;
 char * argv[16];
-int idx;
+HANDLE hProc;
+DWORD pid;
 
 cPtr = callocMemory(len + 1);
 memcpy(cPtr, command, len + 1);
 
 init_argv(cPtr, argv);
 
-idx = spawnvp(P_NOWAIT, argv[0], (const char * const *)argv);
+hProc = (HANDLE)_spawnvp(P_NOWAIT, argv[0], (const char * const *)argv);
 
 free(cPtr);
-if(idx == -1) return(nilCell);
+if (hProc == INVALID_HANDLE_VALUE) return nilCell;
 
-return(stuffInteger(idx));
+pid = GetProcessId(hProc);
+CloseHandle(hProc);
+return stuffInteger(pid);
 }
 
-
+#if 0
 CELL * p_destroyProcess(CELL * params)
 {
 UINT pid;
@@ -1171,7 +1176,7 @@
 
 return(trueCell);
 }
-
+#endif
 
 #else /* not WIN_32 */
 
@@ -1843,23 +1848,6 @@
 /* --------------------------- end Cilk ------------------------------------- */
 
 
-CELL * p_destroyProcess(CELL * params)
-{
-UINT pid;
-UINT sig;
-
-params = getInteger(params, &pid);
-if(params != nilCell)  
-    getInteger(params, &sig);
-else
-    sig = 9;
-
-if(kill(pid, sig) != 0)
-    return(nilCell);
-
-return(trueCell);
-}
-
 extern SYMBOL * symHandler[];
 
 CELL * p_waitpid(CELL * params)
@@ -1889,6 +1877,23 @@
 
 #endif
 
+CELL * p_destroyProcess(CELL * params)
+{
+UINT pid;
+UINT sig;
+
+params = getInteger(params, &pid);
+if(params != nilCell)  
+    getInteger(params, &sig);
+else
+    sig = 9;
+
+if(kill(pid, sig) != 0)
+    return(nilCell);
+
+return(trueCell);
+}
+
 /* ------------------------------ semaphores --------------------------------- */
 
 #ifdef WIN_32
diff -wru newlisp-10.4.4-orig/win32-util.c newlisp-10.4.4/win32-util.c
--- newlisp-10.4.4-orig/win32-util.c 2012-09-14 00:00:27 +0900
+++ newlisp-10.4.4/win32-util.c 2012-10-05 14:29:12 +0900
@@ -25,6 +25,19 @@
 #include <windows.h>
 #include <io.h>
 
+/* kill for MinGW */
+int kill(pid_t pid, int sig)
+{
+  int ret;
+  HANDLE h;
+  /* if (pid > 0 && sig == SIGTERM) { */
+  h = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
+  if (h == NULL) return -1;
+  ret = TerminateProcess(h, 0) ? 0 : -1;
+  CloseHandle(h);
+  /* } */
+  return ret;
+}
 
 /*
 typedef struct _PROCESS_INFORMATION { // pi  


Example:
$ newlisp
> (setq pid (process "notepad.exe"))
5156
> (destroy pid) ; kll process
true
> (destroy pid) ; already killed
nil
> (destroy (sys-info -3)) ; kill your own process
$

Lutz

#1
Thanks Kosh, changes can be found here:



http://www.newlisp.org/downloads/development/inprogress/">http://www.newlisp.org/downloads/develo ... nprogress/">http://www.newlisp.org/downloads/development/inprogress/



Note that I also added similar changes to the function winPipedProcess() in win32-util.c.