]> xenbits.xensource.com Git - libvirt.git/commitdiff
qemu: domain: Implement helper for one-shot log entries to the VM log file
authorPeter Krempa <pkrempa@redhat.com>
Tue, 7 Jun 2016 14:19:03 +0000 (16:19 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 7 Jun 2016 16:10:29 +0000 (18:10 +0200)
Along with the virtlogd addition of the log file appending API implement
a helper for logging one-shot entries to the log file including the
fallback approach of using direct file access.

This will be used for noting the shutdown of the qemu proces and
possibly other actions such as VM migration and other critical VM
lifecycle events.

src/qemu/qemu_domain.c
src/qemu/qemu_domain.h

index 7e64545066dc32b15563bca22387ce62c3b02958..b14afe04401626bf83588e094b311eafb3dca068 100644 (file)
@@ -3506,6 +3506,68 @@ ssize_t qemuDomainLogContextRead(qemuDomainLogContextPtr ctxt,
 }
 
 
+/**
+ * qemuDomainLogAppendMessage:
+ *
+ * This is a best-effort attempt to add a log message to the qemu log file
+ * either by using virtlogd or the legacy approach */
+int
+qemuDomainLogAppendMessage(virQEMUDriverPtr driver,
+                           virDomainObjPtr vm,
+                           const char *fmt,
+                           ...)
+{
+    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+    virLogManagerPtr manager = NULL;
+    va_list ap;
+    char *path = NULL;
+    int writefd = -1;
+    char *message = NULL;
+    int ret = -1;
+
+    va_start(ap, fmt);
+
+    if (virVasprintf(&message, fmt, ap) < 0)
+        goto cleanup;
+
+    VIR_DEBUG("Append log message (vm='%s' message='%s) stdioLogD=%d",
+              vm->def->name, message, cfg->stdioLogD);
+
+    if (virAsprintf(&path, "%s/%s.log", cfg->logDir, vm->def->name) < 0)
+        goto cleanup;
+
+    if (cfg->stdioLogD) {
+        if (!(manager = virLogManagerNew(virQEMUDriverIsPrivileged(driver))))
+            goto cleanup;
+
+        if (virLogManagerDomainAppendMessage(manager, "qemu", vm->def->uuid,
+                                             vm->def->name, path, message, 0) < 0)
+            goto cleanup;
+    } else {
+        if ((writefd = open(path, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR)) < 0) {
+            virReportSystemError(errno, _("failed to create logfile %s"),
+                                 path);
+            goto cleanup;
+        }
+
+        if (safewrite(writefd, message, strlen(message)) < 0)
+            goto cleanup;
+    }
+
+    ret = 0;
+
+ cleanup:
+    va_end(ap);
+    VIR_FREE(message);
+    VIR_FORCE_CLOSE(writefd);
+    virLogManagerFree(manager);
+    virObjectUnref(cfg);
+    VIR_FREE(path);
+
+    return ret;
+}
+
+
 int qemuDomainLogContextGetWriteFD(qemuDomainLogContextPtr ctxt)
 {
     return ctxt->writefd;
index a9a7295557ca080fddb28843015338a61dd70ce6..25381b1310180dd0d8beae0cdab94938edab759f 100644 (file)
@@ -480,6 +480,11 @@ void qemuDomainLogContextFree(qemuDomainLogContextPtr ctxt);
 
 virLogManagerPtr qemuDomainLogContextGetManager(qemuDomainLogContextPtr ctxt);
 
+int qemuDomainLogAppendMessage(virQEMUDriverPtr driver,
+                               virDomainObjPtr vm,
+                               const char *fmt,
+                               ...) ATTRIBUTE_FMT_PRINTF(3, 4);
+
 const char *qemuFindQemuImgBinary(virQEMUDriverPtr driver);
 
 int qemuDomainSnapshotWriteMetadata(virDomainObjPtr vm,