[libvirt] [PATCH 1/5] Tighten libvirt's parsing of logging environment variables

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

 



Don't convert high priority levels to the debug level. Don't parse
LIBVIRT_LOG_FILTERS and LIBVIRT_LOG_OUTPUTS when they're set to the
empty string. Warn when the user specifies an invalid value (empty
string remains a noop).
---

 src/libvirt.c |   13 ++++++++-----
 src/logging.c |   52 ++++++++++++++++++++++++++++++++--------------------
 2 files changed, 40 insertions(+), 25 deletions(-)

diff --git a/src/libvirt.c b/src/libvirt.c
index 61a9b7c..9e96410 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -274,21 +274,24 @@ virInitialize(void)
         return -1;
 
     debugEnv = getenv("LIBVIRT_DEBUG");
-    if (debugEnv && *debugEnv && *debugEnv != '0') {
-        if (STREQ(debugEnv, "2") || STREQ(debugEnv, "info"))
+    if (debugEnv && *debugEnv) {
+        if (STREQ(debugEnv, "1") || STREQ(debugEnv, "debug"))
+            virLogSetDefaultPriority(VIR_LOG_DEBUG);
+        else if (STREQ(debugEnv, "2") || STREQ(debugEnv, "info"))
             virLogSetDefaultPriority(VIR_LOG_INFO);
         else if (STREQ(debugEnv, "3") || STREQ(debugEnv, "warning"))
             virLogSetDefaultPriority(VIR_LOG_WARN);
         else if (STREQ(debugEnv, "4") || STREQ(debugEnv, "error"))
             virLogSetDefaultPriority(VIR_LOG_ERROR);
         else
-            virLogSetDefaultPriority(VIR_LOG_DEBUG);
+            VIR_WARN0(_("Ignoring invalid log level setting."));
     }
     debugEnv = getenv("LIBVIRT_LOG_FILTERS");
-    if (debugEnv)
+    if (debugEnv && *debugEnv)
         virLogParseFilters(debugEnv);
+
     debugEnv = getenv("LIBVIRT_LOG_OUTPUTS");
-    if (debugEnv)
+    if (debugEnv && *debugEnv)
         virLogParseOutputs(debugEnv);
 
     DEBUG0("register drivers");
diff --git a/src/logging.c b/src/logging.c
index 46a4f9f..27d6e4b 100644
--- a/src/logging.c
+++ b/src/logging.c
@@ -314,8 +314,10 @@ error:
  * Returns 0 if successful, -1 in case of error.
  */
 int virLogSetDefaultPriority(int priority) {
-    if ((priority < VIR_LOG_DEBUG) || (priority > VIR_LOG_ERROR))
+    if ((priority < VIR_LOG_DEBUG) || (priority > VIR_LOG_ERROR)) {
+        VIR_WARN0(_("Ignoring invalid log level setting."));
         return(-1);
+    }
     if (!virLogInitialized)
         virLogStartup();
     virLogDefaultPriority = priority;
@@ -681,7 +683,8 @@ int virLogParseOutputs(const char *outputs) {
     const char *cur = outputs, *str;
     char *name;
     int prio;
-    int ret = 0;
+    int ret = -1;
+    int count = 0;
 
     if (cur == NULL)
         return(-1);
@@ -690,53 +693,57 @@ int virLogParseOutputs(const char *outputs) {
     while (*cur != 0) {
         prio= virParseNumber(&cur);
         if ((prio < VIR_LOG_DEBUG) || (prio > VIR_LOG_ERROR))
-            return(-1);
+            goto cleanup;
         if (*cur != ':')
-            return(-1);
+            goto cleanup;
         cur++;
         if (STREQLEN(cur, "stderr", 6)) {
             cur += 6;
             if (virLogAddOutputToStderr(prio) == 0)
-                ret++;
+                count++;
         } else if (STREQLEN(cur, "syslog", 6)) {
             cur += 6;
             if (*cur != ':')
-                return(-1);
+                goto cleanup;
             cur++;
             str = cur;
             while ((*cur != 0) && (!IS_SPACE(cur)))
                 cur++;
             if (str == cur)
-                return(-1);
+                goto cleanup;
 #if HAVE_SYSLOG_H
             name = strndup(str, cur - str);
             if (name == NULL)
-                return(-1);
+                goto cleanup;
             if (virLogAddOutputToSyslog(prio, name) == 0)
-                ret++;
+                count++;
             VIR_FREE(name);
 #endif /* HAVE_SYSLOG_H */
         } else if (STREQLEN(cur, "file", 4)) {
             cur += 4;
             if (*cur != ':')
-                return(-1);
+                goto cleanup;
             cur++;
             str = cur;
             while ((*cur != 0) && (!IS_SPACE(cur)))
                 cur++;
             if (str == cur)
-                return(-1);
+                goto cleanup;
             name = strndup(str, cur - str);
             if (name == NULL)
-                return(-1);
+                goto cleanup;
             if (virLogAddOutputToFile(prio, name) == 0)
-                ret++;
+                count++;
             VIR_FREE(name);
         } else {
-            return(-1);
+            goto cleanup;
         }
         virSkipSpaces(&cur);
     }
+    ret = count;
+cleanup:
+    if (ret == -1)
+        VIR_WARN0(_("Ignoring invalid log output setting."));
     return(ret);
 }
 
@@ -762,7 +769,8 @@ int virLogParseFilters(const char *filters) {
     const char *cur = filters, *str;
     char *name;
     int prio;
-    int ret = 0;
+    int ret = -1;
+    int count = 0;
 
     if (cur == NULL)
         return(-1);
@@ -771,22 +779,26 @@ int virLogParseFilters(const char *filters) {
     while (*cur != 0) {
         prio= virParseNumber(&cur);
         if ((prio < VIR_LOG_DEBUG) || (prio > VIR_LOG_ERROR))
-            return(-1);
+            goto cleanup;
         if (*cur != ':')
-            return(-1);
+            goto cleanup;
         cur++;
         str = cur;
         while ((*cur != 0) && (!IS_SPACE(cur)))
             cur++;
         if (str == cur)
-            return(-1);
+            goto cleanup;
         name = strndup(str, cur - str);
         if (name == NULL)
-            return(-1);
+            goto cleanup;
         if (virLogDefineFilter(name, prio, 0) >= 0)
-            ret++;
+            count++;
         VIR_FREE(name);
         virSkipSpaces(&cur);
     }
+    ret = count;
+cleanup:
+    if (ret == -1)
+        VIR_WARN0(_("Ignoring invalid log filter setting."));
     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]