]> xenbits.xensource.com Git - libvirt.git/commitdiff
Add virRun() helper function (Dan Berrange)
authorMark McLoughlin <markmc@redhat.com>
Thu, 10 Jan 2008 13:46:10 +0000 (13:46 +0000)
committerMark McLoughlin <markmc@redhat.com>
Thu, 10 Jan 2008 13:46:10 +0000 (13:46 +0000)
ChangeLog
src/util.c
src/util.h

index 6322dbfd7820546fe8272925179490295c26adb1..c9b275aa37d5f2157c0f9a8576d15621174ac6eb 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Thu Jan 10 13:44:17 GMT 2008 Mark McLoughlin <markmc@redhat.com>
+
+       * src/util.[ch]: Add virRun() helper function (Dan Berrange)
+
 Wed Jan  9 16:04:00 EST 2008 Daniel P. Berrange <berrange@redhat.com>
 
        * src/xen_internal.c: Ensure cpumap is at least 8 bytes long
index a4112370e9dacdabcf6a209f414ab522f44d4673..755c4c2c79cf00d18a5d184e82368316a64aca7a 100644 (file)
@@ -33,6 +33,7 @@
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/wait.h>
 #include <string.h>
 
 #ifdef HAVE_PATHS_H
@@ -203,6 +204,43 @@ virExecNonBlock(virConnectPtr conn,
     return(_virExec(conn, argv, retpid, infd, outfd, errfd, 1));
 }
 
+/**
+ * @conn connection to report errors against
+ * @argv NULL terminated argv to run
+ * @status optional variable to return exit status in
+ *
+ * Run a command without using the shell.
+ *
+ * If status is NULL, then return 0 if the command run and
+ * exited with 0 status; Otherwise return -1
+ *
+ * If status is not-NULL, then return 0 if the command ran.
+ * The status variable is filled with the command exit status
+ * and should be checked by caller for success. Return -1
+ * only if the command could not be run.
+ */
+int
+virRun(virConnectPtr conn,
+       char **argv,
+       int *status) {
+    int childpid, exitstatus, ret;
+
+    if ((ret = virExec(conn, argv, &childpid, -1, NULL, NULL)) < 0)
+        return ret;
+
+    while ((ret = waitpid(childpid, &exitstatus, 0) == -1) && errno == EINTR);
+    if (ret == -1)
+        return -1;
+
+    if (status == NULL) {
+        errno = EINVAL;
+        return (WIFEXITED(exitstatus) && WEXITSTATUS(exitstatus) == 0) ? 0 : -1;
+    } else {
+        *status = exitstatus;
+        return 0;
+    }
+}
+
 #else /* __MINGW32__ */
 
 int
index 393f71f3192c18ef6ebfc03064bfa7d7912086bf..b54df0ae91c161deb10b2f4fee9470974be5ac3c 100644 (file)
@@ -28,6 +28,7 @@
 
 int virExec(virConnectPtr conn, char **argv, int *retpid, int infd, int *outfd, int *errfd);
 int virExecNonBlock(virConnectPtr conn, char **argv, int *retpid, int infd, int *outfd, int *errfd);
+int virRun(virConnectPtr conn, char **argv, int *status);
 
 int saferead(int fd, void *buf, size_t count);
 ssize_t safewrite(int fd, const void *buf, size_t count);