summaryrefslogtreecommitdiff
path: root/src/journal
diff options
context:
space:
mode:
authorMike Yuan <me@yhndnzj.com>2023-03-04 19:06:00 +0800
committerMike Yuan <me@yhndnzj.com>2023-03-05 11:53:03 +0800
commit81fb5375b3b3bfc22d023d7908ad9eee4b3c1ffb (patch)
treef005bd203f944c157b6c86b2950aba0a6f50d20d /src/journal
parent561f17a1e45d631953f18af7f0ef7ef2e6666fa5 (diff)
downloadsystemd-81fb5375b3b3bfc22d023d7908ad9eee4b3c1ffb.tar.gz
journalctl: fix output when --until is used with --lines
Before this commit, when --lines is specified, we jump to the tail and search afterwards from there, thus breaking --until if used together. After this commit: If both --until and any of --reverse and --lines is specified, things get a little tricky. We seek to the place of --until first. If only --reverse or --reverse and --lines is specified, we search backwards and let the output counter handle --lines for us. If only --lines is used, we just jump backwards arg_lines and search afterwards from there.
Diffstat (limited to 'src/journal')
-rw-r--r--src/journal/journalctl.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index f148ea9144..76af3e35a0 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -2482,12 +2482,21 @@ static int run(int argc, char *argv[]) {
r = sd_journal_next(j);
- } else if (arg_until_set && arg_reverse) {
+ } else if (arg_until_set && (arg_reverse || arg_lines >= 0)) {
+ /* If both --until and any of --reverse and --lines is specified, things get
+ * a little tricky. We seek to the place of --until first. If only --reverse or
+ * --reverse and --lines is specified, we search backwards and let the output
+ * counter handle --lines for us. If only --lines is used, we just jump backwards
+ * arg_lines and search afterwards from there. */
+
r = sd_journal_seek_realtime_usec(j, arg_until);
if (r < 0)
return log_error_errno(r, "Failed to seek to date: %m");
- r = sd_journal_previous(j);
+ if (arg_reverse)
+ r = sd_journal_previous(j);
+ else /* arg_lines >= 0 */
+ r = sd_journal_previous_skip(j, arg_lines);
} else if (arg_reverse) {
r = sd_journal_seek_tail(j);
@@ -2552,7 +2561,7 @@ static int run(int argc, char *argv[]) {
break;
}
- if (arg_until_set && !arg_reverse) {
+ if (arg_until_set && !arg_reverse && arg_lines < 0) {
usec_t usec;
r = sd_journal_get_realtime_usec(j, &usec);