Debian Codegraph @

src/libsystemd/sd-journal/test-journal-append.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <unistd.h>
6
7 #include "chattr-util.h"
8 #include "format-table.h"
9 #include "iovec-util.h"
10 #include "journal-file-util.h"
11 #include "log.h"
12 #include "mmap-cache.h"
13 #include "options.h"
14 #include "parse-util.h"
15 #include "random-util.h"
16 #include "rm-rf.h"
17 #include "tests.h"
18 #include "time-util.h"
19 #include "tmpfile-util.h"
20
21 static int journal_append_message(JournalFile *mj, const char *message) {
22 struct iovec iovec;
23 struct dual_timestamp ts;
24
25 assert(mj);
26 assert(message);
27
28 dual_timestamp_now(&ts);
29 iovec = IOVEC_MAKE_STRING(message);
30 return journal_file_append_entry(
31 mj,
32 &ts,
33 /* boot_id= */ NULL,
34 &iovec,
35 /* n_iovec= */ 1,
36 /* seqnum= */ NULL,
37 /* seqnum_id= */ NULL,
38 /* ret_object= */ NULL,
39 /* ret_offset= */ NULL);
40 }
41
42 static int journal_corrupt_and_append(uint64_t start_offset, uint64_t step) {
43 _cleanup_(mmap_cache_unrefp) MMapCache *mmap_cache = NULL;
44 _cleanup_(rm_rf_physical_and_freep) char *tempdir = NULL;
45 _cleanup_(journal_file_offline_closep) JournalFile *mj = NULL;
46 uint64_t start, end;
47 int r;
48
49 ASSERT_NOT_NULL(mmap_cache = mmap_cache_new());
50
51 /* journal_file_open() requires a valid machine id */
52 if (sd_id128_get_machine(NULL) < 0)
53 return log_tests_skipped("No valid machine ID found");
54
55 ASSERT_OK(mkdtemp_malloc("/tmp/journal-append-XXXXXX", &tempdir));
56 ASSERT_OK_ERRNO(chdir(tempdir));
57 (void) chattr_path(tempdir, FS_NOCOW_FL, FS_NOCOW_FL);
58
59 log_debug("Opening journal %s/system.journal", tempdir);
60
61 r = journal_file_open(
62 /* fd= */ -EBADF,
63 "system.journal",
64 O_RDWR|O_CREAT,
65 JOURNAL_COMPRESS,
66 0644,
67 /* compress_threshold_bytes= */ UINT64_MAX,
68 /* metrics= */ NULL,
69 mmap_cache,
70 /* template= */ NULL,
71 &mj);
72 if (r < 0)
73 return log_error_errno(r, "Failed to open the journal: %m");
74
75 ASSERT_NOT_NULL(mj);
76
77 /* Add a couple of initial messages */
78 for (int i = 0; i < 10; i++) {
79 _cleanup_free_ char *message = NULL;
80
81 ASSERT_OK_ERRNO(asprintf(&message, "MESSAGE=Initial message %d", i));
82 r = journal_append_message(mj, message);
83 if (r < 0)
84 return log_error_errno(r, "Failed to write to the journal: %m");
85 }
86
87 start = start_offset == UINT64_MAX ? random_u64() % mj->last_stat.st_size : start_offset;
88 end = (uint64_t) mj->last_stat.st_size;
89
90 /* Print the initial offset at which we start flipping bits, which can be
91 * later used to reproduce a potential fail */
92 log_info("Start offset: %" PRIu64 ", corrupt-step: %" PRIu64, start, step);
93 fflush(stdout);
94
95 if (start >= end)
96 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
97 "Start offset >= journal size, can't continue");
98
99 for (uint64_t offset = start; offset < end; offset += step) {
100 _cleanup_free_ char *message = NULL;
101 uint8_t b;
102
103 /* Flip a bit in the journal file */
104 ASSERT_EQ(pread(mj->fd, &b, 1, offset), 1);
105 b |= 0x1;
106 ASSERT_EQ(pwrite(mj->fd, &b, 1, offset), 1);
107
108 /* Close and reopen the journal to flush all caches and remap
109 * the corrupted journal */
110 mj = journal_file_offline_close(mj);
111 r = journal_file_open(
112 /* fd= */ -EBADF,
113 "system.journal",
114 O_RDWR|O_CREAT,
115 JOURNAL_COMPRESS,
116 0644,
117 /* compress_threshold_bytes= */ UINT64_MAX,
118 /* metrics= */ NULL,
119 mmap_cache,
120 /* template= */ NULL,
121 &mj);
122 if (r < 0) {
123 /* The corrupted journal might get rejected during reopening
124 * if it's corrupted enough (especially its header), so
125 * treat this as a success if it doesn't crash */
126 log_info_errno(r, "Failed to reopen the journal: %m");
127 break;
128 }
129
130 /* Try to write something to the (possibly corrupted) journal */
131 ASSERT_OK_ERRNO(asprintf(&message, "MESSAGE=Hello world %" PRIu64, offset));
132 r = journal_append_message(mj, message);
133 if (r < 0) {
134 /* We care only about crashes or sanitizer errors,
135 * failing to write without any crash is a success */
136 log_info_errno(r, "Failed to write to the journal: %m");
137 break;
138 }
139 }
140
141 return 0;
142 }
143
144 int main(int argc, char *argv[]) {
145 uint64_t start_offset = UINT64_MAX;
146 uint64_t iterations = 100;
147 uint64_t iteration_step = 1;
148 uint64_t corrupt_step = 31;
149 bool sequential = false, run_one = false;
150 int r;
151
152 test_setup_logging(LOG_DEBUG);
153
154 OptionParser opts = { argc, argv };
155
156 FOREACH_OPTION_OR_RETURN(c, &opts)
157 switch (c) {
158
159 OPTION_COMMON_HELP: {
160 _cleanup_(table_unrefp) Table *options = NULL;
161
162 r = option_parser_get_help_table(&options);
163 if (r < 0)
164 return r;
165
166 printf("Syntax:\n"
167 " %s [OPTION...]\n"
168 "\nOptions:\n",
169 program_invocation_short_name);
170
171 r = table_print_or_warn(options);
172 if (r < 0)
173 return r;
174
175 return 0;
176 }
177
178 OPTION_LONG("start-offset", "OFFSET",
179 "Offset at which to start corrupting the journal "
180 "(default: random offset is picked, unless --sequential is used"
181 " - in that case we use 0 + iteration)"):
182 r = safe_atou64(opts.arg, &start_offset);
183 if (r < 0)
184 return log_error_errno(r, "Invalid starting offset: %m");
185 break;
186
187 OPTION_LONG("iterations", "ITER",
188 "Number of iterations to perform before exiting (default: 100)"):
189 r = safe_atou64(opts.arg, &iterations);
190 if (r < 0)
191 return log_error_errno(r, "Invalid value for iterations: %m");
192 break;
193
194 OPTION_LONG("corrupt-step", "STEP",
195 "Corrupt every n-th byte starting from OFFSET (default: 31)"):
196 r = safe_atou64(opts.arg, &corrupt_step);
197 if (r < 0)
198 return log_error_errno(r, "Invalid value for corrupt-step: %m");
199 break;
200
201 OPTION_LONG("iteration-step", "STEP", "Iteration step (default: 1)"):
202 r = safe_atou64(opts.arg, &iteration_step);
203 if (r < 0)
204 return log_error_errno(r, "Invalid value for iteration-step: %m");
205 break;
206
207 OPTION_LONG("sequential", NULL,
208 "Go through offsets sequentially instead of picking a random one on each iteration. "
209 "Goes through offsets [OFFSET, ITER) if --start-offset= is used, [0, ITER) otherwise "
210 "(default: false)"):
211 sequential = true;
212 break;
213
214 OPTION_LONG("run-one", "OFFSET",
215 "Single shot mode for reproducing issues. "
216 "Takes the same offset as --start-offset= and does only one iteration"):
217 r = safe_atou64(opts.arg, &start_offset);
218 if (r < 0)
219 return log_error_errno(r, "Invalid offset: %m");
220
221 run_one = true;
222 break;
223 }
224
225 if (run_one)
226 /* Reproducer mode */
227 return journal_corrupt_and_append(start_offset, corrupt_step);
228
229 for (uint64_t i = 0; i < iterations; i++) {
230 uint64_t offset = UINT64_MAX;
231
232 log_info("Iteration #%" PRIu64 ", step: %" PRIu64, i, iteration_step);
233
234 if (sequential)
235 offset = (start_offset == UINT64_MAX ? 0 : start_offset) + i * iteration_step;
236
237 r = journal_corrupt_and_append(offset, corrupt_step);
238 if (r < 0)
239 return EXIT_FAILURE;
240 if (r > 0)
241 /* Reached the end of the journal file */
242 break;
243 }
244
245 return EXIT_SUCCESS;
246 }