From d180c3499867bc7c70951c6158025b107d3b90dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sun, 27 May 2018 11:27:58 +0200 Subject: [PATCH] journal: allow boot_id to be passed to journal_append_entry() In this commit, this is done only in testing code, i.e. there is no functional change apart from tests. --- src/journal-remote/journal-remote-write.c | 6 ++++-- src/journal/journal-file.c | 19 +++++++++++++++---- src/journal/journal-file.h | 9 ++++++++- src/journal/journald-server.c | 4 ++-- src/journal/test-journal-interleaving.c | 2 +- src/journal/test-journal-stream.c | 6 +++--- src/journal/test-journal-verify.c | 2 +- src/journal/test-journal.c | 13 ++++++++----- 8 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/journal-remote/journal-remote-write.c b/src/journal-remote/journal-remote-write.c index 494ee71e95..949fdca372 100644 --- a/src/journal-remote/journal-remote-write.c +++ b/src/journal-remote/journal-remote-write.c @@ -92,7 +92,8 @@ int writer_write(Writer *w, return r; } - r = journal_file_append_entry(w->journal, ts, iovw->iovec, iovw->count, + r = journal_file_append_entry(w->journal, ts, NULL, + iovw->iovec, iovw->count, &w->seqnum, NULL, NULL); if (r >= 0) { if (w->server) @@ -109,7 +110,8 @@ int writer_write(Writer *w, log_debug("%s: Successfully rotated journal", w->journal->path); log_debug("Retrying write."); - r = journal_file_append_entry(w->journal, ts, iovw->iovec, iovw->count, + r = journal_file_append_entry(w->journal, ts, NULL, + iovw->iovec, iovw->count, &w->seqnum, NULL, NULL); if (r < 0) return r; diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c index 62ef40f5b4..a6e85aee60 100644 --- a/src/journal/journal-file.c +++ b/src/journal/journal-file.c @@ -1801,6 +1801,7 @@ static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) { static int journal_file_append_entry_internal( JournalFile *f, const dual_timestamp *ts, + const sd_id128_t *boot_id, uint64_t xor_hash, const EntryItem items[], unsigned n_items, uint64_t *seqnum, @@ -1826,7 +1827,7 @@ static int journal_file_append_entry_internal( o->entry.realtime = htole64(ts->realtime); o->entry.monotonic = htole64(ts->monotonic); o->entry.xor_hash = htole64(xor_hash); - o->entry.boot_id = f->header->boot_id; + o->entry.boot_id = boot_id ? *boot_id : f->header->boot_id; #if HAVE_GCRYPT r = journal_file_hmac_put_object(f, OBJECT_ENTRY, o, np); @@ -1947,7 +1948,14 @@ static int entry_item_cmp(const void *_a, const void *_b) { return 0; } -int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, uint64_t *seqnum, Object **ret, uint64_t *offset) { +int journal_file_append_entry( + JournalFile *f, + const dual_timestamp *ts, + const sd_id128_t *boot_id, + const struct iovec iovec[], unsigned n_iovec, + uint64_t *seqnum, + Object **ret, uint64_t *offset) { + unsigned i; EntryItem *items; int r; @@ -1998,7 +2006,7 @@ int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const st * times for rotating media. */ qsort_safe(items, n_iovec, sizeof(EntryItem), entry_item_cmp); - r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqnum, ret, offset); + r = journal_file_append_entry_internal(f, ts, boot_id, xor_hash, items, n_iovec, seqnum, 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 @@ -3581,6 +3589,7 @@ int journal_file_copy_entry(JournalFile *from, JournalFile *to, Object *o, uint6 int r; EntryItem *items; dual_timestamp ts; + const sd_id128_t *boot_id; assert(from); assert(to); @@ -3592,6 +3601,7 @@ int journal_file_copy_entry(JournalFile *from, JournalFile *to, Object *o, uint6 ts.monotonic = le64toh(o->entry.monotonic); ts.realtime = le64toh(o->entry.realtime); + boot_id = &o->entry.boot_id; n = journal_file_entry_n_items(o); /* alloca() can't take 0, hence let's allocate at least one */ @@ -3651,7 +3661,8 @@ int journal_file_copy_entry(JournalFile *from, JournalFile *to, Object *o, uint6 return r; } - r = journal_file_append_entry_internal(to, &ts, xor_hash, items, n, seqnum, ret, offset); + r = journal_file_append_entry_internal(to, &ts, boot_id, xor_hash, items, n, + NULL, NULL, NULL); if (mmap_cache_got_sigbus(to->mmap, to->cache_fd)) return -EIO; diff --git a/src/journal/journal-file.h b/src/journal/journal-file.h index 869d0437ce..bfd4bbac90 100644 --- a/src/journal/journal-file.h +++ b/src/journal/journal-file.h @@ -205,7 +205,14 @@ uint64_t journal_file_entry_array_n_items(Object *o) _pure_; uint64_t journal_file_hash_table_n_items(Object *o) _pure_; int journal_file_append_object(JournalFile *f, ObjectType type, uint64_t size, Object **ret, uint64_t *offset); -int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, uint64_t *seqno, Object **ret, uint64_t *offset); +int journal_file_append_entry( + JournalFile *f, + const dual_timestamp *ts, + const sd_id128_t *boot_id, + const struct iovec iovec[], unsigned n_iovec, + uint64_t *seqno, + Object **ret, + uint64_t *offset); int journal_file_find_data_object(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset); int journal_file_find_data_object_with_hash(JournalFile *f, const void *data, uint64_t size, uint64_t hash, Object **ret, uint64_t *offset); diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index 40fbdcdddc..40c6714f99 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -692,7 +692,7 @@ static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, size_t n s->last_realtime_clock = ts.realtime; - r = journal_file_append_entry(f, &ts, iovec, n, &s->seqnum, NULL, NULL); + r = journal_file_append_entry(f, &ts, NULL, iovec, n, &s->seqnum, NULL, NULL); if (r >= 0) { server_schedule_sync(s, priority); return; @@ -711,7 +711,7 @@ static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, size_t n return; log_debug("Retrying write."); - r = journal_file_append_entry(f, &ts, iovec, n, &s->seqnum, NULL, NULL); + r = journal_file_append_entry(f, &ts, NULL, iovec, n, &s->seqnum, NULL, NULL); if (r < 0) log_error_errno(r, "Failed to write entry (%zu items, %zu bytes) despite vacuuming, ignoring: %m", n, IOVEC_TOTAL_SIZE(iovec, n)); else diff --git a/src/journal/test-journal-interleaving.c b/src/journal/test-journal-interleaving.c index e0481088ea..6c0d0f5645 100644 --- a/src/journal/test-journal-interleaving.c +++ b/src/journal/test-journal-interleaving.c @@ -66,7 +66,7 @@ static void append_number(JournalFile *f, int n, uint64_t *seqnum) { assert_se(asprintf(&p, "NUMBER=%d", n) >= 0); iovec[0].iov_base = p; iovec[0].iov_len = strlen(p); - assert_ret(journal_file_append_entry(f, &ts, iovec, 1, seqnum, NULL, NULL)); + assert_ret(journal_file_append_entry(f, &ts, NULL, iovec, 1, seqnum, NULL, NULL)); free(p); } diff --git a/src/journal/test-journal-stream.c b/src/journal/test-journal-stream.c index 05cb5cb988..052ea49959 100644 --- a/src/journal/test-journal-stream.c +++ b/src/journal/test-journal-stream.c @@ -109,12 +109,12 @@ int main(int argc, char *argv[]) { iovec[1].iov_len = strlen(q); if (i % 10 == 0) - assert_se(journal_file_append_entry(three, &ts, iovec, 2, NULL, NULL, NULL) == 0); + assert_se(journal_file_append_entry(three, &ts, NULL, iovec, 2, NULL, NULL, NULL) == 0); else { if (i % 3 == 0) - assert_se(journal_file_append_entry(two, &ts, iovec, 2, NULL, NULL, NULL) == 0); + assert_se(journal_file_append_entry(two, &ts, NULL, iovec, 2, NULL, NULL, NULL) == 0); - assert_se(journal_file_append_entry(one, &ts, iovec, 2, NULL, NULL, NULL) == 0); + assert_se(journal_file_append_entry(one, &ts, NULL, iovec, 2, NULL, NULL, NULL) == 0); } free(p); diff --git a/src/journal/test-journal-verify.c b/src/journal/test-journal-verify.c index 82b32554c0..04b5ccbc22 100644 --- a/src/journal/test-journal-verify.c +++ b/src/journal/test-journal-verify.c @@ -90,7 +90,7 @@ int main(int argc, char *argv[]) { iovec.iov_base = (void*) test; iovec.iov_len = strlen(test); - assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0); + assert_se(journal_file_append_entry(f, &ts, NULL, &iovec, 1, NULL, NULL, NULL) == 0); free(test); } diff --git a/src/journal/test-journal.c b/src/journal/test-journal.c index 5765da4895..1acb43e689 100644 --- a/src/journal/test-journal.c +++ b/src/journal/test-journal.c @@ -23,6 +23,7 @@ static void test_non_empty(void) { static const char test[] = "TEST1=1", test2[] = "TEST2=2"; Object *o; uint64_t p; + sd_id128_t fake_boot_id; char t[] = "/tmp/journal-XXXXXX"; log_set_max_level(LOG_DEBUG); @@ -32,19 +33,20 @@ static void test_non_empty(void) { assert_se(journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0666, true, (uint64_t) -1, true, NULL, NULL, NULL, NULL, &f) == 0); - dual_timestamp_get(&ts); + assert_se(dual_timestamp_get(&ts)); + assert_se(sd_id128_randomize(&fake_boot_id) == 0); iovec.iov_base = (void*) test; iovec.iov_len = strlen(test); - assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0); + assert_se(journal_file_append_entry(f, &ts, NULL, &iovec, 1, NULL, NULL, NULL) == 0); iovec.iov_base = (void*) test2; iovec.iov_len = strlen(test2); - assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0); + assert_se(journal_file_append_entry(f, &ts, NULL, &iovec, 1, NULL, NULL, NULL) == 0); iovec.iov_base = (void*) test; iovec.iov_len = strlen(test); - assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0); + assert_se(journal_file_append_entry(f, &ts, &fake_boot_id, &iovec, 1, NULL, NULL, NULL) == 0); #if HAVE_GCRYPT journal_file_append_tag(f); @@ -59,6 +61,7 @@ static void test_non_empty(void) { assert_se(journal_file_next_entry(f, p, DIRECTION_DOWN, &o, &p) == 1); assert_se(le64toh(o->entry.seqnum) == 3); + assert_se(sd_id128_equal(o->entry.boot_id, fake_boot_id)); assert_se(journal_file_next_entry(f, p, DIRECTION_DOWN, &o, &p) == 0); @@ -177,7 +180,7 @@ static bool check_compressed(uint64_t compress_threshold, uint64_t data_size) { iovec.iov_base = (void*) data; iovec.iov_len = data_size; - assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0); + assert_se(journal_file_append_entry(f, &ts, NULL, &iovec, 1, NULL, NULL, NULL) == 0); #if HAVE_GCRYPT journal_file_append_tag(f);