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