803fb7
From cc5710c3ad0ff51fa84b736d66d5f70aa0ade2b3 Mon Sep 17 00:00:00 2001
803fb7
From: Lennart Poettering <lennart@poettering.net>
803fb7
Date: Mon, 25 Apr 2016 18:08:42 +0200
803fb7
Subject: [PATCH] journalctl: don't trust the per-field entry tables when
803fb7
 looking for boot IDs
803fb7
803fb7
When appending to a journal file, journald will:
803fb7
803fb7
a) first, append the actual entry to the end of the journal file
803fb7
b) second, add an offset reference to it to the global entry array stored at
803fb7
   the beginning of the file
803fb7
c) third, add offset references to it to the per-field entry array stored at
803fb7
   various places of the file
803fb7
803fb7
The global entry array, maintained by b) is used when iterating through the
803fb7
journal without matches applied.
803fb7
803fb7
The per-field entry array maintained by c) is used when iterating through the
803fb7
journal with a match for that specific field applied.
803fb7
803fb7
In the wild, there are journal files where a) and b) were completed, but c)
803fb7
was not before the files were abandoned. This means, that in some cases log
803fb7
entries are at the end of these files that appear in the global entry array,
803fb7
but not in the per-field entry array of the _BOOT_ID= field. Now, the
803fb7
"journalctl --list-boots" command alternatingly uses the global entry array
803fb7
and the per-field entry array of the _BOOT_ID= field. It seeks to the last
803fb7
entry of a specific _BOOT_ID=field by having the right match installed, and
803fb7
then jumps to the next following entry with no match installed anymore, under
803fb7
the assumption this would bring it to the next boot ID. However, if the
803fb7
per-field entry wasn't written fully, it might actually turn out that the
803fb7
global entry array might know one more entry with the same _BOOT_ID, thus
803fb7
resulting in a indefinite loop around the same _BOOT_ID.
803fb7
803fb7
This patch fixes that, by updating the boot search logic to always continue
803fb7
reading entries until the boot ID actually changed from the previous. Thus, the
803fb7
per-field entry array is used as quick jump index (i.e. as an optimization),
803fb7
but not trusted otherwise.  Only the global entry array is trusted.
803fb7
803fb7
This replaces PR #1904, which is actually very similar to this one. However,
803fb7
this one actually reads the boot ID directly from the entry header, and doesn't
803fb7
try to read it at all until the read pointer is actually really located on the
803fb7
first item to read.
803fb7
803fb7
Fixes: #617
803fb7
803fb7
Replaces: #1904
803fb7
803fb7
Cherry-picked from: dc00966228ff90c554fd034e588ea55eb605ec52
803fb7
Related: #1318994
803fb7
---
de8967
 src/journal/journalctl.c | 71 ++++++++++++++++++++++++----------------
803fb7
 1 file changed, 42 insertions(+), 29 deletions(-)
803fb7
803fb7
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
803fb7
index 5864ff50a..723854a2e 100644
803fb7
--- a/src/journal/journalctl.c
803fb7
+++ b/src/journal/journalctl.c
803fb7
@@ -941,18 +941,18 @@ static void boot_id_free_all(BootId *l) {
803fb7
         }
803fb7
 }
803fb7
 
803fb7
-static int discover_next_boot(
803fb7
-                sd_journal *j,
803fb7
-                BootId **boot,
803fb7
+static int discover_next_boot(sd_journal *j,
803fb7
+                sd_id128_t previous_boot_id,
803fb7
                 bool advance_older,
803fb7
-                bool read_realtime) {
803fb7
+                BootId **ret) {
803fb7
 
803fb7
-        int r;
803fb7
-        char match[9+32+1] = "_BOOT_ID=";
803fb7
         _cleanup_free_ BootId *next_boot = NULL;
803fb7
+        char match[9+32+1] = "_BOOT_ID=";
803fb7
+        sd_id128_t boot_id;
803fb7
+        int r;
803fb7
 
803fb7
         assert(j);
803fb7
-        assert(boot);
803fb7
+        assert(ret);
803fb7
 
803fb7
         /* We expect the journal to be on the last position of a boot
803fb7
          * (in relation to the direction we are going), so that the next
803fb7
@@ -965,29 +965,40 @@ static int discover_next_boot(
803fb7
          * we can actually advance to a *different* boot. */
803fb7
         sd_journal_flush_matches(j);
803fb7
 
803fb7
-        if (advance_older)
803fb7
-                r = sd_journal_previous(j);
803fb7
-        else
803fb7
-                r = sd_journal_next(j);
803fb7
-        if (r < 0)
803fb7
-                return r;
803fb7
-        else if (r == 0)
803fb7
-                return 0; /* End of journal, yay. */
803fb7
+        do {
803fb7
+                if (advance_older)
803fb7
+                        r = sd_journal_previous(j);
803fb7
+                else
803fb7
+                        r = sd_journal_next(j);
803fb7
+                if (r < 0)
803fb7
+                        return r;
803fb7
+                else if (r == 0)
803fb7
+                        return 0; /* End of journal, yay. */
803fb7
+
803fb7
+                r = sd_journal_get_monotonic_usec(j, NULL, &boot_id);
803fb7
+                if (r < 0)
803fb7
+                        return r;
803fb7
+
803fb7
+                /* We iterate through this in a loop, until the boot ID differs from the previous one. Note that
803fb7
+                 * normally, this will only require a single iteration, as we seeked to the last entry of the previous
803fb7
+                 * boot entry already. However, it might happen that the per-journal-field entry arrays are less
803fb7
+                 * complete than the main entry array, and hence might reference an entry that's not actually the last
803fb7
+                 * one of the boot ID as last one. Let's hence use the per-field array is initial seek position to
803fb7
+                 * speed things up, but let's not trust that it is complete, and hence, manually advance as
803fb7
+                 * necessary. */
803fb7
+
803fb7
+        } while (sd_id128_equal(boot_id, previous_boot_id));
803fb7
 
803fb7
         next_boot = new0(BootId, 1);
803fb7
         if (!next_boot)
803fb7
                 return log_oom();
803fb7
 
803fb7
-        r = sd_journal_get_monotonic_usec(j, NULL, &next_boot->id);
803fb7
+        next_boot->id = boot_id;
803fb7
+
803fb7
+        r = sd_journal_get_realtime_usec(j, &next_boot->first);
803fb7
         if (r < 0)
803fb7
                 return r;
803fb7
 
803fb7
-        if (read_realtime) {
803fb7
-                r = sd_journal_get_realtime_usec(j, &next_boot->first);
803fb7
-                if (r < 0)
803fb7
-                        return r;
803fb7
-        }
803fb7
-
803fb7
         /* Now seek to the last occurrence of this boot ID. */
803fb7
         sd_id128_to_string(next_boot->id, match + 9);
803fb7
         r = sd_journal_add_match(j, match, sizeof(match) - 1);
803fb7
@@ -1010,13 +1021,11 @@ static int discover_next_boot(
803fb7
         else if (r == 0)
803fb7
                 return -ENODATA; /* This shouldn't happen. We just came from this very boot ID. */
803fb7
 
803fb7
-        if (read_realtime) {
803fb7
-                r = sd_journal_get_realtime_usec(j, &next_boot->last);
803fb7
-                if (r < 0)
803fb7
-                        return r;
803fb7
-        }
803fb7
+        r = sd_journal_get_realtime_usec(j, &next_boot->last);
803fb7
+        if (r < 0)
803fb7
+                return r;
803fb7
 
803fb7
-        *boot = next_boot;
803fb7
+        *ret = next_boot;
803fb7
         next_boot = NULL;
803fb7
 
803fb7
         return 0;
803fb7
@@ -1032,6 +1041,7 @@ static int get_boots(
803fb7
         int r, count = 0;
803fb7
         BootId *head = NULL, *tail = NULL;
803fb7
         const bool advance_older = query_ref_boot && ref_boot_offset <= 0;
803fb7
+        sd_id128_t previous_boot_id;
803fb7
 
803fb7
         assert(j);
803fb7
 
803fb7
@@ -1085,10 +1095,11 @@ static int get_boots(
803fb7
                 /* No sd_journal_next/previous here. */
803fb7
         }
803fb7
 
803fb7
+        previous_boot_id = SD_ID128_NULL;
803fb7
         for (;;) {
803fb7
                 _cleanup_free_ BootId *current = NULL;
803fb7
 
803fb7
-                r = discover_next_boot(j, &current, advance_older, !query_ref_boot);
803fb7
+                r = discover_next_boot(j, previous_boot_id, advance_older, ¤t;;
803fb7
                 if (r < 0) {
803fb7
                         boot_id_free_all(head);
803fb7
                         return r;
803fb7
@@ -1097,6 +1108,8 @@ static int get_boots(
803fb7
                 if (!current)
803fb7
                         break;
803fb7
 
803fb7
+                previous_boot_id = current->id;
803fb7
+
803fb7
                 if (query_ref_boot) {
803fb7
                         if (!skip_once)
803fb7
                                 ref_boot_offset += advance_older ? 1 : -1;