From 51ab0afed48dc55a211fbb610e188221446eb61f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 31 Jan 2023 13:37:12 +0100 Subject: [PATCH] journal-file: lazily fill in machine ID into journal header, if needed Previously, if we ran in an environment where /etc/machine-id was not defined, we'd never bother to write it ever again. So it would stay at all zeroes till the end of times. Let's make this more robust: whenever we try to append an entry, let's try to refresh it from the status quo if not initialized yet. Moreover, when copying records from a different journal file, let's propagate the machine ID from there. This should make things more robust and systematic, and match how we propagate the boot ID and the seqnum ID to some level. --- src/libsystemd/sd-journal/journal-file.c | 43 ++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c index 7300ed07810..3e721ef937d 100644 --- a/src/libsystemd/sd-journal/journal-file.c +++ b/src/libsystemd/sd-journal/journal-file.c @@ -2087,6 +2087,7 @@ static int journal_file_append_entry_internal( JournalFile *f, const dual_timestamp *ts, const sd_id128_t *boot_id, + const sd_id128_t *machine_id, uint64_t xor_hash, const EntryItem items[], size_t n_items, @@ -2152,6 +2153,10 @@ static int journal_file_append_entry_internal( } } + if (machine_id && sd_id128_is_null(f->header->machine_id)) + /* Initialize machine ID when not set yet */ + f->header->machine_id = *machine_id; + osize = offsetof(Object, entry.items) + (n_items * journal_file_entry_item_size(f)); r = journal_file_append_object(f, OBJECT_ENTRY, osize, &o, &np); @@ -2311,7 +2316,7 @@ int journal_file_append_entry( EntryItem *items; uint64_t xor_hash = 0; struct dual_timestamp _ts; - sd_id128_t _boot_id; + sd_id128_t _boot_id, _machine_id, *machine_id; int r; assert(f); @@ -2341,6 +2346,16 @@ int journal_file_append_entry( boot_id = &_boot_id; } + r = sd_id128_get_machine(&_machine_id); + if (r < 0) { + if (!ERRNO_IS_MACHINE_ID_UNSET(r)) + return r; + + /* If the machine ID is not initialized yet, handle gracefully */ + machine_id = NULL; + } else + machine_id = &_machine_id; + #if HAVE_GCRYPT r = journal_file_maybe_append_tag(f, ts->realtime); if (r < 0) @@ -2390,7 +2405,18 @@ int journal_file_append_entry( typesafe_qsort(items, n_iovec, entry_item_cmp); n_iovec = remove_duplicate_entry_items(items, n_iovec); - r = journal_file_append_entry_internal(f, ts, boot_id, xor_hash, items, n_iovec, seqnum, seqnum_id, ret_object, ret_offset); + r = journal_file_append_entry_internal( + f, + ts, + boot_id, + machine_id, + xor_hash, + items, + n_iovec, + seqnum, + seqnum_id, + ret_object, + ret_offset); /* If the memory mapping triggered a SIGBUS then we return an * IO error and ignore the error code passed down to us, since @@ -4190,7 +4216,18 @@ int journal_file_copy_entry( return r; } - r = journal_file_append_entry_internal(to, &ts, boot_id, xor_hash, items, n, seqnum, seqnum_id, NULL, NULL); + r = journal_file_append_entry_internal( + to, + &ts, + boot_id, + &from->header->machine_id, + xor_hash, + items, + n, + seqnum, + seqnum_id, + /* ret_object= */ NULL, + /* ret_offset= */ NULL); if (mmap_cache_fd_got_sigbus(to->cache_fd)) return -EIO; -- 2.47.3