]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/test-journal-interleaving.c
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / journal / test-journal-interleaving.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
7a050b54
MV
2/***
3 This file is part of systemd.
4
5 Copyright 2013 Marius Vollmer
5cb24cd3 6 Copyright 2013 Zbigniew Jędrzejewski-Szmek
7a050b54
MV
7***/
8
7a050b54 9#include <fcntl.h>
cf0fbc49 10#include <unistd.h>
7a050b54 11
c6878637 12#include "sd-journal.h"
6bedfcbb 13
b5efdb8a 14#include "alloc-util.h"
7a050b54 15#include "journal-file.h"
5cb24cd3 16#include "journal-vacuum.h"
7a050b54 17#include "log.h"
6bedfcbb 18#include "parse-util.h"
c6878637 19#include "rm-rf.h"
6bedfcbb 20#include "util.h"
7a050b54
MV
21
22/* This program tests skipping around in a multi-file journal.
23 */
24
5cb24cd3
ZJS
25static bool arg_keep = false;
26
848e863a 27_noreturn_ static void log_assert_errno(const char *text, int error, const char *file, int line, const char *func) {
9eec7d12
ZJS
28 log_internal(LOG_CRIT, error, file, line, func,
29 "'%s' failed at %s:%u (%s): %m", text, file, line, func);
7a050b54
MV
30 abort();
31}
32
33#define assert_ret(expr) \
34 do { \
35 int _r_ = (expr); \
36 if (_unlikely_(_r_ < 0)) \
37 log_assert_errno(#expr, -_r_, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
38 } while (false)
39
510b857f 40static JournalFile *test_open(const char *name) {
7a050b54 41 JournalFile *f;
57850536 42 assert_ret(journal_file_open(-1, name, O_RDWR|O_CREAT, 0644, true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &f));
7a050b54
MV
43 return f;
44}
45
510b857f 46static void test_close(JournalFile *f) {
69a3a6fd 47 (void) journal_file_close (f);
7a050b54
MV
48}
49
510b857f 50static void append_number(JournalFile *f, int n, uint64_t *seqnum) {
7a050b54
MV
51 char *p;
52 dual_timestamp ts;
44cf96e3 53 static dual_timestamp previous_ts = {};
7a050b54
MV
54 struct iovec iovec[1];
55
56 dual_timestamp_get(&ts);
57
44cf96e3
LP
58 if (ts.monotonic <= previous_ts.monotonic)
59 ts.monotonic = previous_ts.monotonic + 1;
60
61 if (ts.realtime <= previous_ts.realtime)
62 ts.realtime = previous_ts.realtime + 1;
63
64 previous_ts = ts;
65
7a050b54
MV
66 assert_se(asprintf(&p, "NUMBER=%d", n) >= 0);
67 iovec[0].iov_base = p;
68 iovec[0].iov_len = strlen(p);
5cb24cd3 69 assert_ret(journal_file_append_entry(f, &ts, iovec, 1, seqnum, NULL, NULL));
510b857f 70 free(p);
7a050b54
MV
71}
72
510b857f 73static void test_check_number (sd_journal *j, int n) {
7a050b54 74 const void *d;
510b857f 75 _cleanup_free_ char *k;
7a050b54
MV
76 size_t l;
77 int x;
78
79 assert_ret(sd_journal_get_data(j, "NUMBER", &d, &l));
80 assert_se(k = strndup(d, l));
81 printf("%s\n", k);
82
83 assert_se(safe_atoi(k + 7, &x) >= 0);
84 assert_se(n == x);
85}
86
510b857f
LP
87static void test_check_numbers_down (sd_journal *j, int count) {
88 int i;
89
90 for (i = 1; i <= count; i++) {
7a050b54
MV
91 int r;
92 test_check_number(j, i);
93 assert_ret(r = sd_journal_next(j));
94 if (i == count)
95 assert_se(r == 0);
96 else
97 assert_se(r == 1);
98 }
99
100}
101
510b857f 102static void test_check_numbers_up (sd_journal *j, int count) {
7a050b54
MV
103 for (int i = count; i >= 1; i--) {
104 int r;
105 test_check_number(j, i);
106 assert_ret(r = sd_journal_previous(j));
107 if (i == 1)
108 assert_se(r == 0);
109 else
110 assert_se(r == 1);
111 }
112
113}
114
115static void setup_sequential(void) {
116 JournalFile *one, *two;
117 one = test_open("one.journal");
118 two = test_open("two.journal");
5cb24cd3
ZJS
119 append_number(one, 1, NULL);
120 append_number(one, 2, NULL);
121 append_number(two, 3, NULL);
122 append_number(two, 4, NULL);
7a050b54
MV
123 test_close(one);
124 test_close(two);
125}
126
127static void setup_interleaved(void) {
128 JournalFile *one, *two;
129 one = test_open("one.journal");
130 two = test_open("two.journal");
5cb24cd3
ZJS
131 append_number(one, 1, NULL);
132 append_number(two, 2, NULL);
133 append_number(one, 3, NULL);
134 append_number(two, 4, NULL);
7a050b54
MV
135 test_close(one);
136 test_close(two);
137}
138
510b857f 139static void test_skip(void (*setup)(void)) {
7a050b54
MV
140 char t[] = "/tmp/journal-skip-XXXXXX";
141 sd_journal *j;
142 int r;
143
7a050b54
MV
144 assert_se(mkdtemp(t));
145 assert_se(chdir(t) >= 0);
146
147 setup();
148
149 /* Seek to head, iterate down.
150 */
151 assert_ret(sd_journal_open_directory(&j, t, 0));
152 assert_ret(sd_journal_seek_head(j));
153 assert_ret(sd_journal_next(j));
154 test_check_numbers_down(j, 4);
155 sd_journal_close(j);
156
157 /* Seek to tail, iterate up.
158 */
159 assert_ret(sd_journal_open_directory(&j, t, 0));
160 assert_ret(sd_journal_seek_tail(j));
161 assert_ret(sd_journal_previous(j));
162 test_check_numbers_up(j, 4);
163 sd_journal_close(j);
164
165 /* Seek to tail, skip to head, iterate down.
166 */
167 assert_ret(sd_journal_open_directory(&j, t, 0));
168 assert_ret(sd_journal_seek_tail(j));
169 assert_ret(r = sd_journal_previous_skip(j, 4));
170 assert_se(r == 4);
171 test_check_numbers_down(j, 4);
172 sd_journal_close(j);
173
174 /* Seek to head, skip to tail, iterate up.
175 */
176 assert_ret(sd_journal_open_directory(&j, t, 0));
177 assert_ret(sd_journal_seek_head(j));
178 assert_ret(r = sd_journal_next_skip(j, 4));
179 assert_se(r == 4);
180 test_check_numbers_up(j, 4);
181 sd_journal_close(j);
182
5cb24cd3
ZJS
183 log_info("Done...");
184
185 if (arg_keep)
186 log_info("Not removing %s", t);
187 else {
8580d1f7 188 journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
5cb24cd3 189
c6878637 190 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
5cb24cd3
ZJS
191 }
192
193 puts("------------------------------------------------------------");
194}
195
196static void test_sequence_numbers(void) {
197
198 char t[] = "/tmp/journal-seq-XXXXXX";
199 JournalFile *one, *two;
200 uint64_t seqnum = 0;
201 sd_id128_t seqnum_id;
202
203 assert_se(mkdtemp(t));
204 assert_se(chdir(t) >= 0);
205
5d1ce257 206 assert_se(journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, 0644,
57850536 207 true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &one) == 0);
5cb24cd3
ZJS
208
209 append_number(one, 1, &seqnum);
210 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 211 assert_se(seqnum == 1);
5cb24cd3
ZJS
212 append_number(one, 2, &seqnum);
213 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 214 assert_se(seqnum == 2);
5cb24cd3 215
0c0cdb06
RC
216 assert_se(one->header->state == STATE_ONLINE);
217 assert_se(!sd_id128_equal(one->header->file_id, one->header->machine_id));
218 assert_se(!sd_id128_equal(one->header->file_id, one->header->boot_id));
219 assert_se(sd_id128_equal(one->header->file_id, one->header->seqnum_id));
5cb24cd3
ZJS
220
221 memcpy(&seqnum_id, &one->header->seqnum_id, sizeof(sd_id128_t));
222
5d1ce257 223 assert_se(journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, 0644,
57850536 224 true, (uint64_t) -1, false, NULL, NULL, NULL, one, &two) == 0);
5cb24cd3 225
0c0cdb06
RC
226 assert_se(two->header->state == STATE_ONLINE);
227 assert_se(!sd_id128_equal(two->header->file_id, one->header->file_id));
228 assert_se(sd_id128_equal(one->header->machine_id, one->header->machine_id));
229 assert_se(sd_id128_equal(one->header->boot_id, one->header->boot_id));
230 assert_se(sd_id128_equal(one->header->seqnum_id, one->header->seqnum_id));
5cb24cd3
ZJS
231
232 append_number(two, 3, &seqnum);
233 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 234 assert_se(seqnum == 3);
5cb24cd3
ZJS
235 append_number(two, 4, &seqnum);
236 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 237 assert_se(seqnum == 4);
5cb24cd3
ZJS
238
239 test_close(two);
240
241 append_number(one, 5, &seqnum);
242 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 243 assert_se(seqnum == 5);
5cb24cd3
ZJS
244
245 append_number(one, 6, &seqnum);
246 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 247 assert_se(seqnum == 6);
5cb24cd3
ZJS
248
249 test_close(one);
250
251 /* restart server */
252 seqnum = 0;
253
5d1ce257 254 assert_se(journal_file_open(-1, "two.journal", O_RDWR, 0,
57850536 255 true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &two) == 0);
5cb24cd3 256
0c0cdb06 257 assert_se(sd_id128_equal(two->header->seqnum_id, seqnum_id));
5cb24cd3
ZJS
258
259 append_number(two, 7, &seqnum);
260 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 261 assert_se(seqnum == 5);
5cb24cd3
ZJS
262
263 /* So..., here we have the same seqnum in two files with the
264 * same seqnum_id. */
265
266 test_close(two);
267
268 log_info("Done...");
269
270 if (arg_keep)
271 log_info("Not removing %s", t);
272 else {
8580d1f7 273 journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
5cb24cd3 274
c6878637 275 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
5cb24cd3 276 }
7a050b54
MV
277}
278
279int main(int argc, char *argv[]) {
5cb24cd3
ZJS
280 log_set_max_level(LOG_DEBUG);
281
143bfdaf
HHPF
282 /* journal_file_open requires a valid machine id */
283 if (access("/etc/machine-id", F_OK) != 0)
284 return EXIT_TEST_SKIP;
285
5cb24cd3
ZJS
286 arg_keep = argc > 1;
287
7a050b54
MV
288 test_skip(setup_sequential);
289 test_skip(setup_interleaved);
290
5cb24cd3
ZJS
291 test_sequence_numbers();
292
7a050b54
MV
293 return 0;
294}