From c4cdbb978f681e7356c6c6367c1730d156a6a4e0 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 18 Feb 2023 21:49:21 +0800 Subject: [PATCH] journalctl: fix output when --lines is used with --grep Previously, we skip the entries before arg_lines unconditionally, which doesn't behave correctly when used with --grep. After this commit, when a pattern is specified, we don't skip the entries early, but rely on the count of the lines shown to tell us when to stop. To achieve that we would have to search backwards instead. Fixes #25147 (cherry picked from commit db4691961ca52759fe6645d0fddb659ee4299ac2) --- man/journalctl.xml | 8 ++++++-- src/journal/journalctl.c | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/man/journalctl.xml b/man/journalctl.xml index 109797776e..ae86c50d62 100644 --- a/man/journalctl.xml +++ b/man/journalctl.xml @@ -363,7 +363,9 @@ If the pattern is all lowercase, matching is case insensitive. Otherwise, matching is case sensitive. This can be overridden with the option, see - below. + below. + + When used with , is implied. @@ -540,7 +542,9 @@ Show the most recent journal events and limit the number of events shown. If is used, this option is implied. The argument is a positive integer or all to disable line limiting. The default value is 10 if no argument is - given. + given. + + When used with , is implied. diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index b2883ff177..da0fac548e 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -1083,6 +1083,11 @@ static int parse_argv(int argc, char *argv[]) { r = pattern_compile_and_log(arg_pattern, arg_case, &arg_compiled_pattern); if (r < 0) return r; + + /* When --grep is used along with --lines, we don't know how many lines we can print. + * So we search backwards and count until enough lines have been printed or we hit the head. */ + if (arg_lines >= 0) + arg_reverse = true; } return 1;