[libvirt] [PATCH 2/2] Use virGetLastErrorMessage()

Jovanka Gulicoska jovanka.gulicoska at gmail.com
Tue May 10 12:16:10 UTC 2016


virGetLastErrorMessage() in daemon, examples,bhyve, lxc,
logging, locking, libvirt, libxl, lxc, rpc, storage,
uml, and util.

Fix syntax error in tests.
---
 daemon/libvirtd.c                   |  8 ++------
 examples/object-events/event-test.c |  9 +++------
 src/bhyve/bhyve_driver.c            |  3 +--
 src/libvirt.c                       |  3 +--
 src/libxl/libxl_domain.c            |  3 +--
 src/libxl/libxl_driver.c            |  4 +---
 src/locking/lock_daemon.c           |  8 ++------
 src/logging/log_daemon.c            |  8 ++------
 src/lxc/lxc_container.c             |  9 +++------
 src/lxc/lxc_controller.c            |  9 +++------
 src/lxc/lxc_domain.c                |  4 ++--
 src/lxc/lxc_process.c               |  6 ++----
 src/rpc/virnettlscontext.c          |  3 +--
 src/storage/storage_driver.c        | 16 ++++------------
 src/uml/uml_driver.c                |  3 +--
 src/util/iohelper.c                 | 10 ++--------
 src/util/virhook.c                  |  3 +--
 src/util/virhostdev.c               | 20 ++++++++------------
 tests/securityselinuxlabeltest.c    |  4 ++--
 19 files changed, 42 insertions(+), 91 deletions(-)

diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c
index cc5190f..32c6655 100644
--- a/daemon/libvirtd.c
+++ b/daemon/libvirtd.c
@@ -1315,12 +1315,8 @@ int main(int argc, char **argv) {
     /* Read the config file if it exists*/
     if (remote_config_file &&
         daemonConfigLoadFile(config, remote_config_file, implicit_conf) < 0) {
-        virErrorPtr err = virGetLastError();
-        if (err && err->message)
-            VIR_ERROR(_("Can't load config file: %s: %s"),
-                      err->message, remote_config_file);
-        else
-            VIR_ERROR(_("Can't load config file: %s"), remote_config_file);
+        VIR_ERROR(_("Can't load config file: %s: %s"),
+                  virGetLastErrorMessage(), remote_config_file);
         exit(EXIT_FAILURE);
     }
 
diff --git a/examples/object-events/event-test.c b/examples/object-events/event-test.c
index 2063536..c1ff4a7 100644
--- a/examples/object-events/event-test.c
+++ b/examples/object-events/event-test.c
@@ -917,9 +917,8 @@ main(int argc, char **argv)
     }
 
     if (virEventRegisterDefaultImpl() < 0) {
-        virErrorPtr err = virGetLastError();
         fprintf(stderr, "Failed to register event implementation: %s\n",
-                err && err->message ? err->message: "Unknown error");
+                virGetLastErrorMessage());
         goto cleanup;
     }
 
@@ -972,17 +971,15 @@ main(int argc, char **argv)
         goto cleanup;
 
     if (virConnectSetKeepAlive(dconn, 5, 3) < 0) {
-        virErrorPtr err = virGetLastError();
         fprintf(stderr, "Failed to start keepalive protocol: %s\n",
-                err && err->message ? err->message : "Unknown error");
+                virGetLastErrorMessage());
         run = 0;
     }
 
     while (run) {
         if (virEventRunDefaultImpl() < 0) {
-            virErrorPtr err = virGetLastError();
             fprintf(stderr, "Failed to run event loop: %s\n",
-                    err && err->message ? err->message : "Unknown error");
+                    virGetLastErrorMessage());
         }
     }
 
diff --git a/src/bhyve/bhyve_driver.c b/src/bhyve/bhyve_driver.c
index 4fc504e..ddfcbc8 100644
--- a/src/bhyve/bhyve_driver.c
+++ b/src/bhyve/bhyve_driver.c
@@ -88,9 +88,8 @@ bhyveAutostartDomain(virDomainObjPtr vm, void *opaque)
         ret = virBhyveProcessStart(data->conn, data->driver, vm,
                                    VIR_DOMAIN_RUNNING_BOOTED, 0);
         if (ret < 0) {
-            virErrorPtr err = virGetLastError();
             VIR_ERROR(_("Failed to autostart VM '%s': %s"),
-                      vm->def->name, err ? err->message : _("unknown error"));
+                      vm->def->name, virGetLastErrorMessage());
         }
     }
     virObjectUnlock(vm);
diff --git a/src/libvirt.c b/src/libvirt.c
index 114e88c..0e7e435 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -770,10 +770,9 @@ virStateInitialize(bool privileged,
             if (virStateDriverTab[i]->stateInitialize(privileged,
                                                       callback,
                                                       opaque) < 0) {
-                virErrorPtr err = virGetLastError();
                 VIR_ERROR(_("Initialization of %s state driver failed: %s"),
                           virStateDriverTab[i]->name,
-                          err && err->message ? err->message : _("Unknown problem"));
+                          virGetLastErrorMessage());
                 return -1;
             }
         }
diff --git a/src/libxl/libxl_domain.c b/src/libxl/libxl_domain.c
index 14a900c..8128525 100644
--- a/src/libxl/libxl_domain.c
+++ b/src/libxl/libxl_domain.c
@@ -515,9 +515,8 @@ libxlDomainShutdownThread(void *opaque)
     libxlDomainDestroyInternal(driver, vm);
     libxlDomainCleanup(driver, vm);
     if (libxlDomainStart(driver, vm, false, -1) < 0) {
-        virErrorPtr err = virGetLastError();
         VIR_ERROR(_("Failed to restart VM '%s': %s"),
-                  vm->def->name, err ? err->message : _("unknown error"));
+                  vm->def->name, virGetLastErrorMessage());
     }
 
  endjob:
diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
index bf97c9c..e58bc22 100644
--- a/src/libxl/libxl_driver.c
+++ b/src/libxl/libxl_driver.c
@@ -311,7 +311,6 @@ libxlAutostartDomain(virDomainObjPtr vm,
                      void *opaque)
 {
     libxlDriverPrivatePtr driver = opaque;
-    virErrorPtr err;
     int ret = -1;
 
     virObjectLock(vm);
@@ -324,10 +323,9 @@ libxlAutostartDomain(virDomainObjPtr vm,
 
     if (vm->autostart && !virDomainObjIsActive(vm) &&
         libxlDomainStart(driver, vm, false, -1) < 0) {
-        err = virGetLastError();
         VIR_ERROR(_("Failed to autostart VM '%s': %s"),
                   vm->def->name,
-                  err ? err->message : _("unknown error"));
+                  virGetLastErrorMessage());
         goto endjob;
     }
 
diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c
index f889a34..2c45349 100644
--- a/src/locking/lock_daemon.c
+++ b/src/locking/lock_daemon.c
@@ -1264,12 +1264,8 @@ int main(int argc, char **argv) {
     /* Read the config file if it exists*/
     if (remote_config_file &&
         virLockDaemonConfigLoadFile(config, remote_config_file, implicit_conf) < 0) {
-        virErrorPtr err = virGetLastError();
-        if (err && err->message)
-            VIR_ERROR(_("Can't load config file: %s: %s"),
-                      err->message, remote_config_file);
-        else
-            VIR_ERROR(_("Can't load config file: %s"), remote_config_file);
+        VIR_ERROR(_("Can't load config file: %s: %s"),
+                  virGetLastErrorMessage(), remote_config_file);
         exit(EXIT_FAILURE);
     }
 
diff --git a/src/logging/log_daemon.c b/src/logging/log_daemon.c
index 90f8427..8f1ccc2 100644
--- a/src/logging/log_daemon.c
+++ b/src/logging/log_daemon.c
@@ -1021,12 +1021,8 @@ int main(int argc, char **argv) {
     /* Read the config file if it exists*/
     if (remote_config_file &&
         virLogDaemonConfigLoadFile(config, remote_config_file, implicit_conf) < 0) {
-        virErrorPtr err = virGetLastError();
-        if (err && err->message)
-            VIR_ERROR(_("Can't load config file: %s: %s"),
-                      err->message, remote_config_file);
-        else
-            VIR_ERROR(_("Can't load config file: %s"), remote_config_file);
+        VIR_ERROR(_("Can't load config file: %s: %s"),
+                  virGetLastErrorMessage(), remote_config_file);
         exit(EXIT_FAILURE);
     }
 
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
index a909b66..33dcfec 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -2290,12 +2290,9 @@ static int lxcContainerChild(void *data)
 
     if (ret != 0) {
         VIR_DEBUG("Tearing down container");
-        virErrorPtr err = virGetLastError();
-        if (err && err->message)
-            fprintf(stderr, "%s\n", err->message);
-        else
-            fprintf(stderr, "%s\n",
-                    _("Unknown failure in libvirt_lxc startup"));
+        fprintf(stderr,
+                _("Failure in libvirt_lxc startup: %s\n"),
+                virGetLastErrorMessage());
     }
 
     virCommandFree(cmd);
diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
index 0304354..dc3921b 100644
--- a/src/lxc/lxc_controller.c
+++ b/src/lxc/lxc_controller.c
@@ -2736,12 +2736,9 @@ int main(int argc, char *argv[])
 
  cleanup:
     if (rc < 0) {
-        virErrorPtr err = virGetLastError();
-        if (err && err->message)
-            fprintf(stderr, "%s\n", err->message);
-        else
-            fprintf(stderr, "%s\n",
-                    _("Unknown failure in libvirt_lxc startup"));
+        fprintf(stderr,
+                _("Failure in libvirt_lxc startup: %s\n"),
+                virGetLastErrorMessage());
     }
 
     virPidFileDelete(LXC_STATE_DIR, name);
diff --git a/src/lxc/lxc_domain.c b/src/lxc/lxc_domain.c
index 3177a62..57fd170 100644
--- a/src/lxc/lxc_domain.c
+++ b/src/lxc/lxc_domain.c
@@ -221,8 +221,8 @@ virLXCDomainObjPrivateXMLParse(xmlXPathContextPtr ctxt,
     unsigned long long thepid;
 
     if (virXPathULongLong("string(./init[1]/@pid)", ctxt, &thepid) < 0) {
-        virErrorPtr err = virGetLastError();
-        VIR_WARN("Failed to load init pid from state %s", err ? err->message : "null");
+        VIR_WARN("Failed to load init pid from state %s",
+                 virGetLastErrorMessage());
         priv->initpid = 0;
     } else {
         priv->initpid = thepid;
diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c
index 0044ee5..5164535 100644
--- a/src/lxc/lxc_process.c
+++ b/src/lxc/lxc_process.c
@@ -755,10 +755,9 @@ static void virLXCProcessMonitorInitNotify(virLXCMonitorPtr mon ATTRIBUTE_UNUSED
     priv->initpid = initpid;
 
     if (virLXCProcessGetNsInode(initpid, "pid", &inode) < 0) {
-        virErrorPtr err = virGetLastError();
         VIR_WARN("Cannot obtain pid NS inode for %llu: %s",
                  (unsigned long long)initpid,
-                 err && err->message ? err->message : "<unknown>");
+                 virGetLastErrorMessage());
         virResetLastError();
     }
     virDomainAuditInit(vm, initpid, inode);
@@ -1617,10 +1616,9 @@ virLXCProcessAutostartDomain(virDomainObjPtr vm,
                                  VIR_DOMAIN_RUNNING_BOOTED);
         virDomainAuditStart(vm, "booted", ret >= 0);
         if (ret < 0) {
-            virErrorPtr err = virGetLastError();
             VIR_ERROR(_("Failed to autostart VM '%s': %s"),
                       vm->def->name,
-                      err ? err->message : "");
+                      virGetLastErrorMessage());
         } else {
             virObjectEventPtr event =
                 virDomainEventLifecycleNewFromObj(vm,
diff --git a/src/rpc/virnettlscontext.c b/src/rpc/virnettlscontext.c
index 947038d..6e78623 100644
--- a/src/rpc/virnettlscontext.c
+++ b/src/rpc/virnettlscontext.c
@@ -1141,8 +1141,7 @@ int virNetTLSContextCheckCertificate(virNetTLSContextPtr ctxt,
     virObjectLock(ctxt);
     virObjectLock(sess);
     if (virNetTLSContextValidCertificate(ctxt, sess) < 0) {
-        virErrorPtr err = virGetLastError();
-        VIR_WARN("Certificate check failed %s", err && err->message ? err->message : "<unknown>");
+        VIR_WARN("Certificate check failed %s", virGetLastErrorMessage());
         if (ctxt->requireValidCert) {
             virReportError(VIR_ERR_AUTH_FAILED, "%s",
                            _("Failed to verify peer's certificate"));
diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c
index 1d42f24..38e90c0 100644
--- a/src/storage/storage_driver.c
+++ b/src/storage/storage_driver.c
@@ -98,10 +98,8 @@ storagePoolUpdateState(virStoragePoolObjPtr pool)
     active = false;
     if (backend->checkPool &&
         backend->checkPool(pool, &active) < 0) {
-        virErrorPtr err = virGetLastError();
         VIR_ERROR(_("Failed to initialize storage pool '%s': %s"),
-                  pool->def->name, err ? err->message :
-                  _("no error message found"));
+                  pool->def->name, virGetLastErrorMessage());
         goto error;
     }
 
@@ -112,12 +110,10 @@ storagePoolUpdateState(virStoragePoolObjPtr pool)
     if (active) {
         virStoragePoolObjClearVols(pool);
         if (backend->refreshPool(NULL, pool) < 0) {
-            virErrorPtr err = virGetLastError();
             if (backend->stopPool)
                 backend->stopPool(NULL, pool);
             VIR_ERROR(_("Failed to restart storage pool '%s': %s"),
-                      pool->def->name, err ? err->message :
-                      _("no error message found"));
+                      pool->def->name, virGetLastErrorMessage());
             goto error;
         }
     }
@@ -176,10 +172,8 @@ storageDriverAutostart(void)
             !virStoragePoolObjIsActive(pool)) {
             if (backend->startPool &&
                 backend->startPool(conn, pool) < 0) {
-                virErrorPtr err = virGetLastError();
                 VIR_ERROR(_("Failed to autostart storage pool '%s': %s"),
-                          pool->def->name, err ? err->message :
-                          _("no error message found"));
+                          pool->def->name, virGetLastErrorMessage());
                 virStoragePoolObjUnlock(pool);
                 continue;
             }
@@ -195,14 +189,12 @@ storageDriverAutostart(void)
             if (!stateFile ||
                 virStoragePoolSaveState(stateFile, pool->def) < 0 ||
                 backend->refreshPool(conn, pool) < 0) {
-                virErrorPtr err = virGetLastError();
                 if (stateFile)
                     unlink(stateFile);
                 if (backend->stopPool)
                     backend->stopPool(conn, pool);
                 VIR_ERROR(_("Failed to autostart storage pool '%s': %s"),
-                          pool->def->name, err ? err->message :
-                          _("no error message found"));
+                          pool->def->name, virGetLastErrorMessage());
             } else {
                 pool->active = true;
             }
diff --git a/src/uml/uml_driver.c b/src/uml/uml_driver.c
index 84e1df8..923c3f6 100644
--- a/src/uml/uml_driver.c
+++ b/src/uml/uml_driver.c
@@ -188,9 +188,8 @@ umlAutostartDomain(virDomainObjPtr vm,
         ret = umlStartVMDaemon(data->conn, data->driver, vm, false);
         virDomainAuditStart(vm, "booted", ret >= 0);
         if (ret < 0) {
-            virErrorPtr err = virGetLastError();
             VIR_ERROR(_("Failed to autostart VM '%s': %s"),
-                      vm->def->name, err ? err->message : _("unknown error"));
+                      vm->def->name, virGetLastErrorMessage());
         } else {
             virObjectEventPtr event =
                 virDomainEventLifecycleNewFromObj(vm,
diff --git a/src/util/iohelper.c b/src/util/iohelper.c
index 275f993..00f31e7 100644
--- a/src/util/iohelper.c
+++ b/src/util/iohelper.c
@@ -218,7 +218,6 @@ int
 main(int argc, char **argv)
 {
     const char *path;
-    virErrorPtr err;
     unsigned long long offset;
     unsigned long long length;
     int oflags = -1;
@@ -303,12 +302,7 @@ main(int argc, char **argv)
     return 0;
 
  error:
-    err = virGetLastError();
-    if (err) {
-        fprintf(stderr, "%s: %s\n", program_name, err->message);
-    } else {
-        fprintf(stderr, _("%s: unknown failure with %s\n"),
-                program_name, path);
-    }
+    fprintf(stderr, _("%s: failure with %s\n: %s"),
+            program_name, path, virGetLastErrorMessage());
     exit(EXIT_FAILURE);
 }
diff --git a/src/util/virhook.c b/src/util/virhook.c
index ba50598..d37d6da 100644
--- a/src/util/virhook.c
+++ b/src/util/virhook.c
@@ -297,9 +297,8 @@ virHookCall(int driver,
     ret = virCommandRun(cmd, NULL);
     if (ret < 0) {
         /* Convert INTERNAL_ERROR into known error.  */
-        virErrorPtr err = virGetLastError();
         virReportError(VIR_ERR_HOOK_SCRIPT_FAILED, "%s",
-                       err ? err->message : _("unknown error"));
+                       virGetLastErrorMessage());
     }
 
     virCommandFree(cmd);
diff --git a/src/util/virhostdev.c b/src/util/virhostdev.c
index 933c942..e90c2df 100644
--- a/src/util/virhostdev.c
+++ b/src/util/virhostdev.c
@@ -801,10 +801,9 @@ virHostdevReattachPCIDevice(virHostdevManagerPtr mgr,
     VIR_DEBUG("Reattaching PCI device %s", virPCIDeviceGetName(actual));
     if (virPCIDeviceReattach(actual, mgr->activePCIHostdevs,
                              mgr->inactivePCIHostdevs) < 0) {
-        virErrorPtr err = virGetLastError();
         VIR_ERROR(_("Failed to re-attach PCI device: %s"),
-                  err ? err->message : _("unknown error"));
-        virResetError(err);
+                  virGetLastErrorMessage());
+        virResetLastError();
     }
 }
 
@@ -829,10 +828,9 @@ virHostdevReAttachPCIDevices(virHostdevManagerPtr mgr,
     virObjectLock(mgr->inactivePCIHostdevs);
 
     if (!(pcidevs = virHostdevGetPCIHostDeviceList(hostdevs, nhostdevs))) {
-        virErrorPtr err = virGetLastError();
         VIR_ERROR(_("Failed to allocate PCI device list: %s"),
-                  err ? err->message : _("unknown error"));
-        virResetError(err);
+                  virGetLastErrorMessage());
+        virResetLastError();
         goto cleanup;
     }
 
@@ -883,10 +881,9 @@ virHostdevReAttachPCIDevices(virHostdevManagerPtr mgr,
         if (!actual ||
             virPCIDeviceListAdd(mgr->inactivePCIHostdevs, actual) < 0) {
 
-            virErrorPtr err = virGetLastError();
             VIR_ERROR(_("Failed to add PCI device %s to the inactive list"),
-                      err ? err->message : _("unknown error"));
-            virResetError(err);
+                      virGetLastErrorMessage());
+            virResetLastError();
         }
     }
 
@@ -928,10 +925,9 @@ virHostdevReAttachPCIDevices(virHostdevManagerPtr mgr,
         VIR_DEBUG("Resetting PCI device %s", virPCIDeviceGetName(pci));
         if (virPCIDeviceReset(pci, mgr->activePCIHostdevs,
                               mgr->inactivePCIHostdevs) < 0) {
-            virErrorPtr err = virGetLastError();
             VIR_ERROR(_("Failed to reset PCI device: %s"),
-                      err ? err->message : _("unknown error"));
-            virResetError(err);
+                      virGetLastErrorMessage());
+            virResetLastError();
         }
     }
 
diff --git a/tests/securityselinuxlabeltest.c b/tests/securityselinuxlabeltest.c
index f6caa30..0f6cb66 100644
--- a/tests/securityselinuxlabeltest.c
+++ b/tests/securityselinuxlabeltest.c
@@ -331,9 +331,9 @@ testSELinuxLabeling(const void *opaque)
         VIR_FREE(files[i].context);
     }
     VIR_FREE(files);
-    if (ret < 0) {
+    if (ret < 0)
         VIR_TEST_VERBOSE("%s\n", virGetLastErrorMessage());
-    }
+
     return ret;
 }
 
-- 
2.5.5




More information about the libvir-list mailing list