[libvirt] [PATCH] eliminate strerror from qemu_driver.c: use virReportSystemError instead

Jim Meyering jim at meyering.net
Thu Jan 29 20:54:59 UTC 2009


I've begun eliminating the remaining problematic uses of strerror.
For example, those in virsh.c aren't a problem.
This is required for thread safety.
After this patch, there are about 60 uses left.

Note while reviewing:
  - are there places where I've added uses of "conn" that I should not have?
  - are there places where I've used NULL but it should be "conn"?
     (there may well be -- I haven't tried hard, but that matters less)
  - I've assumed that qemudEscapeMonitorArg failure will always be due to OOM
     Currently it is.

I removed the calls to qemudLog from each of the qemudSet* functions
because now each caller of those functions already diagnoses failure, and
includes strerror(errno) information, while the qemudLog calls did not.
In addition, the eliminated qemudLog calls would clobber errno, making
each callers' attempt to report strerror(errno) use a potentially invalid
and misleading errno value.

>From ebe2ee1fbf20edbb07a78ecce76de6866e2ef558 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering at redhat.com>
Date: Wed, 28 Jan 2009 19:20:08 +0100
Subject: [PATCH] eliminate strerror from qemu_driver.c: use virReportSystemError instead

* src/qemu_driver.c (qemudSetCloseExec): Don't use qemudLog here.
Now, every caller diagnoses the failure.
Simplify, now that there's no logging.
* src/qemu_driver.c (qemudSetNonBlock): Rewrite not to use qemudLog.
---
 src/qemu_driver.c |  126 ++++++++++++++++++++++++-----------------------------
 1 files changed, 57 insertions(+), 69 deletions(-)

diff --git a/src/qemu_driver.c b/src/qemu_driver.c
index 36e12b2..8fd789d 100644
--- a/src/qemu_driver.c
+++ b/src/qemu_driver.c
@@ -89,31 +89,19 @@ static void qemuDriverUnlock(struct qemud_driver *driver)

 static int qemudSetCloseExec(int fd) {
     int flags;
-    if ((flags = fcntl(fd, F_GETFD)) < 0)
-        goto error;
-    flags |= FD_CLOEXEC;
-    if ((fcntl(fd, F_SETFD, flags)) < 0)
-        goto error;
-    return 0;
- error:
-    qemudLog(QEMUD_ERR,
-             "%s", _("Failed to set close-on-exec file descriptor flag\n"));
-    return -1;
+    return ((flags = fcntl(fd, F_GETFD)) < 0
+             || fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0
+            ? -1
+            : 0);
 }


 static int qemudSetNonBlock(int fd) {
     int flags;
-    if ((flags = fcntl(fd, F_GETFL)) < 0)
-        goto error;
-    flags |= O_NONBLOCK;
-    if ((fcntl(fd, F_SETFL, flags)) < 0)
-        goto error;
-    return 0;
- error:
-    qemudLog(QEMUD_ERR,
-             "%s", _("Failed to set non-blocking file descriptor flag\n"));
-    return -1;
+    return ((flags = fcntl(fd, F_GETFL)) < 0
+             || fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0
+            ? -1
+            : 0);
 }


@@ -198,22 +186,21 @@ qemudLogReadFD(virConnectPtr conn, const char* logDir, const char* name, off_t p


     if ((fd = open(logfile, logmode)) < 0) {
-        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
-                         _("failed to create logfile %s: %s"),
-                         logfile, strerror(errno));
+        virReportSystemError(conn, errno,
+                             _("failed to create logfile %s"),
+                             logfile);
         return -1;
     }
     if (qemudSetCloseExec(fd) < 0) {
-        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
-                         _("Unable to set VM logfile close-on-exec flag: %s"),
-                         strerror(errno));
+        virReportSystemError(conn, errno, "%s",
+                             _("Unable to set VM logfile close-on-exec flag"));
         close(fd);
         return -1;
     }
     if (lseek(fd, pos, SEEK_SET) < 0) {
-        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
-                         _("Unable to seek to %lld in %s: %s"),
-                         (long long) pos, logfile, strerror(errno));
+        virReportSystemError(conn, errno,
+                             _("Unable to seek to %lld in %s"),
+                             (long long) pos, logfile);
         close(fd);
     }
     return fd;
@@ -441,8 +428,9 @@ qemudStartup(void) {
     }

     if (virFileMakePath(qemu_driver->stateDir) < 0) {
-        qemudLog(QEMUD_ERR, _("Failed to create state dir '%s': %s\n"),
-                 qemu_driver->stateDir, strerror(errno));
+        virReportSystemError(NULL, errno,
+                             _("Failed to create state dir '%s'"),
+                             qemu_driver->stateDir);
         goto error;
     }

@@ -854,8 +842,7 @@ static int qemudWaitForMonitor(virConnectPtr conn,
                                  qemudFindCharDevicePTYs,
                                  "console", 3000);
     if (close(logfd) < 0)
-        qemudLog(QEMUD_WARN, _("Unable to close logfile: %s\n"),
-                 strerror(errno));
+        virReportSystemError(NULL, errno, "%s", _("Unable to close logfile"));
     return ret;
 }

@@ -1134,30 +1121,30 @@ static int qemudStartVMDaemon(virConnectPtr conn,
     tmp = progenv;
     while (*tmp) {
         if (safewrite(vm->logfile, *tmp, strlen(*tmp)) < 0)
-            qemudLog(QEMUD_WARN, _("Unable to write envv to logfile %d: %s\n"),
-                     errno, strerror(errno));
+            virReportSystemError(NULL, errno,
+                                 "%s", _("Unable to write envv to logfile"));
         if (safewrite(vm->logfile, " ", 1) < 0)
-            qemudLog(QEMUD_WARN, _("Unable to write envv to logfile %d: %s\n"),
-                     errno, strerror(errno));
+            virReportSystemError(NULL, errno,
+                                 "%s", _("Unable to write envv to logfile"));
         tmp++;
     }
     tmp = argv;
     while (*tmp) {
         if (safewrite(vm->logfile, *tmp, strlen(*tmp)) < 0)
-            qemudLog(QEMUD_WARN, _("Unable to write argv to logfile %d: %s\n"),
-                     errno, strerror(errno));
+            virReportSystemError(NULL, errno,
+                                 "%s", _("Unable to write argv to logfile"));
         if (safewrite(vm->logfile, " ", 1) < 0)
-            qemudLog(QEMUD_WARN, _("Unable to write argv to logfile %d: %s\n"),
-                     errno, strerror(errno));
+            virReportSystemError(NULL, errno,
+                                 "%s", _("Unable to write argv to logfile"));
         tmp++;
     }
     if (safewrite(vm->logfile, "\n", 1) < 0)
-        qemudLog(QEMUD_WARN, _("Unable to write argv to logfile %d: %s\n"),
-                 errno, strerror(errno));
+        virReportSystemError(NULL, errno,
+                             "%s", _("Unable to write argv to logfile"));

     if ((pos = lseek(vm->logfile, 0, SEEK_END)) < 0)
-        qemudLog(QEMUD_WARN, _("Unable to seek to end of logfile %d: %s\n"),
-                 errno, strerror(errno));
+        virReportSystemError(NULL, errno,
+                             "%s", _("Unable to seek to end of logfile"));

     for (i = 0 ; i < ntapfds ; i++)
         FD_SET(tapfds[i], &keepfd);
@@ -1214,7 +1201,8 @@ static int qemudStartVMDaemon(virConnectPtr conn,


 static void qemudShutdownVMDaemon(virConnectPtr conn ATTRIBUTE_UNUSED,
-                                  struct qemud_driver *driver, virDomainObjPtr vm) {
+                                  struct qemud_driver *driver,
+                                  virDomainObjPtr vm) {
     if (!virDomainIsActive(vm))
         return;

@@ -1222,14 +1210,14 @@ static void qemudShutdownVMDaemon(virConnectPtr conn ATTRIBUTE_UNUSED,

     if (virKillProcess(vm->pid, 0) == 0 &&
         virKillProcess(vm->pid, SIGTERM) < 0)
-        qemudLog(QEMUD_ERROR, _("Failed to send SIGTERM to %s (%d): %s\n"),
-                 vm->def->name, vm->pid, strerror(errno));
+        virReportSystemError(conn, errno,
+                             _("Failed to send SIGTERM to %s (%d)"),
+                             vm->def->name, vm->pid);

     virEventRemoveHandle(vm->monitor_watch);

     if (close(vm->logfile) < 0)
-        qemudLog(QEMUD_WARN, _("Unable to close logfile %d: %s\n"),
-                 errno, strerror(errno));
+        virReportSystemError(conn, errno, "%s", _("Unable to close logfile"));
     if (vm->monitor != -1)
         close(vm->monitor);
     vm->logfile = -1;
@@ -1384,8 +1372,8 @@ qemudMonitorCommand (const virDomainObjPtr vm,

     /* Log, but ignore failures to write logfile for VM */
     if (safewrite(vm->logfile, buf, strlen(buf)) < 0)
-        qemudLog(QEMUD_WARN, _("Unable to log VM console data: %s\n"),
-                 strerror(errno));
+        virReportSystemError(NULL, errno,
+                             "%s", _("Unable to log VM console data"));

     *reply = buf;
     return 0;
@@ -1394,8 +1382,8 @@ qemudMonitorCommand (const virDomainObjPtr vm,
     if (buf) {
         /* Log, but ignore failures to write logfile for VM */
         if (safewrite(vm->logfile, buf, strlen(buf)) < 0)
-            qemudLog(QEMUD_WARN, _("Unable to log VM console data: %s\n"),
-                     strerror(errno));
+            virReportSystemError(NULL, errno,
+                                 "%s", _("Unable to log VM console data"));
         VIR_FREE(buf);
     }
     return -1;
@@ -1492,7 +1480,7 @@ static int kvmGetMaxVCPUs(void) {

     fd = open(KVM_DEVICE, O_RDONLY);
     if (fd < 0) {
-        qemudLog(QEMUD_WARN, _("Unable to open %s: %s\n"), KVM_DEVICE, strerror(errno));
+        virReportSystemError(NULL, errno, _("Unable to open %s"), KVM_DEVICE);
         return maxvcpus;
     }

@@ -1735,8 +1723,8 @@ qemudGetHostname (virConnectPtr conn)

     result = virGetHostname();
     if (result == NULL) {
-        qemudReportError (conn, NULL, NULL, VIR_ERR_SYSTEM_ERROR,
-                          "%s", strerror (errno));
+        virReportSystemError (conn, errno,
+                              "%s", _("failed to determine host name"));
         return NULL;
     }
     /* Caller frees this string. */
@@ -3821,8 +3809,8 @@ qemudDomainBlockPeek (virDomainPtr dom,
         /* The path is correct, now try to open it and get its size. */
         fd = open (path, O_RDONLY);
         if (fd == -1) {
-            qemudReportError (dom->conn, dom, NULL, VIR_ERR_SYSTEM_ERROR,
-                              "%s", strerror (errno));
+            virReportSystemError (dom->conn, errno,
+                                  _("%s: failed to open"), path);
             goto cleanup;
         }

@@ -3832,8 +3820,8 @@ qemudDomainBlockPeek (virDomainPtr dom,
          */
         if (lseek (fd, offset, SEEK_SET) == (off_t) -1 ||
             saferead (fd, buffer, size) == (ssize_t) -1) {
-            qemudReportError (dom->conn, dom, NULL, VIR_ERR_SYSTEM_ERROR,
-                              "%s", strerror (errno));
+            virReportSystemError (dom->conn, errno,
+                                  _("%s: failed to seek or read"), path);
             goto cleanup;
         }

@@ -3887,8 +3875,8 @@ qemudDomainMemoryPeek (virDomainPtr dom,

     /* Create a temporary filename. */
     if ((fd = mkstemp (tmp)) == -1) {
-        qemudReportError (dom->conn, dom, NULL, VIR_ERR_SYSTEM_ERROR,
-                          "%s", strerror (errno));
+        virReportSystemError (dom->conn, errno,
+                              _("mkstemp(\"%s\") failed"), tmp);
         goto cleanup;
     }

@@ -3904,8 +3892,9 @@ qemudDomainMemoryPeek (virDomainPtr dom,

     /* Read the memory file into buffer. */
     if (saferead (fd, buffer, size) == (ssize_t) -1) {
-        qemudReportError (dom->conn, dom, NULL, VIR_ERR_SYSTEM_ERROR,
-                          "%s", strerror (errno));
+        virReportSystemError (dom->conn, errno,
+                              _("failed to read temporary file "
+                                "created with template %s"), tmp);
         goto cleanup;
     }

@@ -4064,8 +4053,8 @@ qemudDomainMigratePrepare2 (virConnectPtr dconn,

         /* Get hostname */
         if (gethostname (hostname, HOST_NAME_MAX+1) == -1) {
-            qemudReportError (dconn, NULL, NULL, VIR_ERR_SYSTEM_ERROR,
-                              "%s", strerror (errno));
+            virReportSystemError (dconn, errno,
+                                  "%s", _("failed to determine host name"));
             goto cleanup;
         }

@@ -4235,8 +4224,7 @@ qemudDomainMigratePerform (virDomainPtr dom,
     /* Issue the migrate command. */
     safe_uri = qemudEscapeMonitorArg (uri);
     if (!safe_uri) {
-        qemudReportError (dom->conn, dom, NULL, VIR_ERR_SYSTEM_ERROR,
-                          "%s", strerror (errno));
+        virReportOOMError (dom->conn);
         goto cleanup;
     }
     snprintf (cmd, sizeof cmd, "migrate \"%s\"", safe_uri);
--
1.6.1.2.418.gd79e6




More information about the libvir-list mailing list