Debian Codegraph @

src/libsystemd/sd-journal/test-journal-interleaving.c

raw
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 "argv-util.h"
11 #include "chattr-util.h"
12 #include "iovec-util.h"
13 #include "journal-file-util.h"
14 #include "journal-vacuum.h"
15 #include "log.h"
16 #include "logs-show.h"
17 #include "parse-util.h"
18 #include "random-util.h"
19 #include "rm-rf.h"
20 #include "strv.h"
21 #include "tests.h"
22 #include "time-util.h"
23 #include "tmpfile-util.h"
24
25 /* This program tests skipping around in a multi-file journal. */
26
27 static bool arg_keep = false;
28 static dual_timestamp previous_ts = {};
29
30 static JournalFile* test_open_internal(const char *name, JournalFileFlags flags) {
31 _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
32 JournalFile *f;
33
34 ASSERT_NOT_NULL((m = mmap_cache_new()));
35 ASSERT_OK(journal_file_open(-EBADF, name, O_RDWR|O_CREAT, flags, 0644, UINT64_MAX, NULL, m, NULL, &f));
36 return f;
37 }
38
39 static JournalFile* test_open(const char *name) {
40 return test_open_internal(name, JOURNAL_COMPRESS);
41 }
42
43 static JournalFile* test_open_strict(const char *name) {
44 return test_open_internal(name, JOURNAL_COMPRESS | JOURNAL_STRICT_ORDER);
45 }
46
47 static char* test_done(char *t) {
48 if (!t)
49 return NULL;
50
51 log_info("Done...");
52
53 if (arg_keep)
54 log_info("Not removing %s", t);
55 else {
56 journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
57
58 ASSERT_OK(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL));
59 }
60
61 log_info("------------------------------------------------------------");
62 return mfree(t);
63 }
64
65 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, test_done);
66
67 static void append_number(JournalFile *f, unsigned n, const sd_id128_t *boot_id, uint64_t *seqnum, uint64_t *ret_offset) {
68 _cleanup_free_ char *p = NULL, *q = NULL, *s = NULL;
69 dual_timestamp ts;
70 struct iovec iovec[3];
71 size_t n_iov = 0;
72
73 dual_timestamp_now(&ts);
74
75 if (ts.monotonic <= previous_ts.monotonic)
76 ts.monotonic = previous_ts.monotonic + 1;
77
78 if (ts.realtime <= previous_ts.realtime)
79 ts.realtime = previous_ts.realtime + 1;
80
81 previous_ts = ts;
82
83 ASSERT_OK(asprintf(&p, "NUMBER=%u", n));
84 iovec[n_iov++] = IOVEC_MAKE_STRING(p);
85
86 ASSERT_NOT_NULL((s = strjoin("LESS_THAN_FIVE=", yes_no(n < 5))));
87 iovec[n_iov++] = IOVEC_MAKE_STRING(s);
88
89 if (boot_id) {
90 ASSERT_NOT_NULL((q = strjoin("_BOOT_ID=", SD_ID128_TO_STRING(*boot_id))));
91 iovec[n_iov++] = IOVEC_MAKE_STRING(q);
92 }
93
94 ASSERT_OK(journal_file_append_entry(f, &ts, boot_id, iovec, n_iov, seqnum, NULL, NULL, ret_offset));
95 }
96
97 static void append_unreferenced_data(JournalFile *f, const sd_id128_t *boot_id) {
98 _cleanup_free_ char *q = NULL;
99 dual_timestamp ts;
100 struct iovec iovec;
101
102 assert(boot_id);
103
104 ts.monotonic = usec_sub_unsigned(previous_ts.monotonic, 10);
105 ts.realtime = usec_sub_unsigned(previous_ts.realtime, 10);
106
107 ASSERT_NOT_NULL((q = strjoin("_BOOT_ID=", SD_ID128_TO_STRING(*boot_id))));
108 iovec = IOVEC_MAKE_STRING(q);
109
110 ASSERT_ERROR(journal_file_append_entry(f, &ts, boot_id, &iovec, 1, NULL, NULL, NULL, NULL), EREMCHG);
111 }
112
113 static void test_check_number(sd_journal *j, unsigned expected) {
114 sd_id128_t boot_id;
115 const void *d;
116 size_t l;
117
118 ASSERT_OK(sd_journal_get_monotonic_usec(j, NULL, &boot_id));
119 ASSERT_OK(sd_journal_get_data(j, "NUMBER", &d, &l));
120
121 _cleanup_free_ char *k = NULL;
122 ASSERT_NOT_NULL((k = strndup(d, l)));
123 printf("%s %s (expected=%u)\n", SD_ID128_TO_STRING(boot_id), k, expected);
124
125 unsigned x;
126 ASSERT_OK(safe_atou(k + STRLEN("NUMBER="), &x));
127 ASSERT_EQ(x, expected);
128 }
129
130 static void test_check_numbers_down(sd_journal *j, unsigned count) {
131 for (unsigned i = 1; i <= count; i++) {
132 test_check_number(j, i);
133 if (i == count)
134 ASSERT_OK_ZERO(sd_journal_next(j));
135 else
136 ASSERT_OK_POSITIVE(sd_journal_next(j));
137 }
138 }
139
140 static void test_check_numbers_up(sd_journal *j, unsigned count) {
141 for (unsigned i = count; i >= 1; i--) {
142 test_check_number(j, i);
143 if (i == 1)
144 ASSERT_OK_ZERO(sd_journal_previous(j));
145 else
146 ASSERT_OK_POSITIVE(sd_journal_previous(j));
147 }
148 }
149
150 static void setup_sequential(void) {
151 _cleanup_(journal_file_offline_closep) JournalFile *f1 = NULL, *f2 = NULL, *f3 = NULL;
152 sd_id128_t id;
153
154 f1 = test_open("one.journal");
155 f2 = test_open("two.journal");
156 f3 = test_open("three.journal");
157 ASSERT_OK(sd_id128_randomize(&id));
158 log_info("boot_id: %s", SD_ID128_TO_STRING(id));
159 append_number(f1, 1, &id, NULL, NULL);
160 append_number(f1, 2, &id, NULL, NULL);
161 append_number(f1, 3, &id, NULL, NULL);
162 append_number(f2, 4, &id, NULL, NULL);
163 ASSERT_OK(sd_id128_randomize(&id));
164 log_info("boot_id: %s", SD_ID128_TO_STRING(id));
165 append_number(f2, 5, &id, NULL, NULL);
166 append_number(f2, 6, &id, NULL, NULL);
167 append_number(f3, 7, &id, NULL, NULL);
168 append_number(f3, 8, &id, NULL, NULL);
169 ASSERT_OK(sd_id128_randomize(&id));
170 log_info("boot_id: %s", SD_ID128_TO_STRING(id));
171 append_number(f3, 9, &id, NULL, NULL);
172 }
173
174 static void setup_interleaved(void) {
175 _cleanup_(journal_file_offline_closep) JournalFile *f1 = NULL, *f2 = NULL, *f3 = NULL;
176 sd_id128_t id;
177
178 f1 = test_open("one.journal");
179 f2 = test_open("two.journal");
180 f3 = test_open("three.journal");
181 ASSERT_OK(sd_id128_randomize(&id));
182 log_info("boot_id: %s", SD_ID128_TO_STRING(id));
183 append_number(f1, 1, &id, NULL, NULL);
184 append_number(f2, 2, &id, NULL, NULL);
185 append_number(f3, 3, &id, NULL, NULL);
186 append_number(f1, 4, &id, NULL, NULL);
187 append_number(f2, 5, &id, NULL, NULL);
188 append_number(f3, 6, &id, NULL, NULL);
189 append_number(f1, 7, &id, NULL, NULL);
190 append_number(f2, 8, &id, NULL, NULL);
191 append_number(f3, 9, &id, NULL, NULL);
192 }
193
194 static void setup_unreferenced_data(void) {
195 _cleanup_(journal_file_offline_closep) JournalFile *f1 = NULL, *f2 = NULL, *f3 = NULL;
196 sd_id128_t id;
197
198 /* For issue #29275. */
199
200 f1 = test_open_strict("one.journal");
201 f2 = test_open_strict("two.journal");
202 f3 = test_open_strict("three.journal");
203 ASSERT_OK(sd_id128_randomize(&id));
204 log_info("boot_id: %s", SD_ID128_TO_STRING(id));
205 append_number(f1, 1, &id, NULL, NULL);
206 append_number(f1, 2, &id, NULL, NULL);
207 append_number(f1, 3, &id, NULL, NULL);
208 ASSERT_OK(sd_id128_randomize(&id));
209 log_info("boot_id: %s", SD_ID128_TO_STRING(id));
210 append_unreferenced_data(f1, &id);
211 append_number(f2, 4, &id, NULL, NULL);
212 append_number(f2, 5, &id, NULL, NULL);
213 append_number(f2, 6, &id, NULL, NULL);
214 ASSERT_OK(sd_id128_randomize(&id));
215 log_info("boot_id: %s", SD_ID128_TO_STRING(id));
216 append_unreferenced_data(f2, &id);
217 append_number(f3, 7, &id, NULL, NULL);
218 append_number(f3, 8, &id, NULL, NULL);
219 append_number(f3, 9, &id, NULL, NULL);
220 }
221
222 static void mkdtemp_chdir_chattr(const char *template, char **ret) {
223 _cleanup_(rm_rf_physical_and_freep) char *path = NULL;
224
225 assert(ret);
226
227 ASSERT_OK(mkdtemp_malloc(template, &path));
228 ASSERT_OK_ERRNO(chdir(path));
229
230 /* Speed up things a bit on btrfs, ensuring that CoW is turned off for all files created in our
231 * directory during the test run */
232 (void) chattr_path(path, FS_NOCOW_FL, FS_NOCOW_FL);
233
234 *ret = TAKE_PTR(path);
235 }
236
237 static void test_cursor(sd_journal *j) {
238 _cleanup_strv_free_ char **cursors = NULL;
239 int r;
240
241 ASSERT_OK(sd_journal_seek_head(j));
242
243 for (;;) {
244 ASSERT_OK(r = sd_journal_next(j));
245 if (r == 0)
246 break;
247
248 _cleanup_free_ char *cursor = NULL;
249 ASSERT_OK(sd_journal_get_cursor(j, &cursor));
250 ASSERT_OK_POSITIVE(sd_journal_test_cursor(j, cursor));
251 ASSERT_OK(strv_consume(&cursors, TAKE_PTR(cursor)));
252 }
253
254 STRV_FOREACH(c, cursors) {
255 ASSERT_OK(sd_journal_seek_cursor(j, *c));
256 ASSERT_OK(sd_journal_next(j));
257 ASSERT_OK_POSITIVE(sd_journal_test_cursor(j, *c));
258 }
259
260 ASSERT_OK(sd_journal_seek_head(j));
261 STRV_FOREACH(c, cursors) {
262 ASSERT_OK(sd_journal_next(j));
263 ASSERT_OK_POSITIVE(sd_journal_test_cursor(j, *c));
264 }
265 }
266
267 static void test_skip_one(void (*setup)(void)) {
268 _cleanup_(test_donep) char *t = NULL;
269 sd_journal *j;
270
271 mkdtemp_chdir_chattr("/var/tmp/journal-skip-XXXXXX", &t);
272
273 setup();
274
275 /* Seek to head, iterate down. */
276 ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
277 ASSERT_OK(sd_journal_seek_head(j));
278 ASSERT_OK_POSITIVE(sd_journal_next(j)); /* pointing to the first entry */
279 test_check_numbers_down(j, 9);
280 sd_journal_close(j);
281
282 /* Seek to head, iterate down. */
283 ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
284 ASSERT_OK(sd_journal_seek_head(j));
285 ASSERT_OK_POSITIVE(sd_journal_next(j)); /* pointing to the first entry */
286 ASSERT_OK_ZERO(sd_journal_previous(j)); /* no-op */
287 test_check_numbers_down(j, 9);
288 sd_journal_close(j);
289
290 /* Seek to head twice, iterate down. */
291 ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
292 ASSERT_OK(sd_journal_seek_head(j));
293 ASSERT_OK_POSITIVE(sd_journal_next(j)); /* pointing to the first entry */
294 ASSERT_OK(sd_journal_seek_head(j));
295 ASSERT_OK_POSITIVE(sd_journal_next(j)); /* pointing to the first entry */
296 test_check_numbers_down(j, 9);
297 sd_journal_close(j);
298
299 /* Seek to head, move to previous, then iterate down. */
300 ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
301 ASSERT_OK(sd_journal_seek_head(j));
302 ASSERT_OK_ZERO(sd_journal_previous(j)); /* no-op */
303 ASSERT_OK_POSITIVE(sd_journal_next(j)); /* pointing to the first entry */
304 test_check_numbers_down(j, 9);
305 sd_journal_close(j);
306
307 /* Seek to head, walk several steps, then iterate down. */
308 ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
309 ASSERT_OK(sd_journal_seek_head(j));
310 ASSERT_OK_ZERO(sd_journal_previous(j)); /* no-op */
311 ASSERT_OK_ZERO(sd_journal_previous(j)); /* no-op */
312 ASSERT_OK_ZERO(sd_journal_previous(j)); /* no-op */
313 ASSERT_OK_POSITIVE(sd_journal_next(j)); /* pointing to the first entry */
314 ASSERT_OK_ZERO(sd_journal_previous(j)); /* no-op */
315 ASSERT_OK_ZERO(sd_journal_previous(j)); /* no-op */
316 test_check_numbers_down(j, 9);
317 sd_journal_close(j);
318
319 /* Seek to tail, iterate up. */
320 ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
321 ASSERT_OK(sd_journal_seek_tail(j));
322 ASSERT_OK_POSITIVE(sd_journal_previous(j)); /* pointing to the last entry */
323 test_check_numbers_up(j, 9);
324 sd_journal_close(j);
325
326 /* Seek to tail twice, iterate up. */
327 ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
328 ASSERT_OK(sd_journal_seek_tail(j));
329 ASSERT_OK_POSITIVE(sd_journal_previous(j)); /* pointing to the last entry */
330 ASSERT_OK(sd_journal_seek_tail(j));
331 ASSERT_OK_POSITIVE(sd_journal_previous(j)); /* pointing to the last entry */
332 test_check_numbers_up(j, 9);
333 sd_journal_close(j);
334
335 /* Seek to tail, move to next, then iterate up. */
336 ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
337 ASSERT_OK(sd_journal_seek_tail(j));
338 ASSERT_OK_ZERO(sd_journal_next(j)); /* no-op */
339 ASSERT_OK_POSITIVE(sd_journal_previous(j)); /* pointing to the last entry */
340 test_check_numbers_up(j, 9);
341 sd_journal_close(j);
342
343 /* Seek to tail, walk several steps, then iterate up. */
344 ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
345 ASSERT_OK(sd_journal_seek_tail(j));
346 ASSERT_OK_ZERO(sd_journal_next(j)); /* no-op */
347 ASSERT_OK_ZERO(sd_journal_next(j)); /* no-op */
348 ASSERT_OK_ZERO(sd_journal_next(j)); /* no-op */
349 ASSERT_OK_POSITIVE(sd_journal_previous(j)); /* pointing to the last entry. */
350 ASSERT_OK_ZERO(sd_journal_next(j)); /* no-op */
351 ASSERT_OK_ZERO(sd_journal_next(j)); /* no-op */
352 test_check_numbers_up(j, 9);
353 sd_journal_close(j);
354
355 /* Seek to tail, skip to head, iterate down. */
356 ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
357 ASSERT_OK(sd_journal_seek_tail(j));
358 ASSERT_EQ(sd_journal_previous_skip(j, 9), 9); /* pointing to the first entry. */
359 test_check_numbers_down(j, 9);
360 sd_journal_close(j);
361
362 /* Seek to tail, skip to head in a more complex way, then iterate down. */
363 ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
364 ASSERT_OK(sd_journal_seek_tail(j));
365 ASSERT_OK_ZERO(sd_journal_next(j));
366 ASSERT_EQ(sd_journal_previous_skip(j, 4), 4);
367 ASSERT_EQ(sd_journal_previous_skip(j, 5), 5);
368 ASSERT_OK_ZERO(sd_journal_previous(j));
369 ASSERT_OK_ZERO(sd_journal_previous_skip(j, 5));
370 ASSERT_OK_POSITIVE(sd_journal_next(j));
371 ASSERT_OK_POSITIVE(sd_journal_previous_skip(j, 5));
372 ASSERT_OK_POSITIVE(sd_journal_next(j));
373 ASSERT_OK_POSITIVE(sd_journal_next(j));
374 ASSERT_OK_POSITIVE(sd_journal_previous(j));
375 ASSERT_OK_POSITIVE(sd_journal_next(j));
376 ASSERT_OK_POSITIVE(sd_journal_next(j));
377 ASSERT_EQ(sd_journal_previous_skip(j, 5), 3);
378 test_check_numbers_down(j, 9);
379 sd_journal_close(j);
380
381 /* Seek to head, skip to tail, iterate up. */
382 ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
383 ASSERT_OK(sd_journal_seek_head(j));
384 ASSERT_EQ(sd_journal_next_skip(j, 9), 9);
385 test_check_numbers_up(j, 9);
386 sd_journal_close(j);
387
388 /* Seek to head, skip to tail in a more complex way, then iterate up. */
389 ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
390 ASSERT_OK(sd_journal_seek_head(j));
391 ASSERT_OK_ZERO(sd_journal_previous(j));
392 ASSERT_EQ(sd_journal_next_skip(j, 4), 4);
393 ASSERT_EQ(sd_journal_next_skip(j, 5), 5);
394 ASSERT_OK_ZERO(sd_journal_next(j));
395 ASSERT_OK_ZERO(sd_journal_next_skip(j, 5));
396 ASSERT_OK_POSITIVE(sd_journal_previous(j));
397 ASSERT_OK_POSITIVE(sd_journal_next_skip(j, 5));
398 ASSERT_OK_POSITIVE(sd_journal_previous(j));
399 ASSERT_OK_POSITIVE(sd_journal_previous(j));
400 ASSERT_OK_POSITIVE(sd_journal_next(j));
401 ASSERT_OK_POSITIVE(sd_journal_previous(j));
402 ASSERT_OK_POSITIVE(sd_journal_previous(j));
403 ASSERT_EQ(sd_journal_next_skip(j, 5), 3);
404 test_check_numbers_up(j, 9);
405 sd_journal_close(j);
406
407 /* For issue #31516. */
408 ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
409 test_cursor(j);
410 sd_journal_flush_matches(j);
411 ASSERT_OK(sd_journal_add_match(j, "LESS_THAN_FIVE=yes", SIZE_MAX));
412 test_cursor(j);
413 sd_journal_flush_matches(j);
414 ASSERT_OK(sd_journal_add_match(j, "LESS_THAN_FIVE=no", SIZE_MAX));
415 test_cursor(j);
416 sd_journal_flush_matches(j);
417 ASSERT_OK(sd_journal_add_match(j, "LESS_THAN_FIVE=hoge", SIZE_MAX));
418 test_cursor(j);
419 sd_journal_flush_matches(j);
420 ASSERT_OK(sd_journal_add_match(j, "LESS_THAN_FIVE=yes", SIZE_MAX));
421 ASSERT_OK(sd_journal_add_match(j, "NUMBER=3", SIZE_MAX));
422 test_cursor(j);
423 sd_journal_flush_matches(j);
424 ASSERT_OK(sd_journal_add_match(j, "LESS_THAN_FIVE=yes", SIZE_MAX));
425 ASSERT_OK(sd_journal_add_match(j, "NUMBER=3", SIZE_MAX));
426 ASSERT_OK(sd_journal_add_match(j, "NUMBER=4", SIZE_MAX));
427 ASSERT_OK(sd_journal_add_match(j, "NUMBER=5", SIZE_MAX));
428 ASSERT_OK(sd_journal_add_match(j, "NUMBER=6", SIZE_MAX));
429 test_cursor(j);
430 sd_journal_close(j);
431 }
432
433 TEST(skip) {
434 test_skip_one(setup_sequential);
435 test_skip_one(setup_interleaved);
436 }
437
438 static void test_boot_id_one(void (*setup)(void), size_t n_ids_expected) {
439 _cleanup_(test_donep) char *t = NULL;
440 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
441 _cleanup_free_ LogId *ids = NULL;
442 size_t n_ids;
443
444 mkdtemp_chdir_chattr("/var/tmp/journal-boot-id-XXXXXX", &t);
445
446 setup();
447
448 ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
449 ASSERT_OK(journal_get_boots(
450 j,
451 /* advance_older= */ false, /* max_ids= */ SIZE_MAX,
452 &ids, &n_ids));
453 ASSERT_NOT_NULL(ids);
454 ASSERT_EQ(n_ids, n_ids_expected);
455
456 for (size_t i = 0; i < n_ids; i++) {
457 sd_id128_t id;
458
459 /* positive offset */
460 ASSERT_OK_POSITIVE(journal_find_boot(j, SD_ID128_NULL, (int) (i + 1), &id));
461 ASSERT_EQ_ID128(id, ids[i].id);
462
463 /* negative offset */
464 ASSERT_OK_POSITIVE(journal_find_boot(j, SD_ID128_NULL, (int) (i + 1) - (int) n_ids, &id));
465 ASSERT_EQ_ID128(id, ids[i].id);
466
467 for (size_t k = 0; k < n_ids; k++) {
468 int offset = (int) k - (int) i;
469
470 /* relative offset */
471 ASSERT_OK_POSITIVE(journal_find_boot(j, ids[i].id, offset, &id));
472 ASSERT_EQ_ID128(id, ids[k].id);
473 }
474 }
475
476 for (size_t i = 0; i <= n_ids_expected + 1; i++) {
477 _cleanup_free_ LogId *ids_limited = NULL;
478 size_t n_ids_limited;
479
480 ASSERT_OK(journal_get_boots(
481 j,
482 /* advance_older= */ false, /* max_ids= */ i,
483 &ids_limited, &n_ids_limited));
484 ASSERT_TRUE(ids_limited || i == 0);
485 ASSERT_EQ(n_ids_limited, MIN(i, n_ids_expected));
486 ASSERT_EQ(memcmp_safe(ids, ids_limited, n_ids_limited * sizeof(LogId)), 0);
487 }
488
489 for (size_t i = 0; i <= n_ids_expected + 1; i++) {
490 _cleanup_free_ LogId *ids_limited = NULL;
491 size_t n_ids_limited;
492
493 ASSERT_OK(journal_get_boots(
494 j,
495 /* advance_older= */ true, /* max_ids= */ i,
496 &ids_limited, &n_ids_limited));
497 ASSERT_TRUE(ids_limited || i == 0);
498 ASSERT_EQ(n_ids_limited, MIN(i, n_ids_expected));
499 for (size_t k = 0; k < n_ids_limited; k++)
500 ASSERT_EQ(memcmp(&ids[n_ids - k - 1], &ids_limited[k], sizeof(LogId)), 0);
501 }
502 }
503
504 TEST(boot_id) {
505 test_boot_id_one(setup_sequential, 3);
506 test_boot_id_one(setup_unreferenced_data, 3);
507 }
508
509 static void test_sequence_numbers_one(void) {
510 _cleanup_(test_donep) char *t = NULL;
511 _cleanup_(journal_file_offline_closep) JournalFile *one = NULL, *two = NULL;
512 _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
513 uint64_t seqnum = 0;
514 sd_id128_t seqnum_id;
515
516 ASSERT_NOT_NULL((m = mmap_cache_new()));
517
518 mkdtemp_chdir_chattr("/var/tmp/journal-seq-XXXXXX", &t);
519
520 ASSERT_OK(journal_file_open(-EBADF, "one.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644,
521 UINT64_MAX, NULL, m, NULL, &one));
522
523 append_number(one, 1, NULL, &seqnum, NULL);
524 printf("seqnum=%"PRIu64"\n", seqnum);
525 ASSERT_EQ(seqnum, UINT64_C(1));
526 append_number(one, 2, NULL, &seqnum, NULL);
527 printf("seqnum=%"PRIu64"\n", seqnum);
528 ASSERT_EQ(seqnum, UINT64_C(2));
529
530 ASSERT_EQ(one->header->state, STATE_ONLINE);
531 ASSERT_NE_ID128(one->header->file_id, one->header->machine_id);
532 ASSERT_NE_ID128(one->header->file_id, one->header->tail_entry_boot_id);
533 ASSERT_EQ_ID128(one->header->file_id, one->header->seqnum_id);
534
535 memcpy(&seqnum_id, &one->header->seqnum_id, sizeof(sd_id128_t));
536
537 ASSERT_OK(journal_file_open(-EBADF, "two.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644,
538 UINT64_MAX, NULL, m, one, &two));
539
540 ASSERT_EQ(two->header->state, STATE_ONLINE);
541 ASSERT_NE_ID128(two->header->file_id, one->header->file_id);
542 ASSERT_EQ_ID128(two->header->machine_id, one->header->machine_id);
543 ASSERT_EQ_ID128(two->header->tail_entry_boot_id, SD_ID128_NULL); /* Not written yet. */
544 ASSERT_EQ_ID128(two->header->seqnum_id, one->header->seqnum_id);
545
546 append_number(two, 3, NULL, &seqnum, NULL);
547 printf("seqnum=%"PRIu64"\n", seqnum);
548 ASSERT_EQ(seqnum, UINT64_C(3));
549 append_number(two, 4, NULL, &seqnum, NULL);
550 printf("seqnum=%"PRIu64"\n", seqnum);
551 ASSERT_EQ(seqnum, UINT64_C(4));
552
553 /* Verify tail_entry_boot_id. */
554 ASSERT_EQ_ID128(two->header->tail_entry_boot_id, one->header->tail_entry_boot_id);
555
556 append_number(one, 5, NULL, &seqnum, NULL);
557 printf("seqnum=%"PRIu64"\n", seqnum);
558 ASSERT_EQ(seqnum, UINT64_C(5));
559
560 append_number(one, 6, NULL, &seqnum, NULL);
561 printf("seqnum=%"PRIu64"\n", seqnum);
562 ASSERT_EQ(seqnum, UINT64_C(6));
563
564 /* If the machine-id is not initialized, the header file verification
565 * (which happens when reopening a journal file) will fail. */
566 if (sd_id128_get_machine(NULL) >= 0) {
567 two = journal_file_offline_close(two);
568
569 /* emulate a system restart */
570 seqnum = 0;
571
572 ASSERT_OK(journal_file_open(-EBADF, "two.journal", O_RDWR, JOURNAL_COMPRESS, 0,
573 UINT64_MAX, NULL, m, NULL, &two));
574
575 ASSERT_EQ_ID128(two->header->seqnum_id, seqnum_id);
576
577 append_number(two, 7, NULL, &seqnum, NULL);
578 printf("seqnum=%"PRIu64"\n", seqnum);
579 ASSERT_EQ(seqnum, UINT64_C(5));
580
581 /* So..., here we have the same seqnum in two files with the same seqnum_id. */
582 }
583 }
584
585 TEST(sequence_numbers) {
586 ASSERT_OK_ERRNO(setenv("SYSTEMD_JOURNAL_COMPACT", "0", 1));
587 test_sequence_numbers_one();
588
589 ASSERT_OK_ERRNO(setenv("SYSTEMD_JOURNAL_COMPACT", "1", 1));
590 test_sequence_numbers_one();
591
592 ASSERT_OK_ERRNO(unsetenv("SYSTEMD_JOURNAL_COMPACT"));
593 }
594
595 static int expected_result(uint64_t needle, const uint64_t *candidates, const uint64_t *offset, size_t n, direction_t direction, uint64_t *ret) {
596 assert(ret);
597
598 switch (direction) {
599 case DIRECTION_DOWN:
600 for (size_t i = 0; i < n; i++) {
601 if (candidates[i] == 0) {
602 *ret = 0;
603 return 0;
604 }
605 if (needle <= candidates[i]) {
606 *ret = offset[i];
607 return 1;
608 }
609 }
610 *ret = 0;
611 return 0;
612
613 case DIRECTION_UP:
614 for (size_t i = 0; i < n; i++)
615 if (needle < candidates[i] || candidates[i] == 0) {
616 if (i == 0) {
617 *ret = 0;
618 return 0;
619 }
620 *ret = offset[i - 1];
621 return 1;
622 }
623 *ret = offset[n - 1];
624 return 1;
625
626 default:
627 assert_not_reached();
628 }
629 }
630
631 static int expected_result_next(uint64_t needle, const uint64_t *candidates, const uint64_t *offset, size_t n, direction_t direction, uint64_t *ret) {
632 assert(ret);
633
634 switch (direction) {
635 case DIRECTION_DOWN:
636 for (size_t i = 0; i < n; i++)
637 if (needle < offset[i]) {
638 *ret = candidates[i];
639 return candidates[i] > 0;
640 }
641 *ret = 0;
642 return 0;
643
644 case DIRECTION_UP:
645 for (size_t i = 0; i < n; i++)
646 if (needle <= offset[i]) {
647 n = i;
648 break;
649 }
650
651 for (; n > 0 && candidates[n - 1] == 0; n--)
652 ;
653
654 if (n == 0) {
655 *ret = 0;
656 return 0;
657 }
658
659 *ret = candidates[n - 1];
660 return candidates[n - 1] > 0;
661
662 default:
663 assert_not_reached();
664 }
665 }
666
667 static void verify(JournalFile *f, const uint64_t *seqnum, const uint64_t *offset_candidates, const uint64_t *offset, size_t n) {
668 uint64_t p, q;
669 int r, e;
670
671 /* by seqnum (sequential) */
672 for (uint64_t i = 0; i < n + 2; i++) {
673 p = 0;
674 r = journal_file_move_to_entry_by_seqnum(f, i, DIRECTION_DOWN, NULL, &p);
675 e = expected_result(i, seqnum, offset, n, DIRECTION_DOWN, &q);
676 ASSERT_EQ(r, e);
677 ASSERT_EQ(p, q);
678
679 p = 0;
680 r = journal_file_move_to_entry_by_seqnum(f, i, DIRECTION_UP, NULL, &p);
681 e = expected_result(i, seqnum, offset, n, DIRECTION_UP, &q);
682 ASSERT_EQ(r, e);
683 ASSERT_EQ(p, q);
684 }
685
686 /* by seqnum (random) */
687 for (size_t trial = 0; trial < 3 * n; trial++) {
688 uint64_t i = random_u64_range(n + 2);
689
690 p = 0;
691 r = journal_file_move_to_entry_by_seqnum(f, i, DIRECTION_DOWN, NULL, &p);
692 e = expected_result(i, seqnum, offset, n, DIRECTION_DOWN, &q);
693 ASSERT_EQ(r, e);
694 ASSERT_EQ(p, q);
695 }
696 for (size_t trial = 0; trial < 3 * n; trial++) {
697 uint64_t i = random_u64_range(n + 2);
698
699 p = 0;
700 r = journal_file_move_to_entry_by_seqnum(f, i, DIRECTION_UP, NULL, &p);
701 e = expected_result(i, seqnum, offset, n, DIRECTION_UP, &q);
702 ASSERT_EQ(r, e);
703 ASSERT_EQ(p, q);
704 }
705
706 /* by offset (sequential) */
707 for (size_t i = 0; i < n; i++) {
708 p = 0;
709 r = journal_file_move_to_entry_by_offset(f, offset[i] - 1, DIRECTION_DOWN, NULL, &p);
710 e = expected_result(offset[i] - 1, offset, offset, n, DIRECTION_DOWN, &q);
711 ASSERT_EQ(r, e);
712 ASSERT_EQ(p, q);
713
714 p = 0;
715 r = journal_file_move_to_entry_by_offset(f, offset[i], DIRECTION_DOWN, NULL, &p);
716 e = expected_result(offset[i], offset, offset, n, DIRECTION_DOWN, &q);
717 ASSERT_EQ(r, e);
718 ASSERT_EQ(p, q);
719
720 p = 0;
721 r = journal_file_move_to_entry_by_offset(f, offset[i] + 1, DIRECTION_DOWN, NULL, &p);
722 e = expected_result(offset[i] + 1, offset, offset, n, DIRECTION_DOWN, &q);
723 ASSERT_EQ(r, e);
724 ASSERT_EQ(p, q);
725
726 p = 0;
727 r = journal_file_move_to_entry_by_offset(f, offset[i] - 1, DIRECTION_UP, NULL, &p);
728 e = expected_result(offset[i] - 1, offset, offset, n, DIRECTION_UP, &q);
729 ASSERT_EQ(r, e);
730 ASSERT_EQ(p, q);
731
732 p = 0;
733 r = journal_file_move_to_entry_by_offset(f, offset[i], DIRECTION_UP, NULL, &p);
734 e = expected_result(offset[i], offset, offset, n, DIRECTION_UP, &q);
735 ASSERT_EQ(r, e);
736 ASSERT_EQ(p, q);
737
738 p = 0;
739 r = journal_file_move_to_entry_by_offset(f, offset[i] + 1, DIRECTION_UP, NULL, &p);
740 e = expected_result(offset[i] + 1, offset, offset, n, DIRECTION_UP, &q);
741 ASSERT_EQ(r, e);
742 ASSERT_EQ(p, q);
743 }
744
745 /* by offset (random) */
746 for (size_t trial = 0; trial < 3 * n; trial++) {
747 uint64_t i = offset[0] - 1 + random_u64_range(offset[n-1] - offset[0] + 2);
748
749 p = 0;
750 r = journal_file_move_to_entry_by_offset(f, i, DIRECTION_DOWN, NULL, &p);
751 e = expected_result(i, offset, offset, n, DIRECTION_DOWN, &q);
752 ASSERT_EQ(r, e);
753 ASSERT_EQ(p, q);
754 }
755 for (size_t trial = 0; trial < 3 * n; trial++) {
756 uint64_t i = offset[0] - 1 + random_u64_range(offset[n-1] - offset[0] + 2);
757
758 p = 0;
759 r = journal_file_move_to_entry_by_offset(f, i, DIRECTION_UP, NULL, &p);
760 e = expected_result(i, offset, offset, n, DIRECTION_UP, &q);
761 ASSERT_EQ(r, e);
762 ASSERT_EQ(p, q);
763 }
764
765 /* by journal_file_next_entry() */
766 for (size_t i = 0; i < n; i++) {
767 p = 0;
768 r = journal_file_next_entry(f, offset[i] - 2, DIRECTION_DOWN, NULL, &p);
769 e = expected_result_next(offset[i] - 2, offset_candidates, offset, n, DIRECTION_DOWN, &q);
770 ASSERT_EQ(e == 0, r <= 0);
771 ASSERT_EQ(p, q);
772
773 p = 0;
774 r = journal_file_next_entry(f, offset[i] - 1, DIRECTION_DOWN, NULL, &p);
775 e = expected_result_next(offset[i] - 1, offset_candidates, offset, n, DIRECTION_DOWN, &q);
776 ASSERT_EQ(e == 0, r <= 0);
777 ASSERT_EQ(p, q);
778
779 p = 0;
780 r = journal_file_next_entry(f, offset[i], DIRECTION_DOWN, NULL, &p);
781 e = expected_result_next(offset[i], offset_candidates, offset, n, DIRECTION_DOWN, &q);
782 ASSERT_EQ(e == 0, r <= 0);
783 ASSERT_EQ(p, q);
784
785 p = 0;
786 r = journal_file_next_entry(f, offset[i] + 1, DIRECTION_DOWN, NULL, &p);
787 e = expected_result_next(offset[i] + 1, offset_candidates, offset, n, DIRECTION_DOWN, &q);
788 ASSERT_EQ(e == 0, r <= 0);
789 ASSERT_EQ(p, q);
790
791 p = 0;
792 r = journal_file_next_entry(f, offset[i] - 1, DIRECTION_UP, NULL, &p);
793 e = expected_result_next(offset[i] - 1, offset_candidates, offset, n, DIRECTION_UP, &q);
794 ASSERT_EQ(e == 0, r <= 0);
795 ASSERT_EQ(p, q);
796
797 p = 0;
798 r = journal_file_next_entry(f, offset[i], DIRECTION_UP, NULL, &p);
799 e = expected_result_next(offset[i], offset_candidates, offset, n, DIRECTION_UP, &q);
800 ASSERT_EQ(e == 0, r <= 0);
801 ASSERT_EQ(p, q);
802
803 p = 0;
804 r = journal_file_next_entry(f, offset[i] + 1, DIRECTION_UP, NULL, &p);
805 e = expected_result_next(offset[i] + 1, offset_candidates, offset, n, DIRECTION_UP, &q);
806 ASSERT_EQ(e == 0, r <= 0);
807 ASSERT_EQ(p, q);
808
809 p = 0;
810 r = journal_file_next_entry(f, offset[i] + 2, DIRECTION_UP, NULL, &p);
811 e = expected_result_next(offset[i] + 2, offset_candidates, offset, n, DIRECTION_UP, &q);
812 ASSERT_EQ(e == 0, r <= 0);
813 ASSERT_EQ(p, q);
814 }
815 for (size_t trial = 0; trial < 3 * n; trial++) {
816 uint64_t i = offset[0] - 1 + random_u64_range(offset[n-1] - offset[0] + 2);
817
818 p = 0;
819 r = journal_file_next_entry(f, i, DIRECTION_DOWN, NULL, &p);
820 e = expected_result_next(i, offset_candidates, offset, n, DIRECTION_DOWN, &q);
821 ASSERT_EQ(e == 0, r <= 0);
822 ASSERT_EQ(p, q);
823 }
824 for (size_t trial = 0; trial < 3 * n; trial++) {
825 uint64_t i = offset[0] - 1 + random_u64_range(offset[n-1] - offset[0] + 2);
826
827 p = 0;
828 r = journal_file_next_entry(f, i, DIRECTION_UP, NULL, &p);
829 e = expected_result_next(i, offset_candidates, offset, n, DIRECTION_UP, &q);
830 ASSERT_EQ(e == 0, r <= 0);
831 ASSERT_EQ(p, q);
832 }
833 }
834
835 static void test_generic_array_bisect_one(size_t n, size_t num_corrupted) {
836 _cleanup_(test_donep) char *t = NULL;
837 _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
838 _cleanup_free_ uint64_t *seqnum = NULL, *offset = NULL, *offset_candidates = NULL;
839 _cleanup_(journal_file_offline_closep) JournalFile *f = NULL;
840
841 log_info("/* %s(%zu, %zu) */", __func__, n, num_corrupted);
842
843 ASSERT_NOT_NULL((m = mmap_cache_new()));
844
845 mkdtemp_chdir_chattr("/var/tmp/journal-seq-XXXXXX", &t);
846
847 ASSERT_OK(journal_file_open(-EBADF, "test.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644,
848 UINT64_MAX, NULL, m, NULL, &f));
849
850 ASSERT_NOT_NULL((seqnum = new0(uint64_t, n)));
851 ASSERT_NOT_NULL((offset = new0(uint64_t, n)));
852
853 for (size_t i = 0; i < n; i++) {
854 append_number(f, i, NULL, seqnum + i, offset + i);
855 ASSERT_GT(seqnum[i], i == 0 ? 0 : seqnum[i-1]);
856 ASSERT_GT(offset[i], i == 0 ? 0 : offset[i-1]);
857 }
858
859 ASSERT_NOT_NULL((offset_candidates = newdup(uint64_t, offset, n)));
860
861 verify(f, seqnum, offset_candidates, offset, n);
862
863 /* Reset chain cache. */
864 ASSERT_OK_POSITIVE(journal_file_move_to_entry_by_offset(f, offset[0], DIRECTION_DOWN, NULL, NULL));
865
866 /* make journal corrupted by clearing seqnum. */
867 for (size_t i = n - num_corrupted; i < n; i++) {
868 Object *o;
869
870 ASSERT_OK(journal_file_move_to_object(f, OBJECT_ENTRY, offset[i], &o));
871 ASSERT_NOT_NULL(o);
872 o->entry.seqnum = 0;
873 seqnum[i] = 0;
874 offset_candidates[i] = 0;
875 }
876
877 verify(f, seqnum, offset_candidates, offset, n);
878 }
879
880 TEST(generic_array_bisect) {
881 for (size_t n = 1; n < 10; n++)
882 for (size_t m = 1; m <= n; m++)
883 test_generic_array_bisect_one(n, m);
884
885 test_generic_array_bisect_one(100, 40);
886 }
887
888 typedef struct TestEntry {
889 uint64_t seqnum;
890 sd_id128_t seqnum_id;
891 sd_id128_t boot_id;
892 dual_timestamp ts;
893 unsigned number;
894 unsigned data;
895 } TestEntry;
896
897 static bool find_entry_monotonic_one(
898 const TestEntry *e,
899 bool next,
900 sd_id128_t boot_id,
901 usec_t usec,
902 unsigned data,
903 bool *boot_found) {
904
905 assert(e);
906 assert(boot_found);
907
908 if (sd_id128_equal(boot_id, e->boot_id))
909 *boot_found = true;
910
911 if (data != 0 && data != e->data)
912 return false;
913
914 if (sd_id128_equal(boot_id, e->boot_id))
915 return next ? usec <= e->ts.monotonic : usec >= e->ts.monotonic;
916
917 return *boot_found;
918 }
919
920 static size_t find_entry_monotonic(
921 const TestEntry *entries,
922 size_t n_entries,
923 bool next,
924 sd_id128_t boot_id,
925 usec_t usec,
926 unsigned data) {
927
928 bool boot_found = false;
929
930 assert(entries || n_entries == 0);
931
932 for (size_t i = 0; i < n_entries; i++) {
933 size_t j = next ? i : n_entries - i - 1;
934 const TestEntry *e = &entries[j];
935
936 if (find_entry_monotonic_one(e, next, boot_id, usec, data, &boot_found))
937 return j;
938 }
939
940 return SIZE_MAX;
941 }
942
943 static size_t find_entry_realtime(
944 const TestEntry *entries,
945 size_t n_entries,
946 bool next,
947 usec_t usec,
948 unsigned data) {
949
950 assert(entries || n_entries == 0);
951
952 for (size_t i = 0; i < n_entries; i++) {
953 size_t j = next ? i : n_entries - i - 1;
954 const TestEntry *e = &entries[j];
955
956 if (data != 0 && data != e->data)
957 continue;
958
959 if (next ? usec <= e->ts.realtime : usec >= e->ts.realtime)
960 return j;
961 }
962
963 return SIZE_MAX;
964 }
965
966 static size_t next_entry(
967 const TestEntry *entries,
968 size_t n_entries,
969 bool next,
970 size_t prev,
971 unsigned data) {
972
973 assert(entries || n_entries == 0);
974
975 if (next)
976 for (size_t i = prev + 1; i < n_entries; i++) {
977 const TestEntry *e = &entries[i];
978
979 if (data != 0 && data != e->data)
980 continue;
981
982 return i;
983 }
984 else
985 for (size_t i = prev; i > 0; i--) {
986 const TestEntry *e = &entries[i-1];
987
988 if (data != 0 && data != e->data)
989 continue;
990
991 return i-1;
992 }
993
994 return SIZE_MAX;
995 }
996
997 static void verify_entry(sd_journal *j, const TestEntry *entry) {
998 _cleanup_free_ char *s = NULL, *e = NULL;
999 sd_id128_t id;
1000 usec_t t;
1001 const void *d;
1002 size_t l;
1003
1004 assert(j);
1005 assert(entry);
1006
1007 ASSERT_OK(sd_journal_get_monotonic_usec(j, &t, &id));
1008 ASSERT_EQ_ID128(id, entry->boot_id);
1009 ASSERT_EQ(t, entry->ts.monotonic);
1010
1011 ASSERT_OK(sd_journal_get_realtime_usec(j, &t));
1012 ASSERT_EQ(t, entry->ts.realtime);
1013
1014 ASSERT_OK(sd_journal_get_data(j, "NUMBER", &d, &l));
1015 ASSERT_NOT_NULL((s = strndup(d, l)));
1016 ASSERT_OK(asprintf(&e, "NUMBER=%u", entry->number));
1017 ASSERT_STREQ(s, e);
1018
1019 s = mfree(s);
1020 e = mfree(e);
1021
1022 ASSERT_OK(sd_journal_get_data(j, "DATA", &d, &l));
1023 ASSERT_NOT_NULL((s = strndup(d, l)));
1024 ASSERT_OK(asprintf(&e, "DATA=%u", entry->data));
1025 ASSERT_STREQ(s, e);
1026 }
1027
1028 static void test_sd_journal_seek_monotonic_usec(
1029 sd_journal *j,
1030 const TestEntry *entries,
1031 size_t n_entries,
1032 bool next,
1033 sd_id128_t boot_id,
1034 usec_t usec,
1035 unsigned data) {
1036
1037 assert(j);
1038 assert(entries || n_entries == 0);
1039
1040 log_debug("/* %s(next=%s, boot_id=%s, usec="USEC_FMT") */",
1041 __func__, yes_no(next), SD_ID128_TO_STRING(boot_id), usec);
1042
1043 ASSERT_OK(sd_journal_seek_monotonic_usec(j, boot_id, usec));
1044
1045 for (size_t i = find_entry_monotonic(entries, n_entries, next, boot_id, usec, data);
1046 i != SIZE_MAX;
1047 i = next_entry(entries, n_entries, next, i, data)) {
1048
1049 if (next)
1050 ASSERT_OK_POSITIVE(sd_journal_next(j));
1051 else
1052 ASSERT_OK_POSITIVE(sd_journal_previous(j));
1053
1054 verify_entry(j, &entries[i]);
1055 }
1056
1057 if (next)
1058 ASSERT_OK_ZERO(sd_journal_next(j));
1059 else
1060 ASSERT_OK_ZERO(sd_journal_previous(j));
1061 }
1062
1063 static void test_sd_journal_seek_realtime_usec(
1064 sd_journal *j,
1065 const TestEntry *entries,
1066 size_t n_entries,
1067 bool next,
1068 usec_t usec,
1069 unsigned data) {
1070
1071 assert(j);
1072 assert(entries || n_entries == 0);
1073
1074 log_debug("/* %s(next=%s, usec="USEC_FMT") */",
1075 __func__, yes_no(next), usec);
1076
1077 ASSERT_OK(sd_journal_seek_realtime_usec(j, usec));
1078
1079 for (size_t i = find_entry_realtime(entries, n_entries, next, usec, data);
1080 i != SIZE_MAX;
1081 i = next_entry(entries, n_entries, next, i, data)) {
1082
1083 if (next)
1084 ASSERT_OK_POSITIVE(sd_journal_next(j));
1085 else
1086 ASSERT_OK_POSITIVE(sd_journal_previous(j));
1087
1088 verify_entry(j, &entries[i]);
1089 }
1090
1091 if (next)
1092 ASSERT_OK_ZERO(sd_journal_next(j));
1093 else
1094 ASSERT_OK_ZERO(sd_journal_previous(j));
1095 }
1096
1097 static void append_test_entry_full(
1098 JournalFile **f,
1099 MMapCache *m,
1100 TestEntry **entries,
1101 size_t *n_entries,
1102 uint64_t *seqnum,
1103 sd_id128_t *seqnum_id,
1104 const sd_id128_t *boot_id,
1105 const dual_timestamp *ts,
1106 unsigned *number,
1107 unsigned data,
1108 bool expect_rotate) {
1109
1110 struct iovec iovec[3];
1111 size_t n_iovec = 0;
1112
1113 assert(f);
1114 assert(*f);
1115 assert(entries);
1116 assert(n_entries);
1117 assert(*entries || *n_entries == 0);
1118 assert(seqnum);
1119 assert(seqnum_id);
1120 assert(boot_id);
1121 assert(ts);
1122 assert(number);
1123
1124 (*number)++;
1125
1126 const char *q = strjoina("_BOOT_ID=", SD_ID128_TO_STRING(*boot_id));
1127 iovec[n_iovec++] = IOVEC_MAKE_STRING(q);
1128
1129 _cleanup_free_ char *n = NULL;
1130 ASSERT_OK(asprintf(&n, "NUMBER=%u", *number));
1131 iovec[n_iovec++] = IOVEC_MAKE_STRING(n);
1132
1133 _cleanup_free_ char *d = NULL;
1134 ASSERT_OK(asprintf(&d, "DATA=%u", data));
1135 iovec[n_iovec++] = IOVEC_MAKE_STRING(d);
1136
1137 if (expect_rotate) {
1138 ASSERT_ERROR(journal_file_append_entry(
1139 *f,
1140 ts,
1141 boot_id,
1142 iovec, n_iovec,
1143 seqnum,
1144 seqnum_id,
1145 /* ret_object= */ NULL,
1146 /* ret_offset= */ NULL), EREMCHG);
1147
1148 ASSERT_OK(journal_file_rotate(
1149 f,
1150 m,
1151 /* file_flags= */ JOURNAL_STRICT_ORDER,
1152 /* compress_threshold_bytes= */ UINT64_MAX,
1153 /* deferred_closes= */ NULL));
1154 }
1155
1156 ASSERT_OK(journal_file_append_entry(
1157 *f,
1158 ts,
1159 boot_id,
1160 iovec, n_iovec,
1161 seqnum,
1162 seqnum_id,
1163 /* ret_object= */ NULL,
1164 /* ret_offset= */ NULL));
1165
1166 ASSERT_NOT_NULL(GREEDY_REALLOC(*entries, *n_entries + 1));
1167 (*entries)[(*n_entries)++] = (TestEntry) {
1168 .seqnum = *seqnum,
1169 .seqnum_id = *seqnum_id,
1170 .boot_id = *boot_id,
1171 .ts = *ts,
1172 .number = *number,
1173 .data = data,
1174 };
1175 }
1176
1177 static void append_test_entry(
1178 JournalFile *f,
1179 TestEntry **entries,
1180 size_t *n_entries,
1181 uint64_t *seqnum,
1182 sd_id128_t *seqnum_id,
1183 const sd_id128_t *boot_id,
1184 const dual_timestamp *ts,
1185 unsigned *number,
1186 unsigned data) {
1187
1188 append_test_entry_full(&f, NULL, entries, n_entries, seqnum, seqnum_id, boot_id, ts, number, data, /* expect_rotate= */ false);
1189 }
1190
1191 TEST(seek_time) {
1192 _cleanup_(test_donep) char *t = NULL;
1193 _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
1194 _cleanup_free_ TestEntry *entries = NULL;
1195 size_t n_entries = 0;
1196 JournalFile *f;
1197
1198 mkdtemp_chdir_chattr("/var/tmp/journal-seek-time-XXXXXX", &t);
1199
1200 ASSERT_NOT_NULL((m = mmap_cache_new()));
1201
1202 ASSERT_OK(journal_file_open(
1203 -EBADF,
1204 "test.journal",
1205 O_RDWR|O_CREAT,
1206 JOURNAL_STRICT_ORDER,
1207 0644,
1208 /* compress_threshold_bytes= */ UINT64_MAX,
1209 /* metrics= */ NULL,
1210 m,
1211 /* template= */ NULL,
1212 &f));
1213
1214 uint64_t seqnum = 1;
1215 sd_id128_t seqnum_id, boot_id;
1216 ASSERT_OK(sd_id128_randomize(&seqnum_id));
1217 ASSERT_OK(sd_id128_randomize(&boot_id));
1218
1219 dual_timestamp base, ts;
1220 dual_timestamp_now(&base);
1221
1222 unsigned n = 0;
1223
1224 ts = base;
1225 append_test_entry(f, &entries, &n_entries, &seqnum, &seqnum_id, &boot_id, &ts, &n, 100);
1226
1227 ts.realtime += 10;
1228 ts.monotonic += 10;
1229 append_test_entry(f, &entries, &n_entries, &seqnum, &seqnum_id, &boot_id, &ts, &n, 100);
1230
1231 ts.realtime += 10;
1232 ts.monotonic += 10;
1233 append_test_entry(f, &entries, &n_entries, &seqnum, &seqnum_id, &boot_id, &ts, &n, 200);
1234
1235 /* realtime goes to backward */
1236 ts.realtime -= 100;
1237 ts.monotonic += 10;
1238 append_test_entry_full(&f, m, &entries, &n_entries, &seqnum, &seqnum_id, &boot_id, &ts, &n, 200, /* expect_rotate= */ true);
1239
1240 ts.realtime += 10;
1241 ts.monotonic += 10;
1242 append_test_entry(f, &entries, &n_entries, &seqnum, &seqnum_id, &boot_id, &ts, &n, 200);
1243
1244 ts.realtime += 10;
1245 ts.monotonic += 10;
1246 append_test_entry(f, &entries, &n_entries, &seqnum, &seqnum_id, &boot_id, &ts, &n, 100);
1247
1248 /* realtime goes to forward */
1249 ts.realtime += 100;
1250 ts.monotonic += 10;
1251 append_test_entry(f, &entries, &n_entries, &seqnum, &seqnum_id, &boot_id, &ts, &n, 100);
1252
1253 ts.realtime += 10;
1254 ts.monotonic += 10;
1255 append_test_entry(f, &entries, &n_entries, &seqnum, &seqnum_id, &boot_id, &ts, &n, 200);
1256
1257 ts.realtime += 10;
1258 ts.monotonic += 10;
1259 append_test_entry(f, &entries, &n_entries, &seqnum, &seqnum_id, &boot_id, &ts, &n, 100);
1260
1261 /* reboot */
1262 ASSERT_OK(sd_id128_randomize(&boot_id));
1263 ts.realtime += 10;
1264 ts.monotonic -= 1000;
1265 append_test_entry(f, &entries, &n_entries, &seqnum, &seqnum_id, &boot_id, &ts, &n, 100);
1266
1267 ts.realtime += 10;
1268 ts.monotonic += 10;
1269 append_test_entry(f, &entries, &n_entries, &seqnum, &seqnum_id, &boot_id, &ts, &n, 100);
1270
1271 ts.realtime += 10;
1272 ts.monotonic += 10;
1273 append_test_entry(f, &entries, &n_entries, &seqnum, &seqnum_id, &boot_id, &ts, &n, 200);
1274
1275 ts.realtime += 10;
1276 ts.monotonic += 10;
1277 append_test_entry(f, &entries, &n_entries, &seqnum, &seqnum_id, &boot_id, &ts, &n, 200);
1278
1279 /* reboot */
1280 ASSERT_OK(sd_id128_randomize(&boot_id));
1281 ts.realtime += 10;
1282 ts.monotonic -= 2000;
1283 append_test_entry(f, &entries, &n_entries, &seqnum, &seqnum_id, &boot_id, &ts, &n, 100);
1284
1285 journal_file_offline_close(f);
1286
1287 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
1288 ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
1289
1290 log_debug("Testing sequential read");
1291 ASSERT_OK(sd_journal_seek_head(j));
1292 ASSERT_OK_POSITIVE(sd_journal_next(j));
1293 test_check_numbers_down(j, n);
1294
1295 ASSERT_OK(sd_journal_seek_tail(j));
1296 ASSERT_OK_POSITIVE(sd_journal_previous(j));
1297 test_check_numbers_up(j, n);
1298
1299 unsigned data;
1300 FOREACH_ARGUMENT(data, 0, 100, 200, 300) {
1301
1302 sd_journal_flush_matches(j);
1303
1304 if (data == 0)
1305 log_info("no match");
1306 else {
1307 log_info("match: DATA=%u", data);
1308 _cleanup_free_ char *match_str = NULL;
1309 ASSERT_OK(asprintf(&match_str, "DATA=%u", data));
1310 ASSERT_OK(sd_journal_add_match(j, match_str, SIZE_MAX));
1311 }
1312
1313 FOREACH_ARRAY(e, entries, n_entries) {
1314 test_sd_journal_seek_monotonic_usec(j, entries, n_entries, /* next= */ true, e->boot_id, e->ts.monotonic - 1, data);
1315 test_sd_journal_seek_monotonic_usec(j, entries, n_entries, /* next= */ true, e->boot_id, e->ts.monotonic, data);
1316 test_sd_journal_seek_monotonic_usec(j, entries, n_entries, /* next= */ true, e->boot_id, e->ts.monotonic + 1, data);
1317 test_sd_journal_seek_monotonic_usec(j, entries, n_entries, /* next= */ false, e->boot_id, e->ts.monotonic - 1, data);
1318 test_sd_journal_seek_monotonic_usec(j, entries, n_entries, /* next= */ false, e->boot_id, e->ts.monotonic, data);
1319 test_sd_journal_seek_monotonic_usec(j, entries, n_entries, /* next= */ false, e->boot_id, e->ts.monotonic + 1, data);
1320
1321 test_sd_journal_seek_realtime_usec(j, entries, n_entries, /* next= */ true, e->ts.monotonic - 1, data);
1322 test_sd_journal_seek_realtime_usec(j, entries, n_entries, /* next= */ true, e->ts.monotonic, data);
1323 test_sd_journal_seek_realtime_usec(j, entries, n_entries, /* next= */ true, e->ts.monotonic + 1, data);
1324 test_sd_journal_seek_realtime_usec(j, entries, n_entries, /* next= */ false, e->ts.monotonic - 1, data);
1325 test_sd_journal_seek_realtime_usec(j, entries, n_entries, /* next= */ false, e->ts.monotonic, data);
1326 test_sd_journal_seek_realtime_usec(j, entries, n_entries, /* next= */ false, e->ts.monotonic + 1, data);
1327 }
1328 }
1329 }
1330
1331 static int intro(void) {
1332 /* journal_file_open() requires a valid machine id */
1333 if (access("/etc/machine-id", F_OK) != 0)
1334 return log_tests_skipped("/etc/machine-id not found");
1335
1336 arg_keep = saved_argc > 1;
1337
1338 return EXIT_SUCCESS;
1339 }
1340
1341 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);