[libvirt] PATCH: Implement virKill for Windows

Daniel P. Berrange berrange at redhat.com
Tue Jan 6 20:50:48 UTC 2009


The addition of virKill to src/util.c breaks Windows build which lacks the
kill() function. Now we technically don't have any code which runs virKill
when built on Windows, but after the 2 weeks holiday I'm very motivated so
wrote a basic implement of virKill() which is a starting point for Win32
in case we need it based on the recommendations in this thread

  http://www.nabble.com/kill-signal-td149789.html

Seems Perl does much the same for its kill() implement on Win32 (and is
even more thorough/complete), though we can't just use the Perl code 
directly since its  GPL licensed. So I've done a minimal stub

Daniel


Index: src/util.c
===================================================================
RCS file: /data/cvs/libvirt/src/util.c,v
retrieving revision 1.78
diff -u -p -u -p -r1.78 util.c
--- src/util.c	6 Jan 2009 17:46:46 -0000	1.78
+++ src/util.c	6 Jan 2009 20:47:09 -0000
@@ -1346,5 +1346,50 @@ int virKillProcess(pid_t pid, int sig)
         return -1;
     }
 
+#ifdef WIN32
+    /* Mingw / Windows don't have many signals (AFAIK) */
+    switch (sig) {
+    case SIGINT:
+        /* This does a Ctrl+C equiv */
+        if (!GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid)) {
+            errno = ESRCH;
+            return -1;
+        }
+        break;
+
+    case SIGTERM:
+        /* Since TerminateProcess is closer to SIG_KILL, we do
+         * a Ctrl+Break equiv which is more pleasant like the
+         * good old unix SIGTERM/HUP
+         */
+        if (!GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pid)) {
+            errno = ESRCH;
+            return -1;
+        }
+        break;
+
+    default:
+    {
+        HANDLE proc;
+        proc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
+        if (!proc) {
+            errno = ESRCH; /* Not entirely accurate, but close enough */
+            return -1;
+        }
+
+        /*
+         * TerminateProcess is more or less equiv to SIG_KILL, in that
+         * a process can't trap / block it
+         */
+        if (!TerminateProcess(proc, sig)) {
+            errno = ESRCH;
+            return -1;
+        }
+        CloseHandle(proc);
+    }
+    }
+    return 0;
+#else
     return kill(pid, sig);
+#endif
 }

-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|




More information about the libvir-list mailing list