]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-journal-interleaving.c
5d01e9d92f4cbf53c7f386330aec2a4af88ecbdb
[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 "logs-show.h"
15 #include "managed-journal-file.h"
16 #include "parse-util.h"
17 #include "rm-rf.h"
18 #include "tests.h"
19
20 /* This program tests skipping around in a multi-file journal. */
21
22 static bool arg_keep = false;
23
24 _noreturn_ static void log_assert_errno(const char *text, int error, const char *file, unsigned line, const char *func) {
25 log_internal(LOG_CRIT, error, file, line, func,
26 "'%s' failed at %s:%u (%s): %m", text, file, line, func);
27 abort();
28 }
29
30 #define assert_ret(expr) \
31 do { \
32 int _r_ = (expr); \
33 if (_unlikely_(_r_ < 0)) \
34 log_assert_errno(#expr, -_r_, PROJECT_FILE, __LINE__, __func__); \
35 } while (false)
36
37 static ManagedJournalFile *test_open(const char *name) {
38 _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
39 ManagedJournalFile *f;
40
41 m = mmap_cache_new();
42 assert_se(m != NULL);
43
44 assert_ret(managed_journal_file_open(-1, name, O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644, UINT64_MAX, NULL, m, NULL, NULL, &f));
45 return f;
46 }
47
48 static void test_close(ManagedJournalFile *f) {
49 (void) managed_journal_file_close(f);
50 }
51
52 static void append_number(ManagedJournalFile *f, int n, const sd_id128_t *boot_id, uint64_t *seqnum) {
53 _cleanup_free_ char *p = NULL, *q = NULL;
54 dual_timestamp ts;
55 static dual_timestamp previous_ts = {};
56 struct iovec iovec[2];
57 size_t n_iov = 0;
58
59 dual_timestamp_get(&ts);
60
61 if (ts.monotonic <= previous_ts.monotonic)
62 ts.monotonic = previous_ts.monotonic + 1;
63
64 if (ts.realtime <= previous_ts.realtime)
65 ts.realtime = previous_ts.realtime + 1;
66
67 previous_ts = ts;
68
69 assert_se(asprintf(&p, "NUMBER=%d", n) >= 0);
70 iovec[n_iov++] = IOVEC_MAKE_STRING(p);
71
72 if (boot_id) {
73 assert_se(q = strjoin("_BOOT_ID=", SD_ID128_TO_STRING(*boot_id)));
74 iovec[n_iov++] = IOVEC_MAKE_STRING(q);
75 }
76
77 assert_ret(journal_file_append_entry(f->file, &ts, boot_id, iovec, n_iov, seqnum, NULL, NULL, NULL));
78 }
79
80 static void test_check_number(sd_journal *j, int n) {
81 sd_id128_t boot_id;
82 const void *d;
83 _cleanup_free_ char *k = NULL;
84 size_t l;
85 int x;
86
87 assert_se(sd_journal_get_monotonic_usec(j, NULL, &boot_id) >= 0);
88 assert_ret(sd_journal_get_data(j, "NUMBER", &d, &l));
89 assert_se(k = strndup(d, l));
90 printf("%s %s (expected=%i)\n", SD_ID128_TO_STRING(boot_id), k, n);
91
92 assert_se(safe_atoi(k + STRLEN("NUMBER="), &x) >= 0);
93 assert_se(n == x);
94 }
95
96 static void test_check_numbers_down(sd_journal *j, int count) {
97 int i;
98
99 for (i = 1; i <= count; i++) {
100 int r;
101 test_check_number(j, i);
102 assert_ret(r = sd_journal_next(j));
103 if (i == count)
104 assert_se(r == 0);
105 else
106 assert_se(r == 1);
107 }
108
109 }
110
111 static void test_check_numbers_up(sd_journal *j, int count) {
112 for (int i = count; i >= 1; i--) {
113 int r;
114 test_check_number(j, i);
115 assert_ret(r = sd_journal_previous(j));
116 if (i == 1)
117 assert_se(r == 0);
118 else
119 assert_se(r == 1);
120 }
121
122 }
123
124 static void setup_sequential(void) {
125 ManagedJournalFile *f1, *f2, *f3;
126 sd_id128_t id;
127
128 f1 = test_open("one.journal");
129 f2 = test_open("two.journal");
130 f3 = test_open("three.journal");
131 assert_se(sd_id128_randomize(&id) >= 0);
132 log_info("boot_id: %s", SD_ID128_TO_STRING(id));
133 append_number(f1, 1, &id, NULL);
134 append_number(f1, 2, &id, NULL);
135 append_number(f1, 3, &id, NULL);
136 append_number(f2, 4, &id, NULL);
137 assert_se(sd_id128_randomize(&id) >= 0);
138 log_info("boot_id: %s", SD_ID128_TO_STRING(id));
139 append_number(f2, 5, &id, NULL);
140 append_number(f2, 6, &id, NULL);
141 append_number(f3, 7, &id, NULL);
142 append_number(f3, 8, &id, NULL);
143 assert_se(sd_id128_randomize(&id) >= 0);
144 log_info("boot_id: %s", SD_ID128_TO_STRING(id));
145 append_number(f3, 9, &id, NULL);
146 test_close(f1);
147 test_close(f2);
148 test_close(f3);
149 }
150
151 static void setup_interleaved(void) {
152 ManagedJournalFile *f1, *f2, *f3;
153 sd_id128_t id;
154
155 f1 = test_open("one.journal");
156 f2 = test_open("two.journal");
157 f3 = test_open("three.journal");
158 assert_se(sd_id128_randomize(&id) >= 0);
159 log_info("boot_id: %s", SD_ID128_TO_STRING(id));
160 append_number(f1, 1, &id, NULL);
161 append_number(f2, 2, &id, NULL);
162 append_number(f3, 3, &id, NULL);
163 append_number(f1, 4, &id, NULL);
164 append_number(f2, 5, &id, NULL);
165 append_number(f3, 6, &id, NULL);
166 append_number(f1, 7, &id, NULL);
167 append_number(f2, 8, &id, NULL);
168 append_number(f3, 9, &id, NULL);
169 test_close(f1);
170 test_close(f2);
171 test_close(f3);
172 }
173
174 static void mkdtemp_chdir_chattr(char *path) {
175 assert_se(mkdtemp(path));
176 assert_se(chdir(path) >= 0);
177
178 /* Speed up things a bit on btrfs, ensuring that CoW is turned off for all files created in our
179 * directory during the test run */
180 (void) chattr_path(path, FS_NOCOW_FL, FS_NOCOW_FL, NULL);
181 }
182
183 static void test_skip_one(void (*setup)(void)) {
184 char t[] = "/var/tmp/journal-skip-XXXXXX";
185 sd_journal *j;
186 int r;
187
188 mkdtemp_chdir_chattr(t);
189
190 setup();
191
192 /* Seek to head, iterate down. */
193 assert_ret(sd_journal_open_directory(&j, t, 0));
194 assert_ret(sd_journal_seek_head(j));
195 assert_se(sd_journal_next(j) == 1); /* pointing to the first entry */
196 test_check_numbers_down(j, 9);
197 sd_journal_close(j);
198
199 /* Seek to head, iterate down. */
200 assert_ret(sd_journal_open_directory(&j, t, 0));
201 assert_ret(sd_journal_seek_head(j));
202 assert_se(sd_journal_next(j) == 1); /* pointing to the first entry */
203 assert_se(sd_journal_previous(j) == 0); /* no-op */
204 test_check_numbers_down(j, 9);
205 sd_journal_close(j);
206
207 /* Seek to head twice, iterate down. */
208 assert_ret(sd_journal_open_directory(&j, t, 0));
209 assert_ret(sd_journal_seek_head(j));
210 assert_se(sd_journal_next(j) == 1); /* pointing to the first entry */
211 assert_ret(sd_journal_seek_head(j));
212 assert_se(sd_journal_next(j) == 1); /* pointing to the first entry */
213 test_check_numbers_down(j, 9);
214 sd_journal_close(j);
215
216 /* Seek to head, move to previous, then iterate down. */
217 assert_ret(sd_journal_open_directory(&j, t, 0));
218 assert_ret(sd_journal_seek_head(j));
219 assert_se(sd_journal_previous(j) == 0); /* no-op */
220 assert_se(sd_journal_next(j) == 1); /* pointing to the first entry */
221 test_check_numbers_down(j, 9);
222 sd_journal_close(j);
223
224 /* Seek to head, walk several steps, then iterate down. */
225 assert_ret(sd_journal_open_directory(&j, t, 0));
226 assert_ret(sd_journal_seek_head(j));
227 assert_se(sd_journal_previous(j) == 0); /* no-op */
228 assert_se(sd_journal_previous(j) == 0); /* no-op */
229 assert_se(sd_journal_previous(j) == 0); /* no-op */
230 assert_se(sd_journal_next(j) == 1); /* pointing to the first entry */
231 assert_se(sd_journal_previous(j) == 0); /* no-op */
232 assert_se(sd_journal_previous(j) == 0); /* no-op */
233 test_check_numbers_down(j, 9);
234 sd_journal_close(j);
235
236 /* Seek to tail, iterate up. */
237 assert_ret(sd_journal_open_directory(&j, t, 0));
238 assert_ret(sd_journal_seek_tail(j));
239 assert_se(sd_journal_previous(j) == 1); /* pointing to the last entry */
240 test_check_numbers_up(j, 9);
241 sd_journal_close(j);
242
243 /* Seek to tail twice, iterate up. */
244 assert_ret(sd_journal_open_directory(&j, t, 0));
245 assert_ret(sd_journal_seek_tail(j));
246 assert_se(sd_journal_previous(j) == 1); /* pointing to the last entry */
247 assert_ret(sd_journal_seek_tail(j));
248 assert_se(sd_journal_previous(j) == 1); /* pointing to the last entry */
249 test_check_numbers_up(j, 9);
250 sd_journal_close(j);
251
252 /* Seek to tail, move to next, then iterate up. */
253 assert_ret(sd_journal_open_directory(&j, t, 0));
254 assert_ret(sd_journal_seek_tail(j));
255 assert_se(sd_journal_next(j) == 0); /* no-op */
256 assert_se(sd_journal_previous(j) == 1); /* pointing to the last entry */
257 test_check_numbers_up(j, 9);
258 sd_journal_close(j);
259
260 /* Seek to tail, walk several steps, then iterate up. */
261 assert_ret(sd_journal_open_directory(&j, t, 0));
262 assert_ret(sd_journal_seek_tail(j));
263 assert_se(sd_journal_next(j) == 0); /* no-op */
264 assert_se(sd_journal_next(j) == 0); /* no-op */
265 assert_se(sd_journal_next(j) == 0); /* no-op */
266 assert_se(sd_journal_previous(j) == 1); /* pointing to the last entry. */
267 assert_se(sd_journal_next(j) == 0); /* no-op */
268 assert_se(sd_journal_next(j) == 0); /* no-op */
269 test_check_numbers_up(j, 9);
270 sd_journal_close(j);
271
272 /* Seek to tail, skip to head, iterate down. */
273 assert_ret(sd_journal_open_directory(&j, t, 0));
274 assert_ret(sd_journal_seek_tail(j));
275 assert_se(sd_journal_previous_skip(j, 9) == 9); /* pointing to the first entry. */
276 test_check_numbers_down(j, 9);
277 sd_journal_close(j);
278
279 /* Seek to tail, skip to head in a more complex way, then iterate down. */
280 assert_ret(sd_journal_open_directory(&j, t, 0));
281 assert_ret(sd_journal_seek_tail(j));
282 assert_se(sd_journal_next(j) == 0);
283 assert_se(sd_journal_previous_skip(j, 4) == 4);
284 assert_se(sd_journal_previous_skip(j, 5) == 5);
285 assert_se(sd_journal_previous(j) == 0);
286 assert_se(sd_journal_previous_skip(j, 5) == 0);
287 assert_se(sd_journal_next(j) == 1);
288 assert_se(sd_journal_previous_skip(j, 5) == 1);
289 assert_se(sd_journal_next(j) == 1);
290 assert_se(sd_journal_next(j) == 1);
291 assert_se(sd_journal_previous(j) == 1);
292 assert_se(sd_journal_next(j) == 1);
293 assert_se(sd_journal_next(j) == 1);
294 assert_se(sd_journal_previous_skip(j, 5) == 3);
295 test_check_numbers_down(j, 9);
296 sd_journal_close(j);
297
298 /* Seek to head, skip to tail, iterate up. */
299 assert_ret(sd_journal_open_directory(&j, t, 0));
300 assert_ret(sd_journal_seek_head(j));
301 assert_se(sd_journal_next_skip(j, 9) == 9);
302 test_check_numbers_up(j, 9);
303 sd_journal_close(j);
304
305 /* Seek to head, skip to tail in a more complex way, then iterate up. */
306 assert_ret(sd_journal_open_directory(&j, t, 0));
307 assert_ret(sd_journal_seek_head(j));
308 assert_se(sd_journal_previous(j) == 0);
309 assert_se(sd_journal_next_skip(j, 4) == 4);
310 assert_se(sd_journal_next_skip(j, 5) == 5);
311 assert_se(sd_journal_next(j) == 0);
312 assert_se(sd_journal_next_skip(j, 5) == 0);
313 assert_se(sd_journal_previous(j) == 1);
314 assert_se(sd_journal_next_skip(j, 5) == 1);
315 assert_se(sd_journal_previous(j) == 1);
316 assert_se(sd_journal_previous(j) == 1);
317 assert_se(sd_journal_next(j) == 1);
318 assert_se(sd_journal_previous(j) == 1);
319 assert_se(sd_journal_previous(j) == 1);
320 assert_se(r = sd_journal_next_skip(j, 5) == 3);
321 test_check_numbers_up(j, 9);
322 sd_journal_close(j);
323
324 log_info("Done...");
325
326 if (arg_keep)
327 log_info("Not removing %s", t);
328 else {
329 journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
330
331 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
332 }
333
334 puts("------------------------------------------------------------");
335 }
336
337 TEST(skip) {
338 test_skip_one(setup_sequential);
339 test_skip_one(setup_interleaved);
340 }
341
342 static void test_boot_id_one(void (*setup)(void), size_t n_boots_expected) {
343 char t[] = "/var/tmp/journal-boot-id-XXXXXX";
344 sd_journal *j;
345 _cleanup_free_ BootId *boots = NULL;
346 size_t n_boots;
347
348 mkdtemp_chdir_chattr(t);
349
350 setup();
351
352 assert_ret(sd_journal_open_directory(&j, t, 0));
353 assert_se(journal_get_boots(j, &boots, &n_boots) >= 0);
354 assert_se(boots);
355 assert_se(n_boots == n_boots_expected);
356 sd_journal_close(j);
357
358 FOREACH_ARRAY(b, boots, n_boots) {
359 assert_ret(sd_journal_open_directory(&j, t, 0));
360 assert_se(journal_find_boot_by_id(j, b->id) == 1);
361 sd_journal_close(j);
362 }
363
364 for (int i = - (int) n_boots + 1; i <= (int) n_boots; i++) {
365 sd_id128_t id;
366
367 assert_ret(sd_journal_open_directory(&j, t, 0));
368 assert_se(journal_find_boot_by_offset(j, i, &id) == 1);
369 if (i <= 0)
370 assert_se(sd_id128_equal(id, boots[n_boots + i - 1].id));
371 else
372 assert_se(sd_id128_equal(id, boots[i - 1].id));
373 sd_journal_close(j);
374 }
375
376 log_info("Done...");
377
378 if (arg_keep)
379 log_info("Not removing %s", t);
380 else {
381 journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
382
383 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
384 }
385
386 puts("------------------------------------------------------------");
387 }
388
389 TEST(boot_id) {
390 test_boot_id_one(setup_sequential, 3);
391 }
392
393 static void test_sequence_numbers_one(void) {
394 _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
395 char t[] = "/var/tmp/journal-seq-XXXXXX";
396 ManagedJournalFile *one, *two;
397 uint64_t seqnum = 0;
398 sd_id128_t seqnum_id;
399
400 m = mmap_cache_new();
401 assert_se(m != NULL);
402
403 mkdtemp_chdir_chattr(t);
404
405 assert_se(managed_journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644,
406 UINT64_MAX, NULL, m, NULL, NULL, &one) == 0);
407
408 append_number(one, 1, NULL, &seqnum);
409 printf("seqnum=%"PRIu64"\n", seqnum);
410 assert_se(seqnum == 1);
411 append_number(one, 2, NULL, &seqnum);
412 printf("seqnum=%"PRIu64"\n", seqnum);
413 assert_se(seqnum == 2);
414
415 assert_se(one->file->header->state == STATE_ONLINE);
416 assert_se(!sd_id128_equal(one->file->header->file_id, one->file->header->machine_id));
417 assert_se(!sd_id128_equal(one->file->header->file_id, one->file->header->tail_entry_boot_id));
418 assert_se(sd_id128_equal(one->file->header->file_id, one->file->header->seqnum_id));
419
420 memcpy(&seqnum_id, &one->file->header->seqnum_id, sizeof(sd_id128_t));
421
422 assert_se(managed_journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644,
423 UINT64_MAX, NULL, m, NULL, one, &two) == 0);
424
425 assert_se(two->file->header->state == STATE_ONLINE);
426 assert_se(!sd_id128_equal(two->file->header->file_id, one->file->header->file_id));
427 assert_se(sd_id128_equal(two->file->header->machine_id, one->file->header->machine_id));
428 assert_se(sd_id128_is_null(two->file->header->tail_entry_boot_id)); /* Not written yet. */
429 assert_se(sd_id128_equal(two->file->header->seqnum_id, one->file->header->seqnum_id));
430
431 append_number(two, 3, NULL, &seqnum);
432 printf("seqnum=%"PRIu64"\n", seqnum);
433 assert_se(seqnum == 3);
434 append_number(two, 4, NULL, &seqnum);
435 printf("seqnum=%"PRIu64"\n", seqnum);
436 assert_se(seqnum == 4);
437
438 /* Verify tail_entry_boot_id. */
439 assert_se(sd_id128_equal(two->file->header->tail_entry_boot_id, one->file->header->tail_entry_boot_id));
440
441 test_close(two);
442
443 append_number(one, 5, NULL, &seqnum);
444 printf("seqnum=%"PRIu64"\n", seqnum);
445 assert_se(seqnum == 5);
446
447 append_number(one, 6, NULL, &seqnum);
448 printf("seqnum=%"PRIu64"\n", seqnum);
449 assert_se(seqnum == 6);
450
451 test_close(one);
452
453 /* If the machine-id is not initialized, the header file verification
454 * (which happens when re-opening a journal file) will fail. */
455 if (sd_id128_get_machine(NULL) >= 0) {
456 /* restart server */
457 seqnum = 0;
458
459 assert_se(managed_journal_file_open(-1, "two.journal", O_RDWR, JOURNAL_COMPRESS, 0,
460 UINT64_MAX, NULL, m, NULL, NULL, &two) == 0);
461
462 assert_se(sd_id128_equal(two->file->header->seqnum_id, seqnum_id));
463
464 append_number(two, 7, NULL, &seqnum);
465 printf("seqnum=%"PRIu64"\n", seqnum);
466 assert_se(seqnum == 5);
467
468 /* So..., here we have the same seqnum in two files with the
469 * same seqnum_id. */
470
471 test_close(two);
472 }
473
474 log_info("Done...");
475
476 if (arg_keep)
477 log_info("Not removing %s", t);
478 else {
479 journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
480
481 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
482 }
483 }
484
485 TEST(sequence_numbers) {
486 assert_se(setenv("SYSTEMD_JOURNAL_COMPACT", "0", 1) >= 0);
487 test_sequence_numbers_one();
488
489 assert_se(setenv("SYSTEMD_JOURNAL_COMPACT", "1", 1) >= 0);
490 test_sequence_numbers_one();
491 }
492
493 static int intro(void) {
494 /* managed_journal_file_open requires a valid machine id */
495 if (access("/etc/machine-id", F_OK) != 0)
496 return log_tests_skipped("/etc/machine-id not found");
497
498 arg_keep = saved_argc > 1;
499
500 return EXIT_SUCCESS;
501 }
502
503 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);