From 34d17ac128d1e44dd36531b311c9a856d1624e6d Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 30 Sep 2023 15:05:23 +0900 Subject: [PATCH 1/7] journald: do not close all files stored in Server.deferred_closes Just before the call of managed_journal_file_open(), we already called server_vacuum_deferred_closes(), so there is a room to store the file. --- src/journal/journald-server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index b6f494b4ce..8b60369b49 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -655,7 +655,7 @@ static int server_archive_offline_user_journals(Server *s) { s->compress.threshold_bytes, &s->system_storage.metrics, s->mmap, - s->deferred_closes, + /* deferred_closes= */ NULL, /* template= */ NULL, &f); if (r < 0) { From 5f6b79b73b7eeff1b47eff0f2351542de3980c4b Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 30 Sep 2023 15:15:28 +0900 Subject: [PATCH 2/7] journald: split-out closing journal files from managed_journal_file_open() No functional change, just refactoring and preparation for later commits. --- src/journal-remote/journal-remote.c | 1 - src/journal/journald-server.c | 5 ++--- src/journal/managed-journal-file.c | 12 ++++-------- src/journal/managed-journal-file.h | 2 -- src/journal/test-journal-append.c | 2 -- src/journal/test-journal-flush.c | 2 +- src/journal/test-journal-interleaving.c | 10 +++++----- src/journal/test-journal-stream.c | 8 ++++---- src/journal/test-journal-verify.c | 3 +-- src/journal/test-journal.c | 14 +++++++------- 10 files changed, 24 insertions(+), 35 deletions(-) diff --git a/src/journal-remote/journal-remote.c b/src/journal-remote/journal-remote.c index 5b845a520f..29da82e65a 100644 --- a/src/journal-remote/journal-remote.c +++ b/src/journal-remote/journal-remote.c @@ -88,7 +88,6 @@ static int open_output(RemoteServer *s, Writer *w, const char* host) { &w->metrics, w->mmap, NULL, - NULL, &w->journal); if (r < 0) return log_error_errno(r, "Failed to open output journal %s: %m", filename); diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index 8b60369b49..83857d0116 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -276,6 +276,8 @@ static int server_open_journal( (seal ? JOURNAL_SEAL : 0) | JOURNAL_STRICT_ORDER; + set_clear_with_destructor(s->deferred_closes, managed_journal_file_close); + if (reliably) r = managed_journal_file_open_reliably( fname, @@ -285,7 +287,6 @@ static int server_open_journal( s->compress.threshold_bytes, metrics, s->mmap, - s->deferred_closes, /* template= */ NULL, &f); else @@ -298,7 +299,6 @@ static int server_open_journal( s->compress.threshold_bytes, metrics, s->mmap, - s->deferred_closes, /* template= */ NULL, &f); if (r < 0) @@ -655,7 +655,6 @@ static int server_archive_offline_user_journals(Server *s) { s->compress.threshold_bytes, &s->system_storage.metrics, s->mmap, - /* deferred_closes= */ NULL, /* template= */ NULL, &f); if (r < 0) { diff --git a/src/journal/managed-journal-file.c b/src/journal/managed-journal-file.c index 9b1fe30747..aa616988b4 100644 --- a/src/journal/managed-journal-file.c +++ b/src/journal/managed-journal-file.c @@ -421,15 +421,12 @@ int managed_journal_file_open( uint64_t compress_threshold_bytes, JournalMetrics *metrics, MMapCache *mmap_cache, - Set *deferred_closes, ManagedJournalFile *template, ManagedJournalFile **ret) { _cleanup_free_ ManagedJournalFile *f = NULL; int r; - set_clear_with_destructor(deferred_closes, managed_journal_file_close); - f = new0(ManagedJournalFile, 1); if (!f) return -ENOMEM; @@ -489,6 +486,8 @@ int managed_journal_file_rotate( if (r < 0) return r; + set_clear_with_destructor(deferred_closes, managed_journal_file_close); + r = managed_journal_file_open( /* fd= */ -1, path, @@ -498,7 +497,6 @@ int managed_journal_file_rotate( compress_threshold_bytes, /* metrics= */ NULL, mmap_cache, - deferred_closes, /* template= */ *f, &new_file); @@ -516,7 +514,6 @@ int managed_journal_file_open_reliably( uint64_t compress_threshold_bytes, JournalMetrics *metrics, MMapCache *mmap_cache, - Set *deferred_closes, ManagedJournalFile *template, ManagedJournalFile **ret) { @@ -532,7 +529,6 @@ int managed_journal_file_open_reliably( compress_threshold_bytes, metrics, mmap_cache, - deferred_closes, template, ret); if (!IN_SET(r, @@ -565,7 +561,7 @@ int managed_journal_file_open_reliably( r = managed_journal_file_open(-1, fname, (open_flags & ~(O_ACCMODE|O_CREAT|O_EXCL)) | O_RDONLY, file_flags, 0, compress_threshold_bytes, NULL, - mmap_cache, deferred_closes, NULL, &old_file); + mmap_cache, NULL, &old_file); if (r < 0) log_debug_errno(r, "Failed to continue sequence from file %s, ignoring: %m", fname); else @@ -577,5 +573,5 @@ int managed_journal_file_open_reliably( return r; return managed_journal_file_open(-1, fname, open_flags, file_flags, mode, compress_threshold_bytes, metrics, - mmap_cache, deferred_closes, template, ret); + mmap_cache, template, ret); } diff --git a/src/journal/managed-journal-file.h b/src/journal/managed-journal-file.h index 0ac69a7985..2fd1e2d6b4 100644 --- a/src/journal/managed-journal-file.h +++ b/src/journal/managed-journal-file.h @@ -16,7 +16,6 @@ int managed_journal_file_open( uint64_t compress_threshold_bytes, JournalMetrics *metrics, MMapCache *mmap_cache, - Set *deferred_closes, ManagedJournalFile *template, ManagedJournalFile **ret); @@ -33,7 +32,6 @@ int managed_journal_file_open_reliably( uint64_t compress_threshold_bytes, JournalMetrics *metrics, MMapCache *mmap_cache, - Set *deferred_closes, ManagedJournalFile *template, ManagedJournalFile **ret); diff --git a/src/journal/test-journal-append.c b/src/journal/test-journal-append.c index 84fa4d5d54..95b6fc1ee5 100644 --- a/src/journal/test-journal-append.c +++ b/src/journal/test-journal-append.c @@ -70,7 +70,6 @@ static int journal_corrupt_and_append(uint64_t start_offset, uint64_t step) { /* compress_threshold_bytes= */ UINT64_MAX, /* metrics= */ NULL, mmap_cache, - /* deferred_closes= */ NULL, /* template= */ NULL, &mj); if (r < 0) @@ -124,7 +123,6 @@ static int journal_corrupt_and_append(uint64_t start_offset, uint64_t step) { /* compress_threshold_bytes= */ UINT64_MAX, /* metrics= */ NULL, mmap_cache, - /* deferred_closes= */ NULL, /* template= */ NULL, &mj); if (r < 0) { diff --git a/src/journal/test-journal-flush.c b/src/journal/test-journal-flush.c index f2fe0e51c0..677d37edc1 100644 --- a/src/journal/test-journal-flush.c +++ b/src/journal/test-journal-flush.c @@ -30,7 +30,7 @@ static void test_journal_flush(int argc, char *argv[]) { assert_se(fn = path_join(dn, "test.journal")); - r = managed_journal_file_open(-1, fn, O_CREAT|O_RDWR, 0, 0644, 0, NULL, m, NULL, NULL, &new_journal); + r = managed_journal_file_open(-1, fn, O_CREAT|O_RDWR, 0, 0644, 0, NULL, m, NULL, &new_journal); assert_se(r >= 0); if (argc > 1) diff --git a/src/journal/test-journal-interleaving.c b/src/journal/test-journal-interleaving.c index dfd4497cfd..32427b8b62 100644 --- a/src/journal/test-journal-interleaving.c +++ b/src/journal/test-journal-interleaving.c @@ -42,7 +42,7 @@ static ManagedJournalFile *test_open_internal(const char *name, JournalFileFlags m = mmap_cache_new(); assert_se(m != NULL); - assert_ret(managed_journal_file_open(-1, name, O_RDWR|O_CREAT, flags, 0644, UINT64_MAX, NULL, m, NULL, NULL, &f)); + assert_ret(managed_journal_file_open(-1, name, O_RDWR|O_CREAT, flags, 0644, UINT64_MAX, NULL, m, NULL, &f)); return f; } @@ -459,7 +459,7 @@ static void test_sequence_numbers_one(void) { mkdtemp_chdir_chattr(t); assert_se(managed_journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644, - UINT64_MAX, NULL, m, NULL, NULL, &one) == 0); + UINT64_MAX, NULL, m, NULL, &one) == 0); append_number(one, 1, NULL, &seqnum); printf("seqnum=%"PRIu64"\n", seqnum); @@ -476,7 +476,7 @@ static void test_sequence_numbers_one(void) { memcpy(&seqnum_id, &one->file->header->seqnum_id, sizeof(sd_id128_t)); assert_se(managed_journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644, - UINT64_MAX, NULL, m, NULL, one, &two) == 0); + UINT64_MAX, NULL, m, one, &two) == 0); assert_se(two->file->header->state == STATE_ONLINE); assert_se(!sd_id128_equal(two->file->header->file_id, one->file->header->file_id)); @@ -513,7 +513,7 @@ static void test_sequence_numbers_one(void) { seqnum = 0; assert_se(managed_journal_file_open(-1, "two.journal", O_RDWR, JOURNAL_COMPRESS, 0, - UINT64_MAX, NULL, m, NULL, NULL, &two) == 0); + UINT64_MAX, NULL, m, NULL, &two) == 0); assert_se(sd_id128_equal(two->file->header->seqnum_id, seqnum_id)); @@ -547,7 +547,7 @@ TEST(sequence_numbers) { } static int intro(void) { - /* managed_journal_file_open requires a valid machine id */ + /* managed_journal_file_open() requires a valid machine id */ if (access("/etc/machine-id", F_OK) != 0) return log_tests_skipped("/etc/machine-id not found"); diff --git a/src/journal/test-journal-stream.c b/src/journal/test-journal-stream.c index 940625a084..1220a29e20 100644 --- a/src/journal/test-journal-stream.c +++ b/src/journal/test-journal-stream.c @@ -76,9 +76,9 @@ static void run_test(void) { assert_se(chdir(t) >= 0); (void) chattr_path(t, FS_NOCOW_FL, FS_NOCOW_FL, NULL); - assert_se(managed_journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0666, UINT64_MAX, NULL, m, NULL, NULL, &one) == 0); - assert_se(managed_journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0666, UINT64_MAX, NULL, m, NULL, NULL, &two) == 0); - assert_se(managed_journal_file_open(-1, "three.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0666, UINT64_MAX, NULL, m, NULL, NULL, &three) == 0); + assert_se(managed_journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0666, UINT64_MAX, NULL, m, NULL, &one) == 0); + assert_se(managed_journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0666, UINT64_MAX, NULL, m, NULL, &two) == 0); + assert_se(managed_journal_file_open(-1, "three.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0666, UINT64_MAX, NULL, m, NULL, &three) == 0); for (i = 0; i < N_ENTRIES; i++) { char *p, *q; @@ -177,7 +177,7 @@ static void run_test(void) { int main(int argc, char *argv[]) { - /* managed_journal_file_open requires a valid machine id */ + /* managed_journal_file_open() requires a valid machine id */ if (access("/etc/machine-id", F_OK) != 0) return log_tests_skipped("/etc/machine-id not found"); diff --git a/src/journal/test-journal-verify.c b/src/journal/test-journal-verify.c index 97cc0b1514..9a7fa2a734 100644 --- a/src/journal/test-journal-verify.c +++ b/src/journal/test-journal-verify.c @@ -79,7 +79,7 @@ static int run_test(const char *verification_key, ssize_t max_iterations) { m = mmap_cache_new(); assert_se(m != NULL); - /* managed_journal_file_open requires a valid machine id */ + /* managed_journal_file_open() requires a valid machine id */ if (sd_id128_get_machine(NULL) < 0) return log_tests_skipped("No valid machine ID found"); @@ -100,7 +100,6 @@ static int run_test(const char *verification_key, ssize_t max_iterations) { /* compress_threshold_bytes= */ UINT64_MAX, /* metrics= */ NULL, m, - /* deferred_closes= */ NULL, /* template= */ NULL, &df) == 0); diff --git a/src/journal/test-journal.c b/src/journal/test-journal.c index b227b9be6c..92c5b66fe5 100644 --- a/src/journal/test-journal.c +++ b/src/journal/test-journal.c @@ -39,7 +39,7 @@ static void test_non_empty_one(void) { mkdtemp_chdir_chattr(t); - assert_se(managed_journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS|JOURNAL_SEAL, 0666, UINT64_MAX, NULL, m, NULL, NULL, &f) == 0); + assert_se(managed_journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS|JOURNAL_SEAL, 0666, UINT64_MAX, NULL, m, NULL, &f) == 0); assert_se(dual_timestamp_get(&ts)); assert_se(sd_id128_randomize(&fake_boot_id) == 0); @@ -136,10 +136,10 @@ static void test_empty_one(void) { mkdtemp_chdir_chattr(t); - assert_se(managed_journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0, 0666, UINT64_MAX, NULL, m, NULL, NULL, &f1) == 0); - assert_se(managed_journal_file_open(-1, "test-compress.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0666, UINT64_MAX, NULL, m, NULL, NULL, &f2) == 0); - assert_se(managed_journal_file_open(-1, "test-seal.journal", O_RDWR|O_CREAT, JOURNAL_SEAL, 0666, UINT64_MAX, NULL, m, NULL, NULL, &f3) == 0); - assert_se(managed_journal_file_open(-1, "test-seal-compress.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS|JOURNAL_SEAL, 0666, UINT64_MAX, NULL, m, NULL, NULL, &f4) == 0); + assert_se(managed_journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0, 0666, UINT64_MAX, NULL, m, NULL, &f1) == 0); + assert_se(managed_journal_file_open(-1, "test-compress.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0666, UINT64_MAX, NULL, m, NULL, &f2) == 0); + assert_se(managed_journal_file_open(-1, "test-seal.journal", O_RDWR|O_CREAT, JOURNAL_SEAL, 0666, UINT64_MAX, NULL, m, NULL, &f3) == 0); + assert_se(managed_journal_file_open(-1, "test-seal-compress.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS|JOURNAL_SEAL, 0666, UINT64_MAX, NULL, m, NULL, &f4) == 0); journal_file_print_header(f1->file); puts(""); @@ -194,7 +194,7 @@ static bool check_compressed(uint64_t compress_threshold, uint64_t data_size) { mkdtemp_chdir_chattr(t); - assert_se(managed_journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS|JOURNAL_SEAL, 0666, compress_threshold, NULL, m, NULL, NULL, &f) == 0); + assert_se(managed_journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS|JOURNAL_SEAL, 0666, compress_threshold, NULL, m, NULL, &f) == 0); dual_timestamp_get(&ts); @@ -270,7 +270,7 @@ TEST(min_compress_size) { static int intro(void) { arg_keep = saved_argc > 1; - /* managed_journal_file_open requires a valid machine id */ + /* managed_journal_file_open() requires a valid machine id */ if (access("/etc/machine-id", F_OK) != 0) return log_tests_skipped("/etc/machine-id not found"); From 45c0ecba2d502acb85405bf1330f1a466fc0cc3b Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 30 Sep 2023 15:46:16 +0900 Subject: [PATCH 3/7] journald: drop ManagedJournalFile The ManagedJournalFile object is a trivial wrapper of JournalFile. Let's drop it, and also drop 'managed_' prefix from the functions. --- src/journal-remote/journal-remote-write.c | 22 +-- src/journal-remote/journal-remote-write.h | 2 +- src/journal-remote/journal-remote.c | 4 +- src/journal/journald-server.c | 114 ++++++------- src/journal/journald-server.h | 4 +- src/journal/journald.c | 2 +- src/journal/managed-journal-file.c | 196 +++++++++------------- src/journal/managed-journal-file.h | 37 ++-- src/journal/test-journal-append.c | 63 ++++--- src/journal/test-journal-flush.c | 8 +- src/journal/test-journal-interleaving.c | 68 ++++---- src/journal/test-journal-stream.c | 22 +-- src/journal/test-journal-verify.c | 10 +- src/journal/test-journal.c | 98 +++++------ 14 files changed, 299 insertions(+), 351 deletions(-) diff --git a/src/journal-remote/journal-remote-write.c b/src/journal-remote/journal-remote-write.c index 158d015d89..f972b1fd47 100644 --- a/src/journal-remote/journal-remote-write.c +++ b/src/journal-remote/journal-remote-write.c @@ -7,13 +7,13 @@ #include "path-util.h" #include "stat-util.h" -static int do_rotate(ManagedJournalFile **f, MMapCache *m, JournalFileFlags file_flags) { +static int do_rotate(JournalFile **f, MMapCache *m, JournalFileFlags file_flags) { int r; - r = managed_journal_file_rotate(f, m, file_flags, UINT64_MAX, NULL); + r = journal_file_rotate(f, m, file_flags, UINT64_MAX, NULL); if (r < 0) { if (*f) - log_error_errno(r, "Failed to rotate %s: %m", (*f)->file->path); + log_error_errno(r, "Failed to rotate %s: %m", (*f)->path); else log_error_errno(r, "Failed to create rotated journal: %m"); } @@ -61,8 +61,8 @@ static Writer* writer_free(Writer *w) { return NULL; if (w->journal) { - log_debug("Closing journal file %s.", w->journal->file->path); - managed_journal_file_close(w->journal); + log_debug("Closing journal file %s.", w->journal->path); + journal_file_offline_close(w->journal); } if (w->server && w->hashmap_key) @@ -90,9 +90,9 @@ int writer_write(Writer *w, assert(w); assert(!iovw_isempty(iovw)); - if (journal_file_rotate_suggested(w->journal->file, 0, LOG_DEBUG)) { + if (journal_file_rotate_suggested(w->journal, 0, LOG_DEBUG)) { log_info("%s: Journal header limits reached or header out-of-date, rotating", - w->journal->file->path); + w->journal->path); r = do_rotate(&w->journal, w->mmap, file_flags); if (r < 0) return r; @@ -102,7 +102,7 @@ int writer_write(Writer *w, } r = journal_file_append_entry( - w->journal->file, + w->journal, ts, boot_id, iovw->iovec, @@ -118,19 +118,19 @@ int writer_write(Writer *w, } else if (r == -EBADMSG) return r; - log_debug_errno(r, "%s: Write failed, rotating: %m", w->journal->file->path); + log_debug_errno(r, "%s: Write failed, rotating: %m", w->journal->path); r = do_rotate(&w->journal, w->mmap, file_flags); if (r < 0) return r; else - log_debug("%s: Successfully rotated journal", w->journal->file->path); + log_debug("%s: Successfully rotated journal", w->journal->path); r = journal_directory_vacuum(w->output, w->metrics.max_use, w->metrics.n_max_files, 0, NULL, /* verbose = */ true); if (r < 0) return r; log_debug("Retrying write."); r = journal_file_append_entry( - w->journal->file, + w->journal, ts, boot_id, iovw->iovec, iovw->count, diff --git a/src/journal-remote/journal-remote-write.h b/src/journal-remote/journal-remote-write.h index 55513d9bf7..65ec2a599a 100644 --- a/src/journal-remote/journal-remote-write.h +++ b/src/journal-remote/journal-remote-write.h @@ -7,7 +7,7 @@ typedef struct RemoteServer RemoteServer; typedef struct Writer { - ManagedJournalFile *journal; + JournalFile *journal; JournalMetrics metrics; char *output; /* directory where we write, for vacuuming */ diff --git a/src/journal-remote/journal-remote.c b/src/journal-remote/journal-remote.c index 29da82e65a..16f74ef066 100644 --- a/src/journal-remote/journal-remote.c +++ b/src/journal-remote/journal-remote.c @@ -79,7 +79,7 @@ static int open_output(RemoteServer *s, Writer *w, const char* host) { assert_not_reached(); } - r = managed_journal_file_open_reliably( + r = journal_file_open_reliably( filename, O_RDWR|O_CREAT, s->file_flags, @@ -92,7 +92,7 @@ static int open_output(RemoteServer *s, Writer *w, const char* host) { if (r < 0) return log_error_errno(r, "Failed to open output journal %s: %m", filename); - log_debug("Opened output file %s", w->journal->file->path); + log_debug("Opened output file %s", w->journal->path); return 0; } diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index 83857d0116..3b07a4bf0d 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -238,7 +238,7 @@ void server_space_usage_message(Server *s, JournalStorage *storage) { NULL); } -static void server_add_acls(ManagedJournalFile *f, uid_t uid) { +static void server_add_acls(JournalFile *f, uid_t uid) { assert(f); #if HAVE_ACL @@ -247,10 +247,10 @@ static void server_add_acls(ManagedJournalFile *f, uid_t uid) { if (uid_for_system_journal(uid)) return; - r = fd_add_uid_acl_permission(f->file->fd, uid, ACL_READ); + r = fd_add_uid_acl_permission(f->fd, uid, ACL_READ); if (r < 0) log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT, - "Failed to set ACL on %s, ignoring: %m", f->file->path); + "Failed to set ACL on %s, ignoring: %m", f->path); #endif } @@ -261,9 +261,9 @@ static int server_open_journal( int open_flags, bool seal, JournalMetrics *metrics, - ManagedJournalFile **ret) { + JournalFile **ret) { - _cleanup_(managed_journal_file_closep) ManagedJournalFile *f = NULL; + _cleanup_(journal_file_offline_closep) JournalFile *f = NULL; JournalFileFlags file_flags; int r; @@ -276,10 +276,10 @@ static int server_open_journal( (seal ? JOURNAL_SEAL : 0) | JOURNAL_STRICT_ORDER; - set_clear_with_destructor(s->deferred_closes, managed_journal_file_close); + set_clear_with_destructor(s->deferred_closes, journal_file_offline_close); if (reliably) - r = managed_journal_file_open_reliably( + r = journal_file_open_reliably( fname, open_flags, file_flags, @@ -290,7 +290,7 @@ static int server_open_journal( /* template= */ NULL, &f); else - r = managed_journal_file_open( + r = journal_file_open( /* fd= */ -1, fname, open_flags, @@ -304,7 +304,7 @@ static int server_open_journal( if (r < 0) return r; - r = journal_file_enable_post_change_timer(f->file, s->event, POST_CHANGE_TIMER_INTERVAL_USEC); + r = journal_file_enable_post_change_timer(f, s->event, POST_CHANGE_TIMER_INTERVAL_USEC); if (r < 0) return r; @@ -434,8 +434,8 @@ static int server_system_journal_open( return r; } -static int server_find_user_journal(Server *s, uid_t uid, ManagedJournalFile **ret) { - _cleanup_(managed_journal_file_closep) ManagedJournalFile *f = NULL; +static int server_find_user_journal(Server *s, uid_t uid, JournalFile **ret) { + _cleanup_(journal_file_offline_closep) JournalFile *f = NULL; _cleanup_free_ char *p = NULL; int r; @@ -450,10 +450,10 @@ static int server_find_user_journal(Server *s, uid_t uid, ManagedJournalFile **r /* Too many open? Then let's close one (or more) */ while (ordered_hashmap_size(s->user_journals) >= USER_JOURNALS_MAX) { - ManagedJournalFile *first; + JournalFile *first; assert_se(first = ordered_hashmap_steal_first(s->user_journals)); - (void) managed_journal_file_close(first); + (void) journal_file_offline_close(first); } r = server_open_journal( @@ -478,7 +478,7 @@ found: return 0; } -static ManagedJournalFile* server_find_journal(Server *s, uid_t uid) { +static JournalFile* server_find_journal(Server *s, uid_t uid) { int r; assert(s); @@ -507,7 +507,7 @@ static ManagedJournalFile* server_find_journal(Server *s, uid_t uid) { return NULL; if (!uid_for_system_journal(uid)) { - ManagedJournalFile *f = NULL; + JournalFile *f = NULL; r = server_find_user_journal(s, uid, &f); if (r >= 0) @@ -521,7 +521,7 @@ static ManagedJournalFile* server_find_journal(Server *s, uid_t uid) { static int server_do_rotate( Server *s, - ManagedJournalFile **f, + JournalFile **f, const char* name, bool seal, uint32_t uid) { @@ -539,11 +539,11 @@ static int server_do_rotate( (seal ? JOURNAL_SEAL : 0) | JOURNAL_STRICT_ORDER; - r = managed_journal_file_rotate(f, s->mmap, file_flags, s->compress.threshold_bytes, s->deferred_closes); + r = journal_file_rotate(f, s->mmap, file_flags, s->compress.threshold_bytes, s->deferred_closes); if (r < 0) { if (*f) return log_ratelimit_error_errno(r, JOURNAL_LOG_RATELIMIT, - "Failed to rotate %s: %m", (*f)->file->path); + "Failed to rotate %s: %m", (*f)->path); else return log_ratelimit_error_errno(r, JOURNAL_LOG_RATELIMIT, "Failed to create new %s journal: %m", name); @@ -554,15 +554,15 @@ static int server_do_rotate( } static void server_process_deferred_closes(Server *s) { - ManagedJournalFile *f; + JournalFile *f; /* Perform any deferred closes which aren't still offlining. */ SET_FOREACH(f, s->deferred_closes) { - if (managed_journal_file_is_offlining(f)) + if (journal_file_is_offlining(f)) continue; (void) set_remove(s->deferred_closes, f); - (void) managed_journal_file_close(f); + (void) journal_file_offline_close(f); } } @@ -578,10 +578,10 @@ static void server_vacuum_deferred_closes(Server *s) { /* And now, let's close some more until we reach the limit again. */ while (set_size(s->deferred_closes) >= DEFERRED_CLOSES_MAX) { - ManagedJournalFile *f; + JournalFile *f; assert_se(f = set_steal_first(s->deferred_closes)); - managed_journal_file_close(f); + journal_file_offline_close(f); } } @@ -604,7 +604,7 @@ static int server_archive_offline_user_journals(Server *s) { _cleanup_free_ char *full = NULL; _cleanup_close_ int fd = -EBADF; struct dirent *de; - ManagedJournalFile *f; + JournalFile *f; uid_t uid; errno = 0; @@ -645,7 +645,7 @@ static int server_archive_offline_user_journals(Server *s) { server_vacuum_deferred_closes(s); /* Open the file briefly, so that we can archive it */ - r = managed_journal_file_open( + r = journal_file_open( fd, full, O_RDWR, @@ -673,20 +673,20 @@ static int server_archive_offline_user_journals(Server *s) { continue; } - TAKE_FD(fd); /* Donated to managed_journal_file_open() */ + TAKE_FD(fd); /* Donated to journal_file_open() */ - r = journal_file_archive(f->file, NULL); + r = journal_file_archive(f, NULL); if (r < 0) log_debug_errno(r, "Failed to archive journal file '%s', ignoring: %m", full); - managed_journal_file_initiate_close(TAKE_PTR(f), s->deferred_closes); + journal_file_initiate_close(TAKE_PTR(f), s->deferred_closes); } return 0; } void server_rotate(Server *s) { - ManagedJournalFile *f; + JournalFile *f; void *k; int r; @@ -715,18 +715,18 @@ void server_rotate(Server *s) { } void server_sync(Server *s) { - ManagedJournalFile *f; + JournalFile *f; int r; if (s->system_journal) { - r = managed_journal_file_set_offline(s->system_journal, false); + r = journal_file_set_offline(s->system_journal, false); if (r < 0) log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT, "Failed to sync system journal, ignoring: %m"); } ORDERED_HASHMAP_FOREACH(f, s->user_journals) { - r = managed_journal_file_set_offline(f, false); + r = journal_file_set_offline(f, false); if (r < 0) log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT, "Failed to sync user journal, ignoring: %m"); @@ -900,7 +900,7 @@ static void server_write_to_journal( bool vacuumed = false, rotate = false; struct dual_timestamp ts; - ManagedJournalFile *f; + JournalFile *f; int r; assert(s); @@ -927,9 +927,9 @@ static void server_write_to_journal( if (!f) return; - if (journal_file_rotate_suggested(f->file, s->max_file_usec, LOG_DEBUG)) { + if (journal_file_rotate_suggested(f, s->max_file_usec, LOG_DEBUG)) { log_debug("%s: Journal header limits reached or header out-of-date, rotating.", - f->file->path); + f->path); rotate = true; } } @@ -947,7 +947,7 @@ static void server_write_to_journal( s->last_realtime_clock = ts.realtime; r = journal_file_append_entry( - f->file, + f, &ts, /* boot_id= */ NULL, iovec, n, @@ -960,9 +960,9 @@ static void server_write_to_journal( return; } - log_debug_errno(r, "Failed to write entry to %s (%zu items, %zu bytes): %m", f->file->path, n, IOVEC_TOTAL_SIZE(iovec, n)); + log_debug_errno(r, "Failed to write entry to %s (%zu items, %zu bytes): %m", f->path, n, IOVEC_TOTAL_SIZE(iovec, n)); - if (!shall_try_append_again(f->file, r)) + if (!shall_try_append_again(f, r)) return; if (vacuumed) { log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT, @@ -979,7 +979,7 @@ static void server_write_to_journal( log_debug_errno(r, "Retrying write."); r = journal_file_append_entry( - f->file, + f, &ts, /* boot_id= */ NULL, iovec, n, @@ -990,7 +990,7 @@ static void server_write_to_journal( if (r < 0) log_ratelimit_error_errno(r, FAILED_TO_WRITE_ENTRY_RATELIMIT, "Failed to write entry to %s (%zu items, %zu bytes) despite vacuuming, ignoring: %m", - f->file->path, n, IOVEC_TOTAL_SIZE(iovec, n)); + f->path, n, IOVEC_TOTAL_SIZE(iovec, n)); else server_schedule_sync(s, priority); } @@ -1300,7 +1300,7 @@ int server_flush_to_var(Server *s, bool require_flag_file) { r = journal_file_copy_entry( f, - s->system_journal->file, + s->system_journal, o, f->current_offset, &s->seqnum->seqnum, @@ -1308,7 +1308,7 @@ int server_flush_to_var(Server *s, bool require_flag_file) { if (r >= 0) continue; - if (!shall_try_append_again(s->system_journal->file, r)) { + if (!shall_try_append_again(s->system_journal, r)) { log_ratelimit_error_errno(r, JOURNAL_LOG_RATELIMIT, "Can't write entry: %m"); goto finish; } @@ -1328,7 +1328,7 @@ int server_flush_to_var(Server *s, bool require_flag_file) { log_debug("Retrying write."); r = journal_file_copy_entry( f, - s->system_journal->file, + s->system_journal, o, f->current_offset, &s->seqnum->seqnum, @@ -1343,9 +1343,9 @@ int server_flush_to_var(Server *s, bool require_flag_file) { finish: if (s->system_journal) - journal_file_post_change(s->system_journal->file); + journal_file_post_change(s->system_journal); - s->runtime_journal = managed_journal_file_close(s->runtime_journal); + s->runtime_journal = journal_file_offline_close(s->runtime_journal); if (r >= 0) (void) rm_rf(s->runtime_storage.path, REMOVE_ROOT); @@ -1386,9 +1386,9 @@ static int server_relinquish_var(Server *s) { (void) server_system_journal_open(s, /* flush_requested */ false, /* relinquish_requested=*/ true); - s->system_journal = managed_journal_file_close(s->system_journal); - ordered_hashmap_clear_with_destructor(s->user_journals, managed_journal_file_close); - set_clear_with_destructor(s->deferred_closes, managed_journal_file_close); + s->system_journal = journal_file_offline_close(s->system_journal); + ordered_hashmap_clear_with_destructor(s->user_journals, journal_file_offline_close); + set_clear_with_destructor(s->deferred_closes, journal_file_offline_close); fn = strjoina(s->runtime_directory, "/flushed"); if (unlink(fn) < 0 && errno != ENOENT) @@ -2405,12 +2405,12 @@ static int server_memory_pressure(sd_event_source *es, void *userdata) { /* Let's also close all user files (but keep the system/runtime one open) */ for (;;) { - ManagedJournalFile *first = ordered_hashmap_steal_first(s->user_journals); + JournalFile *first = ordered_hashmap_steal_first(s->user_journals); if (!first) break; - (void) managed_journal_file_close(first); + (void) journal_file_offline_close(first); } sd_event_trim_memory(); @@ -2713,16 +2713,16 @@ int server_init(Server *s, const char *namespace) { void server_maybe_append_tags(Server *s) { #if HAVE_GCRYPT - ManagedJournalFile *f; + JournalFile *f; usec_t n; n = now(CLOCK_REALTIME); if (s->system_journal) - journal_file_maybe_append_tag(s->system_journal->file, n); + journal_file_maybe_append_tag(s->system_journal, n); ORDERED_HASHMAP_FOREACH(f, s->user_journals) - journal_file_maybe_append_tag(f->file, n); + journal_file_maybe_append_tag(f, n); #endif } @@ -2732,17 +2732,17 @@ void server_done(Server *s) { free(s->namespace); free(s->namespace_field); - set_free_with_destructor(s->deferred_closes, managed_journal_file_close); + set_free_with_destructor(s->deferred_closes, journal_file_offline_close); while (s->stdout_streams) stdout_stream_free(s->stdout_streams); client_context_flush_all(s); - (void) managed_journal_file_close(s->system_journal); - (void) managed_journal_file_close(s->runtime_journal); + (void) journal_file_offline_close(s->system_journal); + (void) journal_file_offline_close(s->runtime_journal); - ordered_hashmap_free_with_destructor(s->user_journals, managed_journal_file_close); + ordered_hashmap_free_with_destructor(s->user_journals, journal_file_offline_close); varlink_server_unref(s->varlink_server); diff --git a/src/journal/journald-server.h b/src/journal/journald-server.h index e03cfc04c9..4e9a67c306 100644 --- a/src/journal/journald-server.h +++ b/src/journal/journald-server.h @@ -98,8 +98,8 @@ struct Server { sd_event_source *idle_event_source; struct sigrtmin18_info sigrtmin18_info; - ManagedJournalFile *runtime_journal; - ManagedJournalFile *system_journal; + JournalFile *runtime_journal; + JournalFile *system_journal; OrderedHashmap *user_journals; SeqnumData *seqnum; diff --git a/src/journal/journald.c b/src/journal/journald.c index 3d4044295e..94aad05de9 100644 --- a/src/journal/journald.c +++ b/src/journal/journald.c @@ -102,7 +102,7 @@ int main(int argc, char *argv[]) { if (server.system_journal) { usec_t u; - if (journal_file_next_evolve_usec(server.system_journal->file, &u)) { + if (journal_file_next_evolve_usec(server.system_journal, &u)) { if (n >= u) t = 0; else diff --git a/src/journal/managed-journal-file.c b/src/journal/managed-journal-file.c index aa616988b4..34f3497621 100644 --- a/src/journal/managed-journal-file.c +++ b/src/journal/managed-journal-file.c @@ -19,7 +19,7 @@ #define PAYLOAD_BUFFER_SIZE (16U * 1024U) #define MINIMUM_HOLE_SIZE (1U * 1024U * 1024U / 2U) -static int managed_journal_file_truncate(JournalFile *f) { +static int journal_file_truncate(JournalFile *f) { uint64_t p; int r; @@ -37,7 +37,7 @@ static int managed_journal_file_truncate(JournalFile *f) { return journal_file_fstat(f); } -static int managed_journal_file_entry_array_punch_hole(JournalFile *f, uint64_t p, uint64_t n_entries) { +static int journal_file_entry_array_punch_hole(JournalFile *f, uint64_t p, uint64_t n_entries) { Object o; uint64_t offset, sz, n_items = 0, n_unused; int r; @@ -103,13 +103,13 @@ static int managed_journal_file_entry_array_punch_hole(JournalFile *f, uint64_t return 0; } -static int managed_journal_file_punch_holes(JournalFile *f) { +static int journal_file_punch_holes(JournalFile *f) { HashItem items[PAYLOAD_BUFFER_SIZE / sizeof(HashItem)]; uint64_t p, sz; ssize_t n = SSIZE_MAX; int r; - r = managed_journal_file_entry_array_punch_hole( + r = journal_file_entry_array_punch_hole( f, le64toh(f->header->entry_array_offset), le64toh(f->header->n_entries)); if (r < 0) return r; @@ -141,7 +141,7 @@ static int managed_journal_file_punch_holes(JournalFile *f) { if (le64toh(o.data.n_entries) == 0) continue; - r = managed_journal_file_entry_array_punch_hole( + r = journal_file_entry_array_punch_hole( f, le64toh(o.data.entry_array_offset), le64toh(o.data.n_entries) - 1); if (r == -EOPNOTSUPP) return -EOPNOTSUPP; @@ -157,18 +157,18 @@ static int managed_journal_file_punch_holes(JournalFile *f) { /* This may be called from a separate thread to prevent blocking the caller for the duration of fsync(). * As a result we use atomic operations on f->offline_state for inter-thread communications with * journal_file_set_offline() and journal_file_set_online(). */ -static void managed_journal_file_set_offline_internal(ManagedJournalFile *f) { +static void journal_file_set_offline_internal(JournalFile *f) { int r; assert(f); - assert(f->file->fd >= 0); - assert(f->file->header); + assert(f->fd >= 0); + assert(f->header); for (;;) { - switch (f->file->offline_state) { + switch (f->offline_state) { case OFFLINE_CANCEL: { OfflineState tmp_state = OFFLINE_CANCEL; - if (!__atomic_compare_exchange_n(&f->file->offline_state, &tmp_state, OFFLINE_DONE, + if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_DONE, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) continue; } @@ -176,7 +176,7 @@ static void managed_journal_file_set_offline_internal(ManagedJournalFile *f) { case OFFLINE_AGAIN_FROM_SYNCING: { OfflineState tmp_state = OFFLINE_AGAIN_FROM_SYNCING; - if (!__atomic_compare_exchange_n(&f->file->offline_state, &tmp_state, OFFLINE_SYNCING, + if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_SYNCING, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) continue; } @@ -184,29 +184,29 @@ static void managed_journal_file_set_offline_internal(ManagedJournalFile *f) { case OFFLINE_AGAIN_FROM_OFFLINING: { OfflineState tmp_state = OFFLINE_AGAIN_FROM_OFFLINING; - if (!__atomic_compare_exchange_n(&f->file->offline_state, &tmp_state, OFFLINE_SYNCING, + if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_SYNCING, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) continue; } break; case OFFLINE_SYNCING: - if (f->file->archive) { - (void) managed_journal_file_truncate(f->file); - (void) managed_journal_file_punch_holes(f->file); + if (f->archive) { + (void) journal_file_truncate(f); + (void) journal_file_punch_holes(f); } - (void) fsync(f->file->fd); + (void) fsync(f->fd); { OfflineState tmp_state = OFFLINE_SYNCING; - if (!__atomic_compare_exchange_n(&f->file->offline_state, &tmp_state, OFFLINE_OFFLINING, + if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_OFFLINING, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) continue; } - f->file->header->state = f->file->archive ? STATE_ARCHIVED : STATE_OFFLINE; - (void) fsync(f->file->fd); + f->header->state = f->archive ? STATE_ARCHIVED : STATE_OFFLINE; + (void) fsync(f->fd); /* If we've archived the journal file, first try to re-enable COW on the file. If the * FS_NOCOW_FL flag was never set or we successfully removed it, continue. If we fail @@ -215,20 +215,20 @@ static void managed_journal_file_set_offline_internal(ManagedJournalFile *f) { * be removed after data has been written to a file. The only way to remove it is to * copy all data to a new file without the NOCOW flag set. */ - if (f->file->archive) { - r = chattr_fd(f->file->fd, 0, FS_NOCOW_FL, NULL); + if (f->archive) { + r = chattr_fd(f->fd, 0, FS_NOCOW_FL, NULL); if (r >= 0) continue; - log_debug_errno(r, "Failed to re-enable copy-on-write for %s: %m, rewriting file", f->file->path); + log_debug_errno(r, "Failed to re-enable copy-on-write for %s: %m, rewriting file", f->path); - r = copy_file_atomic_full(FORMAT_PROC_FD_PATH(f->file->fd), f->file->path, f->file->mode, + r = copy_file_atomic_full(FORMAT_PROC_FD_PATH(f->fd), f->path, f->mode, 0, FS_NOCOW_FL, COPY_REPLACE | COPY_FSYNC | COPY_HOLES | COPY_ALL_XATTRS, NULL, NULL); if (r < 0) { - log_debug_errno(r, "Failed to rewrite %s: %m", f->file->path); + log_debug_errno(r, "Failed to rewrite %s: %m", f->path); continue; } } @@ -237,7 +237,7 @@ static void managed_journal_file_set_offline_internal(ManagedJournalFile *f) { case OFFLINE_OFFLINING: { OfflineState tmp_state = OFFLINE_OFFLINING; - if (!__atomic_compare_exchange_n(&f->file->offline_state, &tmp_state, OFFLINE_DONE, + if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_DONE, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) continue; } @@ -252,27 +252,27 @@ static void managed_journal_file_set_offline_internal(ManagedJournalFile *f) { } } -static void * managed_journal_file_set_offline_thread(void *arg) { - ManagedJournalFile *f = arg; +static void * journal_file_set_offline_thread(void *arg) { + JournalFile *f = arg; (void) pthread_setname_np(pthread_self(), "journal-offline"); - managed_journal_file_set_offline_internal(f); + journal_file_set_offline_internal(f); return NULL; } /* Trigger a restart if the offline thread is mid-flight in a restartable state. */ -static bool managed_journal_file_set_offline_try_restart(ManagedJournalFile *f) { +static bool journal_file_set_offline_try_restart(JournalFile *f) { for (;;) { - switch (f->file->offline_state) { + switch (f->offline_state) { case OFFLINE_AGAIN_FROM_SYNCING: case OFFLINE_AGAIN_FROM_OFFLINING: return true; case OFFLINE_CANCEL: { OfflineState tmp_state = OFFLINE_CANCEL; - if (!__atomic_compare_exchange_n(&f->file->offline_state, &tmp_state, OFFLINE_AGAIN_FROM_SYNCING, + if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_AGAIN_FROM_SYNCING, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) continue; } @@ -280,7 +280,7 @@ static bool managed_journal_file_set_offline_try_restart(ManagedJournalFile *f) case OFFLINE_SYNCING: { OfflineState tmp_state = OFFLINE_SYNCING; - if (!__atomic_compare_exchange_n(&f->file->offline_state, &tmp_state, OFFLINE_AGAIN_FROM_SYNCING, + if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_AGAIN_FROM_SYNCING, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) continue; } @@ -288,7 +288,7 @@ static bool managed_journal_file_set_offline_try_restart(ManagedJournalFile *f) case OFFLINE_OFFLINING: { OfflineState tmp_state = OFFLINE_OFFLINING; - if (!__atomic_compare_exchange_n(&f->file->offline_state, &tmp_state, OFFLINE_AGAIN_FROM_OFFLINING, + if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_AGAIN_FROM_OFFLINING, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) continue; } @@ -310,32 +310,32 @@ static bool managed_journal_file_set_offline_try_restart(ManagedJournalFile *f) * and joined, or if none exists the offline is simply performed in this * context without involving another thread. */ -int managed_journal_file_set_offline(ManagedJournalFile *f, bool wait) { +int journal_file_set_offline(JournalFile *f, bool wait) { int target_state; bool restarted; int r; assert(f); - if (!journal_file_writable(f->file)) + if (!journal_file_writable(f)) return -EPERM; - if (f->file->fd < 0 || !f->file->header) + if (f->fd < 0 || !f->header) return -EINVAL; - target_state = f->file->archive ? STATE_ARCHIVED : STATE_OFFLINE; + target_state = f->archive ? STATE_ARCHIVED : STATE_OFFLINE; /* An offlining journal is implicitly online and may modify f->header->state, * we must also join any potentially lingering offline thread when already in * the desired offline state. */ - if (!managed_journal_file_is_offlining(f) && f->file->header->state == target_state) - return journal_file_set_offline_thread_join(f->file); + if (!journal_file_is_offlining(f) && f->header->state == target_state) + return journal_file_set_offline_thread_join(f); /* Restart an in-flight offline thread and wait if needed, or join a lingering done one. */ - restarted = managed_journal_file_set_offline_try_restart(f); + restarted = journal_file_set_offline_try_restart(f); if ((restarted && wait) || !restarted) { - r = journal_file_set_offline_thread_join(f->file); + r = journal_file_set_offline_thread_join(f); if (r < 0) return r; } @@ -344,10 +344,10 @@ int managed_journal_file_set_offline(ManagedJournalFile *f, bool wait) { return 0; /* Initiate a new offline. */ - f->file->offline_state = OFFLINE_SYNCING; + f->offline_state = OFFLINE_SYNCING; if (wait) /* Without using a thread if waiting. */ - managed_journal_file_set_offline_internal(f); + journal_file_set_offline_internal(f); else { sigset_t ss, saved_ss; int k; @@ -361,11 +361,11 @@ int managed_journal_file_set_offline(ManagedJournalFile *f, bool wait) { if (r > 0) return -r; - r = pthread_create(&f->file->offline_thread, NULL, managed_journal_file_set_offline_thread, f); + r = pthread_create(&f->offline_thread, NULL, journal_file_set_offline_thread, f); k = pthread_sigmask(SIG_SETMASK, &saved_ss, NULL); if (r > 0) { - f->file->offline_state = OFFLINE_JOINED; + f->offline_state = OFFLINE_JOINED; return -r; } if (k > 0) @@ -375,82 +375,42 @@ int managed_journal_file_set_offline(ManagedJournalFile *f, bool wait) { return 0; } -bool managed_journal_file_is_offlining(ManagedJournalFile *f) { +bool journal_file_is_offlining(JournalFile *f) { assert(f); __atomic_thread_fence(__ATOMIC_SEQ_CST); - if (IN_SET(f->file->offline_state, OFFLINE_DONE, OFFLINE_JOINED)) + if (IN_SET(f->offline_state, OFFLINE_DONE, OFFLINE_JOINED)) return false; return true; } -ManagedJournalFile* managed_journal_file_close(ManagedJournalFile *f) { +JournalFile* journal_file_offline_close(JournalFile *f) { if (!f) return NULL; #if HAVE_GCRYPT /* Write the final tag */ - if (JOURNAL_HEADER_SEALED(f->file->header) && journal_file_writable(f->file)) { + if (JOURNAL_HEADER_SEALED(f->header) && journal_file_writable(f)) { int r; - r = journal_file_append_tag(f->file); + r = journal_file_append_tag(f); if (r < 0) log_error_errno(r, "Failed to append tag when closing journal: %m"); } #endif - if (sd_event_source_get_enabled(f->file->post_change_timer, NULL) > 0) - journal_file_post_change(f->file); - sd_event_source_disable_unref(f->file->post_change_timer); + if (sd_event_source_get_enabled(f->post_change_timer, NULL) > 0) + journal_file_post_change(f); + sd_event_source_disable_unref(f->post_change_timer); - managed_journal_file_set_offline(f, true); + journal_file_set_offline(f, true); - journal_file_close(f->file); - - return mfree(f); + return journal_file_close(f); } -int managed_journal_file_open( - int fd, - const char *fname, - int open_flags, - JournalFileFlags file_flags, - mode_t mode, - uint64_t compress_threshold_bytes, - JournalMetrics *metrics, - MMapCache *mmap_cache, - ManagedJournalFile *template, - ManagedJournalFile **ret) { - - _cleanup_free_ ManagedJournalFile *f = NULL; - int r; - - f = new0(ManagedJournalFile, 1); - if (!f) - return -ENOMEM; - - r = journal_file_open( - fd, - fname, - open_flags, - file_flags, - mode, - compress_threshold_bytes, - metrics, - mmap_cache, - template ? template->file : NULL, - &f->file); - if (r < 0) - return r; - - *ret = TAKE_PTR(f); - - return 0; -} - -ManagedJournalFile* managed_journal_file_initiate_close(ManagedJournalFile *f, Set *deferred_closes) { +JournalFile* journal_file_initiate_close(JournalFile *f, Set *deferred_closes) { int r; assert(f); @@ -460,53 +420,53 @@ ManagedJournalFile* managed_journal_file_initiate_close(ManagedJournalFile *f, S if (r < 0) log_debug_errno(r, "Failed to add file to deferred close set, closing immediately."); else { - (void) managed_journal_file_set_offline(f, false); + (void) journal_file_set_offline(f, false); return NULL; } } - return managed_journal_file_close(f); + return journal_file_offline_close(f); } -int managed_journal_file_rotate( - ManagedJournalFile **f, +int journal_file_rotate( + JournalFile **f, MMapCache *mmap_cache, JournalFileFlags file_flags, uint64_t compress_threshold_bytes, Set *deferred_closes) { _cleanup_free_ char *path = NULL; - ManagedJournalFile *new_file = NULL; + JournalFile *new_file = NULL; int r; assert(f); assert(*f); - r = journal_file_archive((*f)->file, &path); + r = journal_file_archive(*f, &path); if (r < 0) return r; - set_clear_with_destructor(deferred_closes, managed_journal_file_close); + set_clear_with_destructor(deferred_closes, journal_file_offline_close); - r = managed_journal_file_open( + r = journal_file_open( /* fd= */ -1, path, - (*f)->file->open_flags, + (*f)->open_flags, file_flags, - (*f)->file->mode, + (*f)->mode, compress_threshold_bytes, /* metrics= */ NULL, mmap_cache, /* template= */ *f, &new_file); - managed_journal_file_initiate_close(*f, deferred_closes); + journal_file_initiate_close(*f, deferred_closes); *f = new_file; return r; } -int managed_journal_file_open_reliably( +int journal_file_open_reliably( const char *fname, int open_flags, JournalFileFlags file_flags, @@ -514,13 +474,13 @@ int managed_journal_file_open_reliably( uint64_t compress_threshold_bytes, JournalMetrics *metrics, MMapCache *mmap_cache, - ManagedJournalFile *template, - ManagedJournalFile **ret) { + JournalFile *template, + JournalFile **ret) { - _cleanup_(managed_journal_file_closep) ManagedJournalFile *old_file = NULL; + _cleanup_(journal_file_offline_closep) JournalFile *old_file = NULL; int r; - r = managed_journal_file_open( + r = journal_file_open( /* fd= */ -1, fname, open_flags, @@ -558,10 +518,10 @@ int managed_journal_file_open_reliably( if (!template) { /* The file is corrupted and no template is specified. Try opening it read-only as the * template before rotating to inherit its sequence number and ID. */ - r = managed_journal_file_open(-1, fname, - (open_flags & ~(O_ACCMODE|O_CREAT|O_EXCL)) | O_RDONLY, - file_flags, 0, compress_threshold_bytes, NULL, - mmap_cache, NULL, &old_file); + r = journal_file_open(-1, fname, + (open_flags & ~(O_ACCMODE|O_CREAT|O_EXCL)) | O_RDONLY, + file_flags, 0, compress_threshold_bytes, NULL, + mmap_cache, NULL, &old_file); if (r < 0) log_debug_errno(r, "Failed to continue sequence from file %s, ignoring: %m", fname); else @@ -572,6 +532,6 @@ int managed_journal_file_open_reliably( if (r < 0) return r; - return managed_journal_file_open(-1, fname, open_flags, file_flags, mode, compress_threshold_bytes, metrics, - mmap_cache, template, ret); + return journal_file_open(-1, fname, open_flags, file_flags, mode, compress_threshold_bytes, metrics, + mmap_cache, template, ret); } diff --git a/src/journal/managed-journal-file.h b/src/journal/managed-journal-file.h index 2fd1e2d6b4..cbe29c762e 100644 --- a/src/journal/managed-journal-file.h +++ b/src/journal/managed-journal-file.h @@ -3,12 +3,12 @@ #include "journal-file.h" -typedef struct { - JournalFile *file; -} ManagedJournalFile; +int journal_file_set_offline(JournalFile *f, bool wait); +bool journal_file_is_offlining(JournalFile *f); +JournalFile* journal_file_offline_close(JournalFile *f); +DEFINE_TRIVIAL_CLEANUP_FUNC(JournalFile*, journal_file_offline_close); -int managed_journal_file_open( - int fd, +int journal_file_open_reliably( const char *fname, int open_flags, JournalFileFlags file_flags, @@ -16,24 +16,13 @@ int managed_journal_file_open( uint64_t compress_threshold_bytes, JournalMetrics *metrics, MMapCache *mmap_cache, - ManagedJournalFile *template, - ManagedJournalFile **ret); + JournalFile *template, + JournalFile **ret); -int managed_journal_file_set_offline(ManagedJournalFile *f, bool wait); -bool managed_journal_file_is_offlining(ManagedJournalFile *f); -ManagedJournalFile* managed_journal_file_close(ManagedJournalFile *f); -DEFINE_TRIVIAL_CLEANUP_FUNC(ManagedJournalFile*, managed_journal_file_close); - -int managed_journal_file_open_reliably( - const char *fname, - int open_flags, - JournalFileFlags file_flags, - mode_t mode, - uint64_t compress_threshold_bytes, - JournalMetrics *metrics, +JournalFile* journal_file_initiate_close(JournalFile *f, Set *deferred_closes); +int journal_file_rotate( + JournalFile **f, MMapCache *mmap_cache, - ManagedJournalFile *template, - ManagedJournalFile **ret); - -ManagedJournalFile* managed_journal_file_initiate_close(ManagedJournalFile *f, Set *deferred_closes); -int managed_journal_file_rotate(ManagedJournalFile **f, MMapCache *mmap_cache, JournalFileFlags file_flags, uint64_t compress_threshold_bytes, Set *deferred_closes); + JournalFileFlags file_flags, + uint64_t compress_threshold_bytes, + Set *deferred_closes); diff --git a/src/journal/test-journal-append.c b/src/journal/test-journal-append.c index 95b6fc1ee5..f382b1bd14 100644 --- a/src/journal/test-journal-append.c +++ b/src/journal/test-journal-append.c @@ -20,7 +20,7 @@ #include "tests.h" #include "tmpfile-util.h" -static int journal_append_message(ManagedJournalFile *mj, const char *message) { +static int journal_append_message(JournalFile *mj, const char *message) { struct iovec iovec; struct dual_timestamp ts; @@ -30,7 +30,7 @@ static int journal_append_message(ManagedJournalFile *mj, const char *message) { dual_timestamp_get(&ts); iovec = IOVEC_MAKE_STRING(message); return journal_file_append_entry( - mj->file, + mj, &ts, /* boot_id= */ NULL, &iovec, @@ -44,14 +44,14 @@ static int journal_append_message(ManagedJournalFile *mj, const char *message) { static int journal_corrupt_and_append(uint64_t start_offset, uint64_t step) { _cleanup_(mmap_cache_unrefp) MMapCache *mmap_cache = NULL; _cleanup_(rm_rf_physical_and_freep) char *tempdir = NULL; - _cleanup_(managed_journal_file_closep) ManagedJournalFile *mj = NULL; + _cleanup_(journal_file_offline_closep) JournalFile *mj = NULL; uint64_t start, end; int r; mmap_cache = mmap_cache_new(); assert_se(mmap_cache); - /* managed_journal_file_open() requires a valid machine id */ + /* journal_file_open() requires a valid machine id */ if (sd_id128_get_machine(NULL) < 0) return log_tests_skipped("No valid machine ID found"); @@ -61,22 +61,21 @@ static int journal_corrupt_and_append(uint64_t start_offset, uint64_t step) { log_debug("Opening journal %s/system.journal", tempdir); - r = managed_journal_file_open( - /* fd= */ -1, - "system.journal", - O_RDWR|O_CREAT, - JOURNAL_COMPRESS, - 0644, - /* compress_threshold_bytes= */ UINT64_MAX, - /* metrics= */ NULL, - mmap_cache, - /* template= */ NULL, - &mj); + r = journal_file_open( + /* fd= */ -1, + "system.journal", + O_RDWR|O_CREAT, + JOURNAL_COMPRESS, + 0644, + /* compress_threshold_bytes= */ UINT64_MAX, + /* metrics= */ NULL, + mmap_cache, + /* template= */ NULL, + &mj); if (r < 0) return log_error_errno(r, "Failed to open the journal: %m"); assert_se(mj); - assert_se(mj->file); /* Add a couple of initial messages */ for (int i = 0; i < 10; i++) { @@ -88,8 +87,8 @@ static int journal_corrupt_and_append(uint64_t start_offset, uint64_t step) { return log_error_errno(r, "Failed to write to the journal: %m"); } - start = start_offset == UINT64_MAX ? random_u64() % mj->file->last_stat.st_size : start_offset; - end = (uint64_t) mj->file->last_stat.st_size; + start = start_offset == UINT64_MAX ? random_u64() % mj->last_stat.st_size : start_offset; + end = (uint64_t) mj->last_stat.st_size; /* Print the initial offset at which we start flipping bits, which can be * later used to reproduce a potential fail */ @@ -105,26 +104,26 @@ static int journal_corrupt_and_append(uint64_t start_offset, uint64_t step) { uint8_t b; /* Flip a bit in the journal file */ - r = pread(mj->file->fd, &b, 1, offset); + r = pread(mj->fd, &b, 1, offset); assert_se(r == 1); b |= 0x1; - r = pwrite(mj->file->fd, &b, 1, offset); + r = pwrite(mj->fd, &b, 1, offset); assert_se(r == 1); /* Close and reopen the journal to flush all caches and remap * the corrupted journal */ - mj = managed_journal_file_close(mj); - r = managed_journal_file_open( - /* fd= */ -1, - "system.journal", - O_RDWR|O_CREAT, - JOURNAL_COMPRESS, - 0644, - /* compress_threshold_bytes= */ UINT64_MAX, - /* metrics= */ NULL, - mmap_cache, - /* template= */ NULL, - &mj); + mj = journal_file_offline_close(mj); + r = journal_file_open( + /* fd= */ -1, + "system.journal", + O_RDWR|O_CREAT, + JOURNAL_COMPRESS, + 0644, + /* compress_threshold_bytes= */ UINT64_MAX, + /* metrics= */ NULL, + mmap_cache, + /* template= */ NULL, + &mj); if (r < 0) { /* The corrupted journal might get rejected during reopening * if it's corrupted enough (especially its header), so diff --git a/src/journal/test-journal-flush.c b/src/journal/test-journal-flush.c index 677d37edc1..53ba72bbd2 100644 --- a/src/journal/test-journal-flush.c +++ b/src/journal/test-journal-flush.c @@ -19,7 +19,7 @@ static void test_journal_flush(int argc, char *argv[]) { _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL; _cleanup_free_ char *fn = NULL; _cleanup_(rm_rf_physical_and_freep) char *dn = NULL; - ManagedJournalFile *new_journal = NULL; + JournalFile *new_journal = NULL; sd_journal *j = NULL; unsigned n = 0; int r; @@ -30,7 +30,7 @@ static void test_journal_flush(int argc, char *argv[]) { assert_se(fn = path_join(dn, "test.journal")); - r = managed_journal_file_open(-1, fn, O_CREAT|O_RDWR, 0, 0644, 0, NULL, m, NULL, &new_journal); + r = journal_file_open(-1, fn, O_CREAT|O_RDWR, 0, 0644, 0, NULL, m, NULL, &new_journal); assert_se(r >= 0); if (argc > 1) @@ -53,7 +53,7 @@ static void test_journal_flush(int argc, char *argv[]) { log_error_errno(r, "journal_file_move_to_object failed: %m"); assert_se(r >= 0); - r = journal_file_copy_entry(f, new_journal->file, o, f->current_offset, NULL, NULL); + r = journal_file_copy_entry(f, new_journal, o, f->current_offset, NULL, NULL); if (r < 0) log_warning_errno(r, "journal_file_copy_entry failed: %m"); assert_se(r >= 0 || @@ -68,7 +68,7 @@ static void test_journal_flush(int argc, char *argv[]) { sd_journal_close(j); - (void) managed_journal_file_close(new_journal); + (void) journal_file_offline_close(new_journal); } int main(int argc, char *argv[]) { diff --git a/src/journal/test-journal-interleaving.c b/src/journal/test-journal-interleaving.c index 32427b8b62..81073465bf 100644 --- a/src/journal/test-journal-interleaving.c +++ b/src/journal/test-journal-interleaving.c @@ -35,30 +35,30 @@ _noreturn_ static void log_assert_errno(const char *text, int error, const char log_assert_errno(#expr, -_r_, PROJECT_FILE, __LINE__, __func__); \ } while (false) -static ManagedJournalFile *test_open_internal(const char *name, JournalFileFlags flags) { +static JournalFile *test_open_internal(const char *name, JournalFileFlags flags) { _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL; - ManagedJournalFile *f; + JournalFile *f; m = mmap_cache_new(); assert_se(m != NULL); - assert_ret(managed_journal_file_open(-1, name, O_RDWR|O_CREAT, flags, 0644, UINT64_MAX, NULL, m, NULL, &f)); + assert_ret(journal_file_open(-1, name, O_RDWR|O_CREAT, flags, 0644, UINT64_MAX, NULL, m, NULL, &f)); return f; } -static ManagedJournalFile *test_open(const char *name) { +static JournalFile *test_open(const char *name) { return test_open_internal(name, JOURNAL_COMPRESS); } -static ManagedJournalFile *test_open_strict(const char *name) { +static JournalFile *test_open_strict(const char *name) { return test_open_internal(name, JOURNAL_COMPRESS | JOURNAL_STRICT_ORDER); } -static void test_close(ManagedJournalFile *f) { - (void) managed_journal_file_close(f); +static void test_close(JournalFile *f) { + (void) journal_file_offline_close(f); } -static void append_number(ManagedJournalFile *f, int n, const sd_id128_t *boot_id, uint64_t *seqnum) { +static void append_number(JournalFile *f, int n, const sd_id128_t *boot_id, uint64_t *seqnum) { _cleanup_free_ char *p = NULL, *q = NULL; dual_timestamp ts; struct iovec iovec[2]; @@ -82,10 +82,10 @@ static void append_number(ManagedJournalFile *f, int n, const sd_id128_t *boot_i iovec[n_iov++] = IOVEC_MAKE_STRING(q); } - assert_ret(journal_file_append_entry(f->file, &ts, boot_id, iovec, n_iov, seqnum, NULL, NULL, NULL)); + assert_ret(journal_file_append_entry(f, &ts, boot_id, iovec, n_iov, seqnum, NULL, NULL, NULL)); } -static void append_unreferenced_data(ManagedJournalFile *f, const sd_id128_t *boot_id) { +static void append_unreferenced_data(JournalFile *f, const sd_id128_t *boot_id) { _cleanup_free_ char *q = NULL; dual_timestamp ts; struct iovec iovec; @@ -98,7 +98,7 @@ static void append_unreferenced_data(ManagedJournalFile *f, const sd_id128_t *bo assert_se(q = strjoin("_BOOT_ID=", SD_ID128_TO_STRING(*boot_id))); iovec = IOVEC_MAKE_STRING(q); - assert_se(journal_file_append_entry(f->file, &ts, boot_id, &iovec, 1, NULL, NULL, NULL, NULL) == -EREMCHG); + assert_se(journal_file_append_entry(f, &ts, boot_id, &iovec, 1, NULL, NULL, NULL, NULL) == -EREMCHG); } static void test_check_number(sd_journal *j, int n) { @@ -146,7 +146,7 @@ static void test_check_numbers_up(sd_journal *j, int count) { } static void setup_sequential(void) { - ManagedJournalFile *f1, *f2, *f3; + JournalFile *f1, *f2, *f3; sd_id128_t id; f1 = test_open("one.journal"); @@ -173,7 +173,7 @@ static void setup_sequential(void) { } static void setup_interleaved(void) { - ManagedJournalFile *f1, *f2, *f3; + JournalFile *f1, *f2, *f3; sd_id128_t id; f1 = test_open("one.journal"); @@ -196,7 +196,7 @@ static void setup_interleaved(void) { } static void setup_unreferenced_data(void) { - ManagedJournalFile *f1, *f2, *f3; + JournalFile *f1, *f2, *f3; sd_id128_t id; /* For issue #29275. */ @@ -449,7 +449,7 @@ TEST(boot_id) { static void test_sequence_numbers_one(void) { _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL; char t[] = "/var/tmp/journal-seq-XXXXXX"; - ManagedJournalFile *one, *two; + JournalFile *one, *two; uint64_t seqnum = 0; sd_id128_t seqnum_id; @@ -458,8 +458,8 @@ static void test_sequence_numbers_one(void) { mkdtemp_chdir_chattr(t); - assert_se(managed_journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644, - UINT64_MAX, NULL, m, NULL, &one) == 0); + assert_se(journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644, + UINT64_MAX, NULL, m, NULL, &one) == 0); append_number(one, 1, NULL, &seqnum); printf("seqnum=%"PRIu64"\n", seqnum); @@ -468,21 +468,21 @@ static void test_sequence_numbers_one(void) { printf("seqnum=%"PRIu64"\n", seqnum); assert_se(seqnum == 2); - assert_se(one->file->header->state == STATE_ONLINE); - assert_se(!sd_id128_equal(one->file->header->file_id, one->file->header->machine_id)); - assert_se(!sd_id128_equal(one->file->header->file_id, one->file->header->tail_entry_boot_id)); - assert_se(sd_id128_equal(one->file->header->file_id, one->file->header->seqnum_id)); + assert_se(one->header->state == STATE_ONLINE); + assert_se(!sd_id128_equal(one->header->file_id, one->header->machine_id)); + assert_se(!sd_id128_equal(one->header->file_id, one->header->tail_entry_boot_id)); + assert_se(sd_id128_equal(one->header->file_id, one->header->seqnum_id)); - memcpy(&seqnum_id, &one->file->header->seqnum_id, sizeof(sd_id128_t)); + memcpy(&seqnum_id, &one->header->seqnum_id, sizeof(sd_id128_t)); - assert_se(managed_journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644, - UINT64_MAX, NULL, m, one, &two) == 0); + assert_se(journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644, + UINT64_MAX, NULL, m, one, &two) == 0); - assert_se(two->file->header->state == STATE_ONLINE); - assert_se(!sd_id128_equal(two->file->header->file_id, one->file->header->file_id)); - assert_se(sd_id128_equal(two->file->header->machine_id, one->file->header->machine_id)); - assert_se(sd_id128_is_null(two->file->header->tail_entry_boot_id)); /* Not written yet. */ - assert_se(sd_id128_equal(two->file->header->seqnum_id, one->file->header->seqnum_id)); + assert_se(two->header->state == STATE_ONLINE); + assert_se(!sd_id128_equal(two->header->file_id, one->header->file_id)); + assert_se(sd_id128_equal(two->header->machine_id, one->header->machine_id)); + assert_se(sd_id128_is_null(two->header->tail_entry_boot_id)); /* Not written yet. */ + assert_se(sd_id128_equal(two->header->seqnum_id, one->header->seqnum_id)); append_number(two, 3, NULL, &seqnum); printf("seqnum=%"PRIu64"\n", seqnum); @@ -492,7 +492,7 @@ static void test_sequence_numbers_one(void) { assert_se(seqnum == 4); /* Verify tail_entry_boot_id. */ - assert_se(sd_id128_equal(two->file->header->tail_entry_boot_id, one->file->header->tail_entry_boot_id)); + assert_se(sd_id128_equal(two->header->tail_entry_boot_id, one->header->tail_entry_boot_id)); test_close(two); @@ -512,10 +512,10 @@ static void test_sequence_numbers_one(void) { /* restart server */ seqnum = 0; - assert_se(managed_journal_file_open(-1, "two.journal", O_RDWR, JOURNAL_COMPRESS, 0, - UINT64_MAX, NULL, m, NULL, &two) == 0); + assert_se(journal_file_open(-1, "two.journal", O_RDWR, JOURNAL_COMPRESS, 0, + UINT64_MAX, NULL, m, NULL, &two) == 0); - assert_se(sd_id128_equal(two->file->header->seqnum_id, seqnum_id)); + assert_se(sd_id128_equal(two->header->seqnum_id, seqnum_id)); append_number(two, 7, NULL, &seqnum); printf("seqnum=%"PRIu64"\n", seqnum); @@ -547,7 +547,7 @@ TEST(sequence_numbers) { } static int intro(void) { - /* managed_journal_file_open() requires a valid machine id */ + /* journal_file_open() requires a valid machine id */ if (access("/etc/machine-id", F_OK) != 0) return log_tests_skipped("/etc/machine-id not found"); diff --git a/src/journal/test-journal-stream.c b/src/journal/test-journal-stream.c index 1220a29e20..7de173f5f3 100644 --- a/src/journal/test-journal-stream.c +++ b/src/journal/test-journal-stream.c @@ -60,7 +60,7 @@ static void verify_contents(sd_journal *j, unsigned skip) { static void run_test(void) { _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL; - ManagedJournalFile *one, *two, *three; + JournalFile *one, *two, *three; char t[] = "/var/tmp/journal-stream-XXXXXX"; unsigned i; _cleanup_(sd_journal_closep) sd_journal *j = NULL; @@ -76,9 +76,9 @@ static void run_test(void) { assert_se(chdir(t) >= 0); (void) chattr_path(t, FS_NOCOW_FL, FS_NOCOW_FL, NULL); - assert_se(managed_journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0666, UINT64_MAX, NULL, m, NULL, &one) == 0); - assert_se(managed_journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0666, UINT64_MAX, NULL, m, NULL, &two) == 0); - assert_se(managed_journal_file_open(-1, "three.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0666, UINT64_MAX, NULL, m, NULL, &three) == 0); + assert_se(journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0666, UINT64_MAX, NULL, m, NULL, &one) == 0); + assert_se(journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0666, UINT64_MAX, NULL, m, NULL, &two) == 0); + assert_se(journal_file_open(-1, "three.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0666, UINT64_MAX, NULL, m, NULL, &three) == 0); for (i = 0; i < N_ENTRIES; i++) { char *p, *q; @@ -103,21 +103,21 @@ static void run_test(void) { iovec[1] = IOVEC_MAKE(q, strlen(q)); if (i % 10 == 0) - assert_se(journal_file_append_entry(three->file, &ts, NULL, iovec, 2, NULL, NULL, NULL, NULL) == 0); + assert_se(journal_file_append_entry(three, &ts, NULL, iovec, 2, NULL, NULL, NULL, NULL) == 0); else { if (i % 3 == 0) - assert_se(journal_file_append_entry(two->file, &ts, NULL, iovec, 2, NULL, NULL, NULL, NULL) == 0); + assert_se(journal_file_append_entry(two, &ts, NULL, iovec, 2, NULL, NULL, NULL, NULL) == 0); - assert_se(journal_file_append_entry(one->file, &ts, NULL, iovec, 2, NULL, NULL, NULL, NULL) == 0); + assert_se(journal_file_append_entry(one, &ts, NULL, iovec, 2, NULL, NULL, NULL, NULL) == 0); } free(p); free(q); } - (void) managed_journal_file_close(one); - (void) managed_journal_file_close(two); - (void) managed_journal_file_close(three); + (void) journal_file_offline_close(one); + (void) journal_file_offline_close(two); + (void) journal_file_offline_close(three); assert_se(sd_journal_open_directory(&j, t, 0) >= 0); @@ -177,7 +177,7 @@ static void run_test(void) { int main(int argc, char *argv[]) { - /* managed_journal_file_open() requires a valid machine id */ + /* journal_file_open() requires a valid machine id */ if (access("/etc/machine-id", F_OK) != 0) return log_tests_skipped("/etc/machine-id not found"); diff --git a/src/journal/test-journal-verify.c b/src/journal/test-journal-verify.c index 9a7fa2a734..8a77f919c5 100644 --- a/src/journal/test-journal-verify.c +++ b/src/journal/test-journal-verify.c @@ -71,7 +71,7 @@ static int run_test(const char *verification_key, ssize_t max_iterations) { char t[] = "/var/tmp/journal-XXXXXX"; struct stat st; JournalFile *f; - ManagedJournalFile *df; + JournalFile *df; usec_t from = 0, to = 0, total = 0; uint64_t start, end; int r; @@ -79,7 +79,7 @@ static int run_test(const char *verification_key, ssize_t max_iterations) { m = mmap_cache_new(); assert_se(m != NULL); - /* managed_journal_file_open() requires a valid machine id */ + /* journal_file_open() requires a valid machine id */ if (sd_id128_get_machine(NULL) < 0) return log_tests_skipped("No valid machine ID found"); @@ -91,7 +91,7 @@ static int run_test(const char *verification_key, ssize_t max_iterations) { log_info("Generating a test journal"); - assert_se(managed_journal_file_open( + assert_se(journal_file_open( /* fd= */ -1, "test.journal", O_RDWR|O_CREAT, @@ -112,7 +112,7 @@ static int run_test(const char *verification_key, ssize_t max_iterations) { assert_se(asprintf(&test, "RANDOM=%li", random() % RANDOM_RANGE)); iovec = IOVEC_MAKE_STRING(test); assert_se(journal_file_append_entry( - df->file, + df, &ts, /* boot_id= */ NULL, &iovec, @@ -123,7 +123,7 @@ static int run_test(const char *verification_key, ssize_t max_iterations) { /* ret_offset= */ NULL) == 0); } - (void) managed_journal_file_close(df); + (void) journal_file_offline_close(df); log_info("Verifying with key: %s", strna(verification_key)); diff --git a/src/journal/test-journal.c b/src/journal/test-journal.c index 92c5b66fe5..e89d982a78 100644 --- a/src/journal/test-journal.c +++ b/src/journal/test-journal.c @@ -26,7 +26,7 @@ static void mkdtemp_chdir_chattr(char *path) { static void test_non_empty_one(void) { _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL; dual_timestamp ts; - ManagedJournalFile *f; + JournalFile *f; struct iovec iovec; static const char test[] = "TEST1=1", test2[] = "TEST2=2"; Object *o, *d; @@ -39,71 +39,71 @@ static void test_non_empty_one(void) { mkdtemp_chdir_chattr(t); - assert_se(managed_journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS|JOURNAL_SEAL, 0666, UINT64_MAX, NULL, m, NULL, &f) == 0); + assert_se(journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS|JOURNAL_SEAL, 0666, UINT64_MAX, NULL, m, NULL, &f) == 0); assert_se(dual_timestamp_get(&ts)); assert_se(sd_id128_randomize(&fake_boot_id) == 0); iovec = IOVEC_MAKE_STRING(test); - assert_se(journal_file_append_entry(f->file, &ts, NULL, &iovec, 1, NULL, NULL, NULL, NULL) == 0); + assert_se(journal_file_append_entry(f, &ts, NULL, &iovec, 1, NULL, NULL, NULL, NULL) == 0); iovec = IOVEC_MAKE_STRING(test2); - assert_se(journal_file_append_entry(f->file, &ts, NULL, &iovec, 1, NULL, NULL, NULL, NULL) == 0); + assert_se(journal_file_append_entry(f, &ts, NULL, &iovec, 1, NULL, NULL, NULL, NULL) == 0); iovec = IOVEC_MAKE_STRING(test); - assert_se(journal_file_append_entry(f->file, &ts, &fake_boot_id, &iovec, 1, NULL, NULL, NULL, NULL) == 0); + assert_se(journal_file_append_entry(f, &ts, &fake_boot_id, &iovec, 1, NULL, NULL, NULL, NULL) == 0); #if HAVE_GCRYPT - journal_file_append_tag(f->file); + journal_file_append_tag(f); #endif - journal_file_dump(f->file); + journal_file_dump(f); - assert_se(journal_file_next_entry(f->file, 0, DIRECTION_DOWN, &o, &p) == 1); + assert_se(journal_file_next_entry(f, 0, DIRECTION_DOWN, &o, &p) == 1); assert_se(le64toh(o->entry.seqnum) == 1); - assert_se(journal_file_next_entry(f->file, p, DIRECTION_DOWN, &o, &p) == 1); + assert_se(journal_file_next_entry(f, p, DIRECTION_DOWN, &o, &p) == 1); assert_se(le64toh(o->entry.seqnum) == 2); - assert_se(journal_file_next_entry(f->file, p, DIRECTION_DOWN, &o, &p) == 1); + 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->file, p, DIRECTION_DOWN, &o, &p) == 0); + assert_se(journal_file_next_entry(f, p, DIRECTION_DOWN, &o, &p) == 0); - assert_se(journal_file_next_entry(f->file, 0, DIRECTION_DOWN, &o, &p) == 1); + assert_se(journal_file_next_entry(f, 0, DIRECTION_DOWN, &o, &p) == 1); assert_se(le64toh(o->entry.seqnum) == 1); - assert_se(journal_file_find_data_object(f->file, test, strlen(test), &d, NULL) == 1); - assert_se(journal_file_move_to_entry_for_data(f->file, d, DIRECTION_DOWN, &o, NULL) == 1); + assert_se(journal_file_find_data_object(f, test, strlen(test), &d, NULL) == 1); + assert_se(journal_file_move_to_entry_for_data(f, d, DIRECTION_DOWN, &o, NULL) == 1); assert_se(le64toh(o->entry.seqnum) == 1); - assert_se(journal_file_move_to_entry_for_data(f->file, d, DIRECTION_UP, &o, NULL) == 1); + assert_se(journal_file_move_to_entry_for_data(f, d, DIRECTION_UP, &o, NULL) == 1); assert_se(le64toh(o->entry.seqnum) == 3); - assert_se(journal_file_find_data_object(f->file, test2, strlen(test2), &d, NULL) == 1); - assert_se(journal_file_move_to_entry_for_data(f->file, d, DIRECTION_UP, &o, NULL) == 1); + assert_se(journal_file_find_data_object(f, test2, strlen(test2), &d, NULL) == 1); + assert_se(journal_file_move_to_entry_for_data(f, d, DIRECTION_UP, &o, NULL) == 1); assert_se(le64toh(o->entry.seqnum) == 2); - assert_se(journal_file_move_to_entry_for_data(f->file, d, DIRECTION_DOWN, &o, NULL) == 1); + assert_se(journal_file_move_to_entry_for_data(f, d, DIRECTION_DOWN, &o, NULL) == 1); assert_se(le64toh(o->entry.seqnum) == 2); - assert_se(journal_file_find_data_object(f->file, "quux", 4, &d, NULL) == 0); + assert_se(journal_file_find_data_object(f, "quux", 4, &d, NULL) == 0); - assert_se(journal_file_move_to_entry_by_seqnum(f->file, 1, DIRECTION_DOWN, &o, NULL) == 1); + assert_se(journal_file_move_to_entry_by_seqnum(f, 1, DIRECTION_DOWN, &o, NULL) == 1); assert_se(le64toh(o->entry.seqnum) == 1); - assert_se(journal_file_move_to_entry_by_seqnum(f->file, 3, DIRECTION_DOWN, &o, NULL) == 1); + assert_se(journal_file_move_to_entry_by_seqnum(f, 3, DIRECTION_DOWN, &o, NULL) == 1); assert_se(le64toh(o->entry.seqnum) == 3); - assert_se(journal_file_move_to_entry_by_seqnum(f->file, 2, DIRECTION_DOWN, &o, NULL) == 1); + assert_se(journal_file_move_to_entry_by_seqnum(f, 2, DIRECTION_DOWN, &o, NULL) == 1); assert_se(le64toh(o->entry.seqnum) == 2); - assert_se(journal_file_move_to_entry_by_seqnum(f->file, 10, DIRECTION_DOWN, &o, NULL) == 0); + assert_se(journal_file_move_to_entry_by_seqnum(f, 10, DIRECTION_DOWN, &o, NULL) == 0); - managed_journal_file_rotate(&f, m, JOURNAL_SEAL|JOURNAL_COMPRESS, UINT64_MAX, NULL); - managed_journal_file_rotate(&f, m, JOURNAL_SEAL|JOURNAL_COMPRESS, UINT64_MAX, NULL); + journal_file_rotate(&f, m, JOURNAL_SEAL|JOURNAL_COMPRESS, UINT64_MAX, NULL); + journal_file_rotate(&f, m, JOURNAL_SEAL|JOURNAL_COMPRESS, UINT64_MAX, NULL); - (void) managed_journal_file_close(f); + (void) journal_file_offline_close(f); log_info("Done..."); @@ -128,7 +128,7 @@ TEST(non_empty) { static void test_empty_one(void) { _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL; - ManagedJournalFile *f1, *f2, *f3, *f4; + JournalFile *f1, *f2, *f3, *f4; char t[] = "/var/tmp/journal-XXXXXX"; m = mmap_cache_new(); @@ -136,18 +136,18 @@ static void test_empty_one(void) { mkdtemp_chdir_chattr(t); - assert_se(managed_journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0, 0666, UINT64_MAX, NULL, m, NULL, &f1) == 0); - assert_se(managed_journal_file_open(-1, "test-compress.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0666, UINT64_MAX, NULL, m, NULL, &f2) == 0); - assert_se(managed_journal_file_open(-1, "test-seal.journal", O_RDWR|O_CREAT, JOURNAL_SEAL, 0666, UINT64_MAX, NULL, m, NULL, &f3) == 0); - assert_se(managed_journal_file_open(-1, "test-seal-compress.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS|JOURNAL_SEAL, 0666, UINT64_MAX, NULL, m, NULL, &f4) == 0); + assert_se(journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0, 0666, UINT64_MAX, NULL, m, NULL, &f1) == 0); + assert_se(journal_file_open(-1, "test-compress.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0666, UINT64_MAX, NULL, m, NULL, &f2) == 0); + assert_se(journal_file_open(-1, "test-seal.journal", O_RDWR|O_CREAT, JOURNAL_SEAL, 0666, UINT64_MAX, NULL, m, NULL, &f3) == 0); + assert_se(journal_file_open(-1, "test-seal-compress.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS|JOURNAL_SEAL, 0666, UINT64_MAX, NULL, m, NULL, &f4) == 0); - journal_file_print_header(f1->file); + journal_file_print_header(f1); puts(""); - journal_file_print_header(f2->file); + journal_file_print_header(f2); puts(""); - journal_file_print_header(f3->file); + journal_file_print_header(f3); puts(""); - journal_file_print_header(f4->file); + journal_file_print_header(f4); puts(""); log_info("Done..."); @@ -160,10 +160,10 @@ static void test_empty_one(void) { assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); } - (void) managed_journal_file_close(f1); - (void) managed_journal_file_close(f2); - (void) managed_journal_file_close(f3); - (void) managed_journal_file_close(f4); + (void) journal_file_offline_close(f1); + (void) journal_file_offline_close(f2); + (void) journal_file_offline_close(f3); + (void) journal_file_offline_close(f4); } TEST(empty) { @@ -178,7 +178,7 @@ TEST(empty) { static bool check_compressed(uint64_t compress_threshold, uint64_t data_size) { _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL; dual_timestamp ts; - ManagedJournalFile *f; + JournalFile *f; struct iovec iovec; Object *o; uint64_t p; @@ -194,34 +194,34 @@ static bool check_compressed(uint64_t compress_threshold, uint64_t data_size) { mkdtemp_chdir_chattr(t); - assert_se(managed_journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS|JOURNAL_SEAL, 0666, compress_threshold, NULL, m, NULL, &f) == 0); + assert_se(journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS|JOURNAL_SEAL, 0666, compress_threshold, NULL, m, NULL, &f) == 0); dual_timestamp_get(&ts); iovec = IOVEC_MAKE(data, data_size); - assert_se(journal_file_append_entry(f->file, &ts, NULL, &iovec, 1, NULL, NULL, NULL, NULL) == 0); + assert_se(journal_file_append_entry(f, &ts, NULL, &iovec, 1, NULL, NULL, NULL, NULL) == 0); #if HAVE_GCRYPT - journal_file_append_tag(f->file); + journal_file_append_tag(f); #endif - journal_file_dump(f->file); + journal_file_dump(f); /* We have to partially reimplement some of the dump logic, because the normal next_entry does the * decompression for us. */ - p = le64toh(f->file->header->header_size); + p = le64toh(f->header->header_size); for (;;) { - r = journal_file_move_to_object(f->file, OBJECT_UNUSED, p, &o); + r = journal_file_move_to_object(f, OBJECT_UNUSED, p, &o); assert_se(r == 0); if (o->object.type == OBJECT_DATA) break; - assert_se(p < le64toh(f->file->header->tail_object_offset)); + assert_se(p < le64toh(f->header->tail_object_offset)); p = p + ALIGN64(le64toh(o->object.size)); } is_compressed = COMPRESSION_FROM_OBJECT(o) != COMPRESSION_NONE; - (void) managed_journal_file_close(f); + (void) journal_file_offline_close(f); log_info("Done..."); @@ -270,7 +270,7 @@ TEST(min_compress_size) { static int intro(void) { arg_keep = saved_argc > 1; - /* managed_journal_file_open() requires a valid machine id */ + /* journal_file_open() requires a valid machine id */ if (access("/etc/machine-id", F_OK) != 0) return log_tests_skipped("/etc/machine-id not found"); From 1e094703035c31b0be7baf60d48a67efde185dab Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 30 Sep 2023 16:09:16 +0900 Subject: [PATCH 4/7] journald: move and rename journal/managed-journal-file.[ch] -> shared/journal-file-util.[ch] The files are used by both journald and journal-remote. Let's move them to src/shared. --- src/journal-remote/journal-remote-write.c | 1 + src/journal-remote/journal-remote-write.h | 2 +- src/journal-remote/journal-remote.c | 2 +- src/journal/journald-server.c | 1 + src/journal/journald-server.h | 2 +- src/journal/meson.build | 1 - src/journal/test-journal-append.c | 2 +- src/journal/test-journal-flush.c | 2 +- src/journal/test-journal-interleaving.c | 2 +- src/journal/test-journal-stream.c | 2 +- src/journal/test-journal-verify.c | 2 +- src/journal/test-journal.c | 2 +- .../managed-journal-file.c => shared/journal-file-util.c} | 2 +- .../managed-journal-file.h => shared/journal-file-util.h} | 0 src/shared/meson.build | 1 + 15 files changed, 13 insertions(+), 11 deletions(-) rename src/{journal/managed-journal-file.c => shared/journal-file-util.c} (99%) rename src/{journal/managed-journal-file.h => shared/journal-file-util.h} (100%) diff --git a/src/journal-remote/journal-remote-write.c b/src/journal-remote/journal-remote-write.c index f972b1fd47..d794277b27 100644 --- a/src/journal-remote/journal-remote-write.c +++ b/src/journal-remote/journal-remote-write.c @@ -3,6 +3,7 @@ #include #include "alloc-util.h" +#include "journal-file-util.h" #include "journal-remote.h" #include "path-util.h" #include "stat-util.h" diff --git a/src/journal-remote/journal-remote-write.h b/src/journal-remote/journal-remote-write.h index 65ec2a599a..005eaace4e 100644 --- a/src/journal-remote/journal-remote-write.h +++ b/src/journal-remote/journal-remote-write.h @@ -1,8 +1,8 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include "journal-file.h" #include "journal-importer.h" -#include "managed-journal-file.h" typedef struct RemoteServer RemoteServer; diff --git a/src/journal-remote/journal-remote.c b/src/journal-remote/journal-remote.c index 16f74ef066..06be2d08e2 100644 --- a/src/journal-remote/journal-remote.c +++ b/src/journal-remote/journal-remote.c @@ -14,11 +14,11 @@ #include "errno-util.h" #include "escape.h" #include "fd-util.h" +#include "journal-file-util.h" #include "journal-remote-write.h" #include "journal-remote.h" #include "journald-native.h" #include "macro.h" -#include "managed-journal-file.h" #include "parse-util.h" #include "parse-helpers.h" #include "process-util.h" diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index 3b07a4bf0d..615b3c6c6b 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -30,6 +30,7 @@ #include "initrd-util.h" #include "io-util.h" #include "journal-authenticate.h" +#include "journal-file-util.h" #include "journal-internal.h" #include "journal-vacuum.h" #include "journald-audit.h" diff --git a/src/journal/journald-server.h b/src/journal/journald-server.h index 4e9a67c306..2a176761ed 100644 --- a/src/journal/journald-server.h +++ b/src/journal/journald-server.h @@ -11,11 +11,11 @@ typedef struct Server Server; #include "common-signal.h" #include "conf-parser.h" #include "hashmap.h" +#include "journal-file.h" #include "journald-context.h" #include "journald-rate-limit.h" #include "journald-stream.h" #include "list.h" -#include "managed-journal-file.h" #include "prioq.h" #include "ratelimit.h" #include "time-util.h" diff --git a/src/journal/meson.build b/src/journal/meson.build index 42b3131f65..6ae5cfbf9d 100644 --- a/src/journal/meson.build +++ b/src/journal/meson.build @@ -12,7 +12,6 @@ sources = files( 'journald-stream.c', 'journald-syslog.c', 'journald-wall.c', - 'managed-journal-file.c', ) sources += custom_target( diff --git a/src/journal/test-journal-append.c b/src/journal/test-journal-append.c index f382b1bd14..fbbf44add4 100644 --- a/src/journal/test-journal-append.c +++ b/src/journal/test-journal-append.c @@ -9,8 +9,8 @@ #include "fd-util.h" #include "fs-util.h" #include "io-util.h" +#include "journal-file-util.h" #include "log.h" -#include "managed-journal-file.h" #include "mmap-cache.h" #include "parse-util.h" #include "random-util.h" diff --git a/src/journal/test-journal-flush.c b/src/journal/test-journal-flush.c index 53ba72bbd2..b5956d24fc 100644 --- a/src/journal/test-journal-flush.c +++ b/src/journal/test-journal-flush.c @@ -7,9 +7,9 @@ #include "alloc-util.h" #include "chattr-util.h" +#include "journal-file-util.h" #include "journal-internal.h" #include "macro.h" -#include "managed-journal-file.h" #include "path-util.h" #include "rm-rf.h" #include "string-util.h" diff --git a/src/journal/test-journal-interleaving.c b/src/journal/test-journal-interleaving.c index 81073465bf..d8588759d1 100644 --- a/src/journal/test-journal-interleaving.c +++ b/src/journal/test-journal-interleaving.c @@ -9,10 +9,10 @@ #include "alloc-util.h" #include "chattr-util.h" #include "io-util.h" +#include "journal-file-util.h" #include "journal-vacuum.h" #include "log.h" #include "logs-show.h" -#include "managed-journal-file.h" #include "parse-util.h" #include "rm-rf.h" #include "tests.h" diff --git a/src/journal/test-journal-stream.c b/src/journal/test-journal-stream.c index 7de173f5f3..2897de4905 100644 --- a/src/journal/test-journal-stream.c +++ b/src/journal/test-journal-stream.c @@ -8,10 +8,10 @@ #include "alloc-util.h" #include "chattr-util.h" #include "io-util.h" +#include "journal-file-util.h" #include "journal-internal.h" #include "log.h" #include "macro.h" -#include "managed-journal-file.h" #include "parse-util.h" #include "rm-rf.h" #include "tests.h" diff --git a/src/journal/test-journal-verify.c b/src/journal/test-journal-verify.c index 8a77f919c5..7853b36495 100644 --- a/src/journal/test-journal-verify.c +++ b/src/journal/test-journal-verify.c @@ -7,9 +7,9 @@ #include "chattr-util.h" #include "fd-util.h" #include "io-util.h" +#include "journal-file-util.h" #include "journal-verify.h" #include "log.h" -#include "managed-journal-file.h" #include "mmap-cache.h" #include "rm-rf.h" #include "strv.h" diff --git a/src/journal/test-journal.c b/src/journal/test-journal.c index e89d982a78..a0916a0a24 100644 --- a/src/journal/test-journal.c +++ b/src/journal/test-journal.c @@ -6,9 +6,9 @@ #include "chattr-util.h" #include "io-util.h" #include "journal-authenticate.h" +#include "journal-file-util.h" #include "journal-vacuum.h" #include "log.h" -#include "managed-journal-file.h" #include "rm-rf.h" #include "tests.h" diff --git a/src/journal/managed-journal-file.c b/src/shared/journal-file-util.c similarity index 99% rename from src/journal/managed-journal-file.c rename to src/shared/journal-file-util.c index 34f3497621..90737fb8a8 100644 --- a/src/journal/managed-journal-file.c +++ b/src/shared/journal-file-util.c @@ -9,7 +9,7 @@ #include "fd-util.h" #include "format-util.h" #include "journal-authenticate.h" -#include "managed-journal-file.h" +#include "journal-file-util.h" #include "path-util.h" #include "random-util.h" #include "set.h" diff --git a/src/journal/managed-journal-file.h b/src/shared/journal-file-util.h similarity index 100% rename from src/journal/managed-journal-file.h rename to src/shared/journal-file-util.h diff --git a/src/shared/meson.build b/src/shared/meson.build index 47ebbea54e..3df178bfa1 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -93,6 +93,7 @@ shared_sources = files( 'install.c', 'ip-protocol-list.c', 'ipvlan-util.c', + 'journal-file-util.c', 'journal-importer.c', 'journal-util.c', 'json.c', From f7b349e88dbbf8c0d462e53f3409743e828a44f2 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 30 Sep 2023 19:08:23 +0900 Subject: [PATCH 5/7] journal-remote: drop dependencies to journald Now journal-remote can be built without libjournal_core. --- meson.build | 3 +-- src/journal-remote/journal-remote-parse.c | 1 - src/journal-remote/journal-remote.c | 1 - src/journal-remote/meson.build | 5 +---- 4 files changed, 2 insertions(+), 8 deletions(-) diff --git a/meson.build b/meson.build index 4ef68c7f1c..475f02b66a 100644 --- a/meson.build +++ b/meson.build @@ -2097,8 +2097,6 @@ module_additional_kwargs = { # systemd-analyze requires 'libcore' subdir('src/core') -# systemd-journal-remote requires 'libjournal_core' -subdir('src/journal') # systemd-networkd requires 'libsystemd_network' subdir('src/libsystemd-network') # hwdb requires 'udev_link_with' and 'udev_rpath' @@ -2139,6 +2137,7 @@ subdir('src/id128') subdir('src/import') subdir('src/initctl') subdir('src/integritysetup') +subdir('src/journal') subdir('src/journal-remote') subdir('src/kernel-install') subdir('src/locale') diff --git a/src/journal-remote/journal-remote-parse.c b/src/journal-remote/journal-remote-parse.c index 26e2009e14..e23012c472 100644 --- a/src/journal-remote/journal-remote-parse.c +++ b/src/journal-remote/journal-remote-parse.c @@ -3,7 +3,6 @@ #include "alloc-util.h" #include "fd-util.h" #include "journal-remote-parse.h" -#include "journald-native.h" #include "parse-util.h" #include "string-util.h" diff --git a/src/journal-remote/journal-remote.c b/src/journal-remote/journal-remote.c index 06be2d08e2..79010d0482 100644 --- a/src/journal-remote/journal-remote.c +++ b/src/journal-remote/journal-remote.c @@ -17,7 +17,6 @@ #include "journal-file-util.h" #include "journal-remote-write.h" #include "journal-remote.h" -#include "journald-native.h" #include "macro.h" #include "parse-util.h" #include "parse-helpers.h" diff --git a/src/journal-remote/meson.build b/src/journal-remote/meson.build index fe766dbb01..4251624864 100644 --- a/src/journal-remote/meson.build +++ b/src/journal-remote/meson.build @@ -20,8 +20,7 @@ endif libsystemd_journal_remote = static_library( 'systemd-journal-remote', libsystemd_journal_remote_sources, - include_directories : journal_includes, - link_with : libjournal_core, + include_directories : includes, dependencies : [libgnutls, liblz4, libmicrohttpd, @@ -64,7 +63,6 @@ executables += [ 'HAVE_MICROHTTPD', ], 'sources' : systemd_journal_remote_sources, - 'include_directories' : journal_includes, 'link_with' : [ libshared, libsystemd_journal_remote, @@ -87,7 +85,6 @@ executables += [ libshared, libsystemd_journal_remote, ], - 'include_directories' : journal_includes, }, ] From 1d3ef11dfe9d59d0d8a5eeeee401b372124923e9 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 30 Sep 2023 19:18:34 +0900 Subject: [PATCH 6/7] journal: move several tests to libsystemd/sd-journal This also renames test-journal-xyz for journald (rather than sd-journal) to test-journald-xyz. --- src/journal/meson.build | 24 ++----------------- ...ournal-config.c => test-journald-config.c} | 0 ...ournal-syslog.c => test-journald-syslog.c} | 0 src/libsystemd/meson.build | 12 ++++++++++ .../sd-journal}/test-journal-append.c | 0 .../sd-journal}/test-journal-flush.c | 0 .../sd-journal}/test-journal-interleaving.c | 0 .../sd-journal}/test-journal-stream.c | 0 .../sd-journal}/test-journal-verify.c | 0 .../sd-journal}/test-journal.c | 0 10 files changed, 14 insertions(+), 22 deletions(-) rename src/journal/{test-journal-config.c => test-journald-config.c} (100%) rename src/journal/{test-journal-syslog.c => test-journald-syslog.c} (100%) rename src/{journal => libsystemd/sd-journal}/test-journal-append.c (100%) rename src/{journal => libsystemd/sd-journal}/test-journal-flush.c (100%) rename src/{journal => libsystemd/sd-journal}/test-journal-interleaving.c (100%) rename src/{journal => libsystemd/sd-journal}/test-journal-stream.c (100%) rename src/{journal => libsystemd/sd-journal}/test-journal-verify.c (100%) rename src/{journal => libsystemd/sd-journal}/test-journal.c (100%) diff --git a/src/journal/meson.build b/src/journal/meson.build index 6ae5cfbf9d..a3aa8ad5ee 100644 --- a/src/journal/meson.build +++ b/src/journal/meson.build @@ -103,11 +103,7 @@ executables += [ ], }, journal_test_template + { - 'sources' : files('test-journal-append.c'), - 'type' : 'manual', - }, - journal_test_template + { - 'sources' : files('test-journal-config.c'), + 'sources' : files('test-journald-config.c'), 'dependencies' : [ liblz4, libselinux, @@ -115,16 +111,7 @@ executables += [ ], }, journal_test_template + { - 'sources' : files('test-journal-flush.c'), - }, - journal_test_template + { - 'sources' : files('test-journal-interleaving.c'), - }, - journal_test_template + { - 'sources' : files('test-journal-stream.c'), - }, - journal_test_template + { - 'sources' : files('test-journal-syslog.c'), + 'sources' : files('test-journald-syslog.c'), 'dependencies' : [ liblz4, libselinux, @@ -132,13 +119,6 @@ executables += [ threads, ], }, - journal_test_template + { - 'sources' : files('test-journal-verify.c'), - 'timeout' : 90, - }, - journal_test_template + { - 'sources' : files('test-journal.c'), - }, journal_fuzz_template + { 'sources' : files( 'fuzz-journald-audit.c', diff --git a/src/journal/test-journal-config.c b/src/journal/test-journald-config.c similarity index 100% rename from src/journal/test-journal-config.c rename to src/journal/test-journald-config.c diff --git a/src/journal/test-journal-syslog.c b/src/journal/test-journald-syslog.c similarity index 100% rename from src/journal/test-journal-syslog.c rename to src/journal/test-journald-syslog.c diff --git a/src/libsystemd/meson.build b/src/libsystemd/meson.build index bcf8e1faf8..5d18f974ba 100644 --- a/src/libsystemd/meson.build +++ b/src/libsystemd/meson.build @@ -172,6 +172,10 @@ simple_tests += files( 'sd-device/test-sd-device-monitor.c', 'sd-device/test-sd-device.c', 'sd-event/test-event.c', + 'sd-journal/test-journal-flush.c', + 'sd-journal/test-journal-interleaving.c', + 'sd-journal/test-journal-stream.c', + 'sd-journal/test-journal.c', 'sd-login/test-login.c', 'sd-netlink/test-netlink.c', ) @@ -234,6 +238,14 @@ libsystemd_tests += [ 'dependencies' : threads, 'timeout' : 120, }, + { + 'sources' : files('sd-journal/test-journal-append.c'), + 'type' : 'manual', + }, + { + 'sources' : files('sd-journal/test-journal-verify.c'), + 'timeout' : 90, + }, { 'sources' : files('sd-resolve/test-resolve.c'), 'dependencies' : threads, diff --git a/src/journal/test-journal-append.c b/src/libsystemd/sd-journal/test-journal-append.c similarity index 100% rename from src/journal/test-journal-append.c rename to src/libsystemd/sd-journal/test-journal-append.c diff --git a/src/journal/test-journal-flush.c b/src/libsystemd/sd-journal/test-journal-flush.c similarity index 100% rename from src/journal/test-journal-flush.c rename to src/libsystemd/sd-journal/test-journal-flush.c diff --git a/src/journal/test-journal-interleaving.c b/src/libsystemd/sd-journal/test-journal-interleaving.c similarity index 100% rename from src/journal/test-journal-interleaving.c rename to src/libsystemd/sd-journal/test-journal-interleaving.c diff --git a/src/journal/test-journal-stream.c b/src/libsystemd/sd-journal/test-journal-stream.c similarity index 100% rename from src/journal/test-journal-stream.c rename to src/libsystemd/sd-journal/test-journal-stream.c diff --git a/src/journal/test-journal-verify.c b/src/libsystemd/sd-journal/test-journal-verify.c similarity index 100% rename from src/journal/test-journal-verify.c rename to src/libsystemd/sd-journal/test-journal-verify.c diff --git a/src/journal/test-journal.c b/src/libsystemd/sd-journal/test-journal.c similarity index 100% rename from src/journal/test-journal.c rename to src/libsystemd/sd-journal/test-journal.c From d287ece7f17e1015f3ee6f17fd9fba92abc31716 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 30 Sep 2023 19:45:27 +0900 Subject: [PATCH 7/7] test: split test-tables into three Let's drop dependency to libjournal_core and libudevd_core from test-tables. --- src/journal/meson.build | 8 ++++++-- src/journal/test-journald-tables.c | 11 +++++++++++ src/test/meson.build | 26 +++----------------------- src/test/test-tables.c | 6 +----- src/udev/meson.build | 8 +++++--- src/udev/net/test-link-config-tables.c | 10 ++++++++++ 6 files changed, 36 insertions(+), 33 deletions(-) create mode 100644 src/journal/test-journald-tables.c create mode 100644 src/udev/net/test-link-config-tables.c diff --git a/src/journal/meson.build b/src/journal/meson.build index a3aa8ad5ee..4187dab673 100644 --- a/src/journal/meson.build +++ b/src/journal/meson.build @@ -28,8 +28,6 @@ libjournal_core = static_library( userspace], build_by_default : false) -journal_includes = [includes, include_directories('.')] - if get_option('link-journalctl-shared') journalctl_link_with = [libshared] else @@ -119,6 +117,12 @@ executables += [ threads, ], }, + journal_test_template + { + 'sources' : files('test-journald-tables.c'), + 'dependencies' : [ + libselinux, + ], + }, journal_fuzz_template + { 'sources' : files( 'fuzz-journald-audit.c', diff --git a/src/journal/test-journald-tables.c b/src/journal/test-journald-tables.c new file mode 100644 index 0000000000..958beb8417 --- /dev/null +++ b/src/journal/test-journald-tables.c @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "journald-server.h" +#include "test-tables.h" + +int main(int argc, char **argv) { + test_table(split_mode, SPLIT); + test_table(storage, STORAGE); + + return EXIT_SUCCESS; +} diff --git a/src/test/meson.build b/src/test/meson.build index ae80c72a8b..7e25fcdbaa 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -401,29 +401,6 @@ executables += [ 'sources' : files('test-sizeof.c'), 'link_with' : libbasic, }, - test_template + { - 'sources' : files('test-tables.c'), - 'link_with' : [ - libcore, - libjournal_core, - libshared, - libudevd_core, - ], - 'dependencies' : [ - libblkid, - liblz4, - libmount, - libseccomp, - libselinux, - libxz, - threads, - ], - 'include_directories' : [ - core_includes, - journal_includes, - udev_includes, - ], - }, test_template + { 'sources' : files('test-time-util.c'), 'timeout' : 120, @@ -570,6 +547,9 @@ executables += [ 'dependencies' : libdl, 'conditions' : ['BPF_FRAMEWORK'], }, + core_test_template + { + 'sources' : files('test-tables.c'), + }, core_test_template + { 'sources' : files('test-unit-name.c'), 'dependencies' : common_test_dependencies, diff --git a/src/test/test-tables.c b/src/test/test-tables.c index ee8310f114..975f952a1f 100644 --- a/src/test/test-tables.c +++ b/src/test/test-tables.c @@ -14,13 +14,12 @@ #include "import-util.h" #include "install.h" #include "job.h" -#include "journald-server.h" #include "kill.h" -#include "link-config.h" #include "locale-util.h" #include "log.h" #include "logs-show.h" #include "mount.h" +#include "netif-naming-scheme.h" #include "path.h" #include "process-util.h" #include "resolve-util.h" @@ -72,7 +71,6 @@ int main(int argc, char **argv) { test_table(kill_who, KILL_WHO); test_table(locale_variable, VARIABLE_LC); test_table(log_target, LOG_TARGET); - test_table(mac_address_policy, MAC_ADDRESS_POLICY); test_table(managed_oom_mode, MANAGED_OOM_MODE); test_table(managed_oom_preference, MANAGED_OOM_PREFERENCE); test_table(manager_state, MANAGER_STATE); @@ -107,8 +105,6 @@ int main(int argc, char **argv) { test_table(socket_exec_command, SOCKET_EXEC_COMMAND); test_table(socket_result, SOCKET_RESULT); test_table(socket_state, SOCKET_STATE); - test_table(split_mode, SPLIT); - test_table(storage, STORAGE); test_table(swap_exec_command, SWAP_EXEC_COMMAND); test_table(swap_result, SWAP_RESULT); test_table(swap_state, SWAP_STATE); diff --git a/src/udev/meson.build b/src/udev/meson.build index e0cd47f80b..cd4d1f4277 100644 --- a/src/udev/meson.build +++ b/src/udev/meson.build @@ -105,13 +105,12 @@ endif ############################################################ -udev_includes = [includes, include_directories('net', '.')] libudevd_core = static_library( 'udev-core', libudevd_core_sources, link_config_gperf_c, keyboard_keys_from_name_h, - include_directories : udev_includes, + include_directories : includes + include_directories('net'), link_with : udev_link_with, dependencies : [libblkid, libkmod, @@ -203,6 +202,10 @@ executables += [ ), 'suite' : 'udev', }, + udev_test_template + { + 'sources' : files('net/test-link-config-tables.c'), + 'suite' : 'udev', + }, udev_test_template + { 'sources' : files('test-udev-builtin.c'), }, @@ -236,7 +239,6 @@ executables += [ }, udev_fuzz_template + { 'sources' : files('net/fuzz-link-parser.c'), - 'include_directories' : udev_includes, }, udev_fuzz_template + { 'sources' : files('fuzz-udev-rule-parse-value.c'), diff --git a/src/udev/net/test-link-config-tables.c b/src/udev/net/test-link-config-tables.c new file mode 100644 index 0000000000..4fd004607f --- /dev/null +++ b/src/udev/net/test-link-config-tables.c @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "link-config.h" +#include "test-tables.h" + +int main(int argc, char **argv) { + test_table(mac_address_policy, MAC_ADDRESS_POLICY); + + return EXIT_SUCCESS; +}