]> xenbits.xensource.com Git - libvirt.git/commitdiff
Move virProcess{Kill,Abort,TranslateStatus} into virprocess.{c,h}
authorDaniel P. Berrange <berrange@redhat.com>
Mon, 24 Sep 2012 17:10:37 +0000 (18:10 +0100)
committerDaniel P. Berrange <berrange@redhat.com>
Wed, 26 Sep 2012 09:09:57 +0000 (10:09 +0100)
Continue consolidation of process functions by moving some
helpers out of command.{c,h} into virprocess.{c,h}

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
13 files changed:
daemon/libvirtd.c
daemon/remote.c
po/POTFILES.in
src/libvirt_private.syms
src/lxc/lxc_container.c
src/lxc/lxc_controller.c
src/rpc/virnetsocket.c
src/util/command.c
src/util/command.h
src/util/util.c
src/util/virprocess.c
src/util/virprocess.h
tests/testutils.c

index 8023b39b1d3b6208dd5b9b5363f4a01a835400b8..01511245962c7a636cb0f6c8391a569f7649ff8b 100644 (file)
@@ -36,6 +36,7 @@
 #include "virterror_internal.h"
 #include "virfile.h"
 #include "virpidfile.h"
+#include "virprocess.h"
 
 #define VIR_FROM_THIS VIR_FROM_QEMU
 
index 889bdbec9b5bd8621eb63a422a62b117ce95a279..e7fe128a6f8a0c2b1a9e7c7a9cc4dc932434852e 100644 (file)
@@ -46,6 +46,7 @@
 #include "virfile.h"
 #include "virtypedparam.h"
 #include "virdbus.h"
+#include "virprocess.h"
 #include "remote_protocol.h"
 #include "qemu_protocol.h"
 
index 12a2b25cd5df454572ab781e4408f8768508c163..74d8dcce5e918ff88e480922d8a8677d7acf79a6 100644 (file)
@@ -157,6 +157,7 @@ src/util/virnetdevvportprofile.c
 src/util/virnetlink.c
 src/util/virnodesuspend.c
 src/util/virpidfile.c
+src/util/virprocess.c
 src/util/virrandom.c
 src/util/virsocketaddr.c
 src/util/virterror.c
index 52c2cb23be3950afe04f4c0112c786bf3034a026..4635a4d7705b4dff37cafecf518d3ed0c7da7a19 100644 (file)
@@ -162,12 +162,9 @@ virCommandSetPreExecHook;
 virCommandSetWorkingDirectory;
 virCommandToString;
 virCommandTransferFD;
-virProcessTranslateStatus;
 virCommandWait;
 virCommandWriteArgLog;
 virFork;
-virProcessAbort;
-virProcessWait;
 virRun;
 
 
@@ -1709,7 +1706,11 @@ virPidFileDeletePath;
 
 
 # virprocess.h
+virProcessAbort;
 virProcessKill;
+virProcessTranslateStatus;
+virProcessWait;
+
 
 # virrandom.h
 virRandom;
index 0c7e029274e8aaddeae78d19a979921ee386f78d..2789c17529f26c1deb6d8a3ecc703679f87ed300 100644 (file)
@@ -63,6 +63,7 @@
 #include "virfile.h"
 #include "command.h"
 #include "virnetdev.h"
+#include "virprocess.h"
 
 #define VIR_FROM_THIS VIR_FROM_LXC
 
index 87601b373aa68360a4d079b95056cd0a98fefd2b..d407e708d1f47de8e2bc8a866e8c4b7057083e73 100644 (file)
@@ -70,6 +70,7 @@
 #include "processinfo.h"
 #include "nodeinfo.h"
 #include "virrandom.h"
+#include "virprocess.h"
 #include "rpc/virnetserver.h"
 
 #define VIR_FROM_THIS VIR_FROM_LXC
index e85fbf18929ae8c368dd0142434b288306c24e26..6ffa06e5b986c451e72e2f97c1630a09f844c49f 100644 (file)
@@ -44,6 +44,7 @@
 #include "virfile.h"
 #include "event.h"
 #include "threads.h"
+#include "virprocess.h"
 
 #include "passfd.h"
 
index 4f0ba88e30a74e97cb1f900d570984fb0abca3ff..2657a1d38e26cf6ba778344fc7a8684fc6fd75de 100644 (file)
@@ -40,6 +40,7 @@
 #include "logging.h"
 #include "virfile.h"
 #include "virpidfile.h"
+#include "virprocess.h"
 #include "buf.h"
 
 #define VIR_FROM_THIS VIR_FROM_NONE
@@ -1686,31 +1687,6 @@ virCommandToString(virCommandPtr cmd)
 }
 
 
-/**
- * virProcessTranslateStatus:
- * @status: child exit status to translate
- *
- * Translate an exit status into a malloc'd string.  Generic helper
- * for virCommandRun(), virCommandWait(), virRun(), and virProcessWait()
- * status argument, as well as raw waitpid().
- */
-char *
-virProcessTranslateStatus(int status)
-{
-    char *buf;
-    if (WIFEXITED(status)) {
-        ignore_value(virAsprintf(&buf, _("exit status %d"),
-                                 WEXITSTATUS(status)));
-    } else if (WIFSIGNALED(status)) {
-        ignore_value(virAsprintf(&buf, _("fatal signal %d"),
-                                 WTERMSIG(status)));
-    } else {
-        ignore_value(virAsprintf(&buf, _("invalid value %d"), status));
-    }
-    return buf;
-}
-
-
 /*
  * Manage input and output to the child process.
  */
@@ -2256,55 +2232,6 @@ virCommandRunAsync(virCommandPtr cmd, pid_t *pid)
 }
 
 
-/**
- * virProcessWait:
- * @pid: child to wait on
- * @exitstatus: optional status collection
- *
- * Wait for a child process to complete.
- * Return -1 on any error waiting for
- * completion. Returns 0 if the command
- * finished with the exit status set.  If @exitstatus is NULL, then the
- * child must exit with status 0 for this to succeed.
- */
-int
-virProcessWait(pid_t pid, int *exitstatus)
-{
-    int ret;
-    int status;
-
-    if (pid <= 0) {
-        virReportSystemError(EINVAL, _("unable to wait for process %lld"),
-                             (long long) pid);
-        return -1;
-    }
-
-    /* Wait for intermediate process to exit */
-    while ((ret = waitpid(pid, &status, 0)) == -1 &&
-           errno == EINTR);
-
-    if (ret == -1) {
-        virReportSystemError(errno, _("unable to wait for process %lld"),
-                             (long long) pid);
-        return -1;
-    }
-
-    if (exitstatus == NULL) {
-        if (status != 0) {
-            char *st = virProcessTranslateStatus(status);
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("Child process (%lld) unexpected %s"),
-                           (long long) pid, NULLSTR(st));
-            VIR_FREE(st);
-            return -1;
-        }
-    } else {
-        *exitstatus = status;
-    }
-
-    return 0;
-}
-
 /**
  * virCommandWait:
  * @cmd: command to wait on
@@ -2369,67 +2296,6 @@ virCommandWait(virCommandPtr cmd, int *exitstatus)
 
 
 #ifndef WIN32
-/**
- * virProcessAbort:
- * @pid: child process to kill
- *
- * Abort a child process if PID is positive and that child is still
- * running, without issuing any errors or affecting errno.  Designed
- * for error paths where some but not all paths to the cleanup code
- * might have started the child process.  If @pid is 0 or negative,
- * this does nothing.
- */
-void
-virProcessAbort(pid_t pid)
-{
-    int saved_errno;
-    int ret;
-    int status;
-    char *tmp = NULL;
-
-    if (pid <= 0)
-        return;
-
-    /* See if intermediate process has exited; if not, try a nice
-     * SIGTERM followed by a more severe SIGKILL.
-     */
-    saved_errno = errno;
-    VIR_DEBUG("aborting child process %d", pid);
-    while ((ret = waitpid(pid, &status, WNOHANG)) == -1 &&
-           errno == EINTR);
-    if (ret == pid) {
-        tmp = virProcessTranslateStatus(status);
-        VIR_DEBUG("process has ended: %s", tmp);
-        goto cleanup;
-    } else if (ret == 0) {
-        VIR_DEBUG("trying SIGTERM to child process %d", pid);
-        kill(pid, SIGTERM);
-        usleep(10 * 1000);
-        while ((ret = waitpid(pid, &status, WNOHANG)) == -1 &&
-               errno == EINTR);
-        if (ret == pid) {
-            tmp = virProcessTranslateStatus(status);
-            VIR_DEBUG("process has ended: %s", tmp);
-            goto cleanup;
-        } else if (ret == 0) {
-            VIR_DEBUG("trying SIGKILL to child process %d", pid);
-            kill(pid, SIGKILL);
-            while ((ret = waitpid(pid, &status, 0)) == -1 &&
-                   errno == EINTR);
-            if (ret == pid) {
-                tmp = virProcessTranslateStatus(status);
-                VIR_DEBUG("process has ended: %s", tmp);
-                goto cleanup;
-            }
-        }
-    }
-    VIR_DEBUG("failed to reap child %lld, abandoning it", (long long) pid);
-
-cleanup:
-    VIR_FREE(tmp);
-    errno = saved_errno;
-}
-
 /**
  * virCommandAbort:
  * @cmd: command to abort
@@ -2449,13 +2315,6 @@ virCommandAbort(virCommandPtr cmd)
     cmd->reap = false;
 }
 #else /* WIN32 */
-void
-virProcessAbort(pid_t pid)
-{
-    /* Not yet ported to mingw.  Any volunteers?  */
-    VIR_DEBUG("failed to reap child %lld, abandoning it", (long long)pid);
-}
-
 void
 virCommandAbort(virCommandPtr cmd ATTRIBUTE_UNUSED)
 {
index b91331c64e8a7f1f473db843cbaf51f17d5c5751..177a0eb13509d341da9e9e446f9f0a212b2a4823 100644 (file)
@@ -140,9 +140,6 @@ void virCommandWriteArgLog(virCommandPtr cmd,
 
 char *virCommandToString(virCommandPtr cmd) ATTRIBUTE_RETURN_CHECK;
 
-
-char *virProcessTranslateStatus(int exitstatus) ATTRIBUTE_RETURN_CHECK;
-
 int virCommandExec(virCommandPtr cmd) ATTRIBUTE_RETURN_CHECK;
 
 int virCommandRun(virCommandPtr cmd,
@@ -151,9 +148,6 @@ int virCommandRun(virCommandPtr cmd,
 int virCommandRunAsync(virCommandPtr cmd,
                        pid_t *pid) ATTRIBUTE_RETURN_CHECK;
 
-int virProcessWait(pid_t pid,
-                   int *exitstatus) ATTRIBUTE_RETURN_CHECK;
-
 int virCommandWait(virCommandPtr cmd,
                    int *exitstatus) ATTRIBUTE_RETURN_CHECK;
 
@@ -165,8 +159,6 @@ int virCommandHandshakeWait(virCommandPtr cmd)
 int virCommandHandshakeNotify(virCommandPtr cmd)
     ATTRIBUTE_RETURN_CHECK;
 
-void virProcessAbort(pid_t pid);
-
 void virCommandAbort(virCommandPtr cmd);
 
 void virCommandFree(virCommandPtr cmd);
index 062fbf8a93cff435a5aff2f57e2e006e6f6e5632..28f9ae3365c90145d18d6e93bf4022421f2fc728 100644 (file)
@@ -87,6 +87,7 @@
 #include "command.h"
 #include "nonblocking.h"
 #include "passfd.h"
+#include "virprocess.h"
 
 #ifndef NSIG
 # define NSIG 32
index e7db68fbe42283fd6642481562160f3e1ea565fa..958f5f7960e86d52baf39a753902718fd2659416 100644 (file)
 
 #include <signal.h>
 #include <errno.h>
+#include <sys/wait.h>
 
 #include "virprocess.h"
+#include "virterror_internal.h"
+#include "memory.h"
+#include "logging.h"
+#include "util.h"
+
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+/**
+ * virProcessTranslateStatus:
+ * @status: child exit status to translate
+ *
+ * Translate an exit status into a malloc'd string.  Generic helper
+ * for virCommandRun(), virCommandWait(), virRun(), and virProcessWait()
+ * status argument, as well as raw waitpid().
+ */
+char *
+virProcessTranslateStatus(int status)
+{
+    char *buf;
+    if (WIFEXITED(status)) {
+        ignore_value(virAsprintf(&buf, _("exit status %d"),
+                                 WEXITSTATUS(status)));
+    } else if (WIFSIGNALED(status)) {
+        ignore_value(virAsprintf(&buf, _("fatal signal %d"),
+                                 WTERMSIG(status)));
+    } else {
+        ignore_value(virAsprintf(&buf, _("invalid value %d"), status));
+    }
+    return buf;
+}
+
+
+#ifndef WIN32
+/**
+ * virProcessAbort:
+ * @pid: child process to kill
+ *
+ * Abort a child process if PID is positive and that child is still
+ * running, without issuing any errors or affecting errno.  Designed
+ * for error paths where some but not all paths to the cleanup code
+ * might have started the child process.  If @pid is 0 or negative,
+ * this does nothing.
+ */
+void
+virProcessAbort(pid_t pid)
+{
+    int saved_errno;
+    int ret;
+    int status;
+    char *tmp = NULL;
+
+    if (pid <= 0)
+        return;
+
+    /* See if intermediate process has exited; if not, try a nice
+     * SIGTERM followed by a more severe SIGKILL.
+     */
+    saved_errno = errno;
+    VIR_DEBUG("aborting child process %d", pid);
+    while ((ret = waitpid(pid, &status, WNOHANG)) == -1 &&
+           errno == EINTR);
+    if (ret == pid) {
+        tmp = virProcessTranslateStatus(status);
+        VIR_DEBUG("process has ended: %s", tmp);
+        goto cleanup;
+    } else if (ret == 0) {
+        VIR_DEBUG("trying SIGTERM to child process %d", pid);
+        kill(pid, SIGTERM);
+        usleep(10 * 1000);
+        while ((ret = waitpid(pid, &status, WNOHANG)) == -1 &&
+               errno == EINTR);
+        if (ret == pid) {
+            tmp = virProcessTranslateStatus(status);
+            VIR_DEBUG("process has ended: %s", tmp);
+            goto cleanup;
+        } else if (ret == 0) {
+            VIR_DEBUG("trying SIGKILL to child process %d", pid);
+            kill(pid, SIGKILL);
+            while ((ret = waitpid(pid, &status, 0)) == -1 &&
+                   errno == EINTR);
+            if (ret == pid) {
+                tmp = virProcessTranslateStatus(status);
+                VIR_DEBUG("process has ended: %s", tmp);
+                goto cleanup;
+            }
+        }
+    }
+    VIR_DEBUG("failed to reap child %lld, abandoning it", (long long) pid);
+
+cleanup:
+    VIR_FREE(tmp);
+    errno = saved_errno;
+}
+#else
+void
+virProcessAbort(pid_t pid)
+{
+    /* Not yet ported to mingw.  Any volunteers?  */
+    VIR_DEBUG("failed to reap child %lld, abandoning it", (long long)pid);
+}
+#endif
+
+
+/**
+ * virProcessWait:
+ * @pid: child to wait on
+ * @exitstatus: optional status collection
+ *
+ * Wait for a child process to complete.
+ * Return -1 on any error waiting for
+ * completion. Returns 0 if the command
+ * finished with the exit status set.  If @exitstatus is NULL, then the
+ * child must exit with status 0 for this to succeed.
+ */
+int
+virProcessWait(pid_t pid, int *exitstatus)
+{
+    int ret;
+    int status;
+
+    if (pid <= 0) {
+        virReportSystemError(EINVAL, _("unable to wait for process %lld"),
+                             (long long) pid);
+        return -1;
+    }
+
+    /* Wait for intermediate process to exit */
+    while ((ret = waitpid(pid, &status, 0)) == -1 &&
+           errno == EINTR);
+
+    if (ret == -1) {
+        virReportSystemError(errno, _("unable to wait for process %lld"),
+                             (long long) pid);
+        return -1;
+    }
+
+    if (exitstatus == NULL) {
+        if (status != 0) {
+            char *st = virProcessTranslateStatus(status);
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Child process (%lld) unexpected %s"),
+                           (long long) pid, NULLSTR(st));
+            VIR_FREE(st);
+            return -1;
+        }
+    } else {
+        *exitstatus = status;
+    }
+
+    return 0;
+}
+
 
 /* send signal to a single process */
 int virProcessKill(pid_t pid, int sig)
index b1000c6b568e5d543587de58265131e0899ceafb..048a73c0717db81fee43eee1e79cedfd27098bb6 100644 (file)
 
 # include "internal.h"
 
+char *
+virProcessTranslateStatus(int status);
+
+void
+virProcessAbort(pid_t pid);
+
+int
+virProcessWait(pid_t pid, int *exitstatus)
+    ATTRIBUTE_RETURN_CHECK;
+
 int virProcessKill(pid_t pid, int sig);
 
+
 #endif /* __VIR_PROCESS_H__ */
index 004731d39611a85b38d08850deb59c5a0913ec98..4d4f58975bda049fd7a9e8c9ad40fcaa7ecf4390 100644 (file)
@@ -48,6 +48,7 @@
 #include "command.h"
 #include "virrandom.h"
 #include "dirname.h"
+#include "virprocess.h"
 
 #if TEST_OOM_TRACE
 # include <execinfo.h>