]>
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" |
07630cea | 12 | #include "macro.h" |
b910cc72 | 13 | #include "path-util.h" |
951174e4 | 14 | #include "rm-rf.h" |
07630cea | 15 | #include "string-util.h" |
951174e4 | 16 | #include "tmpfile-util.h" |
d0767ffd | 17 | |
c92f1ebe | 18 | static void test_journal_flush(int argc, char *argv[]) { |
74fb5be6 | 19 | _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL; |
3e348b8a | 20 | _cleanup_free_ char *fn = NULL; |
951174e4 | 21 | _cleanup_(rm_rf_physical_and_freep) char *dn = NULL; |
45c0ecba | 22 | JournalFile *new_journal = NULL; |
d0767ffd LP |
23 | sd_journal *j = NULL; |
24 | unsigned n = 0; | |
25 | int r; | |
26 | ||
951174e4 LP |
27 | assert_se(m = mmap_cache_new()); |
28 | assert_se(mkdtemp_malloc("/var/tmp/test-journal-flush.XXXXXX", &dn) >= 0); | |
949082ac LP |
29 | (void) chattr_path(dn, FS_NOCOW_FL, FS_NOCOW_FL, NULL); |
30 | ||
951174e4 | 31 | assert_se(fn = path_join(dn, "test.journal")); |
d0767ffd | 32 | |
45c0ecba | 33 | r = journal_file_open(-1, fn, O_CREAT|O_RDWR, 0, 0644, 0, NULL, m, NULL, &new_journal); |
d0767ffd LP |
34 | assert_se(r >= 0); |
35 | ||
0fa167cd ZJS |
36 | if (argc > 1) |
37 | r = sd_journal_open_files(&j, (const char **) strv_skip(argv, 1), 0); | |
38 | else | |
39 | r = sd_journal_open(&j, 0); | |
40 | assert_se(r == 0); | |
d0767ffd LP |
41 | |
42 | sd_journal_set_data_threshold(j, 0); | |
43 | ||
44 | SD_JOURNAL_FOREACH(j) { | |
45 | Object *o; | |
46 | JournalFile *f; | |
47 | ||
48 | f = j->current_file; | |
0c0cdb06 | 49 | assert_se(f && f->current_offset > 0); |
d0767ffd LP |
50 | |
51 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); | |
a83577fa ZJS |
52 | if (r < 0) |
53 | log_error_errno(r, "journal_file_move_to_object failed: %m"); | |
0c0cdb06 | 54 | assert_se(r >= 0); |
d0767ffd | 55 | |
45c0ecba | 56 | r = journal_file_copy_entry(f, new_journal, o, f->current_offset, NULL, NULL); |
a83577fa | 57 | if (r < 0) |
b4046d55 ZJS |
58 | log_warning_errno(r, "journal_file_copy_entry failed: %m"); |
59 | assert_se(r >= 0 || | |
60 | IN_SET(r, -EBADMSG, /* corrupted file */ | |
61 | -EPROTONOSUPPORT, /* unsupported compression */ | |
9cd80d8a | 62 | -EIO, /* file rotated */ |
63 | -EREMCHG)); /* clock rollback */ | |
d0767ffd | 64 | |
a83577fa | 65 | if (++n >= 10000) |
d0767ffd LP |
66 | break; |
67 | } | |
68 | ||
69 | sd_journal_close(j); | |
70 | ||
45c0ecba | 71 | (void) journal_file_offline_close(new_journal); |
c92f1ebe DDM |
72 | } |
73 | ||
74 | int main(int argc, char *argv[]) { | |
75 | assert_se(setenv("SYSTEMD_JOURNAL_COMPACT", "0", 1) >= 0); | |
76 | test_journal_flush(argc, argv); | |
77 | ||
78 | assert_se(setenv("SYSTEMD_JOURNAL_COMPACT", "1", 1) >= 0); | |
79 | test_journal_flush(argc, argv); | |
dfd9cf7f | 80 | |
d0767ffd LP |
81 | return 0; |
82 | } |