]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journal: automatically pick up boot ID in journal_file_append_entry()
authorLennart Poettering <lennart@poettering.net>
Thu, 19 Jan 2023 21:45:17 +0000 (22:45 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 26 Jan 2023 08:52:49 +0000 (09:52 +0100)
Let's pick up the boot ID early if unspecified, in
journal_file_append_entry(). This is symmetric to the fact that we
already pick up the monotonic timestamp in journal_file_append_entry()
if unspecified, and given that the monotonic clock is not too useful
without its boot ID it makes a lot of sense to pick them up at the same
time.

There are two relevant callers of journal_file_append_entry() right now:
journald (which leaves the boot ID unspecified) and journal-remote
(there are also some tests, but those don't matter too much). The former
calls it to store new entries in the journal file, the latter for
converting/processing/merging existing ones (where it passes along the
original boot ID). This new code hence only is relevant on the former,
and using the boot ID of the current system is the right choice for live
generated entries.

Note that this effectively changes little, since the lower-level
function journal_file_append_entry_internal() will copy boot ID stored
in the file header into all records if unspecified, and typically that's
the one of the local system. But strictly speaking this is not the right
thing to do, since we actually might end up appending to journal files
from previous boots. (The lower level function is indirectly used by
various tests, where the copying-from-header logic kinda makes sense
since they are detached from any live messages streaming in from the
host after all).

src/libsystemd/sd-journal/journal-file.c

index c517e31cc42c243048e1723b52227e7a7265dd02..d46d4914d4b05411242b8fd7053bc741f864a4ae 100644 (file)
@@ -2256,6 +2256,7 @@ int journal_file_append_entry(
         EntryItem *items;
         uint64_t xor_hash = 0;
         struct dual_timestamp _ts;
+        sd_id128_t _boot_id;
         int r;
 
         assert(f);
@@ -2277,6 +2278,14 @@ int journal_file_append_entry(
                 ts = &_ts;
         }
 
+        if (!boot_id) {
+                r = sd_id128_get_boot(&_boot_id);
+                if (r < 0)
+                        return r;
+
+                boot_id = &_boot_id;
+        }
+
 #if HAVE_GCRYPT
         r = journal_file_maybe_append_tag(f, ts->realtime);
         if (r < 0)