Skip to content

Commit

Permalink
journalctl: fix --follow with non-matching filter
Browse files Browse the repository at this point in the history
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()`.

(cherry picked from commit 80ec3db)

Resolves: RHEL-30567
  • Loading branch information
yuwata authored and dtardon committed Apr 10, 2024
1 parent d1ce407 commit 887be88
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 11 deletions.
5 changes: 1 addition & 4 deletions src/journal/journalctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2155,10 +2155,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)
Expand Down
7 changes: 0 additions & 7 deletions test/units/testsuite-04.sh
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,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
Expand Down

0 comments on commit 887be88

Please sign in to comment.