]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
d0767ffd LP |
2 | |
3 | #include <fcntl.h> | |
ca78ad1d | 4 | #include <unistd.h> |
d0767ffd LP |
5 | |
6 | #include "sd-journal.h" | |
07630cea | 7 | |
b5efdb8a | 8 | #include "alloc-util.h" |
949082ac | 9 | #include "chattr-util.h" |
1e094703 | 10 | #include "journal-file-util.h" |
d0767ffd | 11 | #include "journal-internal.h" |
3b0ae13b | 12 | #include "logs-show.h" |
07630cea | 13 | #include "macro.h" |
b910cc72 | 14 | #include "path-util.h" |
951174e4 | 15 | #include "rm-rf.h" |
07630cea | 16 | #include "string-util.h" |
ff95b60d | 17 | #include "tests.h" |
951174e4 | 18 | #include "tmpfile-util.h" |
d0767ffd | 19 | |
ff95b60d | 20 | static void test_journal_flush_one(int argc, char *argv[]) { |
74fb5be6 | 21 | _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL; |
3e348b8a | 22 | _cleanup_free_ char *fn = NULL; |
951174e4 | 23 | _cleanup_(rm_rf_physical_and_freep) char *dn = NULL; |
ff95b60d YW |
24 | _cleanup_(journal_file_offline_closep) JournalFile *new_journal = NULL; |
25 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; | |
26 | unsigned n, limit; | |
d0767ffd LP |
27 | int r; |
28 | ||
951174e4 LP |
29 | assert_se(m = mmap_cache_new()); |
30 | assert_se(mkdtemp_malloc("/var/tmp/test-journal-flush.XXXXXX", &dn) >= 0); | |
949082ac LP |
31 | (void) chattr_path(dn, FS_NOCOW_FL, FS_NOCOW_FL, NULL); |
32 | ||
951174e4 | 33 | assert_se(fn = path_join(dn, "test.journal")); |
d0767ffd | 34 | |
45c0ecba | 35 | r = journal_file_open(-1, fn, O_CREAT|O_RDWR, 0, 0644, 0, NULL, m, NULL, &new_journal); |
d0767ffd LP |
36 | assert_se(r >= 0); |
37 | ||
0fa167cd ZJS |
38 | if (argc > 1) |
39 | r = sd_journal_open_files(&j, (const char **) strv_skip(argv, 1), 0); | |
40 | else | |
41 | r = sd_journal_open(&j, 0); | |
42 | assert_se(r == 0); | |
d0767ffd LP |
43 | |
44 | sd_journal_set_data_threshold(j, 0); | |
45 | ||
ff95b60d YW |
46 | n = 0; |
47 | limit = slow_tests_enabled() ? 10000 : 1000; | |
d0767ffd LP |
48 | SD_JOURNAL_FOREACH(j) { |
49 | Object *o; | |
50 | JournalFile *f; | |
51 | ||
52 | f = j->current_file; | |
0c0cdb06 | 53 | assert_se(f && f->current_offset > 0); |
d0767ffd LP |
54 | |
55 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); | |
a83577fa ZJS |
56 | if (r < 0) |
57 | log_error_errno(r, "journal_file_move_to_object failed: %m"); | |
0c0cdb06 | 58 | assert_se(r >= 0); |
d0767ffd | 59 | |
45c0ecba | 60 | r = journal_file_copy_entry(f, new_journal, o, f->current_offset, NULL, NULL); |
a83577fa | 61 | if (r < 0) |
b4046d55 ZJS |
62 | log_warning_errno(r, "journal_file_copy_entry failed: %m"); |
63 | assert_se(r >= 0 || | |
64 | IN_SET(r, -EBADMSG, /* corrupted file */ | |
65 | -EPROTONOSUPPORT, /* unsupported compression */ | |
9cd80d8a | 66 | -EIO, /* file rotated */ |
67 | -EREMCHG)); /* clock rollback */ | |
d0767ffd | 68 | |
ff95b60d | 69 | if (++n >= limit) |
d0767ffd LP |
70 | break; |
71 | } | |
3b0ae13b YW |
72 | |
73 | if (n == 0) | |
74 | return (void) log_tests_skipped("No journal entry found"); | |
75 | ||
76 | /* Open the new journal before archiving and offlining the file. */ | |
77 | sd_journal_close(j); | |
78 | assert_se(sd_journal_open_directory(&j, dn, 0) >= 0); | |
79 | ||
80 | /* Read the online journal. */ | |
81 | assert_se(sd_journal_seek_tail(j) >= 0); | |
82 | assert_se(sd_journal_step_one(j, 0) > 0); | |
83 | printf("current_journal: %s (%i)\n", j->current_file->path, j->current_file->fd); | |
84 | assert_se(show_journal_entry(stdout, j, OUTPUT_EXPORT, 0, 0, NULL, NULL, NULL, &(dual_timestamp) {}, &(sd_id128_t) {}) >= 0); | |
85 | ||
86 | uint64_t p; | |
87 | assert_se(journal_file_tail_end_by_mmap(j->current_file, &p) >= 0); | |
88 | for (uint64_t q = ALIGN64(p + 1); q < (uint64_t) j->current_file->last_stat.st_size; q = ALIGN64(q + 1)) { | |
89 | Object *o; | |
90 | ||
91 | r = journal_file_move_to_object(j->current_file, OBJECT_UNUSED, q, &o); | |
92 | assert_se(IN_SET(r, -EBADMSG, -EADDRNOTAVAIL)); | |
93 | } | |
94 | ||
95 | /* Archive and offline file. */ | |
96 | assert_se(journal_file_archive(new_journal, NULL) >= 0); | |
97 | assert_se(journal_file_set_offline(new_journal, /* wait = */ true) >= 0); | |
98 | ||
99 | /* Read the archived and offline journal. */ | |
100 | for (uint64_t q = ALIGN64(p + 1); q < (uint64_t) j->current_file->last_stat.st_size; q = ALIGN64(q + 1)) { | |
101 | Object *o; | |
102 | ||
103 | r = journal_file_move_to_object(j->current_file, OBJECT_UNUSED, q, &o); | |
104 | assert_se(IN_SET(r, -EBADMSG, -EADDRNOTAVAIL, -EIDRM)); | |
105 | } | |
c92f1ebe DDM |
106 | } |
107 | ||
ff95b60d | 108 | TEST(journal_flush) { |
c92f1ebe | 109 | assert_se(setenv("SYSTEMD_JOURNAL_COMPACT", "0", 1) >= 0); |
ff95b60d YW |
110 | test_journal_flush_one(saved_argc, saved_argv); |
111 | } | |
c92f1ebe | 112 | |
ff95b60d | 113 | TEST(journal_flush_compact) { |
c92f1ebe | 114 | assert_se(setenv("SYSTEMD_JOURNAL_COMPACT", "1", 1) >= 0); |
ff95b60d | 115 | test_journal_flush_one(saved_argc, saved_argv); |
d0767ffd | 116 | } |
ff95b60d YW |
117 | |
118 | DEFINE_TEST_MAIN(LOG_INFO); |