]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
87d2c1ff | 2 | |
87d2c1ff | 3 | #include <errno.h> |
87d2c1ff | 4 | #include <fcntl.h> |
82e24b00 | 5 | #include <inttypes.h> |
07630cea LP |
6 | #include <linux/magic.h> |
7 | #include <poll.h> | |
3fbf9cbb | 8 | #include <stddef.h> |
50f20cfd | 9 | #include <sys/inotify.h> |
85210bff | 10 | #include <sys/vfs.h> |
07630cea | 11 | #include <unistd.h> |
87d2c1ff LP |
12 | |
13 | #include "sd-journal.h" | |
c004493c | 14 | |
b5efdb8a | 15 | #include "alloc-util.h" |
07630cea LP |
16 | #include "catalog.h" |
17 | #include "compress.h" | |
a0956174 | 18 | #include "dirent-util.h" |
686d13b9 | 19 | #include "env-file.h" |
9e8b1ec0 | 20 | #include "escape.h" |
3ffd4af2 | 21 | #include "fd-util.h" |
07630cea | 22 | #include "fileio.h" |
f97b34a6 | 23 | #include "format-util.h" |
77601719 | 24 | #include "fs-util.h" |
07630cea LP |
25 | #include "hashmap.h" |
26 | #include "hostname-util.h" | |
a9be0692 | 27 | #include "id128-util.h" |
c004493c | 28 | #include "io-util.h" |
87d2c1ff | 29 | #include "journal-def.h" |
cec736d2 | 30 | #include "journal-file.h" |
07630cea | 31 | #include "journal-internal.h" |
cec736d2 | 32 | #include "list.h" |
de7b95cd | 33 | #include "lookup3.h" |
d8b4d14d | 34 | #include "nulstr-util.h" |
07630cea | 35 | #include "path-util.h" |
dccca82b | 36 | #include "process-util.h" |
d4205751 | 37 | #include "replace-var.h" |
15a5e950 LP |
38 | #include "stat-util.h" |
39 | #include "stdio-util.h" | |
07630cea LP |
40 | #include "string-util.h" |
41 | #include "strv.h" | |
456aa879 | 42 | #include "syslog-util.h" |
87d2c1ff | 43 | |
9e8abdf0 LP |
44 | #define JOURNAL_FILES_MAX 7168 |
45 | ||
85210bff LP |
46 | #define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC) |
47 | ||
d4205751 LP |
48 | #define REPLACE_VAR_MAX 256 |
49 | ||
93b73b06 LP |
50 | #define DEFAULT_DATA_THRESHOLD (64*1024) |
51 | ||
a9a245c1 ZJS |
52 | static void remove_file_real(sd_journal *j, JournalFile *f); |
53 | ||
a65f06bb ZJS |
54 | static bool journal_pid_changed(sd_journal *j) { |
55 | assert(j); | |
56 | ||
57 | /* We don't support people creating a journal object and | |
58 | * keeping it around over a fork(). Let's complain. */ | |
59 | ||
df0ff127 | 60 | return j->original_pid != getpid_cached(); |
a65f06bb ZJS |
61 | } |
62 | ||
5768d259 LP |
63 | static int journal_put_error(sd_journal *j, int r, const char *path) { |
64 | char *copy; | |
3ac251b8 LP |
65 | int k; |
66 | ||
5768d259 LP |
67 | /* Memorize an error we encountered, and store which |
68 | * file/directory it was generated from. Note that we store | |
69 | * only *one* path per error code, as the error code is the | |
70 | * key into the hashmap, and the path is the value. This means | |
71 | * we keep track only of all error kinds, but not of all error | |
72 | * locations. This has the benefit that the hashmap cannot | |
73 | * grow beyond bounds. | |
74 | * | |
75 | * We return an error here only if we didn't manage to | |
76 | * memorize the real error. */ | |
77 | ||
6fe391c5 ZJS |
78 | if (r >= 0) |
79 | return r; | |
80 | ||
5768d259 | 81 | k = hashmap_ensure_allocated(&j->errors, NULL); |
3ac251b8 LP |
82 | if (k < 0) |
83 | return k; | |
84 | ||
5768d259 LP |
85 | if (path) { |
86 | copy = strdup(path); | |
87 | if (!copy) | |
88 | return -ENOMEM; | |
89 | } else | |
90 | copy = NULL; | |
91 | ||
92 | k = hashmap_put(j->errors, INT_TO_PTR(r), copy); | |
93 | if (k < 0) { | |
94 | free(copy); | |
95 | ||
96 | if (k == -EEXIST) | |
97 | return 0; | |
98 | ||
99 | return k; | |
100 | } | |
101 | ||
102 | return 0; | |
6fe391c5 ZJS |
103 | } |
104 | ||
de190aef | 105 | static void detach_location(sd_journal *j) { |
8f9b6cd9 LP |
106 | Iterator i; |
107 | JournalFile *f; | |
108 | ||
109 | assert(j); | |
110 | ||
111 | j->current_file = NULL; | |
112 | j->current_field = 0; | |
113 | ||
c1f906bd | 114 | ORDERED_HASHMAP_FOREACH(f, j->files, i) |
1fc605b0 | 115 | journal_file_reset_location(f); |
8f9b6cd9 LP |
116 | } |
117 | ||
de190aef LP |
118 | static void reset_location(sd_journal *j) { |
119 | assert(j); | |
120 | ||
121 | detach_location(j); | |
122 | zero(j->current_location); | |
123 | } | |
124 | ||
a87247dd | 125 | static void init_location(Location *l, LocationType type, JournalFile *f, Object *o) { |
de190aef | 126 | assert(l); |
3742095b | 127 | assert(IN_SET(type, LOCATION_DISCRETE, LOCATION_SEEK)); |
de190aef LP |
128 | assert(f); |
129 | assert(o->object.type == OBJECT_ENTRY); | |
130 | ||
a87247dd | 131 | l->type = type; |
de190aef LP |
132 | l->seqnum = le64toh(o->entry.seqnum); |
133 | l->seqnum_id = f->header->seqnum_id; | |
134 | l->realtime = le64toh(o->entry.realtime); | |
135 | l->monotonic = le64toh(o->entry.monotonic); | |
ce3fd7e7 | 136 | l->boot_id = o->entry.boot_id; |
de190aef LP |
137 | l->xor_hash = le64toh(o->entry.xor_hash); |
138 | ||
139 | l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true; | |
140 | } | |
141 | ||
1eb6332d | 142 | static void set_location(sd_journal *j, JournalFile *f, Object *o) { |
de190aef LP |
143 | assert(j); |
144 | assert(f); | |
145 | assert(o); | |
146 | ||
1eb6332d | 147 | init_location(&j->current_location, LOCATION_DISCRETE, f, o); |
de190aef LP |
148 | |
149 | j->current_file = f; | |
150 | j->current_field = 0; | |
151 | ||
6573ef05 MS |
152 | /* Let f know its candidate entry was picked. */ |
153 | assert(f->location_type == LOCATION_SEEK); | |
154 | f->location_type = LOCATION_DISCRETE; | |
de190aef LP |
155 | } |
156 | ||
cbdca852 LP |
157 | static int match_is_valid(const void *data, size_t size) { |
158 | const char *b, *p; | |
159 | ||
160 | assert(data); | |
161 | ||
162 | if (size < 2) | |
163 | return false; | |
164 | ||
e0567bc8 | 165 | if (((char*) data)[0] == '_' && ((char*) data)[1] == '_') |
cbdca852 LP |
166 | return false; |
167 | ||
168 | b = data; | |
169 | for (p = b; p < b + size; p++) { | |
170 | ||
171 | if (*p == '=') | |
172 | return p > b; | |
173 | ||
174 | if (*p == '_') | |
175 | continue; | |
176 | ||
177 | if (*p >= 'A' && *p <= 'Z') | |
178 | continue; | |
179 | ||
180 | if (*p >= '0' && *p <= '9') | |
181 | continue; | |
182 | ||
183 | return false; | |
184 | } | |
185 | ||
186 | return false; | |
187 | } | |
188 | ||
189 | static bool same_field(const void *_a, size_t s, const void *_b, size_t t) { | |
de190aef LP |
190 | const uint8_t *a = _a, *b = _b; |
191 | size_t j; | |
de190aef LP |
192 | |
193 | for (j = 0; j < s && j < t; j++) { | |
194 | ||
de190aef | 195 | if (a[j] != b[j]) |
cbdca852 | 196 | return false; |
de190aef | 197 | |
cbdca852 LP |
198 | if (a[j] == '=') |
199 | return true; | |
de190aef LP |
200 | } |
201 | ||
bc302926 | 202 | assert_not_reached("\"=\" not found"); |
cbdca852 LP |
203 | } |
204 | ||
205 | static Match *match_new(Match *p, MatchType t) { | |
206 | Match *m; | |
207 | ||
2f5435a1 | 208 | m = new(Match, 1); |
cbdca852 LP |
209 | if (!m) |
210 | return NULL; | |
211 | ||
2f5435a1 LP |
212 | *m = (Match) { |
213 | .type = t, | |
214 | .parent = p, | |
215 | }; | |
cbdca852 | 216 | |
2f5435a1 | 217 | if (p) |
71fda00f | 218 | LIST_PREPEND(matches, p->matches, m); |
cbdca852 LP |
219 | |
220 | return m; | |
221 | } | |
222 | ||
223 | static void match_free(Match *m) { | |
224 | assert(m); | |
225 | ||
226 | while (m->matches) | |
227 | match_free(m->matches); | |
228 | ||
229 | if (m->parent) | |
71fda00f | 230 | LIST_REMOVE(matches, m->parent->matches, m); |
cbdca852 LP |
231 | |
232 | free(m->data); | |
233 | free(m); | |
234 | } | |
235 | ||
236 | static void match_free_if_empty(Match *m) { | |
c5a10d9c | 237 | if (!m || m->matches) |
cbdca852 LP |
238 | return; |
239 | ||
240 | match_free(m); | |
de190aef LP |
241 | } |
242 | ||
a5344d2c | 243 | _public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size) { |
cd34b3c6 | 244 | Match *l3, *l4, *add_here = NULL, *m; |
4fd052ae | 245 | le64_t le_hash; |
87d2c1ff | 246 | |
1ae464e0 TA |
247 | assert_return(j, -EINVAL); |
248 | assert_return(!journal_pid_changed(j), -ECHILD); | |
249 | assert_return(data, -EINVAL); | |
cbdca852 LP |
250 | |
251 | if (size == 0) | |
252 | size = strlen(data); | |
253 | ||
1ae464e0 | 254 | assert_return(match_is_valid(data, size), -EINVAL); |
1cc101f1 | 255 | |
cd34b3c6 HH |
256 | /* level 0: AND term |
257 | * level 1: OR terms | |
258 | * level 2: AND terms | |
259 | * level 3: OR terms | |
260 | * level 4: concrete matches */ | |
cbdca852 LP |
261 | |
262 | if (!j->level0) { | |
cd34b3c6 | 263 | j->level0 = match_new(NULL, MATCH_AND_TERM); |
cbdca852 LP |
264 | if (!j->level0) |
265 | return -ENOMEM; | |
266 | } | |
267 | ||
268 | if (!j->level1) { | |
cd34b3c6 | 269 | j->level1 = match_new(j->level0, MATCH_OR_TERM); |
cbdca852 LP |
270 | if (!j->level1) |
271 | return -ENOMEM; | |
272 | } | |
273 | ||
cd34b3c6 HH |
274 | if (!j->level2) { |
275 | j->level2 = match_new(j->level1, MATCH_AND_TERM); | |
276 | if (!j->level2) | |
277 | return -ENOMEM; | |
278 | } | |
279 | ||
280 | assert(j->level0->type == MATCH_AND_TERM); | |
281 | assert(j->level1->type == MATCH_OR_TERM); | |
282 | assert(j->level2->type == MATCH_AND_TERM); | |
ab4979d2 | 283 | |
de190aef LP |
284 | le_hash = htole64(hash64(data, size)); |
285 | ||
cd34b3c6 HH |
286 | LIST_FOREACH(matches, l3, j->level2->matches) { |
287 | assert(l3->type == MATCH_OR_TERM); | |
de190aef | 288 | |
cd34b3c6 HH |
289 | LIST_FOREACH(matches, l4, l3->matches) { |
290 | assert(l4->type == MATCH_DISCRETE); | |
de190aef | 291 | |
cbdca852 LP |
292 | /* Exactly the same match already? Then ignore |
293 | * this addition */ | |
cd34b3c6 HH |
294 | if (l4->le_hash == le_hash && |
295 | l4->size == size && | |
296 | memcmp(l4->data, data, size) == 0) | |
cbdca852 LP |
297 | return 0; |
298 | ||
299 | /* Same field? Then let's add this to this OR term */ | |
cd34b3c6 HH |
300 | if (same_field(data, size, l4->data, l4->size)) { |
301 | add_here = l3; | |
cbdca852 LP |
302 | break; |
303 | } | |
304 | } | |
305 | ||
306 | if (add_here) | |
307 | break; | |
de190aef LP |
308 | } |
309 | ||
cbdca852 | 310 | if (!add_here) { |
cd34b3c6 | 311 | add_here = match_new(j->level2, MATCH_OR_TERM); |
cbdca852 LP |
312 | if (!add_here) |
313 | goto fail; | |
314 | } | |
315 | ||
316 | m = match_new(add_here, MATCH_DISCRETE); | |
cec736d2 | 317 | if (!m) |
cbdca852 | 318 | goto fail; |
87d2c1ff | 319 | |
cbdca852 | 320 | m->le_hash = le_hash; |
1cc101f1 | 321 | m->size = size; |
cbdca852 LP |
322 | m->data = memdup(data, size); |
323 | if (!m->data) | |
324 | goto fail; | |
325 | ||
326 | detach_location(j); | |
327 | ||
328 | return 0; | |
329 | ||
330 | fail: | |
c5a10d9c ZJS |
331 | match_free_if_empty(add_here); |
332 | match_free_if_empty(j->level2); | |
333 | match_free_if_empty(j->level1); | |
334 | match_free_if_empty(j->level0); | |
cbdca852 LP |
335 | |
336 | return -ENOMEM; | |
337 | } | |
338 | ||
cd34b3c6 | 339 | _public_ int sd_journal_add_conjunction(sd_journal *j) { |
1ae464e0 TA |
340 | assert_return(j, -EINVAL); |
341 | assert_return(!journal_pid_changed(j), -ECHILD); | |
1cc101f1 | 342 | |
cbdca852 LP |
343 | if (!j->level0) |
344 | return 0; | |
345 | ||
346 | if (!j->level1) | |
347 | return 0; | |
348 | ||
349 | if (!j->level1->matches) | |
350 | return 0; | |
351 | ||
cd34b3c6 HH |
352 | j->level1 = NULL; |
353 | j->level2 = NULL; | |
354 | ||
355 | return 0; | |
356 | } | |
357 | ||
358 | _public_ int sd_journal_add_disjunction(sd_journal *j) { | |
1ae464e0 TA |
359 | assert_return(j, -EINVAL); |
360 | assert_return(!journal_pid_changed(j), -ECHILD); | |
cd34b3c6 HH |
361 | |
362 | if (!j->level0) | |
363 | return 0; | |
364 | ||
365 | if (!j->level1) | |
366 | return 0; | |
367 | ||
368 | if (!j->level2) | |
369 | return 0; | |
370 | ||
371 | if (!j->level2->matches) | |
372 | return 0; | |
cbdca852 | 373 | |
cd34b3c6 | 374 | j->level2 = NULL; |
cbdca852 LP |
375 | return 0; |
376 | } | |
377 | ||
378 | static char *match_make_string(Match *m) { | |
6b430fdb | 379 | char *p = NULL, *r; |
cbdca852 LP |
380 | Match *i; |
381 | bool enclose = false; | |
382 | ||
383 | if (!m) | |
4ad16808 | 384 | return strdup("none"); |
cbdca852 LP |
385 | |
386 | if (m->type == MATCH_DISCRETE) | |
9e8b1ec0 | 387 | return cescape_length(m->data, m->size); |
cbdca852 | 388 | |
cbdca852 LP |
389 | LIST_FOREACH(matches, i, m->matches) { |
390 | char *t, *k; | |
391 | ||
392 | t = match_make_string(i); | |
6b430fdb ZJS |
393 | if (!t) |
394 | return mfree(p); | |
cbdca852 LP |
395 | |
396 | if (p) { | |
605405c6 | 397 | k = strjoin(p, m->type == MATCH_OR_TERM ? " OR " : " AND ", t); |
cbdca852 LP |
398 | free(p); |
399 | free(t); | |
400 | ||
401 | if (!k) | |
402 | return NULL; | |
403 | ||
404 | p = k; | |
405 | ||
406 | enclose = true; | |
bc302926 | 407 | } else |
cbdca852 | 408 | p = t; |
87d2c1ff LP |
409 | } |
410 | ||
cbdca852 | 411 | if (enclose) { |
605405c6 | 412 | r = strjoin("(", p, ")"); |
cbdca852 LP |
413 | free(p); |
414 | return r; | |
415 | } | |
87d2c1ff | 416 | |
cbdca852 LP |
417 | return p; |
418 | } | |
de7b95cd | 419 | |
cbdca852 LP |
420 | char *journal_make_match_string(sd_journal *j) { |
421 | assert(j); | |
8f9b6cd9 | 422 | |
cbdca852 | 423 | return match_make_string(j->level0); |
87d2c1ff LP |
424 | } |
425 | ||
a5344d2c LP |
426 | _public_ void sd_journal_flush_matches(sd_journal *j) { |
427 | if (!j) | |
428 | return; | |
87d2c1ff | 429 | |
cbdca852 LP |
430 | if (j->level0) |
431 | match_free(j->level0); | |
de7b95cd | 432 | |
cd34b3c6 | 433 | j->level0 = j->level1 = j->level2 = NULL; |
8f9b6cd9 | 434 | |
de190aef | 435 | detach_location(j); |
87d2c1ff LP |
436 | } |
437 | ||
b6849042 | 438 | _pure_ static int compare_with_location(const JournalFile *f, const Location *l, const JournalFile *current_file) { |
90c88092 YW |
439 | int r; |
440 | ||
487d3720 | 441 | assert(f); |
de190aef | 442 | assert(l); |
487d3720 | 443 | assert(f->location_type == LOCATION_SEEK); |
4c701096 | 444 | assert(IN_SET(l->type, LOCATION_DISCRETE, LOCATION_SEEK)); |
de190aef LP |
445 | |
446 | if (l->monotonic_set && | |
487d3720 | 447 | sd_id128_equal(f->current_boot_id, l->boot_id) && |
de190aef | 448 | l->realtime_set && |
487d3720 | 449 | f->current_realtime == l->realtime && |
de190aef | 450 | l->xor_hash_set && |
b6849042 GM |
451 | f->current_xor_hash == l->xor_hash && |
452 | f != current_file) | |
de190aef LP |
453 | return 0; |
454 | ||
455 | if (l->seqnum_set && | |
487d3720 | 456 | sd_id128_equal(f->header->seqnum_id, l->seqnum_id)) { |
de190aef | 457 | |
90c88092 YW |
458 | r = CMP(f->current_seqnum, l->seqnum); |
459 | if (r != 0) | |
460 | return r; | |
de190aef LP |
461 | } |
462 | ||
463 | if (l->monotonic_set && | |
487d3720 | 464 | sd_id128_equal(f->current_boot_id, l->boot_id)) { |
de190aef | 465 | |
90c88092 YW |
466 | r = CMP(f->current_monotonic, l->monotonic); |
467 | if (r != 0) | |
468 | return r; | |
de190aef LP |
469 | } |
470 | ||
471 | if (l->realtime_set) { | |
472 | ||
90c88092 YW |
473 | r = CMP(f->current_realtime, l->realtime); |
474 | if (r != 0) | |
475 | return r; | |
de190aef LP |
476 | } |
477 | ||
478 | if (l->xor_hash_set) { | |
de190aef | 479 | |
90c88092 YW |
480 | r = CMP(f->current_xor_hash, l->xor_hash); |
481 | if (r != 0) | |
482 | return r; | |
de190aef LP |
483 | } |
484 | ||
485 | return 0; | |
486 | } | |
487 | ||
cbdca852 LP |
488 | static int next_for_match( |
489 | sd_journal *j, | |
490 | Match *m, | |
491 | JournalFile *f, | |
492 | uint64_t after_offset, | |
493 | direction_t direction, | |
494 | Object **ret, | |
495 | uint64_t *offset) { | |
496 | ||
de7b95cd | 497 | int r; |
cbdca852 LP |
498 | uint64_t np = 0; |
499 | Object *n; | |
de7b95cd LP |
500 | |
501 | assert(j); | |
cbdca852 LP |
502 | assert(m); |
503 | assert(f); | |
de7b95cd | 504 | |
cbdca852 LP |
505 | if (m->type == MATCH_DISCRETE) { |
506 | uint64_t dp; | |
de190aef | 507 | |
cbdca852 | 508 | r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), NULL, &dp); |
de190aef LP |
509 | if (r <= 0) |
510 | return r; | |
511 | ||
cbdca852 | 512 | return journal_file_move_to_entry_by_offset_for_data(f, dp, after_offset, direction, ret, offset); |
de190aef | 513 | |
cbdca852 LP |
514 | } else if (m->type == MATCH_OR_TERM) { |
515 | Match *i; | |
de7b95cd | 516 | |
cbdca852 | 517 | /* Find the earliest match beyond after_offset */ |
de190aef | 518 | |
cbdca852 LP |
519 | LIST_FOREACH(matches, i, m->matches) { |
520 | uint64_t cp; | |
de190aef | 521 | |
cbdca852 | 522 | r = next_for_match(j, i, f, after_offset, direction, NULL, &cp); |
b4e5f920 LP |
523 | if (r < 0) |
524 | return r; | |
cbdca852 | 525 | else if (r > 0) { |
bc302926 | 526 | if (np == 0 || (direction == DIRECTION_DOWN ? cp < np : cp > np)) |
cbdca852 LP |
527 | np = cp; |
528 | } | |
529 | } | |
b4e5f920 | 530 | |
bc302926 ZJS |
531 | if (np == 0) |
532 | return 0; | |
533 | ||
cbdca852 | 534 | } else if (m->type == MATCH_AND_TERM) { |
2bc8ca0c | 535 | Match *i, *last_moved; |
de190aef | 536 | |
cbdca852 | 537 | /* Always jump to the next matching entry and repeat |
2bc8ca0c | 538 | * this until we find an offset that matches for all |
cbdca852 | 539 | * matches. */ |
de190aef | 540 | |
cbdca852 LP |
541 | if (!m->matches) |
542 | return 0; | |
de7b95cd | 543 | |
2bc8ca0c ZJS |
544 | r = next_for_match(j, m->matches, f, after_offset, direction, NULL, &np); |
545 | if (r <= 0) | |
546 | return r; | |
de190aef | 547 | |
2bc8ca0c ZJS |
548 | assert(direction == DIRECTION_DOWN ? np >= after_offset : np <= after_offset); |
549 | last_moved = m->matches; | |
de190aef | 550 | |
2bc8ca0c ZJS |
551 | LIST_LOOP_BUT_ONE(matches, i, m->matches, last_moved) { |
552 | uint64_t cp; | |
de190aef | 553 | |
2bc8ca0c ZJS |
554 | r = next_for_match(j, i, f, np, direction, NULL, &cp); |
555 | if (r <= 0) | |
556 | return r; | |
de190aef | 557 | |
2bc8ca0c ZJS |
558 | assert(direction == DIRECTION_DOWN ? cp >= np : cp <= np); |
559 | if (direction == DIRECTION_DOWN ? cp > np : cp < np) { | |
560 | np = cp; | |
561 | last_moved = i; | |
de190aef | 562 | } |
2bc8ca0c | 563 | } |
cbdca852 | 564 | } |
de190aef | 565 | |
bc302926 | 566 | assert(np > 0); |
de190aef | 567 | |
cbdca852 LP |
568 | r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n); |
569 | if (r < 0) | |
570 | return r; | |
de7b95cd | 571 | |
de190aef | 572 | if (ret) |
cbdca852 | 573 | *ret = n; |
de190aef | 574 | if (offset) |
cbdca852 | 575 | *offset = np; |
de190aef LP |
576 | |
577 | return 1; | |
578 | } | |
579 | ||
cbdca852 LP |
580 | static int find_location_for_match( |
581 | sd_journal *j, | |
582 | Match *m, | |
583 | JournalFile *f, | |
584 | direction_t direction, | |
585 | Object **ret, | |
586 | uint64_t *offset) { | |
587 | ||
de190aef | 588 | int r; |
de190aef LP |
589 | |
590 | assert(j); | |
cbdca852 | 591 | assert(m); |
de190aef | 592 | assert(f); |
de190aef | 593 | |
cbdca852 LP |
594 | if (m->type == MATCH_DISCRETE) { |
595 | uint64_t dp; | |
de190aef | 596 | |
cbdca852 | 597 | r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), NULL, &dp); |
de7b95cd LP |
598 | if (r <= 0) |
599 | return r; | |
600 | ||
cbdca852 | 601 | /* FIXME: missing: find by monotonic */ |
de7b95cd | 602 | |
cbdca852 LP |
603 | if (j->current_location.type == LOCATION_HEAD) |
604 | return journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_DOWN, ret, offset); | |
605 | if (j->current_location.type == LOCATION_TAIL) | |
606 | return journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_UP, ret, offset); | |
607 | if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id)) | |
608 | return journal_file_move_to_entry_by_seqnum_for_data(f, dp, j->current_location.seqnum, direction, ret, offset); | |
609 | if (j->current_location.monotonic_set) { | |
610 | r = journal_file_move_to_entry_by_monotonic_for_data(f, dp, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset); | |
611 | if (r != -ENOENT) | |
612 | return r; | |
613 | } | |
614 | if (j->current_location.realtime_set) | |
615 | return journal_file_move_to_entry_by_realtime_for_data(f, dp, j->current_location.realtime, direction, ret, offset); | |
de190aef | 616 | |
cbdca852 | 617 | return journal_file_next_entry_for_data(f, NULL, 0, dp, direction, ret, offset); |
de7b95cd | 618 | |
cbdca852 LP |
619 | } else if (m->type == MATCH_OR_TERM) { |
620 | uint64_t np = 0; | |
621 | Object *n; | |
622 | Match *i; | |
de7b95cd | 623 | |
cbdca852 | 624 | /* Find the earliest match */ |
de7b95cd | 625 | |
cbdca852 LP |
626 | LIST_FOREACH(matches, i, m->matches) { |
627 | uint64_t cp; | |
628 | ||
629 | r = find_location_for_match(j, i, f, direction, NULL, &cp); | |
630 | if (r < 0) | |
631 | return r; | |
632 | else if (r > 0) { | |
633 | if (np == 0 || (direction == DIRECTION_DOWN ? np > cp : np < cp)) | |
634 | np = cp; | |
de190aef | 635 | } |
cbdca852 | 636 | } |
de190aef | 637 | |
cbdca852 LP |
638 | if (np == 0) |
639 | return 0; | |
de7b95cd | 640 | |
cbdca852 LP |
641 | r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n); |
642 | if (r < 0) | |
643 | return r; | |
de7b95cd | 644 | |
cbdca852 LP |
645 | if (ret) |
646 | *ret = n; | |
647 | if (offset) | |
648 | *offset = np; | |
de190aef | 649 | |
cbdca852 | 650 | return 1; |
e892bd17 | 651 | |
cbdca852 LP |
652 | } else { |
653 | Match *i; | |
654 | uint64_t np = 0; | |
655 | ||
656 | assert(m->type == MATCH_AND_TERM); | |
657 | ||
658 | /* First jump to the last match, and then find the | |
659 | * next one where all matches match */ | |
660 | ||
661 | if (!m->matches) | |
662 | return 0; | |
663 | ||
664 | LIST_FOREACH(matches, i, m->matches) { | |
665 | uint64_t cp; | |
666 | ||
667 | r = find_location_for_match(j, i, f, direction, NULL, &cp); | |
668 | if (r <= 0) | |
4b067dc9 LP |
669 | return r; |
670 | ||
bc302926 | 671 | if (np == 0 || (direction == DIRECTION_DOWN ? cp > np : cp < np)) |
cbdca852 | 672 | np = cp; |
de7b95cd LP |
673 | } |
674 | ||
cbdca852 LP |
675 | return next_for_match(j, m, f, np, direction, ret, offset); |
676 | } | |
677 | } | |
de190aef | 678 | |
cbdca852 LP |
679 | static int find_location_with_matches( |
680 | sd_journal *j, | |
681 | JournalFile *f, | |
682 | direction_t direction, | |
683 | Object **ret, | |
684 | uint64_t *offset) { | |
685 | ||
686 | int r; | |
687 | ||
688 | assert(j); | |
689 | assert(f); | |
690 | assert(ret); | |
691 | assert(offset); | |
692 | ||
693 | if (!j->level0) { | |
694 | /* No matches is simple */ | |
695 | ||
696 | if (j->current_location.type == LOCATION_HEAD) | |
f534928a | 697 | return journal_file_next_entry(f, 0, DIRECTION_DOWN, ret, offset); |
cbdca852 | 698 | if (j->current_location.type == LOCATION_TAIL) |
f534928a | 699 | return journal_file_next_entry(f, 0, DIRECTION_UP, ret, offset); |
cbdca852 LP |
700 | if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id)) |
701 | return journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, ret, offset); | |
702 | if (j->current_location.monotonic_set) { | |
703 | r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset); | |
704 | if (r != -ENOENT) | |
705 | return r; | |
de7b95cd | 706 | } |
cbdca852 LP |
707 | if (j->current_location.realtime_set) |
708 | return journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, ret, offset); | |
de7b95cd | 709 | |
f534928a | 710 | return journal_file_next_entry(f, 0, direction, ret, offset); |
cbdca852 LP |
711 | } else |
712 | return find_location_for_match(j, j->level0, f, direction, ret, offset); | |
713 | } | |
de7b95cd | 714 | |
cbdca852 LP |
715 | static int next_with_matches( |
716 | sd_journal *j, | |
717 | JournalFile *f, | |
718 | direction_t direction, | |
719 | Object **ret, | |
720 | uint64_t *offset) { | |
721 | ||
cbdca852 LP |
722 | assert(j); |
723 | assert(f); | |
724 | assert(ret); | |
725 | assert(offset); | |
726 | ||
cbdca852 LP |
727 | /* No matches is easy. We simple advance the file |
728 | * pointer by one. */ | |
729 | if (!j->level0) | |
b29ddfcb | 730 | return journal_file_next_entry(f, f->current_offset, direction, ret, offset); |
cbdca852 LP |
731 | |
732 | /* If we have a match then we look for the next matching entry | |
49f43d5f | 733 | * with an offset at least one step larger */ |
b29ddfcb MS |
734 | return next_for_match(j, j->level0, f, |
735 | direction == DIRECTION_DOWN ? f->current_offset + 1 | |
736 | : f->current_offset - 1, | |
737 | direction, ret, offset); | |
de7b95cd LP |
738 | } |
739 | ||
58439db4 | 740 | static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction) { |
de190aef | 741 | Object *c; |
6e693b42 | 742 | uint64_t cp, n_entries; |
cbdca852 | 743 | int r; |
de190aef LP |
744 | |
745 | assert(j); | |
746 | assert(f); | |
747 | ||
950c07d4 MS |
748 | n_entries = le64toh(f->header->n_entries); |
749 | ||
750 | /* If we hit EOF before, we don't need to look into this file again | |
751 | * unless direction changed or new entries appeared. */ | |
752 | if (f->last_direction == direction && f->location_type == LOCATION_TAIL && | |
753 | n_entries == f->last_n_entries) | |
754 | return 0; | |
668c965a | 755 | |
950c07d4 MS |
756 | f->last_n_entries = n_entries; |
757 | ||
758 | if (f->last_direction == direction && f->current_offset > 0) { | |
7943f422 MS |
759 | /* LOCATION_SEEK here means we did the work in a previous |
760 | * iteration and the current location already points to a | |
761 | * candidate entry. */ | |
487d3720 MS |
762 | if (f->location_type != LOCATION_SEEK) { |
763 | r = next_with_matches(j, f, direction, &c, &cp); | |
764 | if (r <= 0) | |
765 | return r; | |
466ccd92 | 766 | |
950c07d4 | 767 | journal_file_save_location(f, c, cp); |
487d3720 | 768 | } |
de190aef | 769 | } else { |
950c07d4 MS |
770 | f->last_direction = direction; |
771 | ||
cbdca852 | 772 | r = find_location_with_matches(j, f, direction, &c, &cp); |
de190aef LP |
773 | if (r <= 0) |
774 | return r; | |
487d3720 | 775 | |
950c07d4 | 776 | journal_file_save_location(f, c, cp); |
de190aef LP |
777 | } |
778 | ||
bc302926 | 779 | /* OK, we found the spot, now let's advance until an entry |
cbdca852 LP |
780 | * that is actually different from what we were previously |
781 | * looking at. This is necessary to handle entries which exist | |
782 | * in two (or more) journal files, and which shall all be | |
783 | * suppressed but one. */ | |
784 | ||
de190aef LP |
785 | for (;;) { |
786 | bool found; | |
787 | ||
788 | if (j->current_location.type == LOCATION_DISCRETE) { | |
789 | int k; | |
790 | ||
b6849042 | 791 | k = compare_with_location(f, &j->current_location, j->current_file); |
1cdf7175 CH |
792 | |
793 | found = direction == DIRECTION_DOWN ? k > 0 : k < 0; | |
de190aef LP |
794 | } else |
795 | found = true; | |
796 | ||
487d3720 | 797 | if (found) |
de190aef | 798 | return 1; |
de190aef LP |
799 | |
800 | r = next_with_matches(j, f, direction, &c, &cp); | |
801 | if (r <= 0) | |
802 | return r; | |
487d3720 | 803 | |
950c07d4 | 804 | journal_file_save_location(f, c, cp); |
de190aef LP |
805 | } |
806 | } | |
807 | ||
e892bd17 | 808 | static int real_journal_next(sd_journal *j, direction_t direction) { |
5d4ba7f2 VC |
809 | JournalFile *new_file = NULL; |
810 | unsigned i, n_files; | |
811 | const void **files; | |
a002d44b | 812 | Object *o; |
87d2c1ff LP |
813 | int r; |
814 | ||
1ae464e0 TA |
815 | assert_return(j, -EINVAL); |
816 | assert_return(!journal_pid_changed(j), -ECHILD); | |
87d2c1ff | 817 | |
5d4ba7f2 VC |
818 | r = iterated_cache_get(j->files_cache, NULL, &files, &n_files); |
819 | if (r < 0) | |
820 | return r; | |
821 | ||
822 | for (i = 0; i < n_files; i++) { | |
823 | JournalFile *f = (JournalFile *)files[i]; | |
de190aef | 824 | bool found; |
87d2c1ff | 825 | |
58439db4 | 826 | r = next_beyond_location(j, f, direction); |
e590af26 | 827 | if (r < 0) { |
da927ba9 | 828 | log_debug_errno(r, "Can't iterate through %s, ignoring: %m", f->path); |
a9a245c1 | 829 | remove_file_real(j, f); |
e590af26 | 830 | continue; |
6573ef05 MS |
831 | } else if (r == 0) { |
832 | f->location_type = LOCATION_TAIL; | |
cec736d2 | 833 | continue; |
6573ef05 | 834 | } |
87d2c1ff | 835 | |
468b21de | 836 | if (!new_file) |
de190aef LP |
837 | found = true; |
838 | else { | |
839 | int k; | |
840 | ||
d8ae66d7 | 841 | k = journal_file_compare_locations(f, new_file); |
de190aef | 842 | |
bc302926 | 843 | found = direction == DIRECTION_DOWN ? k < 0 : k > 0; |
de190aef LP |
844 | } |
845 | ||
e499c999 | 846 | if (found) |
468b21de | 847 | new_file = f; |
87d2c1ff LP |
848 | } |
849 | ||
468b21de | 850 | if (!new_file) |
de190aef | 851 | return 0; |
ae2cc8ef | 852 | |
e499c999 | 853 | r = journal_file_move_to_object(new_file, OBJECT_ENTRY, new_file->current_offset, &o); |
468b21de LP |
854 | if (r < 0) |
855 | return r; | |
856 | ||
1eb6332d | 857 | set_location(j, new_file, o); |
ae2cc8ef | 858 | |
de190aef LP |
859 | return 1; |
860 | } | |
ae2cc8ef | 861 | |
a5344d2c | 862 | _public_ int sd_journal_next(sd_journal *j) { |
de190aef LP |
863 | return real_journal_next(j, DIRECTION_DOWN); |
864 | } | |
ae2cc8ef | 865 | |
a5344d2c | 866 | _public_ int sd_journal_previous(sd_journal *j) { |
de190aef LP |
867 | return real_journal_next(j, DIRECTION_UP); |
868 | } | |
ae2cc8ef | 869 | |
6f003b43 | 870 | static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) { |
de190aef | 871 | int c = 0, r; |
ae2cc8ef | 872 | |
1ae464e0 TA |
873 | assert_return(j, -EINVAL); |
874 | assert_return(!journal_pid_changed(j), -ECHILD); | |
de190aef | 875 | |
6f003b43 LP |
876 | if (skip == 0) { |
877 | /* If this is not a discrete skip, then at least | |
878 | * resolve the current location */ | |
04884354 LK |
879 | if (j->current_location.type != LOCATION_DISCRETE) { |
880 | r = real_journal_next(j, direction); | |
881 | if (r < 0) | |
882 | return r; | |
883 | } | |
6f003b43 LP |
884 | |
885 | return 0; | |
886 | } | |
887 | ||
888 | do { | |
889 | r = real_journal_next(j, direction); | |
de190aef LP |
890 | if (r < 0) |
891 | return r; | |
892 | ||
893 | if (r == 0) | |
894 | return c; | |
895 | ||
896 | skip--; | |
897 | c++; | |
6f003b43 | 898 | } while (skip > 0); |
87d2c1ff | 899 | |
de190aef | 900 | return c; |
87d2c1ff LP |
901 | } |
902 | ||
6f003b43 LP |
903 | _public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) { |
904 | return real_journal_next_skip(j, DIRECTION_DOWN, skip); | |
905 | } | |
de190aef | 906 | |
6f003b43 LP |
907 | _public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) { |
908 | return real_journal_next_skip(j, DIRECTION_UP, skip); | |
87d2c1ff LP |
909 | } |
910 | ||
a5344d2c | 911 | _public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) { |
cec736d2 | 912 | Object *o; |
87d2c1ff | 913 | int r; |
5905d7cf | 914 | char bid[SD_ID128_STRING_MAX], sid[SD_ID128_STRING_MAX]; |
87d2c1ff | 915 | |
1ae464e0 TA |
916 | assert_return(j, -EINVAL); |
917 | assert_return(!journal_pid_changed(j), -ECHILD); | |
918 | assert_return(cursor, -EINVAL); | |
87d2c1ff | 919 | |
3fbf9cbb LP |
920 | if (!j->current_file || j->current_file->current_offset <= 0) |
921 | return -EADDRNOTAVAIL; | |
87d2c1ff | 922 | |
de190aef | 923 | r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o); |
87d2c1ff LP |
924 | if (r < 0) |
925 | return r; | |
926 | ||
3fbf9cbb LP |
927 | sd_id128_to_string(j->current_file->header->seqnum_id, sid); |
928 | sd_id128_to_string(o->entry.boot_id, bid); | |
87d2c1ff | 929 | |
3fbf9cbb | 930 | if (asprintf(cursor, |
507f22bd ZJS |
931 | "s=%s;i=%"PRIx64";b=%s;m=%"PRIx64";t=%"PRIx64";x=%"PRIx64, |
932 | sid, le64toh(o->entry.seqnum), | |
933 | bid, le64toh(o->entry.monotonic), | |
934 | le64toh(o->entry.realtime), | |
935 | le64toh(o->entry.xor_hash)) < 0) | |
3fbf9cbb | 936 | return -ENOMEM; |
87d2c1ff | 937 | |
6f47ad30 | 938 | return 0; |
87d2c1ff LP |
939 | } |
940 | ||
a5344d2c | 941 | _public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) { |
a2a5291b | 942 | const char *word, *state; |
de190aef | 943 | size_t l; |
de190aef LP |
944 | unsigned long long seqnum, monotonic, realtime, xor_hash; |
945 | bool | |
946 | seqnum_id_set = false, | |
947 | seqnum_set = false, | |
948 | boot_id_set = false, | |
949 | monotonic_set = false, | |
950 | realtime_set = false, | |
951 | xor_hash_set = false; | |
952 | sd_id128_t seqnum_id, boot_id; | |
953 | ||
1ae464e0 TA |
954 | assert_return(j, -EINVAL); |
955 | assert_return(!journal_pid_changed(j), -ECHILD); | |
956 | assert_return(!isempty(cursor), -EINVAL); | |
de190aef | 957 | |
a2a5291b | 958 | FOREACH_WORD_SEPARATOR(word, l, cursor, ";", state) { |
de190aef LP |
959 | char *item; |
960 | int k = 0; | |
961 | ||
a2a5291b | 962 | if (l < 2 || word[1] != '=') |
de190aef LP |
963 | return -EINVAL; |
964 | ||
a2a5291b | 965 | item = strndup(word, l); |
de190aef LP |
966 | if (!item) |
967 | return -ENOMEM; | |
968 | ||
a2a5291b | 969 | switch (word[0]) { |
de190aef LP |
970 | |
971 | case 's': | |
972 | seqnum_id_set = true; | |
be3ea5ea | 973 | k = sd_id128_from_string(item+2, &seqnum_id); |
de190aef LP |
974 | break; |
975 | ||
976 | case 'i': | |
977 | seqnum_set = true; | |
be3ea5ea | 978 | if (sscanf(item+2, "%llx", &seqnum) != 1) |
de190aef LP |
979 | k = -EINVAL; |
980 | break; | |
981 | ||
982 | case 'b': | |
983 | boot_id_set = true; | |
be3ea5ea | 984 | k = sd_id128_from_string(item+2, &boot_id); |
de190aef LP |
985 | break; |
986 | ||
987 | case 'm': | |
988 | monotonic_set = true; | |
be3ea5ea | 989 | if (sscanf(item+2, "%llx", &monotonic) != 1) |
de190aef LP |
990 | k = -EINVAL; |
991 | break; | |
992 | ||
993 | case 't': | |
994 | realtime_set = true; | |
be3ea5ea | 995 | if (sscanf(item+2, "%llx", &realtime) != 1) |
de190aef LP |
996 | k = -EINVAL; |
997 | break; | |
998 | ||
999 | case 'x': | |
1000 | xor_hash_set = true; | |
be3ea5ea | 1001 | if (sscanf(item+2, "%llx", &xor_hash) != 1) |
de190aef LP |
1002 | k = -EINVAL; |
1003 | break; | |
1004 | } | |
1005 | ||
1006 | free(item); | |
1007 | ||
1008 | if (k < 0) | |
1009 | return k; | |
1010 | } | |
1011 | ||
1012 | if ((!seqnum_set || !seqnum_id_set) && | |
1013 | (!monotonic_set || !boot_id_set) && | |
1014 | !realtime_set) | |
1015 | return -EINVAL; | |
1016 | ||
1017 | reset_location(j); | |
1018 | ||
a87247dd | 1019 | j->current_location.type = LOCATION_SEEK; |
de190aef LP |
1020 | |
1021 | if (realtime_set) { | |
1022 | j->current_location.realtime = (uint64_t) realtime; | |
1023 | j->current_location.realtime_set = true; | |
1024 | } | |
1025 | ||
1026 | if (seqnum_set && seqnum_id_set) { | |
1027 | j->current_location.seqnum = (uint64_t) seqnum; | |
1028 | j->current_location.seqnum_id = seqnum_id; | |
1029 | j->current_location.seqnum_set = true; | |
1030 | } | |
1031 | ||
1032 | if (monotonic_set && boot_id_set) { | |
1033 | j->current_location.monotonic = (uint64_t) monotonic; | |
1034 | j->current_location.boot_id = boot_id; | |
1035 | j->current_location.monotonic_set = true; | |
1036 | } | |
1037 | ||
1038 | if (xor_hash_set) { | |
1039 | j->current_location.xor_hash = (uint64_t) xor_hash; | |
1040 | j->current_location.xor_hash_set = true; | |
1041 | } | |
1042 | ||
1043 | return 0; | |
1044 | } | |
1045 | ||
c6511e85 LP |
1046 | _public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) { |
1047 | int r; | |
c6511e85 LP |
1048 | Object *o; |
1049 | ||
1ae464e0 TA |
1050 | assert_return(j, -EINVAL); |
1051 | assert_return(!journal_pid_changed(j), -ECHILD); | |
1052 | assert_return(!isempty(cursor), -EINVAL); | |
c6511e85 LP |
1053 | |
1054 | if (!j->current_file || j->current_file->current_offset <= 0) | |
1055 | return -EADDRNOTAVAIL; | |
1056 | ||
1057 | r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o); | |
1058 | if (r < 0) | |
1059 | return r; | |
1060 | ||
9ed794a3 | 1061 | for (;;) { |
c6511e85 | 1062 | _cleanup_free_ char *item = NULL; |
c6511e85 | 1063 | unsigned long long ll; |
7294bb5b | 1064 | sd_id128_t id; |
c6511e85 LP |
1065 | int k = 0; |
1066 | ||
7294bb5b SS |
1067 | r = extract_first_word(&cursor, &item, ";", EXTRACT_DONT_COALESCE_SEPARATORS); |
1068 | if (r < 0) | |
1069 | return r; | |
c6511e85 | 1070 | |
7294bb5b SS |
1071 | if (r == 0) |
1072 | break; | |
c6511e85 | 1073 | |
7294bb5b SS |
1074 | if (strlen(item) < 2 || item[1] != '=') |
1075 | return -EINVAL; | |
1076 | ||
1077 | switch (item[0]) { | |
c6511e85 LP |
1078 | |
1079 | case 's': | |
1080 | k = sd_id128_from_string(item+2, &id); | |
1081 | if (k < 0) | |
1082 | return k; | |
1083 | if (!sd_id128_equal(id, j->current_file->header->seqnum_id)) | |
1084 | return 0; | |
1085 | break; | |
1086 | ||
1087 | case 'i': | |
1088 | if (sscanf(item+2, "%llx", &ll) != 1) | |
1089 | return -EINVAL; | |
1090 | if (ll != le64toh(o->entry.seqnum)) | |
1091 | return 0; | |
1092 | break; | |
1093 | ||
1094 | case 'b': | |
1095 | k = sd_id128_from_string(item+2, &id); | |
1096 | if (k < 0) | |
1097 | return k; | |
1098 | if (!sd_id128_equal(id, o->entry.boot_id)) | |
1099 | return 0; | |
1100 | break; | |
1101 | ||
1102 | case 'm': | |
1103 | if (sscanf(item+2, "%llx", &ll) != 1) | |
1104 | return -EINVAL; | |
1105 | if (ll != le64toh(o->entry.monotonic)) | |
1106 | return 0; | |
1107 | break; | |
1108 | ||
1109 | case 't': | |
1110 | if (sscanf(item+2, "%llx", &ll) != 1) | |
1111 | return -EINVAL; | |
1112 | if (ll != le64toh(o->entry.realtime)) | |
1113 | return 0; | |
1114 | break; | |
1115 | ||
1116 | case 'x': | |
1117 | if (sscanf(item+2, "%llx", &ll) != 1) | |
1118 | return -EINVAL; | |
1119 | if (ll != le64toh(o->entry.xor_hash)) | |
1120 | return 0; | |
1121 | break; | |
1122 | } | |
1123 | } | |
1124 | ||
1125 | return 1; | |
1126 | } | |
1127 | ||
a5344d2c | 1128 | _public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) { |
1ae464e0 TA |
1129 | assert_return(j, -EINVAL); |
1130 | assert_return(!journal_pid_changed(j), -ECHILD); | |
de190aef LP |
1131 | |
1132 | reset_location(j); | |
a87247dd | 1133 | j->current_location.type = LOCATION_SEEK; |
de190aef LP |
1134 | j->current_location.boot_id = boot_id; |
1135 | j->current_location.monotonic = usec; | |
1136 | j->current_location.monotonic_set = true; | |
1137 | ||
1138 | return 0; | |
1139 | } | |
1140 | ||
a5344d2c | 1141 | _public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) { |
1ae464e0 TA |
1142 | assert_return(j, -EINVAL); |
1143 | assert_return(!journal_pid_changed(j), -ECHILD); | |
de190aef LP |
1144 | |
1145 | reset_location(j); | |
a87247dd | 1146 | j->current_location.type = LOCATION_SEEK; |
de190aef LP |
1147 | j->current_location.realtime = usec; |
1148 | j->current_location.realtime_set = true; | |
1149 | ||
1150 | return 0; | |
1151 | } | |
1152 | ||
a5344d2c | 1153 | _public_ int sd_journal_seek_head(sd_journal *j) { |
1ae464e0 TA |
1154 | assert_return(j, -EINVAL); |
1155 | assert_return(!journal_pid_changed(j), -ECHILD); | |
de190aef LP |
1156 | |
1157 | reset_location(j); | |
1158 | j->current_location.type = LOCATION_HEAD; | |
1159 | ||
1160 | return 0; | |
1161 | } | |
1162 | ||
a5344d2c | 1163 | _public_ int sd_journal_seek_tail(sd_journal *j) { |
1ae464e0 TA |
1164 | assert_return(j, -EINVAL); |
1165 | assert_return(!journal_pid_changed(j), -ECHILD); | |
de190aef LP |
1166 | |
1167 | reset_location(j); | |
1168 | j->current_location.type = LOCATION_TAIL; | |
1169 | ||
1170 | return 0; | |
87d2c1ff LP |
1171 | } |
1172 | ||
85210bff | 1173 | static void check_network(sd_journal *j, int fd) { |
85210bff LP |
1174 | assert(j); |
1175 | ||
1176 | if (j->on_network) | |
1177 | return; | |
1178 | ||
77f9fa3b | 1179 | j->on_network = fd_is_network_fs(fd); |
85210bff LP |
1180 | } |
1181 | ||
a688baa8 ZJS |
1182 | static bool file_has_type_prefix(const char *prefix, const char *filename) { |
1183 | const char *full, *tilded, *atted; | |
1184 | ||
63c372cb LP |
1185 | full = strjoina(prefix, ".journal"); |
1186 | tilded = strjoina(full, "~"); | |
1187 | atted = strjoina(prefix, "@"); | |
a688baa8 | 1188 | |
0cbd293e | 1189 | return STR_IN_SET(filename, full, tilded) || |
a688baa8 ZJS |
1190 | startswith(filename, atted); |
1191 | } | |
1192 | ||
1193 | static bool file_type_wanted(int flags, const char *filename) { | |
d617408e LP |
1194 | assert(filename); |
1195 | ||
a688baa8 ZJS |
1196 | if (!endswith(filename, ".journal") && !endswith(filename, ".journal~")) |
1197 | return false; | |
1198 | ||
1199 | /* no flags set → every type is OK */ | |
1200 | if (!(flags & (SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER))) | |
1201 | return true; | |
1202 | ||
1203 | if (flags & SD_JOURNAL_SYSTEM && file_has_type_prefix("system", filename)) | |
1204 | return true; | |
1205 | ||
1206 | if (flags & SD_JOURNAL_CURRENT_USER) { | |
1207 | char prefix[5 + DECIMAL_STR_MAX(uid_t) + 1]; | |
1208 | ||
5ffa8c81 | 1209 | xsprintf(prefix, "user-"UID_FMT, getuid()); |
a688baa8 ZJS |
1210 | |
1211 | if (file_has_type_prefix(prefix, filename)) | |
1212 | return true; | |
1213 | } | |
1214 | ||
1215 | return false; | |
1216 | } | |
1217 | ||
5d1ce257 LP |
1218 | static bool path_has_prefix(sd_journal *j, const char *path, const char *prefix) { |
1219 | assert(j); | |
1220 | assert(path); | |
1221 | assert(prefix); | |
1222 | ||
1223 | if (j->toplevel_fd >= 0) | |
1224 | return false; | |
1225 | ||
1226 | return path_startswith(path, prefix); | |
1227 | } | |
1228 | ||
9c66f528 LP |
1229 | static void track_file_disposition(sd_journal *j, JournalFile *f) { |
1230 | assert(j); | |
1231 | assert(f); | |
1232 | ||
1233 | if (!j->has_runtime_files && path_has_prefix(j, f->path, "/run")) | |
1234 | j->has_runtime_files = true; | |
1235 | else if (!j->has_persistent_files && path_has_prefix(j, f->path, "/var")) | |
1236 | j->has_persistent_files = true; | |
1237 | } | |
1238 | ||
5d1ce257 LP |
1239 | static const char *skip_slash(const char *p) { |
1240 | ||
1241 | if (!p) | |
1242 | return NULL; | |
1243 | ||
1244 | while (*p == '/') | |
1245 | p++; | |
1246 | ||
1247 | return p; | |
1248 | } | |
1249 | ||
9c66f528 LP |
1250 | static int add_any_file( |
1251 | sd_journal *j, | |
1252 | int fd, | |
1253 | const char *path) { | |
1254 | ||
5d1ce257 | 1255 | bool close_fd = false; |
9c66f528 LP |
1256 | JournalFile *f; |
1257 | struct stat st; | |
d617408e | 1258 | int r, k; |
3fbf9cbb LP |
1259 | |
1260 | assert(j); | |
5d1ce257 | 1261 | assert(fd >= 0 || path); |
3fbf9cbb | 1262 | |
9c66f528 LP |
1263 | if (fd < 0) { |
1264 | if (j->toplevel_fd >= 0) | |
1265 | /* If there's a top-level fd defined make the path relative, explicitly, since otherwise | |
1266 | * openat() ignores the first argument. */ | |
1267 | ||
1268 | fd = openat(j->toplevel_fd, skip_slash(path), O_RDONLY|O_CLOEXEC|O_NONBLOCK); | |
1269 | else | |
1270 | fd = open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK); | |
1271 | if (fd < 0) { | |
1272 | r = log_debug_errno(errno, "Failed to open journal file %s: %m", path); | |
1273 | goto finish; | |
1274 | } | |
1275 | ||
1276 | close_fd = true; | |
1277 | ||
1278 | r = fd_nonblock(fd, false); | |
1279 | if (r < 0) { | |
1280 | r = log_debug_errno(errno, "Failed to turn off O_NONBLOCK for %s: %m", path); | |
1281 | goto finish; | |
858749f7 LP |
1282 | } |
1283 | } | |
50f20cfd | 1284 | |
9c66f528 LP |
1285 | if (fstat(fd, &st) < 0) { |
1286 | r = log_debug_errno(errno, "Failed to fstat file '%s': %m", path); | |
1287 | goto finish; | |
1288 | } | |
3cc44114 LP |
1289 | |
1290 | r = stat_verify_regular(&st); | |
1291 | if (r < 0) { | |
1292 | log_debug_errno(r, "Refusing to open '%s', as it is not a regular file.", path); | |
9c66f528 | 1293 | goto finish; |
9e8abdf0 LP |
1294 | } |
1295 | ||
9c66f528 LP |
1296 | f = ordered_hashmap_get(j->files, path); |
1297 | if (f) { | |
1298 | if (f->last_stat.st_dev == st.st_dev && | |
1299 | f->last_stat.st_ino == st.st_ino) { | |
5d1ce257 | 1300 | |
9c66f528 LP |
1301 | /* We already track this file, under the same path and with the same device/inode numbers, it's |
1302 | * hence really the same. Mark this file as seen in this generation. This is used to GC old | |
1303 | * files in process_q_overflow() to detect journal files that are still there and discern them | |
1304 | * from those which are gone. */ | |
5d1ce257 | 1305 | |
9c66f528 LP |
1306 | f->last_seen_generation = j->generation; |
1307 | r = 0; | |
1308 | goto finish; | |
5d1ce257 LP |
1309 | } |
1310 | ||
9c66f528 LP |
1311 | /* So we tracked a file under this name, but it has a different inode/device. In that case, it got |
1312 | * replaced (probably due to rotation?), let's drop it hence from our list. */ | |
1313 | remove_file_real(j, f); | |
1314 | f = NULL; | |
1315 | } | |
1316 | ||
1317 | if (ordered_hashmap_size(j->files) >= JOURNAL_FILES_MAX) { | |
1318 | log_debug("Too many open journal files, not adding %s.", path); | |
1319 | r = -ETOOMANYREFS; | |
1320 | goto finish; | |
5d1ce257 LP |
1321 | } |
1322 | ||
57850536 | 1323 | r = journal_file_open(fd, path, O_RDONLY, 0, false, 0, false, NULL, j->mmap, NULL, NULL, &f); |
d617408e LP |
1324 | if (r < 0) { |
1325 | log_debug_errno(r, "Failed to open journal file %s: %m", path); | |
9c66f528 | 1326 | goto finish; |
d617408e | 1327 | } |
3fbf9cbb | 1328 | |
72f59706 | 1329 | /* journal_file_dump(f); */ |
de190aef | 1330 | |
c1f906bd | 1331 | r = ordered_hashmap_put(j->files, f->path, f); |
3fbf9cbb | 1332 | if (r < 0) { |
9c66f528 | 1333 | f->close_fd = false; /* make sure journal_file_close() doesn't close the caller's fd (or our own). We'll let the caller do that, or ourselves */ |
69a3a6fd | 1334 | (void) journal_file_close(f); |
9c66f528 | 1335 | goto finish; |
3fbf9cbb LP |
1336 | } |
1337 | ||
9c66f528 | 1338 | close_fd = false; /* the fd is now owned by the JournalFile object */ |
5d1ce257 | 1339 | |
9c66f528 | 1340 | f->last_seen_generation = j->generation; |
a50d7d43 | 1341 | |
9c66f528 | 1342 | track_file_disposition(j, f); |
85210bff LP |
1343 | check_network(j, f->fd); |
1344 | ||
313cefa1 | 1345 | j->current_invalidate_counter++; |
a963990f | 1346 | |
9c66f528 | 1347 | log_debug("File %s added.", f->path); |
d617408e | 1348 | |
9c66f528 LP |
1349 | r = 0; |
1350 | ||
1351 | finish: | |
1352 | if (close_fd) | |
1353 | safe_close(fd); | |
1354 | ||
1355 | if (r < 0) { | |
1356 | k = journal_put_error(j, r, path); | |
1357 | if (k < 0) | |
1358 | return k; | |
1359 | } | |
d617408e LP |
1360 | |
1361 | return r; | |
50f20cfd LP |
1362 | } |
1363 | ||
fc1813c0 LP |
1364 | static int add_file_by_name( |
1365 | sd_journal *j, | |
1366 | const char *prefix, | |
1367 | const char *filename) { | |
1368 | ||
d617408e | 1369 | const char *path; |
5302ebe1 ZJS |
1370 | |
1371 | assert(j); | |
1372 | assert(prefix); | |
1373 | assert(filename); | |
1374 | ||
5d1ce257 | 1375 | if (j->no_new_files) |
5302ebe1 ZJS |
1376 | return 0; |
1377 | ||
5d1ce257 LP |
1378 | if (!file_type_wanted(j->flags, filename)) |
1379 | return 0; | |
39fd5b08 | 1380 | |
270384b2 | 1381 | path = prefix_roota(prefix, filename); |
5d1ce257 | 1382 | return add_any_file(j, -1, path); |
5302ebe1 ZJS |
1383 | } |
1384 | ||
fc1813c0 LP |
1385 | static void remove_file_by_name( |
1386 | sd_journal *j, | |
1387 | const char *prefix, | |
1388 | const char *filename) { | |
1389 | ||
d617408e | 1390 | const char *path; |
50f20cfd LP |
1391 | JournalFile *f; |
1392 | ||
1393 | assert(j); | |
1394 | assert(prefix); | |
1395 | assert(filename); | |
1396 | ||
270384b2 | 1397 | path = prefix_roota(prefix, filename); |
c1f906bd | 1398 | f = ordered_hashmap_get(j->files, path); |
50f20cfd | 1399 | if (!f) |
d617408e | 1400 | return; |
50f20cfd | 1401 | |
a9a245c1 | 1402 | remove_file_real(j, f); |
a9a245c1 ZJS |
1403 | } |
1404 | ||
1405 | static void remove_file_real(sd_journal *j, JournalFile *f) { | |
1406 | assert(j); | |
1407 | assert(f); | |
1408 | ||
fc1813c0 | 1409 | (void) ordered_hashmap_remove(j->files, f->path); |
44a5fa34 | 1410 | |
5ec76417 | 1411 | log_debug("File %s removed.", f->path); |
44a5fa34 | 1412 | |
3c1668da LP |
1413 | if (j->current_file == f) { |
1414 | j->current_file = NULL; | |
1415 | j->current_field = 0; | |
1416 | } | |
1417 | ||
1418 | if (j->unique_file == f) { | |
360af4cf | 1419 | /* Jump to the next unique_file or NULL if that one was last */ |
c1f906bd | 1420 | j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path); |
3c1668da | 1421 | j->unique_offset = 0; |
360af4cf ZJS |
1422 | if (!j->unique_file) |
1423 | j->unique_file_lost = true; | |
3c1668da LP |
1424 | } |
1425 | ||
eb86030e LP |
1426 | if (j->fields_file == f) { |
1427 | j->fields_file = ordered_hashmap_next(j->files, j->fields_file->path); | |
1428 | j->fields_offset = 0; | |
1429 | if (!j->fields_file) | |
1430 | j->fields_file_lost = true; | |
1431 | } | |
1432 | ||
69a3a6fd | 1433 | (void) journal_file_close(f); |
50f20cfd | 1434 | |
313cefa1 | 1435 | j->current_invalidate_counter++; |
3fbf9cbb LP |
1436 | } |
1437 | ||
d617408e LP |
1438 | static int dirname_is_machine_id(const char *fn) { |
1439 | sd_id128_t id, machine; | |
456aa879 | 1440 | const char *e; |
d617408e LP |
1441 | int r; |
1442 | ||
456aa879 LP |
1443 | /* Returns true if the specified directory name matches the local machine ID */ |
1444 | ||
d617408e LP |
1445 | r = sd_id128_get_machine(&machine); |
1446 | if (r < 0) | |
1447 | return r; | |
1448 | ||
456aa879 LP |
1449 | e = strchr(fn, '.'); |
1450 | if (e) { | |
1451 | const char *k; | |
1452 | ||
1453 | /* Looks like it has a namespace suffix. Verify that. */ | |
1454 | if (!log_namespace_name_valid(e + 1)) | |
1455 | return false; | |
1456 | ||
1457 | k = strndupa(fn, e - fn); | |
1458 | r = sd_id128_from_string(k, &id); | |
1459 | } else | |
1460 | r = sd_id128_from_string(fn, &id); | |
d617408e LP |
1461 | if (r < 0) |
1462 | return r; | |
1463 | ||
1464 | return sd_id128_equal(id, machine); | |
1465 | } | |
1466 | ||
456aa879 LP |
1467 | static int dirname_has_namespace(const char *fn, const char *namespace) { |
1468 | const char *e; | |
1469 | ||
1470 | /* Returns true if the specified directory name matches the specified namespace */ | |
1471 | ||
1472 | e = strchr(fn, '.'); | |
1473 | if (e) { | |
1474 | const char *k; | |
1475 | ||
1476 | if (!namespace) | |
1477 | return false; | |
1478 | ||
1479 | if (!streq(e + 1, namespace)) | |
1480 | return false; | |
1481 | ||
1482 | k = strndupa(fn, e - fn); | |
1483 | return id128_is_valid(k); | |
1484 | } | |
1485 | ||
1486 | if (namespace) | |
1487 | return false; | |
1488 | ||
1489 | return id128_is_valid(fn); | |
1490 | } | |
1491 | ||
858749f7 LP |
1492 | static bool dirent_is_journal_file(const struct dirent *de) { |
1493 | assert(de); | |
1494 | ||
456aa879 LP |
1495 | /* Returns true if the specified directory entry looks like a journal file we might be interested in */ |
1496 | ||
858749f7 LP |
1497 | if (!IN_SET(de->d_type, DT_REG, DT_LNK, DT_UNKNOWN)) |
1498 | return false; | |
1499 | ||
1500 | return endswith(de->d_name, ".journal") || | |
1501 | endswith(de->d_name, ".journal~"); | |
1502 | } | |
1503 | ||
456aa879 LP |
1504 | static bool dirent_is_journal_subdir(const struct dirent *de) { |
1505 | const char *e, *n; | |
858749f7 LP |
1506 | assert(de); |
1507 | ||
456aa879 LP |
1508 | /* returns true if the specified directory entry looks like a directory that might contain journal |
1509 | * files we might be interested in, i.e. is either a 128bit ID or a 128bit ID suffixed by a | |
1510 | * namespace. */ | |
1511 | ||
858749f7 LP |
1512 | if (!IN_SET(de->d_type, DT_DIR, DT_LNK, DT_UNKNOWN)) |
1513 | return false; | |
1514 | ||
456aa879 LP |
1515 | e = strchr(de->d_name, '.'); |
1516 | if (!e) | |
1517 | return id128_is_valid(de->d_name); /* No namespace */ | |
1518 | ||
1519 | n = strndupa(de->d_name, e - de->d_name); | |
1520 | if (!id128_is_valid(n)) | |
1521 | return false; | |
1522 | ||
1523 | return log_namespace_name_valid(e + 1); | |
858749f7 LP |
1524 | } |
1525 | ||
1526 | static int directory_open(sd_journal *j, const char *path, DIR **ret) { | |
1527 | DIR *d; | |
1528 | ||
1529 | assert(j); | |
1530 | assert(path); | |
1531 | assert(ret); | |
1532 | ||
1533 | if (j->toplevel_fd < 0) | |
1534 | d = opendir(path); | |
1535 | else | |
1536 | /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is | |
1537 | * relative, by dropping the initial slash */ | |
1538 | d = xopendirat(j->toplevel_fd, skip_slash(path), 0); | |
1539 | if (!d) | |
1540 | return -errno; | |
1541 | ||
1542 | *ret = d; | |
1543 | return 0; | |
1544 | } | |
1545 | ||
1546 | static int add_directory(sd_journal *j, const char *prefix, const char *dirname); | |
1547 | ||
1548 | static void directory_enumerate(sd_journal *j, Directory *m, DIR *d) { | |
1549 | struct dirent *de; | |
1550 | ||
1551 | assert(j); | |
1552 | assert(m); | |
1553 | assert(d); | |
1554 | ||
1555 | FOREACH_DIRENT_ALL(de, d, goto fail) { | |
fc1813c0 | 1556 | |
858749f7 | 1557 | if (dirent_is_journal_file(de)) |
fc1813c0 | 1558 | (void) add_file_by_name(j, m->path, de->d_name); |
858749f7 | 1559 | |
456aa879 | 1560 | if (m->is_root && dirent_is_journal_subdir(de)) |
858749f7 LP |
1561 | (void) add_directory(j, m->path, de->d_name); |
1562 | } | |
1563 | ||
1564 | return; | |
1565 | ||
1566 | fail: | |
1567 | log_debug_errno(errno, "Failed to enumerate directory %s, ignoring: %m", m->path); | |
1568 | } | |
1569 | ||
1570 | static void directory_watch(sd_journal *j, Directory *m, int fd, uint32_t mask) { | |
1571 | int r; | |
1572 | ||
1573 | assert(j); | |
1574 | assert(m); | |
1575 | assert(fd >= 0); | |
1576 | ||
1577 | /* Watch this directory if that's enabled and if it not being watched yet. */ | |
1578 | ||
1579 | if (m->wd > 0) /* Already have a watch? */ | |
1580 | return; | |
1581 | if (j->inotify_fd < 0) /* Not watching at all? */ | |
1582 | return; | |
1583 | ||
1584 | m->wd = inotify_add_watch_fd(j->inotify_fd, fd, mask); | |
1585 | if (m->wd < 0) { | |
1586 | log_debug_errno(errno, "Failed to watch journal directory '%s', ignoring: %m", m->path); | |
1587 | return; | |
1588 | } | |
1589 | ||
1590 | r = hashmap_put(j->directories_by_wd, INT_TO_PTR(m->wd), m); | |
1591 | if (r == -EEXIST) | |
1592 | log_debug_errno(r, "Directory '%s' already being watched under a different path, ignoring: %m", m->path); | |
1593 | if (r < 0) { | |
1594 | log_debug_errno(r, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m->path); | |
1595 | (void) inotify_rm_watch(j->inotify_fd, m->wd); | |
1596 | m->wd = -1; | |
1597 | } | |
1598 | } | |
1599 | ||
456aa879 LP |
1600 | static int add_directory( |
1601 | sd_journal *j, | |
1602 | const char *prefix, | |
1603 | const char *dirname) { | |
1604 | ||
7fd1b19b | 1605 | _cleanup_free_ char *path = NULL; |
7fd1b19b | 1606 | _cleanup_closedir_ DIR *d = NULL; |
a963990f | 1607 | Directory *m; |
d617408e | 1608 | int r, k; |
3fbf9cbb LP |
1609 | |
1610 | assert(j); | |
1611 | assert(prefix); | |
3fbf9cbb | 1612 | |
5d1ce257 LP |
1613 | /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch |
1614 | * and reenumerates directory contents */ | |
d95b1fb3 | 1615 | |
657ee2d8 | 1616 | path = path_join(prefix, dirname); |
d617408e LP |
1617 | if (!path) { |
1618 | r = -ENOMEM; | |
1619 | goto fail; | |
1620 | } | |
3fbf9cbb | 1621 | |
858749f7 | 1622 | log_debug("Considering directory '%s'.", path); |
5d1ce257 LP |
1623 | |
1624 | /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */ | |
1625 | if ((j->flags & SD_JOURNAL_LOCAL_ONLY) && | |
1626 | !((dirname && dirname_is_machine_id(dirname) > 0) || path_has_prefix(j, path, "/run"))) | |
858749f7 | 1627 | return 0; |
5d1ce257 | 1628 | |
456aa879 LP |
1629 | if (!(FLAGS_SET(j->flags, SD_JOURNAL_ALL_NAMESPACES) || |
1630 | dirname_has_namespace(dirname, j->namespace) > 0 || | |
1631 | (FLAGS_SET(j->flags, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE) && dirname_has_namespace(dirname, NULL) > 0))) | |
1632 | return 0; | |
1633 | ||
858749f7 LP |
1634 | r = directory_open(j, path, &d); |
1635 | if (r < 0) { | |
1636 | log_debug_errno(r, "Failed to open directory '%s': %m", path); | |
d617408e | 1637 | goto fail; |
3fbf9cbb LP |
1638 | } |
1639 | ||
a963990f LP |
1640 | m = hashmap_get(j->directories_by_path, path); |
1641 | if (!m) { | |
2f5435a1 | 1642 | m = new(Directory, 1); |
d617408e LP |
1643 | if (!m) { |
1644 | r = -ENOMEM; | |
1645 | goto fail; | |
1646 | } | |
a963990f | 1647 | |
2f5435a1 LP |
1648 | *m = (Directory) { |
1649 | .is_root = false, | |
1650 | .path = path, | |
1651 | }; | |
a963990f LP |
1652 | |
1653 | if (hashmap_put(j->directories_by_path, m->path, m) < 0) { | |
a963990f | 1654 | free(m); |
d617408e LP |
1655 | r = -ENOMEM; |
1656 | goto fail; | |
a963990f LP |
1657 | } |
1658 | ||
a50d7d43 | 1659 | path = NULL; /* avoid freeing in cleanup */ |
313cefa1 | 1660 | j->current_invalidate_counter++; |
a963990f | 1661 | |
5ec76417 | 1662 | log_debug("Directory %s added.", m->path); |
a963990f | 1663 | |
a50d7d43 | 1664 | } else if (m->is_root) |
858749f7 | 1665 | return 0; /* Don't 'downgrade' from root directory */ |
a963990f | 1666 | |
858749f7 | 1667 | m->last_seen_generation = j->generation; |
a963990f | 1668 | |
858749f7 LP |
1669 | directory_watch(j, m, dirfd(d), |
1670 | IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE| | |
1671 | IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|IN_MOVED_FROM| | |
1672 | IN_ONLYDIR); | |
a963990f | 1673 | |
858749f7 LP |
1674 | if (!j->no_new_files) |
1675 | directory_enumerate(j, m, d); | |
a963990f | 1676 | |
85210bff LP |
1677 | check_network(j, dirfd(d)); |
1678 | ||
a963990f | 1679 | return 0; |
d617408e LP |
1680 | |
1681 | fail: | |
5d1ce257 | 1682 | k = journal_put_error(j, r, path ?: prefix); |
d617408e LP |
1683 | if (k < 0) |
1684 | return k; | |
1685 | ||
1686 | return r; | |
a963990f LP |
1687 | } |
1688 | ||
d617408e | 1689 | static int add_root_directory(sd_journal *j, const char *p, bool missing_ok) { |
5d1ce257 | 1690 | |
7fd1b19b | 1691 | _cleanup_closedir_ DIR *d = NULL; |
a963990f | 1692 | Directory *m; |
d617408e | 1693 | int r, k; |
a963990f LP |
1694 | |
1695 | assert(j); | |
a963990f | 1696 | |
5d1ce257 LP |
1697 | /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we |
1698 | * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially | |
1699 | * populate the set, as well as to update it later. */ | |
a963990f | 1700 | |
5d1ce257 LP |
1701 | if (p) { |
1702 | /* If there's a path specified, use it. */ | |
b6741478 | 1703 | |
858749f7 LP |
1704 | log_debug("Considering root directory '%s'.", p); |
1705 | ||
5d1ce257 LP |
1706 | if ((j->flags & SD_JOURNAL_RUNTIME_ONLY) && |
1707 | !path_has_prefix(j, p, "/run")) | |
1708 | return -EINVAL; | |
d617408e | 1709 | |
5d1ce257 LP |
1710 | if (j->prefix) |
1711 | p = strjoina(j->prefix, p); | |
1712 | ||
858749f7 LP |
1713 | r = directory_open(j, p, &d); |
1714 | if (r == -ENOENT && missing_ok) | |
1715 | return 0; | |
1716 | if (r < 0) { | |
1717 | log_debug_errno(r, "Failed to open root directory %s: %m", p); | |
5d1ce257 LP |
1718 | goto fail; |
1719 | } | |
1720 | } else { | |
8e06af80 | 1721 | _cleanup_close_ int dfd = -1; |
5d1ce257 LP |
1722 | |
1723 | /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since | |
1724 | * opendir() will take possession of the fd, and close it, which we don't want. */ | |
1725 | ||
1726 | p = "."; /* store this as "." in the directories hashmap */ | |
1727 | ||
1728 | dfd = fcntl(j->toplevel_fd, F_DUPFD_CLOEXEC, 3); | |
1729 | if (dfd < 0) { | |
1730 | r = -errno; | |
1731 | goto fail; | |
1732 | } | |
1733 | ||
8e06af80 | 1734 | d = take_fdopendir(&dfd); |
5d1ce257 LP |
1735 | if (!d) { |
1736 | r = -errno; | |
5d1ce257 LP |
1737 | goto fail; |
1738 | } | |
1739 | ||
1740 | rewinddir(d); | |
d617408e | 1741 | } |
a963990f LP |
1742 | |
1743 | m = hashmap_get(j->directories_by_path, p); | |
1744 | if (!m) { | |
1745 | m = new0(Directory, 1); | |
d617408e LP |
1746 | if (!m) { |
1747 | r = -ENOMEM; | |
1748 | goto fail; | |
1749 | } | |
a963990f LP |
1750 | |
1751 | m->is_root = true; | |
5d1ce257 | 1752 | |
a963990f LP |
1753 | m->path = strdup(p); |
1754 | if (!m->path) { | |
a963990f | 1755 | free(m); |
d617408e LP |
1756 | r = -ENOMEM; |
1757 | goto fail; | |
a963990f LP |
1758 | } |
1759 | ||
1760 | if (hashmap_put(j->directories_by_path, m->path, m) < 0) { | |
a963990f LP |
1761 | free(m->path); |
1762 | free(m); | |
d617408e LP |
1763 | r = -ENOMEM; |
1764 | goto fail; | |
a963990f LP |
1765 | } |
1766 | ||
313cefa1 | 1767 | j->current_invalidate_counter++; |
a963990f | 1768 | |
5ec76417 | 1769 | log_debug("Root directory %s added.", m->path); |
a963990f | 1770 | |
a50d7d43 | 1771 | } else if (!m->is_root) |
a963990f | 1772 | return 0; |
50f20cfd | 1773 | |
858749f7 LP |
1774 | directory_watch(j, m, dirfd(d), |
1775 | IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE| | |
1776 | IN_ONLYDIR); | |
a963990f | 1777 | |
858749f7 LP |
1778 | if (!j->no_new_files) |
1779 | directory_enumerate(j, m, d); | |
3fbf9cbb | 1780 | |
85210bff LP |
1781 | check_network(j, dirfd(d)); |
1782 | ||
a963990f | 1783 | return 0; |
d617408e LP |
1784 | |
1785 | fail: | |
5768d259 | 1786 | k = journal_put_error(j, r, p); |
d617408e LP |
1787 | if (k < 0) |
1788 | return k; | |
1789 | ||
1790 | return r; | |
a963990f LP |
1791 | } |
1792 | ||
b2b46f91 | 1793 | static void remove_directory(sd_journal *j, Directory *d) { |
a963990f LP |
1794 | assert(j); |
1795 | ||
1796 | if (d->wd > 0) { | |
1797 | hashmap_remove(j->directories_by_wd, INT_TO_PTR(d->wd)); | |
1798 | ||
1799 | if (j->inotify_fd >= 0) | |
98d4a4df | 1800 | (void) inotify_rm_watch(j->inotify_fd, d->wd); |
a963990f LP |
1801 | } |
1802 | ||
1803 | hashmap_remove(j->directories_by_path, d->path); | |
1804 | ||
1805 | if (d->is_root) | |
5ec76417 | 1806 | log_debug("Root directory %s removed.", d->path); |
a963990f | 1807 | else |
5ec76417 | 1808 | log_debug("Directory %s removed.", d->path); |
a963990f LP |
1809 | |
1810 | free(d->path); | |
1811 | free(d); | |
3fbf9cbb LP |
1812 | } |
1813 | ||
89739579 | 1814 | static int add_search_paths(sd_journal *j) { |
d617408e LP |
1815 | |
1816 | static const char search_paths[] = | |
a963990f LP |
1817 | "/run/log/journal\0" |
1818 | "/var/log/journal\0"; | |
1819 | const char *p; | |
50f20cfd LP |
1820 | |
1821 | assert(j); | |
50f20cfd | 1822 | |
a963990f LP |
1823 | /* We ignore most errors here, since the idea is to only open |
1824 | * what's actually accessible, and ignore the rest. */ | |
50f20cfd | 1825 | |
d617408e LP |
1826 | NULSTR_FOREACH(p, search_paths) |
1827 | (void) add_root_directory(j, p, true); | |
50f20cfd | 1828 | |
574b77ef MB |
1829 | if (!(j->flags & SD_JOURNAL_LOCAL_ONLY)) |
1830 | (void) add_root_directory(j, "/var/log/journal/remote", true); | |
1831 | ||
a963990f | 1832 | return 0; |
50f20cfd LP |
1833 | } |
1834 | ||
5302ebe1 ZJS |
1835 | static int add_current_paths(sd_journal *j) { |
1836 | Iterator i; | |
1837 | JournalFile *f; | |
1838 | ||
1839 | assert(j); | |
1840 | assert(j->no_new_files); | |
1841 | ||
5d1ce257 | 1842 | /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we |
5302ebe1 ZJS |
1843 | * treat them as fatal. */ |
1844 | ||
c1f906bd | 1845 | ORDERED_HASHMAP_FOREACH(f, j->files, i) { |
5302ebe1 | 1846 | _cleanup_free_ char *dir; |
e9174f29 | 1847 | int r; |
5302ebe1 ZJS |
1848 | |
1849 | dir = dirname_malloc(f->path); | |
1850 | if (!dir) | |
1851 | return -ENOMEM; | |
1852 | ||
5d1ce257 | 1853 | r = add_directory(j, dir, NULL); |
d617408e | 1854 | if (r < 0) |
5302ebe1 | 1855 | return r; |
5302ebe1 ZJS |
1856 | } |
1857 | ||
1858 | return 0; | |
1859 | } | |
1860 | ||
a963990f | 1861 | static int allocate_inotify(sd_journal *j) { |
50f20cfd | 1862 | assert(j); |
50f20cfd | 1863 | |
a963990f LP |
1864 | if (j->inotify_fd < 0) { |
1865 | j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); | |
1866 | if (j->inotify_fd < 0) | |
1867 | return -errno; | |
1868 | } | |
50f20cfd | 1869 | |
cb306f5d | 1870 | return hashmap_ensure_allocated(&j->directories_by_wd, NULL); |
50f20cfd LP |
1871 | } |
1872 | ||
456aa879 | 1873 | static sd_journal *journal_new(int flags, const char *path, const char *namespace) { |
17c9aff8 | 1874 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
50f20cfd | 1875 | |
a963990f LP |
1876 | j = new0(sd_journal, 1); |
1877 | if (!j) | |
1878 | return NULL; | |
50f20cfd | 1879 | |
df0ff127 | 1880 | j->original_pid = getpid_cached(); |
5d1ce257 | 1881 | j->toplevel_fd = -1; |
a963990f LP |
1882 | j->inotify_fd = -1; |
1883 | j->flags = flags; | |
93b73b06 | 1884 | j->data_threshold = DEFAULT_DATA_THRESHOLD; |
50f20cfd | 1885 | |
7827b1a1 | 1886 | if (path) { |
16fefe90 ZJS |
1887 | char *t; |
1888 | ||
1889 | t = strdup(path); | |
1890 | if (!t) | |
17c9aff8 | 1891 | return NULL; |
16fefe90 ZJS |
1892 | |
1893 | if (flags & SD_JOURNAL_OS_ROOT) | |
1894 | j->prefix = t; | |
1895 | else | |
1896 | j->path = t; | |
7827b1a1 LP |
1897 | } |
1898 | ||
456aa879 LP |
1899 | if (namespace) { |
1900 | j->namespace = strdup(namespace); | |
1901 | if (!j->namespace) | |
1902 | return NULL; | |
1903 | } | |
1904 | ||
548f6937 | 1905 | j->files = ordered_hashmap_new(&path_hash_ops); |
5d4ba7f2 | 1906 | if (!j->files) |
17c9aff8 | 1907 | return NULL; |
5d4ba7f2 VC |
1908 | |
1909 | j->files_cache = ordered_hashmap_iterated_cache_new(j->files); | |
548f6937 | 1910 | j->directories_by_path = hashmap_new(&path_hash_ops); |
84168d80 | 1911 | j->mmap = mmap_cache_new(); |
5d4ba7f2 | 1912 | if (!j->files_cache || !j->directories_by_path || !j->mmap) |
17c9aff8 | 1913 | return NULL; |
6180fc61 | 1914 | |
17c9aff8 | 1915 | return TAKE_PTR(j); |
50f20cfd LP |
1916 | } |
1917 | ||
1aaa68f5 ZJS |
1918 | #define OPEN_ALLOWED_FLAGS \ |
1919 | (SD_JOURNAL_LOCAL_ONLY | \ | |
1920 | SD_JOURNAL_RUNTIME_ONLY | \ | |
456aa879 LP |
1921 | SD_JOURNAL_SYSTEM | \ |
1922 | SD_JOURNAL_CURRENT_USER | \ | |
1923 | SD_JOURNAL_ALL_NAMESPACES | \ | |
1924 | SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE) | |
1aaa68f5 | 1925 | |
456aa879 | 1926 | _public_ int sd_journal_open_namespace(sd_journal **ret, const char *namespace, int flags) { |
17c9aff8 | 1927 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
3fbf9cbb | 1928 | int r; |
87d2c1ff | 1929 | |
1ae464e0 | 1930 | assert_return(ret, -EINVAL); |
1aaa68f5 | 1931 | assert_return((flags & ~OPEN_ALLOWED_FLAGS) == 0, -EINVAL); |
87d2c1ff | 1932 | |
456aa879 | 1933 | j = journal_new(flags, NULL, namespace); |
87d2c1ff LP |
1934 | if (!j) |
1935 | return -ENOMEM; | |
1936 | ||
89739579 | 1937 | r = add_search_paths(j); |
a963990f | 1938 | if (r < 0) |
17c9aff8 | 1939 | return r; |
50f20cfd | 1940 | |
17c9aff8 | 1941 | *ret = TAKE_PTR(j); |
a963990f | 1942 | return 0; |
a963990f | 1943 | } |
50f20cfd | 1944 | |
456aa879 LP |
1945 | _public_ int sd_journal_open(sd_journal **ret, int flags) { |
1946 | return sd_journal_open_namespace(ret, NULL, flags); | |
1947 | } | |
1948 | ||
1aaa68f5 ZJS |
1949 | #define OPEN_CONTAINER_ALLOWED_FLAGS \ |
1950 | (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM) | |
1951 | ||
b6741478 LP |
1952 | _public_ int sd_journal_open_container(sd_journal **ret, const char *machine, int flags) { |
1953 | _cleanup_free_ char *root = NULL, *class = NULL; | |
17c9aff8 | 1954 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
b6741478 LP |
1955 | char *p; |
1956 | int r; | |
1957 | ||
68312977 | 1958 | /* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in |
2daa9cbd LP |
1959 | * combination with sd_journal_open_directory_fd(). */ |
1960 | ||
b6741478 LP |
1961 | assert_return(machine, -EINVAL); |
1962 | assert_return(ret, -EINVAL); | |
1aaa68f5 | 1963 | assert_return((flags & ~OPEN_CONTAINER_ALLOWED_FLAGS) == 0, -EINVAL); |
affcf189 | 1964 | assert_return(machine_name_is_valid(machine), -EINVAL); |
b6741478 | 1965 | |
63c372cb | 1966 | p = strjoina("/run/systemd/machines/", machine); |
13df9c39 LP |
1967 | r = parse_env_file(NULL, p, |
1968 | "ROOT", &root, | |
1969 | "CLASS", &class); | |
b6741478 LP |
1970 | if (r == -ENOENT) |
1971 | return -EHOSTDOWN; | |
1972 | if (r < 0) | |
1973 | return r; | |
1974 | if (!root) | |
1975 | return -ENODATA; | |
1976 | ||
1977 | if (!streq_ptr(class, "container")) | |
1978 | return -EIO; | |
1979 | ||
456aa879 | 1980 | j = journal_new(flags, root, NULL); |
b6741478 LP |
1981 | if (!j) |
1982 | return -ENOMEM; | |
1983 | ||
89739579 | 1984 | r = add_search_paths(j); |
b6741478 | 1985 | if (r < 0) |
17c9aff8 | 1986 | return r; |
b6741478 | 1987 | |
17c9aff8 | 1988 | *ret = TAKE_PTR(j); |
b6741478 | 1989 | return 0; |
b6741478 LP |
1990 | } |
1991 | ||
1aaa68f5 | 1992 | #define OPEN_DIRECTORY_ALLOWED_FLAGS \ |
10752e82 ZJS |
1993 | (SD_JOURNAL_OS_ROOT | \ |
1994 | SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER ) | |
1aaa68f5 | 1995 | |
a963990f | 1996 | _public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) { |
17c9aff8 | 1997 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
a963990f | 1998 | int r; |
87d2c1ff | 1999 | |
1ae464e0 TA |
2000 | assert_return(ret, -EINVAL); |
2001 | assert_return(path, -EINVAL); | |
1aaa68f5 | 2002 | assert_return((flags & ~OPEN_DIRECTORY_ALLOWED_FLAGS) == 0, -EINVAL); |
87d2c1ff | 2003 | |
456aa879 | 2004 | j = journal_new(flags, path, NULL); |
a963990f LP |
2005 | if (!j) |
2006 | return -ENOMEM; | |
3fbf9cbb | 2007 | |
d077390c LP |
2008 | if (flags & SD_JOURNAL_OS_ROOT) |
2009 | r = add_search_paths(j); | |
2010 | else | |
2011 | r = add_root_directory(j, path, false); | |
d617408e | 2012 | if (r < 0) |
17c9aff8 | 2013 | return r; |
87d2c1ff | 2014 | |
17c9aff8 | 2015 | *ret = TAKE_PTR(j); |
87d2c1ff | 2016 | return 0; |
a963990f | 2017 | } |
87d2c1ff | 2018 | |
5302ebe1 | 2019 | _public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int flags) { |
17c9aff8 | 2020 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
5302ebe1 ZJS |
2021 | const char **path; |
2022 | int r; | |
2023 | ||
1ae464e0 TA |
2024 | assert_return(ret, -EINVAL); |
2025 | assert_return(flags == 0, -EINVAL); | |
5302ebe1 | 2026 | |
456aa879 | 2027 | j = journal_new(flags, NULL, NULL); |
5302ebe1 ZJS |
2028 | if (!j) |
2029 | return -ENOMEM; | |
2030 | ||
2031 | STRV_FOREACH(path, paths) { | |
5d1ce257 | 2032 | r = add_any_file(j, -1, *path); |
d617408e | 2033 | if (r < 0) |
17c9aff8 | 2034 | return r; |
5302ebe1 ZJS |
2035 | } |
2036 | ||
2037 | j->no_new_files = true; | |
2038 | ||
17c9aff8 | 2039 | *ret = TAKE_PTR(j); |
5302ebe1 | 2040 | return 0; |
5d1ce257 LP |
2041 | } |
2042 | ||
1aaa68f5 | 2043 | #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \ |
10752e82 ZJS |
2044 | (SD_JOURNAL_OS_ROOT | \ |
2045 | SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER ) | |
1aaa68f5 | 2046 | |
5d1ce257 | 2047 | _public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) { |
17c9aff8 | 2048 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
5d1ce257 LP |
2049 | struct stat st; |
2050 | int r; | |
2051 | ||
2052 | assert_return(ret, -EINVAL); | |
2053 | assert_return(fd >= 0, -EBADF); | |
1aaa68f5 | 2054 | assert_return((flags & ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS) == 0, -EINVAL); |
5d1ce257 LP |
2055 | |
2056 | if (fstat(fd, &st) < 0) | |
2057 | return -errno; | |
2058 | ||
2059 | if (!S_ISDIR(st.st_mode)) | |
2060 | return -EBADFD; | |
2061 | ||
456aa879 | 2062 | j = journal_new(flags, NULL, NULL); |
5d1ce257 LP |
2063 | if (!j) |
2064 | return -ENOMEM; | |
2065 | ||
2066 | j->toplevel_fd = fd; | |
2067 | ||
d077390c LP |
2068 | if (flags & SD_JOURNAL_OS_ROOT) |
2069 | r = add_search_paths(j); | |
2070 | else | |
2071 | r = add_root_directory(j, NULL, false); | |
5d1ce257 | 2072 | if (r < 0) |
17c9aff8 | 2073 | return r; |
5d1ce257 | 2074 | |
17c9aff8 | 2075 | *ret = TAKE_PTR(j); |
5d1ce257 | 2076 | return 0; |
5d1ce257 LP |
2077 | } |
2078 | ||
2079 | _public_ int sd_journal_open_files_fd(sd_journal **ret, int fds[], unsigned n_fds, int flags) { | |
2080 | Iterator iterator; | |
2081 | JournalFile *f; | |
17c9aff8 | 2082 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
5d1ce257 LP |
2083 | unsigned i; |
2084 | int r; | |
2085 | ||
2086 | assert_return(ret, -EINVAL); | |
2087 | assert_return(n_fds > 0, -EBADF); | |
2088 | assert_return(flags == 0, -EINVAL); | |
2089 | ||
456aa879 | 2090 | j = journal_new(flags, NULL, NULL); |
5d1ce257 LP |
2091 | if (!j) |
2092 | return -ENOMEM; | |
2093 | ||
2094 | for (i = 0; i < n_fds; i++) { | |
2095 | struct stat st; | |
2096 | ||
2097 | if (fds[i] < 0) { | |
2098 | r = -EBADF; | |
2099 | goto fail; | |
2100 | } | |
2101 | ||
2102 | if (fstat(fds[i], &st) < 0) { | |
2103 | r = -errno; | |
2104 | goto fail; | |
2105 | } | |
2106 | ||
3cc44114 LP |
2107 | r = stat_verify_regular(&st); |
2108 | if (r < 0) | |
5d1ce257 | 2109 | goto fail; |
5d1ce257 LP |
2110 | |
2111 | r = add_any_file(j, fds[i], NULL); | |
2112 | if (r < 0) | |
2113 | goto fail; | |
2114 | } | |
2115 | ||
2116 | j->no_new_files = true; | |
2117 | j->no_inotify = true; | |
5302ebe1 | 2118 | |
17c9aff8 | 2119 | *ret = TAKE_PTR(j); |
5d1ce257 LP |
2120 | return 0; |
2121 | ||
2122 | fail: | |
f8e2f4d6 | 2123 | /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they |
5d1ce257 LP |
2124 | * remain open */ |
2125 | ORDERED_HASHMAP_FOREACH(f, j->files, iterator) | |
2126 | f->close_fd = false; | |
2127 | ||
5302ebe1 ZJS |
2128 | return r; |
2129 | } | |
2130 | ||
a5344d2c | 2131 | _public_ void sd_journal_close(sd_journal *j) { |
a963990f | 2132 | Directory *d; |
a963990f | 2133 | |
a5344d2c LP |
2134 | if (!j) |
2135 | return; | |
87d2c1ff | 2136 | |
54b1da83 LP |
2137 | sd_journal_flush_matches(j); |
2138 | ||
f9168190 | 2139 | ordered_hashmap_free_with_destructor(j->files, journal_file_close); |
5d4ba7f2 | 2140 | iterated_cache_free(j->files_cache); |
260a2be4 | 2141 | |
a963990f LP |
2142 | while ((d = hashmap_first(j->directories_by_path))) |
2143 | remove_directory(j, d); | |
260a2be4 | 2144 | |
a963990f LP |
2145 | while ((d = hashmap_first(j->directories_by_wd))) |
2146 | remove_directory(j, d); | |
87d2c1ff | 2147 | |
a963990f LP |
2148 | hashmap_free(j->directories_by_path); |
2149 | hashmap_free(j->directories_by_wd); | |
1cc101f1 | 2150 | |
03e334a1 | 2151 | safe_close(j->inotify_fd); |
50f20cfd | 2152 | |
bf807d4d LP |
2153 | if (j->mmap) { |
2154 | log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j->mmap), mmap_cache_get_missed(j->mmap)); | |
16e9f408 | 2155 | mmap_cache_unref(j->mmap); |
bf807d4d | 2156 | } |
16e9f408 | 2157 | |
ec1d2909 | 2158 | hashmap_free_free(j->errors); |
5768d259 | 2159 | |
7827b1a1 | 2160 | free(j->path); |
89739579 | 2161 | free(j->prefix); |
456aa879 | 2162 | free(j->namespace); |
3c1668da | 2163 | free(j->unique_field); |
eb86030e | 2164 | free(j->fields_buffer); |
87d2c1ff LP |
2165 | free(j); |
2166 | } | |
3fbf9cbb | 2167 | |
a5344d2c | 2168 | _public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) { |
3fbf9cbb LP |
2169 | Object *o; |
2170 | JournalFile *f; | |
2171 | int r; | |
2172 | ||
1ae464e0 TA |
2173 | assert_return(j, -EINVAL); |
2174 | assert_return(!journal_pid_changed(j), -ECHILD); | |
2175 | assert_return(ret, -EINVAL); | |
3fbf9cbb LP |
2176 | |
2177 | f = j->current_file; | |
2178 | if (!f) | |
de190aef | 2179 | return -EADDRNOTAVAIL; |
3fbf9cbb LP |
2180 | |
2181 | if (f->current_offset <= 0) | |
de190aef | 2182 | return -EADDRNOTAVAIL; |
3fbf9cbb | 2183 | |
de190aef | 2184 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); |
3fbf9cbb LP |
2185 | if (r < 0) |
2186 | return r; | |
2187 | ||
2188 | *ret = le64toh(o->entry.realtime); | |
de190aef | 2189 | return 0; |
3fbf9cbb LP |
2190 | } |
2191 | ||
a5344d2c | 2192 | _public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) { |
3fbf9cbb LP |
2193 | Object *o; |
2194 | JournalFile *f; | |
2195 | int r; | |
2196 | sd_id128_t id; | |
2197 | ||
1ae464e0 TA |
2198 | assert_return(j, -EINVAL); |
2199 | assert_return(!journal_pid_changed(j), -ECHILD); | |
3fbf9cbb LP |
2200 | |
2201 | f = j->current_file; | |
2202 | if (!f) | |
de190aef | 2203 | return -EADDRNOTAVAIL; |
3fbf9cbb LP |
2204 | |
2205 | if (f->current_offset <= 0) | |
de190aef | 2206 | return -EADDRNOTAVAIL; |
3fbf9cbb | 2207 | |
de190aef | 2208 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); |
3fbf9cbb LP |
2209 | if (r < 0) |
2210 | return r; | |
2211 | ||
de190aef LP |
2212 | if (ret_boot_id) |
2213 | *ret_boot_id = o->entry.boot_id; | |
2214 | else { | |
2215 | r = sd_id128_get_boot(&id); | |
2216 | if (r < 0) | |
2217 | return r; | |
3fbf9cbb | 2218 | |
de190aef | 2219 | if (!sd_id128_equal(id, o->entry.boot_id)) |
df50185b | 2220 | return -ESTALE; |
de190aef | 2221 | } |
3fbf9cbb | 2222 | |
14a65d65 LP |
2223 | if (ret) |
2224 | *ret = le64toh(o->entry.monotonic); | |
2225 | ||
de190aef | 2226 | return 0; |
3fbf9cbb LP |
2227 | } |
2228 | ||
362a3f81 LP |
2229 | static bool field_is_valid(const char *field) { |
2230 | const char *p; | |
2231 | ||
2232 | assert(field); | |
2233 | ||
2234 | if (isempty(field)) | |
2235 | return false; | |
2236 | ||
2237 | if (startswith(field, "__")) | |
2238 | return false; | |
2239 | ||
2240 | for (p = field; *p; p++) { | |
2241 | ||
2242 | if (*p == '_') | |
2243 | continue; | |
2244 | ||
2245 | if (*p >= 'A' && *p <= 'Z') | |
2246 | continue; | |
2247 | ||
2248 | if (*p >= '0' && *p <= '9') | |
2249 | continue; | |
2250 | ||
2251 | return false; | |
2252 | } | |
2253 | ||
2254 | return true; | |
2255 | } | |
2256 | ||
a5344d2c | 2257 | _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) { |
3fbf9cbb LP |
2258 | JournalFile *f; |
2259 | uint64_t i, n; | |
2260 | size_t field_length; | |
2261 | int r; | |
2262 | Object *o; | |
2263 | ||
1ae464e0 TA |
2264 | assert_return(j, -EINVAL); |
2265 | assert_return(!journal_pid_changed(j), -ECHILD); | |
2266 | assert_return(field, -EINVAL); | |
2267 | assert_return(data, -EINVAL); | |
2268 | assert_return(size, -EINVAL); | |
2269 | assert_return(field_is_valid(field), -EINVAL); | |
3fbf9cbb LP |
2270 | |
2271 | f = j->current_file; | |
2272 | if (!f) | |
de190aef | 2273 | return -EADDRNOTAVAIL; |
3fbf9cbb LP |
2274 | |
2275 | if (f->current_offset <= 0) | |
de190aef | 2276 | return -EADDRNOTAVAIL; |
3fbf9cbb | 2277 | |
de190aef | 2278 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); |
3fbf9cbb LP |
2279 | if (r < 0) |
2280 | return r; | |
2281 | ||
2282 | field_length = strlen(field); | |
2283 | ||
2284 | n = journal_file_entry_n_items(o); | |
2285 | for (i = 0; i < n; i++) { | |
4fd052ae FC |
2286 | uint64_t p, l; |
2287 | le64_t le_hash; | |
3fbf9cbb | 2288 | size_t t; |
1ec7120e | 2289 | int compression; |
3fbf9cbb LP |
2290 | |
2291 | p = le64toh(o->entry.items[i].object_offset); | |
807e17f0 | 2292 | le_hash = o->entry.items[i].hash; |
de190aef | 2293 | r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); |
3fbf9cbb LP |
2294 | if (r < 0) |
2295 | return r; | |
2296 | ||
de190aef | 2297 | if (le_hash != o->data.hash) |
de7b95cd LP |
2298 | return -EBADMSG; |
2299 | ||
3fbf9cbb LP |
2300 | l = le64toh(o->object.size) - offsetof(Object, data.payload); |
2301 | ||
1ec7120e | 2302 | compression = o->object.flags & OBJECT_COMPRESSION_MASK; |
3b1a55e1 | 2303 | if (compression) { |
349cc4a5 | 2304 | #if HAVE_XZ || HAVE_LZ4 |
2aaec9b4 | 2305 | r = decompress_startswith(compression, |
3b1a55e1 ZJS |
2306 | o->data.payload, l, |
2307 | &f->compress_buffer, &f->compress_buffer_size, | |
2aaec9b4 ZJS |
2308 | field, field_length, '='); |
2309 | if (r < 0) | |
82e24b00 | 2310 | log_debug_errno(r, "Cannot decompress %s object of length %"PRIu64" at offset "OFSfmt": %m", |
2aaec9b4 ZJS |
2311 | object_compressed_to_string(compression), l, p); |
2312 | else if (r > 0) { | |
3b1a55e1 | 2313 | |
fa1c4b51 | 2314 | size_t rsize; |
3b1a55e1 ZJS |
2315 | |
2316 | r = decompress_blob(compression, | |
2317 | o->data.payload, l, | |
2318 | &f->compress_buffer, &f->compress_buffer_size, &rsize, | |
2319 | j->data_threshold); | |
2320 | if (r < 0) | |
2321 | return r; | |
807e17f0 | 2322 | |
3b1a55e1 ZJS |
2323 | *data = f->compress_buffer; |
2324 | *size = (size_t) rsize; | |
807e17f0 | 2325 | |
3b1a55e1 ZJS |
2326 | return 0; |
2327 | } | |
2328 | #else | |
2329 | return -EPROTONOSUPPORT; | |
2330 | #endif | |
807e17f0 LP |
2331 | } else if (l >= field_length+1 && |
2332 | memcmp(o->data.payload, field, field_length) == 0 && | |
2333 | o->data.payload[field_length] == '=') { | |
3fbf9cbb | 2334 | |
161e54f8 | 2335 | t = (size_t) l; |
3fbf9cbb | 2336 | |
161e54f8 LP |
2337 | if ((uint64_t) t != l) |
2338 | return -E2BIG; | |
3fbf9cbb | 2339 | |
161e54f8 LP |
2340 | *data = o->data.payload; |
2341 | *size = t; | |
3fbf9cbb | 2342 | |
99613ec5 | 2343 | return 0; |
161e54f8 | 2344 | } |
3fbf9cbb | 2345 | |
de190aef | 2346 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); |
161e54f8 LP |
2347 | if (r < 0) |
2348 | return r; | |
3fbf9cbb LP |
2349 | } |
2350 | ||
de190aef | 2351 | return -ENOENT; |
3fbf9cbb LP |
2352 | } |
2353 | ||
93b73b06 | 2354 | static int return_data(sd_journal *j, JournalFile *f, Object *o, const void **data, size_t *size) { |
3c1668da LP |
2355 | size_t t; |
2356 | uint64_t l; | |
3b1a55e1 | 2357 | int compression; |
3c1668da LP |
2358 | |
2359 | l = le64toh(o->object.size) - offsetof(Object, data.payload); | |
2360 | t = (size_t) l; | |
2361 | ||
2362 | /* We can't read objects larger than 4G on a 32bit machine */ | |
2363 | if ((uint64_t) t != l) | |
2364 | return -E2BIG; | |
2365 | ||
1ec7120e ZJS |
2366 | compression = o->object.flags & OBJECT_COMPRESSION_MASK; |
2367 | if (compression) { | |
349cc4a5 | 2368 | #if HAVE_XZ || HAVE_LZ4 |
fa1c4b51 | 2369 | size_t rsize; |
3b1a55e1 | 2370 | int r; |
3c1668da | 2371 | |
1ec7120e ZJS |
2372 | r = decompress_blob(compression, |
2373 | o->data.payload, l, &f->compress_buffer, | |
2374 | &f->compress_buffer_size, &rsize, j->data_threshold); | |
2375 | if (r < 0) | |
2376 | return r; | |
3c1668da LP |
2377 | |
2378 | *data = f->compress_buffer; | |
2379 | *size = (size_t) rsize; | |
3b1a55e1 ZJS |
2380 | #else |
2381 | return -EPROTONOSUPPORT; | |
2382 | #endif | |
3c1668da LP |
2383 | } else { |
2384 | *data = o->data.payload; | |
2385 | *size = t; | |
2386 | } | |
2387 | ||
2388 | return 0; | |
2389 | } | |
2390 | ||
a5344d2c | 2391 | _public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) { |
3fbf9cbb | 2392 | JournalFile *f; |
3c1668da | 2393 | uint64_t p, n; |
4fd052ae | 2394 | le64_t le_hash; |
3fbf9cbb LP |
2395 | int r; |
2396 | Object *o; | |
2397 | ||
1ae464e0 TA |
2398 | assert_return(j, -EINVAL); |
2399 | assert_return(!journal_pid_changed(j), -ECHILD); | |
2400 | assert_return(data, -EINVAL); | |
2401 | assert_return(size, -EINVAL); | |
3fbf9cbb LP |
2402 | |
2403 | f = j->current_file; | |
2404 | if (!f) | |
de190aef | 2405 | return -EADDRNOTAVAIL; |
3fbf9cbb LP |
2406 | |
2407 | if (f->current_offset <= 0) | |
de190aef | 2408 | return -EADDRNOTAVAIL; |
3fbf9cbb | 2409 | |
de190aef | 2410 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); |
3fbf9cbb LP |
2411 | if (r < 0) |
2412 | return r; | |
2413 | ||
2414 | n = journal_file_entry_n_items(o); | |
7210bfb3 | 2415 | if (j->current_field >= n) |
3fbf9cbb LP |
2416 | return 0; |
2417 | ||
7210bfb3 | 2418 | p = le64toh(o->entry.items[j->current_field].object_offset); |
de190aef LP |
2419 | le_hash = o->entry.items[j->current_field].hash; |
2420 | r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); | |
3fbf9cbb LP |
2421 | if (r < 0) |
2422 | return r; | |
2423 | ||
de190aef | 2424 | if (le_hash != o->data.hash) |
de7b95cd LP |
2425 | return -EBADMSG; |
2426 | ||
93b73b06 | 2427 | r = return_data(j, f, o, data, size); |
3c1668da LP |
2428 | if (r < 0) |
2429 | return r; | |
3fbf9cbb | 2430 | |
313cefa1 | 2431 | j->current_field++; |
3fbf9cbb LP |
2432 | |
2433 | return 1; | |
2434 | } | |
c2373f84 | 2435 | |
a5344d2c LP |
2436 | _public_ void sd_journal_restart_data(sd_journal *j) { |
2437 | if (!j) | |
2438 | return; | |
8725d60a LP |
2439 | |
2440 | j->current_field = 0; | |
c2373f84 | 2441 | } |
50f20cfd | 2442 | |
858749f7 LP |
2443 | static int reiterate_all_paths(sd_journal *j) { |
2444 | assert(j); | |
2445 | ||
2446 | if (j->no_new_files) | |
2447 | return add_current_paths(j); | |
2448 | ||
2449 | if (j->flags & SD_JOURNAL_OS_ROOT) | |
2450 | return add_search_paths(j); | |
2451 | ||
2452 | if (j->toplevel_fd >= 0) | |
2453 | return add_root_directory(j, NULL, false); | |
2454 | ||
2455 | if (j->path) | |
2456 | return add_root_directory(j, j->path, true); | |
2457 | ||
2458 | return add_search_paths(j); | |
2459 | } | |
2460 | ||
a5344d2c | 2461 | _public_ int sd_journal_get_fd(sd_journal *j) { |
a963990f LP |
2462 | int r; |
2463 | ||
1ae464e0 TA |
2464 | assert_return(j, -EINVAL); |
2465 | assert_return(!journal_pid_changed(j), -ECHILD); | |
50f20cfd | 2466 | |
5d1ce257 LP |
2467 | if (j->no_inotify) |
2468 | return -EMEDIUMTYPE; | |
2469 | ||
a963990f LP |
2470 | if (j->inotify_fd >= 0) |
2471 | return j->inotify_fd; | |
2472 | ||
2473 | r = allocate_inotify(j); | |
2474 | if (r < 0) | |
2475 | return r; | |
2476 | ||
858749f7 | 2477 | log_debug("Reiterating files to get inotify watches established."); |
5d1ce257 | 2478 | |
858749f7 LP |
2479 | /* Iterate through all dirs again, to add them to the inotify */ |
2480 | r = reiterate_all_paths(j); | |
a963990f LP |
2481 | if (r < 0) |
2482 | return r; | |
2483 | ||
50f20cfd LP |
2484 | return j->inotify_fd; |
2485 | } | |
2486 | ||
ee531d94 LP |
2487 | _public_ int sd_journal_get_events(sd_journal *j) { |
2488 | int fd; | |
2489 | ||
1ae464e0 TA |
2490 | assert_return(j, -EINVAL); |
2491 | assert_return(!journal_pid_changed(j), -ECHILD); | |
ee531d94 LP |
2492 | |
2493 | fd = sd_journal_get_fd(j); | |
2494 | if (fd < 0) | |
2495 | return fd; | |
2496 | ||
2497 | return POLLIN; | |
2498 | } | |
2499 | ||
39c155ea LP |
2500 | _public_ int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec) { |
2501 | int fd; | |
2502 | ||
1ae464e0 TA |
2503 | assert_return(j, -EINVAL); |
2504 | assert_return(!journal_pid_changed(j), -ECHILD); | |
2505 | assert_return(timeout_usec, -EINVAL); | |
39c155ea LP |
2506 | |
2507 | fd = sd_journal_get_fd(j); | |
2508 | if (fd < 0) | |
2509 | return fd; | |
2510 | ||
2511 | if (!j->on_network) { | |
2512 | *timeout_usec = (uint64_t) -1; | |
2513 | return 0; | |
2514 | } | |
2515 | ||
2516 | /* If we are on the network we need to regularly check for | |
2517 | * changes manually */ | |
2518 | ||
2519 | *timeout_usec = j->last_process_usec + JOURNAL_FILES_RECHECK_USEC; | |
2520 | return 1; | |
2521 | } | |
2522 | ||
858749f7 LP |
2523 | static void process_q_overflow(sd_journal *j) { |
2524 | JournalFile *f; | |
2525 | Directory *m; | |
2526 | Iterator i; | |
2527 | ||
2528 | assert(j); | |
2529 | ||
2530 | /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list | |
2531 | * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all | |
2532 | * journal files we encounter. All journal files and all directories that don't carry it after reenumeration | |
2533 | * are subject for unloading. */ | |
2534 | ||
2535 | log_debug("Inotify queue overrun, reiterating everything."); | |
2536 | ||
2537 | j->generation++; | |
2538 | (void) reiterate_all_paths(j); | |
2539 | ||
2540 | ORDERED_HASHMAP_FOREACH(f, j->files, i) { | |
2541 | ||
2542 | if (f->last_seen_generation == j->generation) | |
2543 | continue; | |
2544 | ||
2545 | log_debug("File '%s' hasn't been seen in this enumeration, removing.", f->path); | |
2546 | remove_file_real(j, f); | |
2547 | } | |
2548 | ||
2549 | HASHMAP_FOREACH(m, j->directories_by_path, i) { | |
2550 | ||
2551 | if (m->last_seen_generation == j->generation) | |
2552 | continue; | |
2553 | ||
2554 | if (m->is_root) /* Never GC root directories */ | |
2555 | continue; | |
2556 | ||
2557 | log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f->path); | |
2558 | remove_directory(j, m); | |
2559 | } | |
2560 | ||
2561 | log_debug("Reiteration complete."); | |
2562 | } | |
2563 | ||
31e99dd2 | 2564 | static void process_inotify_event(sd_journal *j, const struct inotify_event *e) { |
a963990f | 2565 | Directory *d; |
50f20cfd LP |
2566 | |
2567 | assert(j); | |
2568 | assert(e); | |
2569 | ||
858749f7 LP |
2570 | if (e->mask & IN_Q_OVERFLOW) { |
2571 | process_q_overflow(j); | |
2572 | return; | |
2573 | } | |
2574 | ||
50f20cfd | 2575 | /* Is this a subdirectory we watch? */ |
a963990f LP |
2576 | d = hashmap_get(j->directories_by_wd, INT_TO_PTR(e->wd)); |
2577 | if (d) { | |
de2c3907 LP |
2578 | if (!(e->mask & IN_ISDIR) && e->len > 0 && |
2579 | (endswith(e->name, ".journal") || | |
2580 | endswith(e->name, ".journal~"))) { | |
50f20cfd LP |
2581 | |
2582 | /* Event for a journal file */ | |
2583 | ||
d617408e | 2584 | if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) |
fc1813c0 | 2585 | (void) add_file_by_name(j, d->path, e->name); |
d617408e | 2586 | else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT)) |
fc1813c0 | 2587 | remove_file_by_name(j, d->path, e->name); |
50f20cfd | 2588 | |
a963990f | 2589 | } else if (!d->is_root && e->len == 0) { |
50f20cfd | 2590 | |
a963990f | 2591 | /* Event for a subdirectory */ |
50f20cfd | 2592 | |
b2b46f91 TA |
2593 | if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT)) |
2594 | remove_directory(j, d); | |
50f20cfd | 2595 | |
a9be0692 | 2596 | } else if (d->is_root && (e->mask & IN_ISDIR) && e->len > 0 && id128_is_valid(e->name)) { |
50f20cfd | 2597 | |
a963990f | 2598 | /* Event for root directory */ |
50f20cfd | 2599 | |
d617408e LP |
2600 | if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) |
2601 | (void) add_directory(j, d->path, e->name); | |
50f20cfd LP |
2602 | } |
2603 | ||
2604 | return; | |
2605 | } | |
2606 | ||
2607 | if (e->mask & IN_IGNORED) | |
2608 | return; | |
2609 | ||
a9be0692 | 2610 | log_debug("Unexpected inotify event."); |
50f20cfd LP |
2611 | } |
2612 | ||
a963990f LP |
2613 | static int determine_change(sd_journal *j) { |
2614 | bool b; | |
2615 | ||
2616 | assert(j); | |
2617 | ||
2618 | b = j->current_invalidate_counter != j->last_invalidate_counter; | |
2619 | j->last_invalidate_counter = j->current_invalidate_counter; | |
2620 | ||
2621 | return b ? SD_JOURNAL_INVALIDATE : SD_JOURNAL_APPEND; | |
2622 | } | |
2623 | ||
a5344d2c | 2624 | _public_ int sd_journal_process(sd_journal *j) { |
a963990f | 2625 | bool got_something = false; |
50f20cfd | 2626 | |
1ae464e0 TA |
2627 | assert_return(j, -EINVAL); |
2628 | assert_return(!journal_pid_changed(j), -ECHILD); | |
50f20cfd | 2629 | |
10c4d640 LP |
2630 | if (j->inotify_fd < 0) /* We have no inotify fd yet? Then there's noting to process. */ |
2631 | return 0; | |
2632 | ||
39c155ea | 2633 | j->last_process_usec = now(CLOCK_MONOTONIC); |
f9346444 | 2634 | j->last_invalidate_counter = j->current_invalidate_counter; |
39c155ea | 2635 | |
50f20cfd | 2636 | for (;;) { |
0254e944 | 2637 | union inotify_event_buffer buffer; |
50f20cfd LP |
2638 | struct inotify_event *e; |
2639 | ssize_t l; | |
2640 | ||
0254e944 | 2641 | l = read(j->inotify_fd, &buffer, sizeof(buffer)); |
50f20cfd | 2642 | if (l < 0) { |
3742095b | 2643 | if (IN_SET(errno, EAGAIN, EINTR)) |
a963990f | 2644 | return got_something ? determine_change(j) : SD_JOURNAL_NOP; |
50f20cfd LP |
2645 | |
2646 | return -errno; | |
2647 | } | |
2648 | ||
a963990f LP |
2649 | got_something = true; |
2650 | ||
f7c1ad4f | 2651 | FOREACH_INOTIFY_EVENT(e, buffer, l) |
50f20cfd | 2652 | process_inotify_event(j, e); |
50f20cfd LP |
2653 | } |
2654 | } | |
6ad1d1c3 | 2655 | |
e02d1cf7 | 2656 | _public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) { |
a963990f | 2657 | int r; |
39c155ea | 2658 | uint64_t t; |
e02d1cf7 | 2659 | |
1ae464e0 TA |
2660 | assert_return(j, -EINVAL); |
2661 | assert_return(!journal_pid_changed(j), -ECHILD); | |
e02d1cf7 | 2662 | |
a963990f | 2663 | if (j->inotify_fd < 0) { |
28ca867a MS |
2664 | Iterator i; |
2665 | JournalFile *f; | |
a963990f LP |
2666 | |
2667 | /* This is the first invocation, hence create the | |
2668 | * inotify watch */ | |
2669 | r = sd_journal_get_fd(j); | |
2670 | if (r < 0) | |
2671 | return r; | |
2672 | ||
28ca867a MS |
2673 | /* Server might have done some vacuuming while we weren't watching. |
2674 | Get rid of the deleted files now so they don't stay around indefinitely. */ | |
2675 | ORDERED_HASHMAP_FOREACH(f, j->files, i) { | |
2676 | r = journal_file_fstat(f); | |
8581b9f9 MS |
2677 | if (r == -EIDRM) |
2678 | remove_file_real(j, f); | |
2679 | else if (r < 0) { | |
28ca867a MS |
2680 | log_debug_errno(r,"Failed to fstat() journal file '%s' : %m", f->path); |
2681 | continue; | |
2682 | } | |
28ca867a MS |
2683 | } |
2684 | ||
a963990f LP |
2685 | /* The journal might have changed since the context |
2686 | * object was created and we weren't watching before, | |
2687 | * hence don't wait for anything, and return | |
2688 | * immediately. */ | |
2689 | return determine_change(j); | |
2690 | } | |
2691 | ||
39c155ea LP |
2692 | r = sd_journal_get_timeout(j, &t); |
2693 | if (r < 0) | |
2694 | return r; | |
2695 | ||
2696 | if (t != (uint64_t) -1) { | |
2697 | usec_t n; | |
2698 | ||
2699 | n = now(CLOCK_MONOTONIC); | |
2700 | t = t > n ? t - n : 0; | |
85210bff | 2701 | |
39c155ea LP |
2702 | if (timeout_usec == (uint64_t) -1 || timeout_usec > t) |
2703 | timeout_usec = t; | |
85210bff LP |
2704 | } |
2705 | ||
a963990f LP |
2706 | do { |
2707 | r = fd_wait_for_event(j->inotify_fd, POLLIN, timeout_usec); | |
2708 | } while (r == -EINTR); | |
e02d1cf7 LP |
2709 | |
2710 | if (r < 0) | |
2711 | return r; | |
2712 | ||
a963990f | 2713 | return sd_journal_process(j); |
e02d1cf7 LP |
2714 | } |
2715 | ||
08984293 LP |
2716 | _public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to) { |
2717 | Iterator i; | |
2718 | JournalFile *f; | |
2719 | bool first = true; | |
581483bf | 2720 | uint64_t fmin = 0, tmax = 0; |
08984293 LP |
2721 | int r; |
2722 | ||
1ae464e0 TA |
2723 | assert_return(j, -EINVAL); |
2724 | assert_return(!journal_pid_changed(j), -ECHILD); | |
2725 | assert_return(from || to, -EINVAL); | |
2726 | assert_return(from != to, -EINVAL); | |
08984293 | 2727 | |
c1f906bd | 2728 | ORDERED_HASHMAP_FOREACH(f, j->files, i) { |
08984293 LP |
2729 | usec_t fr, t; |
2730 | ||
2731 | r = journal_file_get_cutoff_realtime_usec(f, &fr, &t); | |
9f8d2983 LP |
2732 | if (r == -ENOENT) |
2733 | continue; | |
08984293 LP |
2734 | if (r < 0) |
2735 | return r; | |
2736 | if (r == 0) | |
2737 | continue; | |
2738 | ||
2739 | if (first) { | |
581483bf LP |
2740 | fmin = fr; |
2741 | tmax = t; | |
08984293 LP |
2742 | first = false; |
2743 | } else { | |
581483bf LP |
2744 | fmin = MIN(fr, fmin); |
2745 | tmax = MAX(t, tmax); | |
08984293 LP |
2746 | } |
2747 | } | |
2748 | ||
581483bf LP |
2749 | if (from) |
2750 | *from = fmin; | |
2751 | if (to) | |
2752 | *to = tmax; | |
2753 | ||
08984293 LP |
2754 | return first ? 0 : 1; |
2755 | } | |
2756 | ||
2757 | _public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t *from, uint64_t *to) { | |
2758 | Iterator i; | |
2759 | JournalFile *f; | |
1651e2c6 | 2760 | bool found = false; |
08984293 LP |
2761 | int r; |
2762 | ||
1ae464e0 TA |
2763 | assert_return(j, -EINVAL); |
2764 | assert_return(!journal_pid_changed(j), -ECHILD); | |
2765 | assert_return(from || to, -EINVAL); | |
2766 | assert_return(from != to, -EINVAL); | |
08984293 | 2767 | |
c1f906bd | 2768 | ORDERED_HASHMAP_FOREACH(f, j->files, i) { |
08984293 LP |
2769 | usec_t fr, t; |
2770 | ||
2771 | r = journal_file_get_cutoff_monotonic_usec(f, boot_id, &fr, &t); | |
9f8d2983 LP |
2772 | if (r == -ENOENT) |
2773 | continue; | |
08984293 LP |
2774 | if (r < 0) |
2775 | return r; | |
2776 | if (r == 0) | |
2777 | continue; | |
2778 | ||
1651e2c6 | 2779 | if (found) { |
08984293 | 2780 | if (from) |
1651e2c6 | 2781 | *from = MIN(fr, *from); |
08984293 | 2782 | if (to) |
1651e2c6 | 2783 | *to = MAX(t, *to); |
08984293 LP |
2784 | } else { |
2785 | if (from) | |
1651e2c6 | 2786 | *from = fr; |
08984293 | 2787 | if (to) |
1651e2c6 ZJS |
2788 | *to = t; |
2789 | found = true; | |
08984293 LP |
2790 | } |
2791 | } | |
2792 | ||
1651e2c6 | 2793 | return found; |
08984293 LP |
2794 | } |
2795 | ||
dca6219e LP |
2796 | void journal_print_header(sd_journal *j) { |
2797 | Iterator i; | |
2798 | JournalFile *f; | |
2799 | bool newline = false; | |
2800 | ||
2801 | assert(j); | |
2802 | ||
c1f906bd | 2803 | ORDERED_HASHMAP_FOREACH(f, j->files, i) { |
dca6219e LP |
2804 | if (newline) |
2805 | putchar('\n'); | |
2806 | else | |
2807 | newline = true; | |
2808 | ||
2809 | journal_file_print_header(f); | |
2810 | } | |
2811 | } | |
08984293 | 2812 | |
a1a03e30 LP |
2813 | _public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) { |
2814 | Iterator i; | |
2815 | JournalFile *f; | |
2816 | uint64_t sum = 0; | |
2817 | ||
1ae464e0 TA |
2818 | assert_return(j, -EINVAL); |
2819 | assert_return(!journal_pid_changed(j), -ECHILD); | |
2820 | assert_return(bytes, -EINVAL); | |
a1a03e30 | 2821 | |
c1f906bd | 2822 | ORDERED_HASHMAP_FOREACH(f, j->files, i) { |
a1a03e30 LP |
2823 | struct stat st; |
2824 | ||
2825 | if (fstat(f->fd, &st) < 0) | |
2826 | return -errno; | |
2827 | ||
2828 | sum += (uint64_t) st.st_blocks * 512ULL; | |
2829 | } | |
2830 | ||
2831 | *bytes = sum; | |
2832 | return 0; | |
2833 | } | |
2834 | ||
3c1668da LP |
2835 | _public_ int sd_journal_query_unique(sd_journal *j, const char *field) { |
2836 | char *f; | |
2837 | ||
1ae464e0 TA |
2838 | assert_return(j, -EINVAL); |
2839 | assert_return(!journal_pid_changed(j), -ECHILD); | |
2840 | assert_return(!isempty(field), -EINVAL); | |
2841 | assert_return(field_is_valid(field), -EINVAL); | |
3c1668da LP |
2842 | |
2843 | f = strdup(field); | |
2844 | if (!f) | |
2845 | return -ENOMEM; | |
2846 | ||
2847 | free(j->unique_field); | |
2848 | j->unique_field = f; | |
2849 | j->unique_file = NULL; | |
2850 | j->unique_offset = 0; | |
360af4cf | 2851 | j->unique_file_lost = false; |
3c1668da LP |
2852 | |
2853 | return 0; | |
2854 | } | |
2855 | ||
2856 | _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l) { | |
3c1668da | 2857 | size_t k; |
19a2bd80 | 2858 | |
1ae464e0 TA |
2859 | assert_return(j, -EINVAL); |
2860 | assert_return(!journal_pid_changed(j), -ECHILD); | |
2861 | assert_return(data, -EINVAL); | |
2862 | assert_return(l, -EINVAL); | |
2863 | assert_return(j->unique_field, -EINVAL); | |
19a2bd80 | 2864 | |
3c1668da | 2865 | k = strlen(j->unique_field); |
19a2bd80 | 2866 | |
3c1668da | 2867 | if (!j->unique_file) { |
360af4cf ZJS |
2868 | if (j->unique_file_lost) |
2869 | return 0; | |
2870 | ||
c1f906bd | 2871 | j->unique_file = ordered_hashmap_first(j->files); |
3c1668da LP |
2872 | if (!j->unique_file) |
2873 | return 0; | |
360af4cf | 2874 | |
3c1668da LP |
2875 | j->unique_offset = 0; |
2876 | } | |
19a2bd80 | 2877 | |
3c1668da LP |
2878 | for (;;) { |
2879 | JournalFile *of; | |
2880 | Iterator i; | |
ae97089d | 2881 | Object *o; |
3c1668da LP |
2882 | const void *odata; |
2883 | size_t ol; | |
2884 | bool found; | |
ae97089d | 2885 | int r; |
3c1668da | 2886 | |
bdc02927 | 2887 | /* Proceed to next data object in the field's linked list */ |
3c1668da LP |
2888 | if (j->unique_offset == 0) { |
2889 | r = journal_file_find_field_object(j->unique_file, j->unique_field, k, &o, NULL); | |
2890 | if (r < 0) | |
2891 | return r; | |
2892 | ||
2893 | j->unique_offset = r > 0 ? le64toh(o->field.head_data_offset) : 0; | |
2894 | } else { | |
2895 | r = journal_file_move_to_object(j->unique_file, OBJECT_DATA, j->unique_offset, &o); | |
2896 | if (r < 0) | |
2897 | return r; | |
2898 | ||
2899 | j->unique_offset = le64toh(o->data.next_field_offset); | |
2900 | } | |
2901 | ||
2902 | /* We reached the end of the list? Then start again, with the next file */ | |
2903 | if (j->unique_offset == 0) { | |
c1f906bd | 2904 | j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path); |
360af4cf | 2905 | if (!j->unique_file) |
3c1668da LP |
2906 | return 0; |
2907 | ||
3c1668da LP |
2908 | continue; |
2909 | } | |
2910 | ||
d05089d8 MS |
2911 | /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED |
2912 | * instead, so that we can look at this data object at the same | |
3c1668da | 2913 | * time as one on another file */ |
d05089d8 | 2914 | r = journal_file_move_to_object(j->unique_file, OBJECT_UNUSED, j->unique_offset, &o); |
3c1668da LP |
2915 | if (r < 0) |
2916 | return r; | |
2917 | ||
2918 | /* Let's do the type check by hand, since we used 0 context above. */ | |
baaa35ad ZJS |
2919 | if (o->object.type != OBJECT_DATA) |
2920 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
2921 | "%s:offset " OFSfmt ": object has type %d, expected %d", | |
2922 | j->unique_file->path, | |
2923 | j->unique_offset, | |
2924 | o->object.type, OBJECT_DATA); | |
ae97089d | 2925 | |
93b73b06 | 2926 | r = return_data(j, j->unique_file, o, &odata, &ol); |
3c1668da LP |
2927 | if (r < 0) |
2928 | return r; | |
2929 | ||
0f99f74a | 2930 | /* Check if we have at least the field name and "=". */ |
baaa35ad ZJS |
2931 | if (ol <= k) |
2932 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
2933 | "%s:offset " OFSfmt ": object has size %zu, expected at least %zu", | |
2934 | j->unique_file->path, | |
2935 | j->unique_offset, ol, k + 1); | |
2936 | ||
2937 | if (memcmp(odata, j->unique_field, k) || ((const char*) odata)[k] != '=') | |
2938 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
2939 | "%s:offset " OFSfmt ": object does not start with \"%s=\"", | |
2940 | j->unique_file->path, | |
2941 | j->unique_offset, | |
2942 | j->unique_field); | |
0f99f74a | 2943 | |
3c1668da LP |
2944 | /* OK, now let's see if we already returned this data |
2945 | * object by checking if it exists in the earlier | |
2946 | * traversed files. */ | |
2947 | found = false; | |
c1f906bd | 2948 | ORDERED_HASHMAP_FOREACH(of, j->files, i) { |
3c1668da LP |
2949 | if (of == j->unique_file) |
2950 | break; | |
2951 | ||
ed71f956 LP |
2952 | /* Skip this file it didn't have any fields indexed */ |
2953 | if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0) | |
3c1668da LP |
2954 | continue; |
2955 | ||
ed71f956 | 2956 | r = journal_file_find_data_object_with_hash(of, odata, ol, le64toh(o->data.hash), NULL, NULL); |
3c1668da LP |
2957 | if (r < 0) |
2958 | return r; | |
ed71f956 | 2959 | if (r > 0) { |
3c1668da | 2960 | found = true; |
ed71f956 LP |
2961 | break; |
2962 | } | |
3c1668da LP |
2963 | } |
2964 | ||
06cc69d4 JJ |
2965 | if (found) |
2966 | continue; | |
2967 | ||
93b73b06 | 2968 | r = return_data(j, j->unique_file, o, data, l); |
3c1668da LP |
2969 | if (r < 0) |
2970 | return r; | |
2971 | ||
2972 | return 1; | |
2973 | } | |
2974 | } | |
2975 | ||
115646c7 | 2976 | _public_ void sd_journal_restart_unique(sd_journal *j) { |
3c1668da LP |
2977 | if (!j) |
2978 | return; | |
2979 | ||
2980 | j->unique_file = NULL; | |
2981 | j->unique_offset = 0; | |
360af4cf | 2982 | j->unique_file_lost = false; |
3c1668da | 2983 | } |
85210bff | 2984 | |
eb86030e LP |
2985 | _public_ int sd_journal_enumerate_fields(sd_journal *j, const char **field) { |
2986 | int r; | |
2987 | ||
2988 | assert_return(j, -EINVAL); | |
2989 | assert_return(!journal_pid_changed(j), -ECHILD); | |
2990 | assert_return(field, -EINVAL); | |
2991 | ||
2992 | if (!j->fields_file) { | |
2993 | if (j->fields_file_lost) | |
2994 | return 0; | |
2995 | ||
2996 | j->fields_file = ordered_hashmap_first(j->files); | |
2997 | if (!j->fields_file) | |
2998 | return 0; | |
2999 | ||
3000 | j->fields_hash_table_index = 0; | |
3001 | j->fields_offset = 0; | |
3002 | } | |
3003 | ||
3004 | for (;;) { | |
3005 | JournalFile *f, *of; | |
3006 | Iterator i; | |
3007 | uint64_t m; | |
3008 | Object *o; | |
3009 | size_t sz; | |
3010 | bool found; | |
3011 | ||
3012 | f = j->fields_file; | |
3013 | ||
3014 | if (j->fields_offset == 0) { | |
3015 | bool eof = false; | |
3016 | ||
3017 | /* We are not yet positioned at any field. Let's pick the first one */ | |
3018 | r = journal_file_map_field_hash_table(f); | |
3019 | if (r < 0) | |
3020 | return r; | |
3021 | ||
3022 | m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem); | |
3023 | for (;;) { | |
3024 | if (j->fields_hash_table_index >= m) { | |
3025 | /* Reached the end of the hash table, go to the next file. */ | |
3026 | eof = true; | |
3027 | break; | |
3028 | } | |
3029 | ||
3030 | j->fields_offset = le64toh(f->field_hash_table[j->fields_hash_table_index].head_hash_offset); | |
3031 | ||
3032 | if (j->fields_offset != 0) | |
3033 | break; | |
3034 | ||
3035 | /* Empty hash table bucket, go to next one */ | |
3036 | j->fields_hash_table_index++; | |
3037 | } | |
3038 | ||
3039 | if (eof) { | |
3040 | /* Proceed with next file */ | |
3041 | j->fields_file = ordered_hashmap_next(j->files, f->path); | |
3042 | if (!j->fields_file) { | |
3043 | *field = NULL; | |
3044 | return 0; | |
3045 | } | |
3046 | ||
3047 | j->fields_offset = 0; | |
3048 | j->fields_hash_table_index = 0; | |
3049 | continue; | |
3050 | } | |
3051 | ||
3052 | } else { | |
3053 | /* We are already positioned at a field. If so, let's figure out the next field from it */ | |
3054 | ||
3055 | r = journal_file_move_to_object(f, OBJECT_FIELD, j->fields_offset, &o); | |
3056 | if (r < 0) | |
3057 | return r; | |
3058 | ||
3059 | j->fields_offset = le64toh(o->field.next_hash_offset); | |
3060 | if (j->fields_offset == 0) { | |
3061 | /* Reached the end of the hash table chain */ | |
3062 | j->fields_hash_table_index++; | |
3063 | continue; | |
3064 | } | |
3065 | } | |
3066 | ||
1f133e0d | 3067 | /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */ |
eb86030e LP |
3068 | r = journal_file_move_to_object(f, OBJECT_UNUSED, j->fields_offset, &o); |
3069 | if (r < 0) | |
3070 | return r; | |
3071 | ||
3072 | /* Because we used OBJECT_UNUSED above, we need to do our type check manually */ | |
baaa35ad ZJS |
3073 | if (o->object.type != OBJECT_FIELD) |
3074 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
3075 | "%s:offset " OFSfmt ": object has type %i, expected %i", | |
3076 | f->path, j->fields_offset, | |
3077 | o->object.type, OBJECT_FIELD); | |
eb86030e LP |
3078 | |
3079 | sz = le64toh(o->object.size) - offsetof(Object, field.payload); | |
3080 | ||
3081 | /* Let's see if we already returned this field name before. */ | |
3082 | found = false; | |
3083 | ORDERED_HASHMAP_FOREACH(of, j->files, i) { | |
3084 | if (of == f) | |
3085 | break; | |
3086 | ||
3087 | /* Skip this file it didn't have any fields indexed */ | |
3088 | if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0) | |
3089 | continue; | |
3090 | ||
3091 | r = journal_file_find_field_object_with_hash(of, o->field.payload, sz, le64toh(o->field.hash), NULL, NULL); | |
3092 | if (r < 0) | |
3093 | return r; | |
3094 | if (r > 0) { | |
3095 | found = true; | |
3096 | break; | |
3097 | } | |
3098 | } | |
3099 | ||
3100 | if (found) | |
3101 | continue; | |
3102 | ||
3103 | /* Check if this is really a valid string containing no NUL byte */ | |
3104 | if (memchr(o->field.payload, 0, sz)) | |
3105 | return -EBADMSG; | |
3106 | ||
3107 | if (sz > j->data_threshold) | |
3108 | sz = j->data_threshold; | |
3109 | ||
3110 | if (!GREEDY_REALLOC(j->fields_buffer, j->fields_buffer_allocated, sz + 1)) | |
3111 | return -ENOMEM; | |
3112 | ||
3113 | memcpy(j->fields_buffer, o->field.payload, sz); | |
3114 | j->fields_buffer[sz] = 0; | |
3115 | ||
3116 | if (!field_is_valid(j->fields_buffer)) | |
3117 | return -EBADMSG; | |
3118 | ||
3119 | *field = j->fields_buffer; | |
3120 | return 1; | |
3121 | } | |
3122 | } | |
3123 | ||
3124 | _public_ void sd_journal_restart_fields(sd_journal *j) { | |
3125 | if (!j) | |
3126 | return; | |
3127 | ||
3128 | j->fields_file = NULL; | |
3129 | j->fields_hash_table_index = 0; | |
3130 | j->fields_offset = 0; | |
3131 | j->fields_file_lost = false; | |
3132 | } | |
3133 | ||
85210bff | 3134 | _public_ int sd_journal_reliable_fd(sd_journal *j) { |
1ae464e0 TA |
3135 | assert_return(j, -EINVAL); |
3136 | assert_return(!journal_pid_changed(j), -ECHILD); | |
85210bff LP |
3137 | |
3138 | return !j->on_network; | |
3139 | } | |
d4205751 LP |
3140 | |
3141 | static char *lookup_field(const char *field, void *userdata) { | |
3142 | sd_journal *j = userdata; | |
3143 | const void *data; | |
3144 | size_t size, d; | |
3145 | int r; | |
3146 | ||
3147 | assert(field); | |
3148 | assert(j); | |
3149 | ||
3150 | r = sd_journal_get_data(j, field, &data, &size); | |
3151 | if (r < 0 || | |
3152 | size > REPLACE_VAR_MAX) | |
3153 | return strdup(field); | |
3154 | ||
3155 | d = strlen(field) + 1; | |
3156 | ||
3157 | return strndup((const char*) data + d, size - d); | |
3158 | } | |
3159 | ||
3160 | _public_ int sd_journal_get_catalog(sd_journal *j, char **ret) { | |
3161 | const void *data; | |
3162 | size_t size; | |
3163 | sd_id128_t id; | |
3164 | _cleanup_free_ char *text = NULL, *cid = NULL; | |
3165 | char *t; | |
3166 | int r; | |
3167 | ||
1ae464e0 TA |
3168 | assert_return(j, -EINVAL); |
3169 | assert_return(!journal_pid_changed(j), -ECHILD); | |
3170 | assert_return(ret, -EINVAL); | |
d4205751 LP |
3171 | |
3172 | r = sd_journal_get_data(j, "MESSAGE_ID", &data, &size); | |
3173 | if (r < 0) | |
3174 | return r; | |
3175 | ||
3176 | cid = strndup((const char*) data + 11, size - 11); | |
3177 | if (!cid) | |
3178 | return -ENOMEM; | |
3179 | ||
3180 | r = sd_id128_from_string(cid, &id); | |
3181 | if (r < 0) | |
3182 | return r; | |
3183 | ||
844ec79b | 3184 | r = catalog_get(CATALOG_DATABASE, id, &text); |
d4205751 LP |
3185 | if (r < 0) |
3186 | return r; | |
3187 | ||
3188 | t = replace_var(text, lookup_field, j); | |
3189 | if (!t) | |
3190 | return -ENOMEM; | |
3191 | ||
3192 | *ret = t; | |
3193 | return 0; | |
3194 | } | |
8f1e860f LP |
3195 | |
3196 | _public_ int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **ret) { | |
1ae464e0 | 3197 | assert_return(ret, -EINVAL); |
8f1e860f | 3198 | |
844ec79b | 3199 | return catalog_get(CATALOG_DATABASE, id, ret); |
8f1e860f | 3200 | } |
93b73b06 LP |
3201 | |
3202 | _public_ int sd_journal_set_data_threshold(sd_journal *j, size_t sz) { | |
1ae464e0 TA |
3203 | assert_return(j, -EINVAL); |
3204 | assert_return(!journal_pid_changed(j), -ECHILD); | |
93b73b06 LP |
3205 | |
3206 | j->data_threshold = sz; | |
3207 | return 0; | |
3208 | } | |
3209 | ||
3210 | _public_ int sd_journal_get_data_threshold(sd_journal *j, size_t *sz) { | |
1ae464e0 TA |
3211 | assert_return(j, -EINVAL); |
3212 | assert_return(!journal_pid_changed(j), -ECHILD); | |
3213 | assert_return(sz, -EINVAL); | |
93b73b06 LP |
3214 | |
3215 | *sz = j->data_threshold; | |
3216 | return 0; | |
3217 | } | |
39fd5b08 JS |
3218 | |
3219 | _public_ int sd_journal_has_runtime_files(sd_journal *j) { | |
3220 | assert_return(j, -EINVAL); | |
3221 | ||
3222 | return j->has_runtime_files; | |
3223 | } | |
3224 | ||
3225 | _public_ int sd_journal_has_persistent_files(sd_journal *j) { | |
3226 | assert_return(j, -EINVAL); | |
3227 | ||
3228 | return j->has_persistent_files; | |
3229 | } |