]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journalctl: if there's not a single matching log entry, seek to head
authorLennart Poettering <lennart@poettering.net>
Mon, 24 Mar 2025 10:52:53 +0000 (06:52 -0400)
committerLennart Poettering <lennart@poettering.net>
Tue, 13 May 2025 12:42:34 +0000 (14:42 +0200)
src/journal/journalctl-show.c

index 7515db0db9f9b615f75767647e1b53d312efcd9b..620f2fc6a8de18057faa6989eea3b794028e5137 100644 (file)
@@ -299,7 +299,37 @@ static int on_journal_event(sd_event_source *s, int fd, uint32_t revents, void *
 }
 
 static int on_first_event(sd_event_source *s, void *userdata) {
-        return show_and_fflush(userdata, s);
+        Context *c = ASSERT_PTR(userdata);
+        int r;
+
+        assert(s);
+
+        r = show_and_fflush(c, s);
+        if (r < 0)
+                return r;
+
+        if (arg_follow && !arg_reverse && !arg_cursor && !arg_after_cursor && !arg_cursor_file && !arg_since_set) {
+                r = sd_journal_get_cursor(c->journal, /* ret_cursor= */ NULL);
+                if (r == -EADDRNOTAVAIL) {
+                        /* If we shall operate in --follow mode, and we are unable to get a cursor after
+                         * doing our first round of output, then this means there was no data to show
+                         * whatsoever, and we hence have no stable position on any line at all. This means,
+                         * when we get notified about changes, we shouldn't try to position the cursor at the
+                         * end of the logs anymore, but at the beginning, since anything showing up from now
+                         * that matches our filters is good now. Hence, simply disable the effect of --lines=
+                         * now. */
+
+                        r = sd_journal_seek_head(c->journal);
+                        if (r < 0)
+                                return log_error_errno(r, "Failed to seek to head: %m");
+
+                        c->need_seek = true;
+
+                } else if (r < 0)
+                        return log_error_errno(r, "Failed to get cursor: %m");
+        }
+
+        return 0;
 }
 
 static int on_signal(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {