File libvirt-udev-fix-crash-in-libudev-logging.patch of Package libvirt

From 201159d5a0cc9a8f94a644279c6b6c6cfd3033a7 Mon Sep 17 00:00:00 2001
Message-Id: <201159d5a0cc9a8f94a644279c6b6c6cfd3033a7.1373271643.git.jdenemar@redhat.com>
From: =?UTF-8?q?J=C3=A1n=20Tomko?= <jtomko@redhat.com>
Date: Fri, 14 Jun 2013 14:22:47 +0200
Subject: [PATCH] udev: fix crash in libudev logging

Call virLogVMessage instead of virLogMessage, since libudev
called us with a va_list object, not a list of arguments.

Honor message priority and strip the trailing newline.

https://bugzilla.redhat.com/show_bug.cgi?id=971904
(cherry picked from commit f753dd62f951cc62e164421d0c6491f39e4c68ad)

Conflicts:
	src/libvirt_private.syms: [1]
	src/node_device/node_device_udev.c [2] [3] [4]
	src/util/logging.c [5] (context)
	src/util/logging.h [2] [3] [4] (context)

[1] missing 936d95d3: Rename logging.{c,h} to virlog.{c,h}
[2] missing 0225c566: Include filename explicitly in logging APIs
[3] missing e8fd8757: Change logging category parameter into an enum
[4] missing 37f7a1fa: Add metadata to virLogOutputFunc
[5] missing f6430390: Add systemd journal support
---
 src/libvirt_private.syms           |  2 ++
 src/node_device/node_device_udev.c | 40 +++++++++++++++++++++++++++++---------
 src/util/logging.c                 | 25 ++++++++++++++++++++++++
 src/util/logging.h                 |  1 +
 4 files changed, 59 insertions(+), 9 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 5a4039d..4669293 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -810,11 +810,13 @@ virLogMessage;
 virLogParseDefaultPriority;
 virLogParseFilters;
 virLogParseOutputs;
+virLogPriorityFromSyslog;
 virLogReset;
 virLogSetBufferSize;
 virLogSetDefaultPriority;
 virLogSetFromEnv;
 virLogUnlock;
+virLogVMessage;
 
 
 # memory.h
diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c
index ba17010..818bd08 100644
--- a/src/node_device/node_device_udev.c
+++ b/src/node_device/node_device_udev.c
@@ -350,15 +350,36 @@ static int udevGenerateDeviceName(struct udev_device *device,
 }
 
 
-static void udevLogFunction(struct udev *udev ATTRIBUTE_UNUSED,
-                            int priority ATTRIBUTE_UNUSED,
-                            const char *file,
-                            int line,
-                            const char *fn,
-                            const char *fmt,
-                            va_list args)
+typedef void (*udevLogFunctionPtr)(struct udev *udev,
+                                   int priority,
+                                   const char *file,
+                                   int line,
+                                   const char *fn,
+                                   const char *format,
+                                   va_list args);
+
+static void
+ATTRIBUTE_FMT_PRINTF(6,0)
+udevLogFunction(struct udev *udev ATTRIBUTE_UNUSED,
+                int priority,
+                const char *file,
+                int line,
+                const char *fn,
+                const char *fmt,
+                va_list args)
 {
-    VIR_ERROR_INT(file, fn, line, fmt, args);
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    const char *format = NULL;
+
+    virBufferAdd(&buf, fmt, -1);
+    virBufferTrim(&buf, "\n", -1);
+
+    format = virBufferContentAndReset(&buf);
+
+    virLogVMessage(file, virLogPriorityFromSyslog(priority),
+                   fn, line, 0, format ? format : fmt, args);
+
+    VIR_FREE(format);
 }
 
 
@@ -1662,7 +1683,8 @@ static int udevDeviceMonitorStartup(int privileged ATTRIBUTE_UNUSED)
      * its return value.
      */
     udev = udev_new();
-    udev_set_log_fn(udev, udevLogFunction);
+    /* cast to get rid of missing-format-attribute warning */
+    udev_set_log_fn(udev, (udevLogFunctionPtr) udevLogFunction);
 
     priv->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
     if (priv->udev_monitor == NULL) {
diff --git a/src/util/logging.c b/src/util/logging.c
index cdaf86e..ae631a9 100644
--- a/src/util/logging.c
+++ b/src/util/logging.c
@@ -919,6 +919,31 @@ static int virLogAddOutputToSyslog(int priority, const char *ident) {
     }
     return 0;
 }
+
+int virLogPriorityFromSyslog(int priority)
+{
+    switch (priority) {
+    case LOG_EMERG:
+    case LOG_ALERT:
+    case LOG_CRIT:
+    case LOG_ERR:
+        return VIR_LOG_ERROR;
+    case LOG_WARNING:
+    case LOG_NOTICE:
+        return VIR_LOG_WARN;
+    case LOG_INFO:
+        return VIR_LOG_INFO;
+    case LOG_DEBUG:
+        return VIR_LOG_DEBUG;
+    }
+    return VIR_LOG_ERROR;
+}
+
+#else /* HAVE_SYSLOG_H */
+int virLogPriorityFromSyslog(int priority ATTRIBUTE_UNUSED)
+{
+    return VIR_LOG_ERROR;
+}
 #endif /* HAVE_SYSLOG_H */
 
 #define IS_SPACE(cur)                                                   \
diff --git a/src/util/logging.h b/src/util/logging.h
index 256e816..bc1355b 100644
--- a/src/util/logging.h
+++ b/src/util/logging.h
@@ -137,6 +137,7 @@ extern int virLogReset(void);
 extern int virLogParseDefaultPriority(const char *priority);
 extern int virLogParseFilters(const char *filters);
 extern int virLogParseOutputs(const char *output);
+extern int virLogPriorityFromSyslog(int priority);
 extern void virLogMessage(const char *category, int priority,
                           const char *funcname, long long linenr,
                           unsigned int flags,
-- 
1.8.2.1

openSUSE Build Service is sponsored by