ryantimwilson / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
5808e7
From d8fabe7a6839eeb0d5d0504471f2d18b07545238 Mon Sep 17 00:00:00 2001
5808e7
From: Lennart Poettering <lennart@poettering.net>
5808e7
Date: Tue, 12 May 2020 18:53:35 +0200
5808e7
Subject: [PATCH] journald: rework end of line marker handling to use a field
5808e7
 table
5808e7
5808e7
(cherry picked from commit 549b7379ba404c33fd448d2bca46a57f6529b00b)
5808e7
5808e7
Related: #2029426
5808e7
---
5808e7
 src/journal/journald-stream.c | 29 ++++++++++++++++++++---------
5808e7
 1 file changed, 20 insertions(+), 9 deletions(-)
5808e7
5808e7
diff --git a/src/journal/journald-stream.c b/src/journal/journald-stream.c
5808e7
index c8de984335..58752a5a24 100644
5808e7
--- a/src/journal/journald-stream.c
5808e7
+++ b/src/journal/journald-stream.c
5808e7
@@ -54,6 +54,8 @@ typedef enum LineBreak {
5808e7
         LINE_BREAK_NUL,
5808e7
         LINE_BREAK_LINE_MAX,
5808e7
         LINE_BREAK_EOF,
5808e7
+        _LINE_BREAK_MAX,
5808e7
+        _LINE_BREAK_INVALID = -1,
5808e7
 } LineBreak;
5808e7
 
5808e7
 struct StdoutStream {
5808e7
@@ -233,7 +235,11 @@ fail:
5808e7
         return log_error_errno(r, "Failed to save stream data %s: %m", s->state_file);
5808e7
 }
5808e7
 
5808e7
-static int stdout_stream_log(StdoutStream *s, const char *p, LineBreak line_break) {
5808e7
+static int stdout_stream_log(
5808e7
+                StdoutStream *s,
5808e7
+                const char *p,
5808e7
+                LineBreak line_break) {
5808e7
+
5808e7
         struct iovec *iovec;
5808e7
         int priority;
5808e7
         char syslog_priority[] = "PRIORITY=\0";
5808e7
@@ -245,6 +251,9 @@ static int stdout_stream_log(StdoutStream *s, const char *p, LineBreak line_brea
5808e7
         assert(s);
5808e7
         assert(p);
5808e7
 
5808e7
+        assert(line_break >= 0);
5808e7
+        assert(line_break < _LINE_BREAK_MAX);
5808e7
+
5808e7
         if (s->context)
5808e7
                 (void) client_context_maybe_refresh(s->server, s->context, NULL, NULL, 0, NULL, USEC_INFINITY);
5808e7
         else if (pid_is_valid(s->ucred.pid)) {
5808e7
@@ -296,17 +305,19 @@ static int stdout_stream_log(StdoutStream *s, const char *p, LineBreak line_brea
5808e7
                         iovec[n++] = IOVEC_MAKE_STRING(syslog_identifier);
5808e7
         }
5808e7
 
5808e7
-        if (line_break != LINE_BREAK_NEWLINE) {
5808e7
-                const char *c;
5808e7
+        static const char * const line_break_field_table[_LINE_BREAK_MAX] = {
5808e7
+                [LINE_BREAK_NEWLINE]    = NULL, /* Do not add field if traditional newline */
5808e7
+                [LINE_BREAK_NUL]        = "_LINE_BREAK=nul",
5808e7
+                [LINE_BREAK_LINE_MAX]   = "_LINE_BREAK=line-max",
5808e7
+                [LINE_BREAK_EOF]        = "_LINE_BREAK=eof",
5808e7
+        };
5808e7
 
5808e7
-                /* If this log message was generated due to an uncommon line break then mention this in the log
5808e7
-                 * entry */
5808e7
+        const char *c = line_break_field_table[line_break];
5808e7
 
5808e7
-                c =     line_break == LINE_BREAK_NUL ?      "_LINE_BREAK=nul" :
5808e7
-                        line_break == LINE_BREAK_LINE_MAX ? "_LINE_BREAK=line-max" :
5808e7
-                                                            "_LINE_BREAK=eof";
5808e7
+        /* If this log message was generated due to an uncommon line break then mention this in the log
5808e7
+         * entry */
5808e7
+        if (c)
5808e7
                 iovec[n++] = IOVEC_MAKE_STRING(c);
5808e7
-        }
5808e7
 
5808e7
         message = strappend("MESSAGE=", p);
5808e7
         if (message)