[PATCH 3/3] Ensure hotplug / monitor commands log taint warnings

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

 



Open the domain log file for hotplug and custom monitor command
usage to write taint warnings.

* src/qemu/qemu_domain.c, src/qemu/qemu_domain.h: Add
  qemuDomainOpenLogWrite/qemuDomainOpenLogRead methods
* src/qemu/qemu_process.c: Remove qemuProcessOpenLogFD
  and qemuProcessReadLogFD methods (moved to qemu_domain)
* src/qemu/qemu_process.c: Open log in hotplug and qemu
  monitor command functions
---
 src/qemu/qemu_domain.c  |   80 ++++++++++++++++++++++++++++++++++++++++++++++
 src/qemu/qemu_domain.h  |    5 +++
 src/qemu/qemu_driver.c  |   16 +++++++--
 src/qemu/qemu_process.c |   81 ++---------------------------------------------
 4 files changed, 101 insertions(+), 81 deletions(-)

diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 78839cc..202340f 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -33,8 +33,10 @@
 #include "cpu/cpu.h"
 #include "ignore-value.h"
 #include "uuid.h"
+#include "files.h"
 
 #include <sys/time.h>
+#include <fcntl.h>
 
 #include <libxml/xpathInternals.h>
 
@@ -699,6 +701,84 @@ cleanup:
     return ret;
 }
 
+
+int
+qemuDomainOpenLogWrite(struct qemud_driver *driver,
+                       virDomainObjPtr vm, bool append)
+{
+    char *logfile;
+    mode_t logmode;
+    int fd = -1;
+
+    if (virAsprintf(&logfile, "%s/%s.log", driver->logDir, vm->def->name) < 0) {
+        virReportOOMError();
+        return -1;
+    }
+
+    logmode = O_CREAT | O_WRONLY;
+    /* Only logrotate files in /var/log, so only append if running privileged */
+    if (driver->privileged || append)
+        logmode |= O_APPEND;
+    else
+        logmode |= O_TRUNC;
+
+    if ((fd = open(logfile, logmode, S_IRUSR | S_IWUSR)) < 0) {
+        virReportSystemError(errno,
+                             _("failed to create logfile %s"),
+                             logfile);
+        VIR_FREE(logfile);
+        return -1;
+    }
+    VIR_FREE(logfile);
+    if (virSetCloseExec(fd) < 0) {
+        virReportSystemError(errno, "%s",
+                             _("Unable to set VM logfile close-on-exec flag"));
+        VIR_FORCE_CLOSE(fd);
+        return -1;
+    }
+    return fd;
+}
+
+
+int
+qemuDomainOpenLogRead(struct qemud_driver *driver,
+                      virDomainObjPtr vm, off_t pos)
+{
+    char *logfile;
+    mode_t logmode = O_RDONLY;
+    int fd = -1;
+
+    if (virAsprintf(&logfile, "%s/%s.log", driver->logDir, vm->def->name) < 0) {
+        virReportOOMError();
+        return -1;
+    }
+
+    if ((fd = open(logfile, logmode)) < 0) {
+        virReportSystemError(errno,
+                             _("failed to create logfile %s"),
+                             logfile);
+        VIR_FREE(logfile);
+        return -1;
+    }
+    if (virSetCloseExec(fd) < 0) {
+        virReportSystemError(errno, "%s",
+                             _("Unable to set VM logfile close-on-exec flag"));
+        VIR_FORCE_CLOSE(fd);
+        VIR_FREE(logfile);
+        return -1;
+    }
+    if (pos < 0 || lseek(fd, pos, SEEK_SET) < 0) {
+        virReportSystemError(pos < 0 ? 0 : errno,
+                             _("Unable to seek to %lld in %s"),
+                             (long long) pos, logfile);
+        VIR_FORCE_CLOSE(fd);
+    }
+    VIR_FREE(logfile);
+    return fd;
+}
+
+
+
 void qemuDomainObjTaint(virDomainObjPtr obj,
                         int taint,
                         int logFD)
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
index dbef4e1..cab7991 100644
--- a/src/qemu/qemu_domain.h
+++ b/src/qemu/qemu_domain.h
@@ -112,6 +112,11 @@ char *qemuDomainFormatXML(struct qemud_driver *driver,
                           virDomainObjPtr vm,
                           int flags);
 
+int qemuDomainOpenLogWrite(struct qemud_driver *driver,
+                           virDomainObjPtr vm, bool append);
+int qemuDomainOpenLogRead(struct qemud_driver *driver,
+                          virDomainObjPtr vm, off_t pos);
+
 void qemuDomainObjTaint(virDomainObjPtr obj,
                         int taint,
                         int logFD);
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 13f9362..75e4631 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -3881,10 +3881,14 @@ qemuDomainAttachDeviceLive(virDomainObjPtr vm,
 {
     struct qemud_driver *driver = dom->conn->privateData;
     int ret = -1;
+    int logFD = -1;
+
+    if ((logFD = qemuDomainOpenLogWrite(driver, vm, true)) < 0)
+        return -1;
 
     switch (dev->type) {
     case VIR_DOMAIN_DEVICE_DISK:
-        qemuDomainObjCheckDiskTaint(driver, vm, dev->data.disk, -1);
+        qemuDomainObjCheckDiskTaint(driver, vm, dev->data.disk, logFD);
         ret = qemuDomainAttachDeviceDiskLive(driver, vm, dev, qemuCaps);
         if (!ret)
             dev->data.disk = NULL;
@@ -3897,7 +3901,7 @@ qemuDomainAttachDeviceLive(virDomainObjPtr vm,
         break;
 
     case VIR_DOMAIN_DEVICE_NET:
-        qemuDomainObjCheckNetTaint(vm, dev->data.net, -1);
+        qemuDomainObjCheckNetTaint(vm, dev->data.net, logFD);
         ret = qemuDomainAttachNetDevice(dom->conn, driver, vm,
                                         dev->data.net, qemuCaps);
         if (!ret)
@@ -3918,6 +3922,7 @@ qemuDomainAttachDeviceLive(virDomainObjPtr vm,
         break;
     }
 
+    VIR_FORCE_CLOSE(logFD);
     return ret;
 }
 
@@ -6982,6 +6987,7 @@ static int qemuDomainMonitorCommand(virDomainPtr domain, const char *cmd,
     int ret = -1;
     qemuDomainObjPrivatePtr priv;
     bool hmp;
+    int logFD = -1;
 
     virCheckFlags(VIR_DOMAIN_QEMU_MONITOR_COMMAND_HMP, -1);
 
@@ -7001,9 +7007,12 @@ static int qemuDomainMonitorCommand(virDomainPtr domain, const char *cmd,
         goto cleanup;
    }
 
+    if ((logFD = qemuDomainOpenLogWrite(driver, vm, true)) < 0)
+        goto cleanup;
+
     priv = vm->privateData;
 
-    qemuDomainObjTaint(vm, VIR_DOMAIN_TAINT_CUSTOM_MONITOR, -1);
+    qemuDomainObjTaint(vm, VIR_DOMAIN_TAINT_CUSTOM_MONITOR, logFD);
 
     hmp = !!(flags & VIR_DOMAIN_QEMU_MONITOR_COMMAND_HMP);
 
@@ -7021,6 +7030,7 @@ cleanup:
     if (vm)
         virDomainObjUnlock(vm);
     qemuDriverUnlock(driver);
+    VIR_FORCE_CLOSE(logFD);
     return ret;
 }
 
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index c521dbf..187c4c9 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -679,81 +679,6 @@ error:
     return ret;
 }
 
-static int
-qemuProcessLogFD(struct qemud_driver *driver, const char* name, bool append)
-{
-    char *logfile;
-    mode_t logmode;
-    int fd = -1;
-
-    if (virAsprintf(&logfile, "%s/%s.log", driver->logDir, name) < 0) {
-        virReportOOMError();
-        return -1;
-    }
-
-    logmode = O_CREAT | O_WRONLY;
-    /* Only logrotate files in /var/log, so only append if running privileged */
-    if (driver->privileged || append)
-        logmode |= O_APPEND;
-    else
-        logmode |= O_TRUNC;
-
-    if ((fd = open(logfile, logmode, S_IRUSR | S_IWUSR)) < 0) {
-        virReportSystemError(errno,
-                             _("failed to create logfile %s"),
-                             logfile);
-        VIR_FREE(logfile);
-        return -1;
-    }
-    VIR_FREE(logfile);
-    if (virSetCloseExec(fd) < 0) {
-        virReportSystemError(errno, "%s",
-                             _("Unable to set VM logfile close-on-exec flag"));
-        VIR_FORCE_CLOSE(fd);
-        return -1;
-    }
-    return fd;
-}
-
-
-static int
-qemuProcessLogReadFD(const char* logDir, const char* name, off_t pos)
-{
-    char *logfile;
-    mode_t logmode = O_RDONLY;
-    int fd = -1;
-
-    if (virAsprintf(&logfile, "%s/%s.log", logDir, name) < 0) {
-        qemuReportError(VIR_ERR_INTERNAL_ERROR,
-                        _("failed to build logfile name %s/%s.log"),
-                        logDir, name);
-        return -1;
-    }
-
-    if ((fd = open(logfile, logmode)) < 0) {
-        virReportSystemError(errno,
-                             _("failed to create logfile %s"),
-                             logfile);
-        VIR_FREE(logfile);
-        return -1;
-    }
-    if (virSetCloseExec(fd) < 0) {
-        virReportSystemError(errno, "%s",
-                             _("Unable to set VM logfile close-on-exec flag"));
-        VIR_FORCE_CLOSE(fd);
-        VIR_FREE(logfile);
-        return -1;
-    }
-    if (pos < 0 || lseek(fd, pos, SEEK_SET) < 0) {
-        virReportSystemError(pos < 0 ? 0 : errno,
-                             _("Unable to seek to %lld in %s"),
-                             (long long) pos, logfile);
-        VIR_FORCE_CLOSE(fd);
-    }
-    VIR_FREE(logfile);
-    return fd;
-}
-
 
 typedef int qemuProcessLogHandleOutput(virDomainObjPtr vm,
                                        const char *output,
@@ -1051,7 +976,7 @@ qemuProcessWaitForMonitor(struct qemud_driver* driver,
     virHashTablePtr paths = NULL;
     qemuDomainObjPrivatePtr priv;
 
-    if ((logfd = qemuProcessLogReadFD(driver->logDir, vm->def->name, pos)) < 0)
+    if ((logfd = qemuDomainOpenLogRead(driver, vm, pos)) < 0)
         return -1;
 
     if (VIR_ALLOC_N(buf, buf_size) < 0) {
@@ -2200,7 +2125,7 @@ int qemuProcessStart(virConnectPtr conn,
     }
 
     VIR_DEBUG0("Creating domain log file");
-    if ((logfile = qemuProcessLogFD(driver, vm->def->name, false)) < 0)
+    if ((logfile = qemuDomainOpenLogWrite(driver, vm, false)) < 0)
         goto cleanup;
 
     VIR_DEBUG0("Determining emulator version");
@@ -2463,7 +2388,7 @@ void qemuProcessStop(struct qemud_driver *driver,
         return;
     }
 
-    if ((logfile = qemuProcessLogFD(driver, vm->def->name, true)) < 0) {
+    if ((logfile = qemuDomainOpenLogWrite(driver, vm, true)) < 0) {
         /* To not break the normal domain shutdown process, skip the
          * timestamp log writing if failed on opening log file. */
         VIR_WARN("Unable to open logfile: %s",
-- 
1.7.4.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]