]> xenbits.xensource.com Git - libvirt.git/commitdiff
virlog: Introduce virLog{Get,Set}DefaultOutput
authorErik Skultety <eskultet@redhat.com>
Mon, 31 Oct 2016 11:50:24 +0000 (12:50 +0100)
committerErik Skultety <eskultet@redhat.com>
Thu, 15 Dec 2016 09:36:23 +0000 (10:36 +0100)
These helpers will manage the log destination defaults (fetch/set). The reason
for this is to stay consistent with the current daemon's behaviour with respect
to /etc/libvirt/<daemon>.conf file, since both assignment of an empty string
or not setting the log output variable at all trigger the daemon's decision on
the default log destination which depends on whether the daemon runs daemonized
or not.
This patch also changes the logic of the selection of the default
logging output compared to how it is done now. The main difference though is
that we should only really care if we're running daemonized or not, disregarding
the fact of (not) having a TTY completely (introduced by commit eba36a3878) as
that should be of the libvirtd's parent concern (what FD it will pass to it).

 Before:
 if (godaemon || !hasTTY):
     if (journald):
         use journald

 if (godaemon):
     if (privileged):
         use SYSCONFIG/libvirtd.log
     else:
         use XDG_CONFIG_HOME/libvirtd.log
 else:
     use stderr

 After:
 if (godaemon):
     if (journald):
         use journald

     else:
         if (privileged):
             use SYSCONFIG/libvirtd.log
         else:
             use XDG_CONFIG_HOME/libvirtd.log
 else:
     use stderr

Signed-off-by: Erik Skultety <eskultet@redhat.com>
src/libvirt_private.syms
src/util/virlog.c
src/util/virlog.h

index d2730f188b7e93d2e8bbcb4a3e3222dfb2fabc14..7beebbf27b5a909a4c4b79f875ba9214368c6b08 100644 (file)
@@ -1891,6 +1891,7 @@ virLogFilterFree;
 virLogFilterListFree;
 virLogFilterNew;
 virLogFindOutput;
+virLogGetDefaultOutput;
 virLogGetDefaultPriority;
 virLogGetFilters;
 virLogGetNbFilters;
@@ -1909,6 +1910,7 @@ virLogParseOutputs;
 virLogPriorityFromSyslog;
 virLogProbablyLogMessage;
 virLogReset;
+virLogSetDefaultOutput;
 virLogSetDefaultPriority;
 virLogSetFilters;
 virLogSetFromEnv;
index 77bc6b3c223fcbdf649e778d8b854769a9672533..a13b471a6371742079dd9481cd656a9e684b83a1 100644 (file)
@@ -50,6 +50,7 @@
 #include "virtime.h"
 #include "intprops.h"
 #include "virstring.h"
+#include "configmake.h"
 
 /* Journald output is only supported on Linux new enough to expose
  * htole64.  */
@@ -105,6 +106,7 @@ struct _virLogOutput {
     char *name;
 };
 
+static char *virLogDefaultOutput;
 static virLogOutputPtr *virLogOutputs;
 static size_t virLogNbOutputs;
 
@@ -147,6 +149,96 @@ virLogUnlock(void)
 }
 
 
+static int
+virLogSetDefaultOutputToStderr(void)
+{
+    return virAsprintf(&virLogDefaultOutput, "%d:stderr",
+                       virLogDefaultPriority);
+}
+
+
+static int
+virLogSetDefaultOutputToJournald(void)
+{
+    virLogPriority priority = virLogDefaultPriority;
+
+    /* By default we don't want to log too much stuff into journald as
+     * it may employ rate limiting and thus block libvirt execution. */
+    if (priority == VIR_LOG_DEBUG)
+        priority = VIR_LOG_INFO;
+
+    return virAsprintf(&virLogDefaultOutput, "%d:journald", priority);
+}
+
+
+static int
+virLogSetDefaultOutputToFile(const char *filename, bool privileged)
+{
+    int ret = -1;
+    char *logdir = NULL;
+    mode_t old_umask;
+
+    if (privileged) {
+        if (virAsprintf(&virLogDefaultOutput,
+                        "%d:file:%s/log/libvirt/%s", virLogDefaultPriority,
+                        LOCALSTATEDIR, filename) < 0)
+            goto cleanup;
+    } else {
+        if (!(logdir = virGetUserCacheDirectory()))
+            goto cleanup;
+
+        old_umask = umask(077);
+        if (virFileMakePath(logdir) < 0) {
+            umask(old_umask);
+            goto cleanup;
+        }
+        umask(old_umask);
+
+        if (virAsprintf(&virLogDefaultOutput, "%d:file:%s/%s",
+                        virLogDefaultPriority, logdir, filename) < 0)
+            goto cleanup;
+    }
+
+    ret = 0;
+ cleanup:
+    VIR_FREE(logdir);
+    return ret;
+}
+
+
+/*
+ * virLogSetDefaultOutput:
+ * @filename: the file that the output should be redirected to (only needed
+ *            when @godaemon equals true
+ * @godaemon: whether we're running daemonized
+ * @privileged: whether we're running with root privileges or not (session)
+ *
+ * Decides on what the default output (journald, file, stderr) should be
+ * according to @filename, @godaemon, @privileged. This function should be run
+ * exactly once at daemon startup, so no locks are used.
+ *
+ * Returns 0 on success, -1 in case of a failure.
+ */
+int
+virLogSetDefaultOutput(const char *filename, bool godaemon, bool privileged)
+{
+    if (!godaemon)
+        return virLogSetDefaultOutputToStderr();
+
+    if (access("/run/systemd/journal/socket", W_OK) >= 0)
+        return virLogSetDefaultOutputToJournald();
+
+    return virLogSetDefaultOutputToFile(filename, privileged);
+}
+
+
+char *
+virLogGetDefaultOutput(void)
+{
+    return virLogDefaultOutput;
+}
+
+
 static const char *
 virLogPriorityString(virLogPriority lvl)
 {
index 3f2d4223bf5b6847ead65654e1c1873140bfb622..b4ffecafb2cd8d1c735eff4f546a7ec68e51553d 100644 (file)
@@ -189,6 +189,8 @@ void virLogFilterFree(virLogFilterPtr filter);
 void virLogFilterListFree(virLogFilterPtr *list, int count);
 int virLogSetOutputs(const char *outputs) ATTRIBUTE_NONNULL(1);
 int virLogSetFilters(const char *filters);
+char *virLogGetDefaultOutput(void);
+int virLogSetDefaultOutput(const char *fname, bool godaemon, bool privileged);
 
 /*
  * Internal logging API