]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journald: minor modernizations in kmsg handling code
authorLennart Poettering <lennart@poettering.net>
Thu, 26 Jan 2023 09:10:20 +0000 (10:10 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 26 Jan 2023 10:20:18 +0000 (11:20 +0100)
Nothing earth shattering. Mostly just fixes (and some more careful
checking of the boolean variables we keep)

src/journal/journald-kmsg.c

index 685863ff50ca6b634a6fa86207708ba50ce80777..c64e626b2cdff9879b5f394e8df47c35d163377f 100644 (file)
@@ -37,7 +37,7 @@ void server_forward_kmsg(
         struct iovec iovec[5];
         char header_priority[DECIMAL_STR_MAX(priority) + 3],
              header_pid[STRLEN("[]: ") + DECIMAL_STR_MAX(pid_t) + 1];
-        int n = 0;
+        size_t n = 0;
 
         assert(s);
         assert(priority >= 0);
@@ -65,11 +65,10 @@ void server_forward_kmsg(
                         identifier = ident_buf;
                 }
 
-                xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid);
-
                 if (identifier)
                         iovec[n++] = IOVEC_MAKE_STRING(identifier);
 
+                xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid);
                 iovec[n++] = IOVEC_MAKE_STRING(header_pid);
         } else if (identifier) {
                 iovec[n++] = IOVEC_MAKE_STRING(identifier);
@@ -81,7 +80,7 @@ void server_forward_kmsg(
         iovec[n++] = IOVEC_MAKE_STRING("\n");
 
         if (writev(s->dev_kmsg_fd, iovec, n) < 0)
-                log_debug_errno(errno, "Failed to write to /dev/kmsg for logging: %m");
+                log_debug_errno(errno, "Failed to write to /dev/kmsg for logging, ignoring: %m");
 }
 
 static bool is_us(const char *identifier, const char *pid) {
@@ -297,7 +296,6 @@ void dev_kmsg_record(Server *s, char *p, size_t l) {
         if (cunescape_length_with_prefix(p, pl, "MESSAGE=", UNESCAPE_RELAX, &message) >= 0)
                 iovec[n++] = IOVEC_MAKE_STRING(message);
 
-
         server_dispatch_message(s, iovec, n, ELEMENTSOF(iovec), c, NULL, priority, 0);
 
         if (saved_log_max_level != INT_MAX)
@@ -319,11 +317,11 @@ static int server_read_dev_kmsg(Server *s) {
         if (l == 0)
                 return 0;
         if (l < 0) {
-                /* Old kernels who don't allow reading from /dev/kmsg
-                 * return EINVAL when we try. So handle this cleanly,
-                 * but don't try to ever read from it again. */
+                /* Old kernels which don't allow reading from /dev/kmsg return EINVAL when we try. So handle
+                 * this cleanly, but don't try to ever read from it again. */
                 if (errno == EINVAL) {
                         s->dev_kmsg_event_source = sd_event_source_unref(s->dev_kmsg_event_source);
+                        s->dev_kmsg_readable = false;
                         return 0;
                 }
 
@@ -400,33 +398,27 @@ int server_open_dev_kmsg(Server *s) {
                 return 0;
 
         r = sd_event_add_io(s->event, &s->dev_kmsg_event_source, s->dev_kmsg_fd, EPOLLIN, dispatch_dev_kmsg, s);
+        if (r == -EPERM) { /* This will fail with EPERM on older kernels where /dev/kmsg is not readable. */
+                r = 0;
+                goto finish;
+        }
         if (r < 0) {
-
-                /* This will fail with EPERM on older kernels where
-                 * /dev/kmsg is not readable. */
-                if (r == -EPERM) {
-                        r = 0;
-                        goto fail;
-                }
-
                 log_error_errno(r, "Failed to add /dev/kmsg fd to event loop: %m");
-                goto fail;
+                goto finish;
         }
 
         r = sd_event_source_set_priority(s->dev_kmsg_event_source, SD_EVENT_PRIORITY_IMPORTANT+10);
         if (r < 0) {
                 log_error_errno(r, "Failed to adjust priority of kmsg event source: %m");
-                goto fail;
+                goto finish;
         }
 
         s->dev_kmsg_readable = true;
-
         return 0;
 
-fail:
+finish:
         s->dev_kmsg_event_source = sd_event_source_unref(s->dev_kmsg_event_source);
         s->dev_kmsg_fd = safe_close(s->dev_kmsg_fd);
-
         return r;
 }
 
@@ -441,7 +433,7 @@ int server_open_kernel_seqnum(Server *s) {
         /* We store the seqnum we last read in an mmapped file. That way we can just use it like a variable,
          * but it is persistent and automatically flushed at reboot. */
 
-        if (!s->read_kmsg)
+        if (!s->dev_kmsg_readable)
                 return 0;
 
         fn = strjoina(s->runtime_directory, "/kernel-seqnum");