chiark / gitweb /
journald: properly unescape messages from /dev/kmsg
[elogind.git] / src / journal / journald.c
index 1d40fa843ef9092d8f9f7f113010b5862c31bd18..765c0e3428c91f0bfb3ed6a8e5accb68ab874dc4 100644 (file)
@@ -70,6 +70,7 @@
 #define RECHECK_AVAILABLE_SPACE_USEC (30*USEC_PER_SEC)
 
 #define N_IOVEC_META_FIELDS 17
+#define N_IOVEC_KERNEL_FIELDS 64
 
 #define ENTRY_SIZE_MAX (1024*1024*32)
 
@@ -1807,13 +1808,14 @@ static bool is_us(const char *pid) {
 }
 
 static void dev_kmsg_record(Server *s, char *p, size_t l) {
-        struct iovec iovec[N_IOVEC_META_FIELDS + 7];
+        struct iovec iovec[N_IOVEC_META_FIELDS + 7 + N_IOVEC_KERNEL_FIELDS];
         char *message = NULL, *syslog_priority = NULL, *syslog_pid = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *source_time = NULL;
         int priority, r;
-        unsigned n = 0;
+        unsigned n = 0, z = 0, j;
         usec_t usec;
-        char *identifier = NULL, *pid = NULL, *e, *f;
+        char *identifier = NULL, *pid = NULL, *e, *f, *k;
         uint64_t serial;
+        size_t pl;
 
         assert(s);
         assert(p);
@@ -1862,9 +1864,40 @@ static void dev_kmsg_record(Server *s, char *p, size_t l) {
         l -= (f - p) + 1;
         p = f + 1;
         e = memchr(p, '\n', l);
-        if (e)
+        if (!e)
+                return;
+        *e = 0;
+
+        pl = e - p;
+        l -= (e - p) + 1;
+        k = e + 1;
+
+        for (j = 0; l > 0 && j < N_IOVEC_KERNEL_FIELDS; j++) {
+                char *m;
+                /* Meta data fields attached */
+
+                if (*k != ' ')
+                        break;
+
+                k ++, l --;
+
+                e = memchr(k, '\n', l);
+                if (!e)
+                        return;
+
                 *e = 0;
 
+                m = cunescape_length_with_prefix(k, e - k, "_KERNEL_");
+                if (!m)
+                        break;
+
+                IOVEC_SET_STRING(iovec[n++], m);
+                z++;
+
+                l -= (e - k) + 1;
+                k = e + 1;
+        }
+
         if (asprintf(&source_time, "_SOURCE_MONOTONIC_TIMESTAMP=%llu",
                      (unsigned long long) usec) >= 0)
                 IOVEC_SET_STRING(iovec[n++], source_time);
@@ -1900,13 +1933,16 @@ static void dev_kmsg_record(Server *s, char *p, size_t l) {
                         IOVEC_SET_STRING(iovec[n++], syslog_facility);
         }
 
-        message = strappend("MESSAGE=", p);
+        message = cunescape_length_with_prefix(p, pl, "MESSAGE=");
         if (message)
                 IOVEC_SET_STRING(iovec[n++], message);
 
         dispatch_message(s, iovec, n, ELEMENTSOF(iovec), NULL, NULL, NULL, 0, NULL, priority);
 
 finish:
+        for (j = 0; j < z; j++)
+                free(iovec[j].iov_base);
+
         free(message);
         free(syslog_priority);
         free(syslog_identifier);