]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-journal-interleaving.c
shutdown: Make all mounts private
[thirdparty/systemd.git] / src / journal / test-journal-interleaving.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <unistd.h>
5
6 #include "sd-id128.h"
7 #include "sd-journal.h"
8
9 #include "alloc-util.h"
10 #include "chattr-util.h"
11 #include "io-util.h"
12 #include "journal-vacuum.h"
13 #include "log.h"
14 #include "managed-journal-file.h"
15 #include "parse-util.h"
16 #include "rm-rf.h"
17 #include "tests.h"
18
19 /* This program tests skipping around in a multi-file journal. */
20
21 static bool arg_keep = false;
22
23 _noreturn_ static void log_assert_errno(const char *text, int error, const char *file, unsigned line, const char *func) {
24 log_internal(LOG_CRIT, error, file, line, func,
25 "'%s' failed at %s:%u (%s): %m", text, file, line, func);
26 abort();
27 }
28
29 #define assert_ret(expr) \
30 do { \
31 int _r_ = (expr); \
32 if (_unlikely_(_r_ < 0)) \
33 log_assert_errno(#expr, -_r_, PROJECT_FILE, __LINE__, __PRETTY_FUNCTION__); \
34 } while (false)
35
36 static ManagedJournalFile *test_open(const char *name) {
37 _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
38 ManagedJournalFile *f;
39
40 m = mmap_cache_new();
41 assert_se(m != NULL);
42
43 assert_ret(managed_journal_file_open(-1, name, O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644, UINT64_MAX, NULL, m, NULL, NULL, &f));
44 return f;
45 }
46
47 static void test_close(ManagedJournalFile *f) {
48 (void) managed_journal_file_close(f);
49 }
50
51 static void append_number(ManagedJournalFile *f, int n, uint64_t *seqnum) {
52 char *p;
53 dual_timestamp ts;
54 static dual_timestamp previous_ts = {};
55 struct iovec iovec[1];
56
57 dual_timestamp_get(&ts);
58
59 if (ts.monotonic <= previous_ts.monotonic)
60 ts.monotonic = previous_ts.monotonic + 1;
61
62 if (ts.realtime <= previous_ts.realtime)
63 ts.realtime = previous_ts.realtime + 1;
64
65 previous_ts = ts;
66
67 assert_se(asprintf(&p, "NUMBER=%d", n) >= 0);
68 iovec[0] = IOVEC_MAKE_STRING(p);
69 assert_ret(journal_file_append_entry(f->file, &ts, NULL, iovec, 1, seqnum, NULL, NULL));
70 free(p);
71 }
72
73 static void test_check_number(sd_journal *j, int n) {
74 const void *d;
75 _cleanup_free_ char *k;
76 size_t l;
77 int x;
78
79 assert_ret(sd_journal_get_data(j, "NUMBER", &d, &l));
80 assert_se(k = strndup(d, l));
81 printf("%s\n", k);
82
83 assert_se(safe_atoi(k + 7, &x) >= 0);
84 assert_se(n == x);
85 }
86
87 static void test_check_numbers_down(sd_journal *j, int count) {
88 int i;
89
90 for (i = 1; i <= count; i++) {
91 int r;
92 test_check_number(j, i);
93 assert_ret(r = sd_journal_next(j));
94 if (i == count)
95 assert_se(r == 0);
96 else
97 assert_se(r == 1);
98 }
99
100 }
101
102 static void test_check_numbers_up(sd_journal *j, int count) {
103 for (int i = count; i >= 1; i--) {
104 int r;
105 test_check_number(j, i);
106 assert_ret(r = sd_journal_previous(j));
107 if (i == 1)
108 assert_se(r == 0);
109 else
110 assert_se(r == 1);
111 }
112
113 }
114
115 static void setup_sequential(void) {
116 ManagedJournalFile *one, *two;
117 one = test_open("one.journal");
118 two = test_open("two.journal");
119 append_number(one, 1, NULL);
120 append_number(one, 2, NULL);
121 append_number(two, 3, NULL);
122 append_number(two, 4, NULL);
123 test_close(one);
124 test_close(two);
125 }
126
127 static void setup_interleaved(void) {
128 ManagedJournalFile *one, *two;
129 one = test_open("one.journal");
130 two = test_open("two.journal");
131 append_number(one, 1, NULL);
132 append_number(two, 2, NULL);
133 append_number(one, 3, NULL);
134 append_number(two, 4, NULL);
135 test_close(one);
136 test_close(two);
137 }
138
139 static void mkdtemp_chdir_chattr(char *path) {
140 assert_se(mkdtemp(path));
141 assert_se(chdir(path) >= 0);
142
143 /* Speed up things a bit on btrfs, ensuring that CoW is turned off for all files created in our
144 * directory during the test run */
145 (void) chattr_path(path, FS_NOCOW_FL, FS_NOCOW_FL, NULL);
146 }
147
148 static void test_skip_one(void (*setup)(void)) {
149 char t[] = "/var/tmp/journal-skip-XXXXXX";
150 sd_journal *j;
151 int r;
152
153 mkdtemp_chdir_chattr(t);
154
155 setup();
156
157 /* Seek to head, iterate down.
158 */
159 assert_ret(sd_journal_open_directory(&j, t, 0));
160 assert_ret(sd_journal_seek_head(j));
161 assert_ret(sd_journal_next(j));
162 test_check_numbers_down(j, 4);
163 sd_journal_close(j);
164
165 /* Seek to tail, iterate up.
166 */
167 assert_ret(sd_journal_open_directory(&j, t, 0));
168 assert_ret(sd_journal_seek_tail(j));
169 assert_ret(sd_journal_previous(j));
170 test_check_numbers_up(j, 4);
171 sd_journal_close(j);
172
173 /* Seek to tail, skip to head, iterate down.
174 */
175 assert_ret(sd_journal_open_directory(&j, t, 0));
176 assert_ret(sd_journal_seek_tail(j));
177 assert_ret(r = sd_journal_previous_skip(j, 4));
178 assert_se(r == 4);
179 test_check_numbers_down(j, 4);
180 sd_journal_close(j);
181
182 /* Seek to head, skip to tail, iterate up.
183 */
184 assert_ret(sd_journal_open_directory(&j, t, 0));
185 assert_ret(sd_journal_seek_head(j));
186 assert_ret(r = sd_journal_next_skip(j, 4));
187 assert_se(r == 4);
188 test_check_numbers_up(j, 4);
189 sd_journal_close(j);
190
191 log_info("Done...");
192
193 if (arg_keep)
194 log_info("Not removing %s", t);
195 else {
196 journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
197
198 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
199 }
200
201 puts("------------------------------------------------------------");
202 }
203
204 TEST(skip) {
205 test_skip_one(setup_sequential);
206 test_skip_one(setup_interleaved);
207 }
208
209 static void test_sequence_numbers_one(void) {
210 _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
211 char t[] = "/var/tmp/journal-seq-XXXXXX";
212 ManagedJournalFile *one, *two;
213 uint64_t seqnum = 0;
214 sd_id128_t seqnum_id;
215
216 m = mmap_cache_new();
217 assert_se(m != NULL);
218
219 mkdtemp_chdir_chattr(t);
220
221 assert_se(managed_journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644,
222 UINT64_MAX, NULL, m, NULL, NULL, &one) == 0);
223
224 append_number(one, 1, &seqnum);
225 printf("seqnum=%"PRIu64"\n", seqnum);
226 assert_se(seqnum == 1);
227 append_number(one, 2, &seqnum);
228 printf("seqnum=%"PRIu64"\n", seqnum);
229 assert_se(seqnum == 2);
230
231 assert_se(one->file->header->state == STATE_ONLINE);
232 assert_se(!sd_id128_equal(one->file->header->file_id, one->file->header->machine_id));
233 assert_se(!sd_id128_equal(one->file->header->file_id, one->file->header->boot_id));
234 assert_se(sd_id128_equal(one->file->header->file_id, one->file->header->seqnum_id));
235
236 memcpy(&seqnum_id, &one->file->header->seqnum_id, sizeof(sd_id128_t));
237
238 assert_se(managed_journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644,
239 UINT64_MAX, NULL, m, NULL, one, &two) == 0);
240
241 assert_se(two->file->header->state == STATE_ONLINE);
242 assert_se(!sd_id128_equal(two->file->header->file_id, one->file->header->file_id));
243 assert_se(sd_id128_equal(one->file->header->machine_id, one->file->header->machine_id));
244 assert_se(sd_id128_equal(one->file->header->boot_id, one->file->header->boot_id));
245 assert_se(sd_id128_equal(one->file->header->seqnum_id, one->file->header->seqnum_id));
246
247 append_number(two, 3, &seqnum);
248 printf("seqnum=%"PRIu64"\n", seqnum);
249 assert_se(seqnum == 3);
250 append_number(two, 4, &seqnum);
251 printf("seqnum=%"PRIu64"\n", seqnum);
252 assert_se(seqnum == 4);
253
254 test_close(two);
255
256 append_number(one, 5, &seqnum);
257 printf("seqnum=%"PRIu64"\n", seqnum);
258 assert_se(seqnum == 5);
259
260 append_number(one, 6, &seqnum);
261 printf("seqnum=%"PRIu64"\n", seqnum);
262 assert_se(seqnum == 6);
263
264 test_close(one);
265
266 /* If the machine-id is not initialized, the header file verification
267 * (which happens when re-opening a journal file) will fail. */
268 if (sd_id128_get_machine(NULL) >= 0) {
269 /* restart server */
270 seqnum = 0;
271
272 assert_se(managed_journal_file_open(-1, "two.journal", O_RDWR, JOURNAL_COMPRESS, 0,
273 UINT64_MAX, NULL, m, NULL, NULL, &two) == 0);
274
275 assert_se(sd_id128_equal(two->file->header->seqnum_id, seqnum_id));
276
277 append_number(two, 7, &seqnum);
278 printf("seqnum=%"PRIu64"\n", seqnum);
279 assert_se(seqnum == 5);
280
281 /* So..., here we have the same seqnum in two files with the
282 * same seqnum_id. */
283
284 test_close(two);
285 }
286
287 log_info("Done...");
288
289 if (arg_keep)
290 log_info("Not removing %s", t);
291 else {
292 journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
293
294 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
295 }
296 }
297
298 TEST(sequence_numbers) {
299 assert_se(setenv("SYSTEMD_JOURNAL_COMPACT", "0", 1) >= 0);
300 test_sequence_numbers_one();
301
302 assert_se(setenv("SYSTEMD_JOURNAL_COMPACT", "1", 1) >= 0);
303 test_sequence_numbers_one();
304 }
305
306 static int intro(void) {
307 /* managed_journal_file_open requires a valid machine id */
308 if (access("/etc/machine-id", F_OK) != 0)
309 return log_tests_skipped("/etc/machine-id not found");
310
311 arg_keep = saved_argc > 1;
312
313 return EXIT_SUCCESS;
314 }
315
316 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);