From e5e2b65cf86ea49eba76b3c274e3b9d2177485bc Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Mon, 24 Sep 2012 17:54:51 +0100 Subject: [PATCH] Move virProcessKill into virprocess.{h,c} There are a number of process related functions spread across multiple files. Start to consolidate them by creating a virprocess.{c,h} file Signed-off-by: Daniel P. Berrange --- src/Makefile.am | 1 + src/libvirt_private.syms | 3 ++ src/qemu/qemu_agent.c | 1 + src/qemu/qemu_monitor.c | 1 + src/qemu/qemu_process.c | 1 + src/uml/uml_driver.c | 1 + src/util/util.c | 57 --------------------------- src/util/util.h | 2 - src/util/virprocess.c | 84 ++++++++++++++++++++++++++++++++++++++++ src/util/virprocess.h | 31 +++++++++++++++ 10 files changed, 123 insertions(+), 59 deletions(-) create mode 100644 src/util/virprocess.c create mode 100644 src/util/virprocess.h diff --git a/src/Makefile.am b/src/Makefile.am index 4ae741be27..ae3d491cad 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -87,6 +87,7 @@ UTIL_SOURCES = \ util/virnodesuspend.c util/virnodesuspend.h \ util/virobject.c util/virobject.h \ util/virpidfile.c util/virpidfile.h \ + util/virprocess.c util/virprocess.h \ util/virtypedparam.c util/virtypedparam.h \ util/xml.c util/xml.h \ util/virterror.c util/virterror_internal.h \ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 4bb6fa3492..52c2cb23be 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1708,6 +1708,9 @@ virPidFileDelete; virPidFileDeletePath; +# virprocess.h +virProcessKill; + # virrandom.h virRandom; virRandomBits; diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c index 2eae3f2a07..ab6dc2238e 100644 --- a/src/qemu/qemu_agent.c +++ b/src/qemu/qemu_agent.c @@ -39,6 +39,7 @@ #include "virterror_internal.h" #include "json.h" #include "virfile.h" +#include "virprocess.h" #include "virtime.h" #include "virobject.h" diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c index aec7b6c511..2796a613dc 100644 --- a/src/qemu/qemu_monitor.c +++ b/src/qemu/qemu_monitor.c @@ -36,6 +36,7 @@ #include "memory.h" #include "logging.h" #include "virfile.h" +#include "virprocess.h" #include "virobject.h" #ifdef WITH_DTRACE_PROBES diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 637f7be5cb..3cd30afb79 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -61,6 +61,7 @@ #include "locking/domain_lock.h" #include "network/bridge_driver.h" #include "uuid.h" +#include "virprocess.h" #include "virtime.h" #include "virnetdevtap.h" #include "bitmap.h" diff --git a/src/uml/uml_driver.c b/src/uml/uml_driver.c index 6d0ef7907b..c341fabeb9 100644 --- a/src/uml/uml_driver.c +++ b/src/uml/uml_driver.c @@ -63,6 +63,7 @@ #include "configmake.h" #include "virnetdevtap.h" #include "virnodesuspend.h" +#include "virprocess.h" #include "viruri.h" #define VIR_FROM_THIS VIR_FROM_UML diff --git a/src/util/util.c b/src/util/util.c index 5bf82dc2f3..062fbf8a93 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -2289,63 +2289,6 @@ check_and_return: return result; } -/* send signal to a single process */ -int virProcessKill(pid_t pid, int sig) -{ - if (pid <= 1) { - errno = ESRCH; - 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 (sig != 0 && !TerminateProcess(proc, sig)) { - errno = ESRCH; - return -1; - } - CloseHandle(proc); - } - } - return 0; -#else - return kill(pid, sig); -#endif -} - - #ifdef HAVE_GETPWUID_R enum { VIR_USER_ENT_DIRECTORY, diff --git a/src/util/util.h b/src/util/util.h index 501373d1ab..5ab36ed35c 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -255,8 +255,6 @@ static inline int getgid (void) { return 0; } char *virGetHostname(virConnectPtr conn); -int virProcessKill(pid_t pid, int sig); - char *virGetUserDirectory(void); char *virGetUserConfigDirectory(void); char *virGetUserCacheDirectory(void); diff --git a/src/util/virprocess.c b/src/util/virprocess.c new file mode 100644 index 0000000000..e7db68fbe4 --- /dev/null +++ b/src/util/virprocess.c @@ -0,0 +1,84 @@ +/* + * virprocess.c: interaction with processes + * + * Copyright (C) 2010-2012 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + */ + + +#include + +#include +#include + +#include "virprocess.h" + +/* send signal to a single process */ +int virProcessKill(pid_t pid, int sig) +{ + if (pid <= 1) { + errno = ESRCH; + 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 (sig != 0 && !TerminateProcess(proc, sig)) { + errno = ESRCH; + return -1; + } + CloseHandle(proc); + } + } + return 0; +#else + return kill(pid, sig); +#endif +} diff --git a/src/util/virprocess.h b/src/util/virprocess.h new file mode 100644 index 0000000000..b1000c6b56 --- /dev/null +++ b/src/util/virprocess.h @@ -0,0 +1,31 @@ +/* + * virprocess.h: interaction with processes + * + * Copyright (C) 2010-2012 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + */ + +#ifndef __VIR_PROCESS_H__ +# define __VIR_PROCESS_H__ + +# include + +# include "internal.h" + +int virProcessKill(pid_t pid, int sig); + +#endif /* __VIR_PROCESS_H__ */ -- GitLab