Blame SOURCES/0643-journal-forward-messages-from-dev-log-unmodified-to-.patch

17b0f1
From d82c40a2377b487ef83aa1fb907ec275a1b3e86e Mon Sep 17 00:00:00 2001
17b0f1
From: Michal Sekletar <msekleta@redhat.com>
17b0f1
Date: Wed, 30 May 2018 16:27:22 +0200
17b0f1
Subject: [PATCH] journal: forward messages from /dev/log unmodified to
17b0f1
 syslog.socket
17b0f1
17b0f1
(cherry picked from commit bb3ff70a86faff85fe482995c8ba5332b1a34f76)
17b0f1
17b0f1
Resolves: #1409659
17b0f1
---
17b0f1
 src/journal/journald-server.c |  2 +-
17b0f1
 src/journal/journald-syslog.c | 39 ++++++++++++++++++++++-------------
17b0f1
 src/journal/journald-syslog.h |  2 +-
17b0f1
 3 files changed, 27 insertions(+), 16 deletions(-)
17b0f1
17b0f1
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
17b0f1
index 7c69061f47..7e67e055e3 100644
17b0f1
--- a/src/journal/journald-server.c
17b0f1
+++ b/src/journal/journald-server.c
17b0f1
@@ -1294,7 +1294,7 @@ int server_process_datagram(sd_event_source *es, int fd, uint32_t revents, void
17b0f1
 
17b0f1
         if (fd == s->syslog_fd) {
17b0f1
                 if (n > 0 && n_fds == 0)
17b0f1
-                        server_process_syslog_message(s, strstrip(s->buffer), ucred, tv, label, label_len);
17b0f1
+                        server_process_syslog_message(s, s->buffer, n, ucred, tv, label, label_len);
17b0f1
                 else if (n_fds > 0)
17b0f1
                         log_warning("Got file descriptors via syslog socket. Ignoring.");
17b0f1
 
17b0f1
diff --git a/src/journal/journald-syslog.c b/src/journal/journald-syslog.c
17b0f1
index b499a0d381..01d2bf69f4 100644
17b0f1
--- a/src/journal/journald-syslog.c
17b0f1
+++ b/src/journal/journald-syslog.c
17b0f1
@@ -109,7 +109,7 @@ static void forward_syslog_iovec(Server *s, const struct iovec *iovec, unsigned
17b0f1
                 log_debug_errno(errno, "Failed to forward syslog message: %m");
17b0f1
 }
17b0f1
 
17b0f1
-static void forward_syslog_raw(Server *s, int priority, const char *buffer, const struct ucred *ucred, const struct timeval *tv) {
17b0f1
+static void forward_syslog_raw(Server *s, int priority, const char *buffer, size_t buffer_len, const struct ucred *ucred, const struct timeval *tv) {
17b0f1
         struct iovec iovec;
17b0f1
 
17b0f1
         assert(s);
17b0f1
@@ -118,7 +118,9 @@ static void forward_syslog_raw(Server *s, int priority, const char *buffer, cons
17b0f1
         if (LOG_PRI(priority) > s->max_level_syslog)
17b0f1
                 return;
17b0f1
 
17b0f1
-        IOVEC_SET_STRING(iovec, buffer);
17b0f1
+        iovec.iov_base = (char *) buffer;
17b0f1
+        iovec.iov_len = buffer_len;
17b0f1
+
17b0f1
         forward_syslog_iovec(s, &iovec, 1, ucred, tv);
17b0f1
 }
17b0f1
 
17b0f1
@@ -311,40 +313,49 @@ static void syslog_skip_date(char **buf) {
17b0f1
 void server_process_syslog_message(
17b0f1
         Server *s,
17b0f1
         const char *buf,
17b0f1
+        size_t buf_len,
17b0f1
         const struct ucred *ucred,
17b0f1
         const struct timeval *tv,
17b0f1
         const char *label,
17b0f1
         size_t label_len) {
17b0f1
 
17b0f1
         char syslog_priority[sizeof("PRIORITY=") + DECIMAL_STR_MAX(int)],
17b0f1
-             syslog_facility[sizeof("SYSLOG_FACILITY=") + DECIMAL_STR_MAX(int)];
17b0f1
+             syslog_facility[sizeof("SYSLOG_FACILITY=") + DECIMAL_STR_MAX(int)], *msg;
17b0f1
         const char *message = NULL, *syslog_identifier = NULL, *syslog_pid = NULL;
17b0f1
         struct iovec iovec[N_IOVEC_META_FIELDS + 6];
17b0f1
-        unsigned n = 0;
17b0f1
+        unsigned n = 0, i;
17b0f1
         int priority = LOG_USER | LOG_INFO;
17b0f1
         _cleanup_free_ char *identifier = NULL, *pid = NULL;
17b0f1
-        const char *orig;
17b0f1
 
17b0f1
         assert(s);
17b0f1
         assert(buf);
17b0f1
 
17b0f1
-        orig = buf;
17b0f1
-        syslog_parse_priority(&buf, &priority, true);
17b0f1
+        /* We are creating copy of the message because we want to forward original message verbatim to the legacy
17b0f1
+           syslog implementation */
17b0f1
+        for (i = buf_len; i > 0; i--)
17b0f1
+                if (!strchr(WHITESPACE, buf[i-1]))
17b0f1
+                        break;
17b0f1
+
17b0f1
+        msg = newa(char, i + 1);
17b0f1
+        *((char *) mempcpy(msg, buf, i)) = 0;
17b0f1
+        msg += strspn(msg, WHITESPACE);
17b0f1
+
17b0f1
+        syslog_parse_priority((const char **)&msg, &priority, true);
17b0f1
 
17b0f1
         if (s->forward_to_syslog)
17b0f1
-                forward_syslog_raw(s, priority, orig, ucred, tv);
17b0f1
+                forward_syslog_raw(s, priority, buf, buf_len, ucred, tv);
17b0f1
 
17b0f1
-        syslog_skip_date((char**) &buf;;
17b0f1
-        syslog_parse_identifier(&buf, &identifier, &pid;;
17b0f1
+        syslog_skip_date(&msg;;
17b0f1
+        syslog_parse_identifier((const char**)&msg, &identifier, &pid;;
17b0f1
 
17b0f1
         if (s->forward_to_kmsg)
17b0f1
-                server_forward_kmsg(s, priority, identifier, buf, ucred);
17b0f1
+                server_forward_kmsg(s, priority, identifier, msg, ucred);
17b0f1
 
17b0f1
         if (s->forward_to_console)
17b0f1
-                server_forward_console(s, priority, identifier, buf, ucred);
17b0f1
+                server_forward_console(s, priority, identifier, msg, ucred);
17b0f1
 
17b0f1
         if (s->forward_to_wall)
17b0f1
-                server_forward_wall(s, priority, identifier, buf, ucred);
17b0f1
+                server_forward_wall(s, priority, identifier, msg, ucred);
17b0f1
 
17b0f1
         IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=syslog");
17b0f1
 
17b0f1
@@ -368,7 +379,7 @@ void server_process_syslog_message(
17b0f1
                         IOVEC_SET_STRING(iovec[n++], syslog_pid);
17b0f1
         }
17b0f1
 
17b0f1
-        message = strjoina("MESSAGE=", buf);
17b0f1
+        message = strjoina("MESSAGE=", msg);
17b0f1
         if (message)
17b0f1
                 IOVEC_SET_STRING(iovec[n++], message);
17b0f1
 
17b0f1
diff --git a/src/journal/journald-syslog.h b/src/journal/journald-syslog.h
17b0f1
index 3774ebdf05..e593be99ab 100644
17b0f1
--- a/src/journal/journald-syslog.h
17b0f1
+++ b/src/journal/journald-syslog.h
17b0f1
@@ -29,7 +29,7 @@ size_t syslog_parse_identifier(const char **buf, char **identifier, char **pid);
17b0f1
 
17b0f1
 void server_forward_syslog(Server *s, int priority, const char *identifier, const char *message, const struct ucred *ucred, const struct timeval *tv);
17b0f1
 
17b0f1
-void server_process_syslog_message(Server *s, const char *buf, const struct ucred *ucred, const struct timeval *tv, const char *label, size_t label_len);
17b0f1
+void server_process_syslog_message(Server *s, const char *buf, size_t buf_len, const struct ucred *ucred, const struct timeval *tv, const char *label, size_t label_len);
17b0f1
 int server_open_syslog_socket(Server *s);
17b0f1
 
17b0f1
 void server_maybe_warn_forward_syslog_missed(Server *s);