]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/test-journal-flush.c
journald: maintain entry seqnum counter in mmap()ed file in /run/
[thirdparty/systemd.git] / src / journal / test-journal-flush.c
CommitLineData
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"
951174e4 14#include "rm-rf.h"
07630cea 15#include "string-util.h"
951174e4 16#include "tmpfile-util.h"
d0767ffd 17
c92f1ebe 18static 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;
d02af6f3 22 ManagedJournalFile *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
49615dbd 33 r = managed_journal_file_open(-1, fn, O_CREAT|O_RDWR, 0, 0644, 0, NULL, m, NULL, 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
e5d60d1b 56 r = journal_file_copy_entry(f, new_journal->file, 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
d02af6f3 71 (void) managed_journal_file_close(new_journal);
c92f1ebe
DDM
72}
73
74int 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}