Re: [libvirt] PATCH: 1/25: Implement virKill for Win32

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patches provides a minimal implementation for virKill
on Win32. We don't particularly need this, but it avoids a
need to #ifdef out code elsewhere and may come in handy.

 util.c |   45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

Daniel

diff --git a/src/util.c b/src/util.c
--- a/src/util.c
+++ b/src/util.c
@@ -1379,5 +1379,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 :|

--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]