[PATCH 2/6] Save daemon logs to libvirtd.log

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

 



Depending on whether it's run as root or user, the log is saved
in the local state dir or in $HOME/.libvirt. The file descriptor
is kept as a global variable used by following patches.
* daemon/libvirtd.c: add libvirtd.log as the output for logs

Signed-off-by: Daniel Veillard <veillard@xxxxxxxxxx>
---
 daemon/libvirtd.c |   90 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 85 insertions(+), 5 deletions(-)

diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c
index b2e5e20..42e8585 100644
--- a/daemon/libvirtd.c
+++ b/daemon/libvirtd.c
@@ -197,6 +197,8 @@ static int audit_logging = 0;
 
 #define DH_BITS 1024
 
+static int logFD = -1;
+
 static sig_atomic_t sig_errors = 0;
 static int sig_lasterrno = 0;
 static const char *argv0;
@@ -2696,19 +2698,73 @@ remoteReadSaslAllowedUsernameList (virConfPtr conf ATTRIBUTE_UNUSED,
 }
 #endif
 
+static int
+qemudLogFileOutput(const char *category ATTRIBUTE_UNUSED,
+                   int priority ATTRIBUTE_UNUSED,
+                   const char *funcname ATTRIBUTE_UNUSED,
+                   long long linenr ATTRIBUTE_UNUSED,
+                   const char *str, int len,
+                   void *data ATTRIBUTE_UNUSED) {
+    int ret;
+
+    if (logFD < 0)
+        return -1;
+    ret = safewrite(logFD, str, len);
+    return ret;
+}
+
 /*
  * Set up the logging environment
  * By default if daemonized all errors go to syslog and the logging
- * is also saved onto the logfile libvird.log, but if verbose or error
+ * is also saved onto the logfile libvirtd.log, but if verbose or error
  * debugging is asked for then output informations or debug.
  */
 static int
-qemudSetLogging(virConfPtr conf, const char *filename)
+qemudSetLogging(struct qemud_server *server, virConfPtr conf,
+                const char *filename)
 {
     int log_level = 0;
     char *log_filters = NULL;
     char *log_outputs = NULL;
-    int ret = -1;
+    char *log_file = NULL;
+    char *log_dir = NULL;
+    int ret = -1, rc;
+    int logmode = O_CREAT | O_WRONLY;
+
+    /*
+     * The log file can potentially grow a lot especially with full debug
+     * turned on.
+     * Only logrotate files in /var/log, so only append if running privileged
+     */
+    if (server->privileged) {
+        if (virAsprintf(&log_dir, "%s/log/libvirt", LOCALSTATEDIR) == -1)
+            goto out_of_memory;
+        if (virAsprintf(&log_file, "%s/log/libvirt/libvirtd.log",
+                        LOCALSTATEDIR) == -1)
+            goto out_of_memory;
+        logmode |= O_APPEND;
+    } else {
+        uid_t uid = geteuid();
+        char *userdir = virGetUserDirectory(uid);
+        if (!userdir)
+            goto free_and_fail;
+
+        if (virAsprintf(&log_dir, "%s/.libvirt", userdir) == -1)
+            goto out_of_memory;
+        if (virAsprintf(&log_file, "%s/.libvirt/libvirtd.log", userdir) == -1)
+            goto out_of_memory;
+        logmode |= O_TRUNC;
+    }
+    if ((rc = virFileMakePath(log_dir)) != 0) {
+        virReportSystemError(rc, _("Failed to create log directory '%s'"),
+                             log_dir);
+        goto free_and_fail;
+    }
+
+    if ((logFD = open(log_file, logmode, S_IRUSR | S_IWUSR)) < 0) {
+        virReportSystemError(errno, _("failed to create logfile %s"), log_file);
+        goto free_and_fail;
+    }
 
     virLogReset();
 
@@ -2762,6 +2818,14 @@ qemudSetLogging(virConfPtr conf, const char *filename)
     }
 
     /*
+     * Also make sure default goes to libvirtd.log
+     */
+    if (virLogDefineOutput(qemudLogFileOutput, NULL, NULL, 0,
+                           VIR_LOG_TO_FILE, "libvirtd.log", 0) <0)
+        goto free_and_fail;
+
+
+    /*
      * Command line override for --verbose
      */
     if ((verbose) && (virLogGetDefaultPriority() > VIR_LOG_INFO))
@@ -2772,7 +2836,23 @@ qemudSetLogging(virConfPtr conf, const char *filename)
 free_and_fail:
     VIR_FREE(log_filters);
     VIR_FREE(log_outputs);
+    VIR_FREE(log_file);
+    VIR_FREE(log_dir);
     return(ret);
+
+out_of_memory:
+    virReportOOMError();
+    goto free_and_fail;
+}
+
+/*
+ * Stop logging
+ */
+static void
+qemudStopLogging(void)
+{
+    virLogShutdown();
+    VIR_FORCE_CLOSE(logFD);
 }
 
 /* Read the config file if it exists.
@@ -2805,7 +2885,7 @@ remoteReadConfigFile (struct qemud_server *server, const char *filename)
     /*
      * First get all the logging settings and activate them
      */
-    if (qemudSetLogging(conf, filename) < 0)
+    if (qemudSetLogging(server, conf, filename) < 0)
         goto free_and_fail;
 
     GET_CONF_INT (conf, filename, listen_tcp);
@@ -3369,6 +3449,6 @@ error:
         qemudCleanup(server);
     if (pid_file)
         unlink (pid_file);
-    virLogShutdown();
+    qemudStopLogging();
     return ret;
 }
--
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]