1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "sd-journal.h"
9 #include "alloc-util.h"
10 #include "chattr-util.h"
12 #include "journal-vacuum.h"
14 #include "logs-show.h"
15 #include "managed-journal-file.h"
16 #include "parse-util.h"
20 /* This program tests skipping around in a multi-file journal. */
22 static bool arg_keep
= false;
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
);
30 #define assert_ret(expr) \
33 if (_unlikely_(_r_ < 0)) \
34 log_assert_errno(#expr, -_r_, PROJECT_FILE, __LINE__, __func__); \
37 static ManagedJournalFile
*test_open(const char *name
) {
38 _cleanup_(mmap_cache_unrefp
) MMapCache
*m
= NULL
;
39 ManagedJournalFile
*f
;
44 assert_ret(managed_journal_file_open(-1, name
, O_RDWR
|O_CREAT
, JOURNAL_COMPRESS
, 0644, UINT64_MAX
, NULL
, m
, NULL
, NULL
, &f
));
48 static void test_close(ManagedJournalFile
*f
) {
49 (void) managed_journal_file_close(f
);
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
;
55 static dual_timestamp previous_ts
= {};
56 struct iovec iovec
[2];
59 dual_timestamp_get(&ts
);
61 if (ts
.monotonic
<= previous_ts
.monotonic
)
62 ts
.monotonic
= previous_ts
.monotonic
+ 1;
64 if (ts
.realtime
<= previous_ts
.realtime
)
65 ts
.realtime
= previous_ts
.realtime
+ 1;
69 assert_se(asprintf(&p
, "NUMBER=%d", n
) >= 0);
70 iovec
[n_iov
++] = IOVEC_MAKE_STRING(p
);
73 assert_se(q
= strjoin("_BOOT_ID=", SD_ID128_TO_STRING(*boot_id
)));
74 iovec
[n_iov
++] = IOVEC_MAKE_STRING(q
);
77 assert_ret(journal_file_append_entry(f
->file
, &ts
, boot_id
, iovec
, n_iov
, seqnum
, NULL
, NULL
, NULL
));
80 static void test_check_number(sd_journal
*j
, int n
) {
83 _cleanup_free_
char *k
= NULL
;
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
);
92 assert_se(safe_atoi(k
+ STRLEN("NUMBER="), &x
) >= 0);
96 static void test_check_numbers_down(sd_journal
*j
, int count
) {
99 for (i
= 1; i
<= count
; i
++) {
101 test_check_number(j
, i
);
102 assert_ret(r
= sd_journal_next(j
));
111 static void test_check_numbers_up(sd_journal
*j
, int count
) {
112 for (int i
= count
; i
>= 1; i
--) {
114 test_check_number(j
, i
);
115 assert_ret(r
= sd_journal_previous(j
));
124 static void setup_sequential(void) {
125 ManagedJournalFile
*f1
, *f2
, *f3
;
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
);
151 static void setup_interleaved(void) {
152 ManagedJournalFile
*f1
, *f2
, *f3
;
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
);
174 static void mkdtemp_chdir_chattr(char *path
) {
175 assert_se(mkdtemp(path
));
176 assert_se(chdir(path
) >= 0);
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
);
183 static void test_skip_one(void (*setup
)(void)) {
184 char t
[] = "/var/tmp/journal-skip-XXXXXX";
188 mkdtemp_chdir_chattr(t
);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
327 log_info("Not removing %s", t
);
329 journal_directory_vacuum(".", 3000000, 0, 0, NULL
, true);
331 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);
334 puts("------------------------------------------------------------");
338 test_skip_one(setup_sequential
);
339 test_skip_one(setup_interleaved
);
342 static void test_boot_id_one(void (*setup
)(void), size_t n_boots_expected
) {
343 char t
[] = "/var/tmp/journal-boot-id-XXXXXX";
345 _cleanup_free_ BootId
*boots
= NULL
;
348 mkdtemp_chdir_chattr(t
);
352 assert_ret(sd_journal_open_directory(&j
, t
, 0));
353 assert_se(journal_get_boots(j
, &boots
, &n_boots
) >= 0);
355 assert_se(n_boots
== n_boots_expected
);
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);
364 for (int i
= - (int) n_boots
+ 1; i
<= (int) n_boots
; i
++) {
367 assert_ret(sd_journal_open_directory(&j
, t
, 0));
368 assert_se(journal_find_boot_by_offset(j
, i
, &id
) == 1);
370 assert_se(sd_id128_equal(id
, boots
[n_boots
+ i
- 1].id
));
372 assert_se(sd_id128_equal(id
, boots
[i
- 1].id
));
379 log_info("Not removing %s", t
);
381 journal_directory_vacuum(".", 3000000, 0, 0, NULL
, true);
383 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);
386 puts("------------------------------------------------------------");
390 test_boot_id_one(setup_sequential
, 3);
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
;
398 sd_id128_t seqnum_id
;
400 m
= mmap_cache_new();
401 assert_se(m
!= NULL
);
403 mkdtemp_chdir_chattr(t
);
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);
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);
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
));
420 memcpy(&seqnum_id
, &one
->file
->header
->seqnum_id
, sizeof(sd_id128_t
));
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);
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
));
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);
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
));
443 append_number(one
, 5, NULL
, &seqnum
);
444 printf("seqnum=%"PRIu64
"\n", seqnum
);
445 assert_se(seqnum
== 5);
447 append_number(one
, 6, NULL
, &seqnum
);
448 printf("seqnum=%"PRIu64
"\n", seqnum
);
449 assert_se(seqnum
== 6);
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) {
459 assert_se(managed_journal_file_open(-1, "two.journal", O_RDWR
, JOURNAL_COMPRESS
, 0,
460 UINT64_MAX
, NULL
, m
, NULL
, NULL
, &two
) == 0);
462 assert_se(sd_id128_equal(two
->file
->header
->seqnum_id
, seqnum_id
));
464 append_number(two
, 7, NULL
, &seqnum
);
465 printf("seqnum=%"PRIu64
"\n", seqnum
);
466 assert_se(seqnum
== 5);
468 /* So..., here we have the same seqnum in two files with the
477 log_info("Not removing %s", t
);
479 journal_directory_vacuum(".", 3000000, 0, 0, NULL
, true);
481 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);
485 TEST(sequence_numbers
) {
486 assert_se(setenv("SYSTEMD_JOURNAL_COMPACT", "0", 1) >= 0);
487 test_sequence_numbers_one();
489 assert_se(setenv("SYSTEMD_JOURNAL_COMPACT", "1", 1) >= 0);
490 test_sequence_numbers_one();
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");
498 arg_keep
= saved_argc
> 1;
503 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG
, intro
);