]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/journal-remote/journal-upload-journal.c
Drop the text argument from assert_not_reached()
[thirdparty/systemd.git] / src / journal-remote / journal-upload-journal.c
index a9751c25c53e40657d76f3364c69113fb12d56c1..899f47eea4354e44c38249b2f46a639c357572c9 100644 (file)
@@ -1,32 +1,16 @@
-/* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2014 Zbigniew JÄ™drzejewski-Szmek
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include <curl/curl.h>
 #include <stdbool.h>
 
+#include "sd-daemon.h"
+
 #include "alloc-util.h"
 #include "journal-upload.h"
 #include "log.h"
+#include "string-util.h"
 #include "utf8.h"
 #include "util.h"
-#include "sd-daemon.h"
 
 /**
  * Write up to size bytes to buf. Return negative on error, and number of
@@ -50,7 +34,8 @@ static ssize_t write_entry(char *buf, size_t size, Uploader *u) {
 
                         r = snprintf(buf + pos, size - pos,
                                      "__CURSOR=%s\n", u->current_cursor);
-                        if (pos + r > size)
+                        assert(r >= 0);
+                        if ((size_t) r > size - pos)
                                 /* not enough space */
                                 return pos;
 
@@ -63,8 +48,8 @@ static ssize_t write_entry(char *buf, size_t size, Uploader *u) {
                         }
 
                         pos += r;
-                }       /* fall through */
-
+                }
+                        _fallthrough_;
                 case ENTRY_REALTIME: {
                         usec_t realtime;
 
@@ -74,7 +59,8 @@ static ssize_t write_entry(char *buf, size_t size, Uploader *u) {
 
                         r = snprintf(buf + pos, size - pos,
                                      "__REALTIME_TIMESTAMP="USEC_FMT"\n", realtime);
-                        if (r + pos > size)
+                        assert(r >= 0);
+                        if ((size_t) r > size - pos)
                                 /* not enough space */
                                 return pos;
 
@@ -87,8 +73,8 @@ static ssize_t write_entry(char *buf, size_t size, Uploader *u) {
                         }
 
                         pos += r;
-                }       /* fall through */
-
+                }
+                        _fallthrough_;
                 case ENTRY_MONOTONIC: {
                         usec_t monotonic;
                         sd_id128_t boot_id;
@@ -99,7 +85,8 @@ static ssize_t write_entry(char *buf, size_t size, Uploader *u) {
 
                         r = snprintf(buf + pos, size - pos,
                                      "__MONOTONIC_TIMESTAMP="USEC_FMT"\n", monotonic);
-                        if (r + pos > size)
+                        assert(r >= 0);
+                        if ((size_t) r > size - pos)
                                 /* not enough space */
                                 return pos;
 
@@ -112,11 +99,11 @@ static ssize_t write_entry(char *buf, size_t size, Uploader *u) {
                         }
 
                         pos += r;
-                }       /* fall through */
-
+                }
+                        _fallthrough_;
                 case ENTRY_BOOT_ID: {
                         sd_id128_t boot_id;
-                        char sid[33];
+                        char sid[SD_ID128_STRING_MAX];
 
                         r = sd_journal_get_monotonic_usec(u->journal, NULL, &boot_id);
                         if (r < 0)
@@ -124,7 +111,8 @@ static ssize_t write_entry(char *buf, size_t size, Uploader *u) {
 
                         r = snprintf(buf + pos, size - pos,
                                      "_BOOT_ID=%s\n", sd_id128_to_string(boot_id, sid));
-                        if (r + pos > size)
+                        assert(r >= 0);
+                        if ((size_t) r > size - pos)
                                 /* not enough space */
                                 return pos;
 
@@ -137,8 +125,8 @@ static ssize_t write_entry(char *buf, size_t size, Uploader *u) {
                         }
 
                         pos += r;
-                }       /* fall through */
-
+                }
+                        _fallthrough_;
                 case ENTRY_NEW_FIELD: {
                         u->field_pos = 0;
 
@@ -152,15 +140,19 @@ static ssize_t write_entry(char *buf, size_t size, Uploader *u) {
                                 continue;
                         }
 
-                        if (!utf8_is_printable_newline(u->field_data,
-                                                       u->field_length, false)) {
+                        /* We already printed the boot id from the data in
+                         * the header, hence let's suppress it here */
+                        if (memory_startswith(u->field_data, u->field_length, "_BOOT_ID="))
+                                continue;
+
+                        if (!utf8_is_printable_newline(u->field_data, u->field_length, false)) {
                                 u->entry_state = ENTRY_BINARY_FIELD_START;
                                 continue;
                         }
 
                         u->entry_state++;
-                }       /* fall through */
-
+                }
+                        _fallthrough_;
                 case ENTRY_TEXT_FIELD:
                 case ENTRY_BINARY_FIELD: {
                         bool done;
@@ -192,10 +184,9 @@ static ssize_t write_entry(char *buf, size_t size, Uploader *u) {
                         size_t len;
 
                         c = memchr(u->field_data, '=', u->field_length);
-                        if (!c || c == u->field_data) {
-                                log_error("Invalid field.");
-                                return -EINVAL;
-                        }
+                        if (!c || c == u->field_data)
+                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+                                                       "Invalid field.");
 
                         len = c - (const char*)u->field_data;
 
@@ -209,8 +200,8 @@ static ssize_t write_entry(char *buf, size_t size, Uploader *u) {
 
                         u->field_pos = len + 1;
                         u->entry_state++;
-                }       /* fall through */
-
+                }
+                        _fallthrough_;
                 case ENTRY_BINARY_FIELD_SIZE: {
                         uint64_t le64;
 
@@ -238,13 +229,13 @@ static ssize_t write_entry(char *buf, size_t size, Uploader *u) {
                         return pos;
 
                 default:
-                        assert_not_reached("WTF?");
+                        assert_not_reached();
                 }
         }
-        assert_not_reached("WTF?");
+        assert_not_reached();
 }
 
-static inline void check_update_watchdog(Uploader *u) {
+static void check_update_watchdog(Uploader *u) {
         usec_t after;
         usec_t elapsed_time;
 
@@ -408,7 +399,7 @@ int open_journal_for_upload(Uploader *u,
                         return log_error_errno(r, "Failed to register input event: %m");
 
                 log_debug("Listening for journal events on fd:%d, timeout %d",
-                          fd, u->timeout == (uint64_t) -1 ? -1 : (int) u->timeout);
+                          fd, u->timeout == UINT64_MAX ? -1 : (int) u->timeout);
         } else
                 log_debug("Not listening for journal events.");
 
@@ -419,5 +410,5 @@ int open_journal_for_upload(Uploader *u,
                                                cursor);
         }
 
-        return process_journal_input(u, 1 + !!after_cursor);
+        return process_journal_input(u, !!after_cursor);
 }