From 80ec3db5c7a4cfef237d4a1a3d33097bd4b50ec4 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 28 May 2023 14:28:32 +0900 Subject: [PATCH] journalctl: fix --follow with non-matching filter When there is no matching entry stored in journal, then initial call of `sd_journal_previous()` following `sd_journal_seek_tail()` returns zero, and does not move the read pointer. In the main loop, on every journal event, we call `sd_journal_next()`, even though the current location is tail, and it takes no effect. In such a case, we need to call `sd_journal_previous()` instead of `sd_journal_next()`. --- src/journal/journalctl.c | 5 +---- test/units/testsuite-04.sh | 7 ------- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 8944c95e64f..4f9c1f4780c 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -2146,10 +2146,7 @@ static int show(Context *c) { size_t highlight[2] = {}; if (c->need_seek) { - if (!arg_reverse) - r = sd_journal_next(j); - else - r = sd_journal_previous(j); + r = sd_journal_step_one(j, !arg_reverse); if (r < 0) return log_error_errno(r, "Failed to iterate through journal: %m"); if (r == 0) diff --git a/test/units/testsuite-04.sh b/test/units/testsuite-04.sh index 5280cf8cb1a..4af619941bc 100755 --- a/test/units/testsuite-04.sh +++ b/test/units/testsuite-04.sh @@ -164,13 +164,6 @@ set -o pipefail # https://github.com/systemd/systemd/issues/26746 rm -f /tmp/issue-26746-log /tmp/issue-26746-cursor ID=$(systemd-id128 new) - -# FIXME: -# After the commit 7a4ee861615101ddd2f95056cf30e69e41da86ce, -# journalctl --follow does not work if no matching entry stored in the journal. -# To workaround the issue, we need to add an entry before calling journalctl below. -systemd-cat -t "$ID" /bin/sh -c 'echo aaa' - journalctl -t "$ID" --follow --cursor-file=/tmp/issue-26746-cursor | tee /tmp/issue-26746-log & systemd-cat -t "$ID" /bin/sh -c 'echo hogehoge' # shellcheck disable=SC2016 -- 2.47.3