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