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