From 77a2fc0061c2be1d07a790c1e684766eb7c6b119 Mon Sep 17 00:00:00 2001 From: Mark McLoughlin Date: Thu, 10 Jan 2008 13:46:10 +0000 Subject: [PATCH] Add virRun() helper function (Dan Berrange) --- ChangeLog | 4 ++++ src/util.c | 38 ++++++++++++++++++++++++++++++++++++++ src/util.h | 1 + 3 files changed, 43 insertions(+) diff --git a/ChangeLog b/ChangeLog index 6322dbfd78..c9b275aa37 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Thu Jan 10 13:44:17 GMT 2008 Mark McLoughlin + + * src/util.[ch]: Add virRun() helper function (Dan Berrange) + Wed Jan 9 16:04:00 EST 2008 Daniel P. Berrange * src/xen_internal.c: Ensure cpumap is at least 8 bytes long diff --git a/src/util.c b/src/util.c index a4112370e9..755c4c2c79 100644 --- a/src/util.c +++ b/src/util.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #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 diff --git a/src/util.h b/src/util.h index 393f71f319..b54df0ae91 100644 --- a/src/util.h +++ b/src/util.h @@ -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);