From 8e64ec04705e029cc891cd1382865ce25b04685c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 26 Jan 2023 17:12:25 +0100 Subject: [PATCH] journal-file: write machine ID when create the file, not when we open it for writing This doesn't actually change much, but makes the code less surprising. Status quo ante: 1. Open a journal file 2. If newly created set header machine ID to zero 3. If existing and open for write check if machine ID in header matches local one, if not, refuse. 4. if open for writing, now refresh the machine ID from the local system Of course, step 4 is pretty much pointless for existing files, as the check in 3 made sure it is already in order or we'd refuse operating on it anyway. With this patch this is simplified to: 1. Open a journal file 2. If newly created initialized machine ID to local machine ID 3. If existing, compare machine ID in header with local one, if not matching refuse. Outcome is the same. --- src/libsystemd/sd-journal/journal-file.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c index d361b35a6e..7300ed0781 100644 --- a/src/libsystemd/sd-journal/journal-file.c +++ b/src/libsystemd/sd-journal/journal-file.c @@ -369,6 +369,11 @@ static int journal_file_init_header( if (r < 0) return r; + r = sd_id128_get_machine(&h.machine_id); + if (r < 0 && !ERRNO_IS_MACHINE_ID_UNSET(r)) + return r; /* If we have no valid machine ID (test environment?), let's simply leave the + * machine ID field all zeroes. */ + if (template) { h.seqnum_id = template->header->seqnum_id; h.tail_entry_seqnum = template->header->tail_entry_seqnum; @@ -390,15 +395,6 @@ static int journal_file_refresh_header(JournalFile *f) { assert(f); assert(f->header); - r = sd_id128_get_machine(&f->header->machine_id); - if (r < 0) { - if (!ERRNO_IS_MACHINE_ID_UNSET(r)) - return r; - - /* don't have a machine-id, let's continue without */ - f->header->machine_id = SD_ID128_NULL; - } - /* We used to update the header's boot ID field here, but we don't do that anymore, as per * HEADER_COMPATIBLE_TAIL_ENTRY_BOOT_ID */ -- 2.25.1