]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journal-file: write machine ID when create the file, not when we open it for writing
authorLennart Poettering <lennart@poettering.net>
Thu, 26 Jan 2023 16:12:25 +0000 (17:12 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 21 Feb 2023 09:47:53 +0000 (10:47 +0100)
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

index d361b35a6effd64aee7d60dbbec3f2e931f0ddf7..7300ed07810205a10f22b653439898fd135229f3 100644 (file)
@@ -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 */