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

[libvirt] [PATCHv2 5/5] sysinfo: convert to virCommand



* src/util/sysinfo.c (virSysinfoRead): Use virCommand instead of
virExec.
---

This patch assumes option 2 of my patch to virCommand guarantees on
outbuf when the child process had no output (it needs a slight tweak
to avoid a NULL dereference if we go with option 1 for virCommand).

 src/util/sysinfo.c |   50 ++++++++++----------------------------------------
 1 files changed, 10 insertions(+), 40 deletions(-)

diff --git a/src/util/sysinfo.c b/src/util/sysinfo.c
index cf41773..f44b112 100644
--- a/src/util/sysinfo.c
+++ b/src/util/sysinfo.c
@@ -36,6 +36,7 @@
 #include "conf/domain_conf.h"
 #include "logging.h"
 #include "memory.h"
+#include "command.h"

 #define VIR_FROM_THIS VIR_FROM_SYSINFO

@@ -94,25 +95,24 @@ virSysinfoRead(void) {
 virSysinfoDefPtr
 virSysinfoRead(void) {
     char *path, *cur, *eol, *base;
-    int pid, outfd = -1, errfd = -1;
     virSysinfoDefPtr ret = NULL;
     const char *argv[] = { NULL, "-q", "-t", "0,1", NULL };
-    int res, waitret, exitstatus;
+    int res;
     char *outbuf = NULL;
-    char *errbuf = NULL;
+    virCommandPtr cmd;

     path = virFindFileInPath(SYSINFO_SMBIOS_DECODER);
     if (path == NULL) {
         virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Failed to find path for %s binary"),
                              SYSINFO_SMBIOS_DECODER);
-        return(NULL);
+        return NULL;
     }
-    argv[0] = path;

-    res = virExec(argv, NULL, NULL, &pid, -1, &outfd, &errfd,
-                  VIR_EXEC_NONE | VIR_EXEC_NONBLOCK);
-    if (res < 0) {
+    argv[0] = path;
+    cmd = virCommandNewArgs(argv);
+    virCommandSetOutputBuffer(cmd, &outbuf);
+    if (virCommandRun(cmd, NULL) < 0) {
         virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Failed to execute command %s"),
                              path);
@@ -120,34 +120,6 @@ virSysinfoRead(void) {
         goto cleanup;
     }

-    /*
-     * we are interested in the output, capture it and errors too
-     */
-    if (virPipeReadUntilEOF(outfd, errfd, &outbuf, &errbuf) < 0) {
-        virReportSystemError(errno, _("cannot wait for '%s'"), path);
-        while (waitpid(pid, &exitstatus, 0) == -1 && errno == EINTR)
-            ;
-        goto cleanup;
-    }
-
-    if (outbuf)
-        VIR_DEBUG("Command stdout: %s", outbuf);
-    if (errbuf)
-        VIR_DEBUG("Command stderr: %s", errbuf);
-
-    while ((waitret = waitpid(pid, &exitstatus, 0) == -1) &&
-           (errno == EINTR));
-    if (waitret == -1) {
-        virReportSystemError(errno, _("Failed to wait for '%s'"), path);
-        goto cleanup;
-    }
-    if (exitstatus != 0) {
-        virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("command %s failed with error code %d:%s"),
-                             path, exitstatus, errbuf);
-        goto cleanup;
-    }
-
     if (VIR_ALLOC(ret) < 0)
         goto no_memory;

@@ -227,15 +199,13 @@ virSysinfoRead(void) {

 cleanup:
     VIR_FREE(outbuf);
-    VIR_FREE(errbuf);
-    VIR_FREE(path);
+    virCommandFree(cmd);

-    return(ret);
+    return ret;

 no_memory:
     virReportOOMError();

-
     virSysinfoDefFree(ret);
     ret = NULL;
     goto cleanup;
-- 
1.7.3.2


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