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