valeriyvdovin / rpms / systemd

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