]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journalctl: fix output when --lines is used with --grep
authorMike Yuan <me@yhndnzj.com>
Sat, 18 Feb 2023 13:49:21 +0000 (21:49 +0800)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 18 Feb 2023 22:06:49 +0000 (07:06 +0900)
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

man/journalctl.xml
src/journal/journalctl.c

index 109797776e6927b5126f1981f55b679ddc59a27c..ae86c50d6248bb445cf545df013b2ee112deaaf5 100644 (file)
 
         <para>If the pattern is all lowercase, matching is case insensitive.  Otherwise, matching is case
         sensitive. This can be overridden with the <option>--case-sensitive</option> option, see
-        below.</para></listitem>
+        below.</para>
+
+        <para>When used with <option>--lines=</option>, <option>--reverse</option> is implied.</para></listitem>
       </varlistentry>
 
       <varlistentry>
         <listitem><para>Show the most recent journal events and limit the number of events shown. If
         <option>--follow</option> is used, this option is implied. The argument is a positive integer or
         <literal>all</literal> to disable line limiting. The default value is 10 if no argument is
-        given.</para></listitem>
+        given.</para>
+
+        <para>When used with <option>--grep=</option>, <option>--reverse</option> is implied.</para></listitem>
       </varlistentry>
 
       <varlistentry>
index e10f0b756433653574730c86265433249055f417..f148ea91442b8e49ad4c0ddb2f87cf04077eb7d2 100644 (file)
@@ -1084,6 +1084,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;