]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/sd-journal.c
sd-journal: use extract_first_word()
[thirdparty/systemd.git] / src / journal / sd-journal.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
87d2c1ff 2
87d2c1ff 3#include <errno.h>
87d2c1ff 4#include <fcntl.h>
82e24b00 5#include <inttypes.h>
07630cea
LP
6#include <linux/magic.h>
7#include <poll.h>
3fbf9cbb 8#include <stddef.h>
50f20cfd 9#include <sys/inotify.h>
85210bff 10#include <sys/vfs.h>
07630cea 11#include <unistd.h>
87d2c1ff
LP
12
13#include "sd-journal.h"
c004493c 14
b5efdb8a 15#include "alloc-util.h"
07630cea
LP
16#include "catalog.h"
17#include "compress.h"
a0956174 18#include "dirent-util.h"
686d13b9 19#include "env-file.h"
9e8b1ec0 20#include "escape.h"
3ffd4af2 21#include "fd-util.h"
07630cea 22#include "fileio.h"
f97b34a6 23#include "format-util.h"
77601719 24#include "fs-util.h"
07630cea
LP
25#include "hashmap.h"
26#include "hostname-util.h"
a9be0692 27#include "id128-util.h"
c004493c 28#include "io-util.h"
87d2c1ff 29#include "journal-def.h"
cec736d2 30#include "journal-file.h"
07630cea 31#include "journal-internal.h"
cec736d2 32#include "list.h"
de7b95cd 33#include "lookup3.h"
d8b4d14d 34#include "nulstr-util.h"
07630cea 35#include "path-util.h"
dccca82b 36#include "process-util.h"
d4205751 37#include "replace-var.h"
15a5e950
LP
38#include "stat-util.h"
39#include "stdio-util.h"
07630cea
LP
40#include "string-util.h"
41#include "strv.h"
456aa879 42#include "syslog-util.h"
87d2c1ff 43
9e8abdf0
LP
44#define JOURNAL_FILES_MAX 7168
45
85210bff
LP
46#define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
47
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 Iterator i;
109 JournalFile *f;
110
111 assert(j);
112
113 j->current_file = NULL;
114 j->current_field = 0;
115
c1f906bd 116 ORDERED_HASHMAP_FOREACH(f, j->files, i)
1fc605b0 117 journal_file_reset_location(f);
8f9b6cd9
LP
118}
119
a87247dd 120static void init_location(Location *l, LocationType type, JournalFile *f, Object *o) {
de190aef 121 assert(l);
3742095b 122 assert(IN_SET(type, LOCATION_DISCRETE, LOCATION_SEEK));
de190aef 123 assert(f);
de190aef 124
bba6e4ae
LP
125 *l = (Location) {
126 .type = type,
127 .seqnum = le64toh(o->entry.seqnum),
128 .seqnum_id = f->header->seqnum_id,
129 .realtime = le64toh(o->entry.realtime),
130 .monotonic = le64toh(o->entry.monotonic),
131 .boot_id = o->entry.boot_id,
132 .xor_hash = le64toh(o->entry.xor_hash),
133 .seqnum_set = true,
134 .realtime_set = true,
135 .monotonic_set = true,
136 .xor_hash_set = true,
137 };
de190aef
LP
138}
139
1eb6332d 140static void set_location(sd_journal *j, JournalFile *f, Object *o) {
de190aef
LP
141 assert(j);
142 assert(f);
143 assert(o);
144
1eb6332d 145 init_location(&j->current_location, LOCATION_DISCRETE, f, o);
de190aef
LP
146
147 j->current_file = f;
148 j->current_field = 0;
149
6573ef05
MS
150 /* Let f know its candidate entry was picked. */
151 assert(f->location_type == LOCATION_SEEK);
152 f->location_type = LOCATION_DISCRETE;
de190aef
LP
153}
154
cbdca852
LP
155static int match_is_valid(const void *data, size_t size) {
156 const char *b, *p;
157
158 assert(data);
159
160 if (size < 2)
161 return false;
162
e0567bc8 163 if (((char*) data)[0] == '_' && ((char*) data)[1] == '_')
cbdca852
LP
164 return false;
165
166 b = data;
167 for (p = b; p < b + size; p++) {
168
169 if (*p == '=')
170 return p > b;
171
172 if (*p == '_')
173 continue;
174
175 if (*p >= 'A' && *p <= 'Z')
176 continue;
177
178 if (*p >= '0' && *p <= '9')
179 continue;
180
181 return false;
182 }
183
184 return false;
185}
186
187static bool same_field(const void *_a, size_t s, const void *_b, size_t t) {
de190aef
LP
188 const uint8_t *a = _a, *b = _b;
189 size_t j;
de190aef
LP
190
191 for (j = 0; j < s && j < t; j++) {
192
de190aef 193 if (a[j] != b[j])
cbdca852 194 return false;
de190aef 195
cbdca852
LP
196 if (a[j] == '=')
197 return true;
de190aef
LP
198 }
199
bc302926 200 assert_not_reached("\"=\" not found");
cbdca852
LP
201}
202
203static Match *match_new(Match *p, MatchType t) {
204 Match *m;
205
2f5435a1 206 m = new(Match, 1);
cbdca852
LP
207 if (!m)
208 return NULL;
209
2f5435a1
LP
210 *m = (Match) {
211 .type = t,
212 .parent = p,
213 };
cbdca852 214
2f5435a1 215 if (p)
71fda00f 216 LIST_PREPEND(matches, p->matches, m);
cbdca852
LP
217
218 return m;
219}
220
221static void match_free(Match *m) {
222 assert(m);
223
224 while (m->matches)
225 match_free(m->matches);
226
227 if (m->parent)
71fda00f 228 LIST_REMOVE(matches, m->parent->matches, m);
cbdca852
LP
229
230 free(m->data);
231 free(m);
232}
233
234static void match_free_if_empty(Match *m) {
c5a10d9c 235 if (!m || m->matches)
cbdca852
LP
236 return;
237
238 match_free(m);
de190aef
LP
239}
240
a5344d2c 241_public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
cd34b3c6 242 Match *l3, *l4, *add_here = NULL, *m;
cde8c5f7 243 uint64_t hash;
87d2c1ff 244
1ae464e0
TA
245 assert_return(j, -EINVAL);
246 assert_return(!journal_pid_changed(j), -ECHILD);
247 assert_return(data, -EINVAL);
cbdca852
LP
248
249 if (size == 0)
250 size = strlen(data);
251
1ae464e0 252 assert_return(match_is_valid(data, size), -EINVAL);
1cc101f1 253
cd34b3c6
HH
254 /* level 0: AND term
255 * level 1: OR terms
256 * level 2: AND terms
257 * level 3: OR terms
258 * level 4: concrete matches */
cbdca852
LP
259
260 if (!j->level0) {
cd34b3c6 261 j->level0 = match_new(NULL, MATCH_AND_TERM);
cbdca852
LP
262 if (!j->level0)
263 return -ENOMEM;
264 }
265
266 if (!j->level1) {
cd34b3c6 267 j->level1 = match_new(j->level0, MATCH_OR_TERM);
cbdca852
LP
268 if (!j->level1)
269 return -ENOMEM;
270 }
271
cd34b3c6
HH
272 if (!j->level2) {
273 j->level2 = match_new(j->level1, MATCH_AND_TERM);
274 if (!j->level2)
275 return -ENOMEM;
276 }
277
278 assert(j->level0->type == MATCH_AND_TERM);
279 assert(j->level1->type == MATCH_OR_TERM);
280 assert(j->level2->type == MATCH_AND_TERM);
ab4979d2 281
4ce534f4
LP
282 /* Old-style Jenkins (unkeyed) hashing only here. We do not cover new-style siphash (keyed) hashing
283 * here, since it's different for each file, and thus can't be pre-calculated in the Match object. */
20b0acfa 284 hash = jenkins_hash64(data, size);
de190aef 285
cd34b3c6
HH
286 LIST_FOREACH(matches, l3, j->level2->matches) {
287 assert(l3->type == MATCH_OR_TERM);
de190aef 288
cd34b3c6
HH
289 LIST_FOREACH(matches, l4, l3->matches) {
290 assert(l4->type == MATCH_DISCRETE);
de190aef 291
cbdca852
LP
292 /* Exactly the same match already? Then ignore
293 * this addition */
cde8c5f7 294 if (l4->hash == hash &&
cd34b3c6
HH
295 l4->size == size &&
296 memcmp(l4->data, data, size) == 0)
cbdca852
LP
297 return 0;
298
299 /* Same field? Then let's add this to this OR term */
cd34b3c6
HH
300 if (same_field(data, size, l4->data, l4->size)) {
301 add_here = l3;
cbdca852
LP
302 break;
303 }
304 }
305
306 if (add_here)
307 break;
de190aef
LP
308 }
309
cbdca852 310 if (!add_here) {
cd34b3c6 311 add_here = match_new(j->level2, MATCH_OR_TERM);
cbdca852
LP
312 if (!add_here)
313 goto fail;
314 }
315
316 m = match_new(add_here, MATCH_DISCRETE);
cec736d2 317 if (!m)
cbdca852 318 goto fail;
87d2c1ff 319
cde8c5f7 320 m->hash = hash;
1cc101f1 321 m->size = size;
cbdca852
LP
322 m->data = memdup(data, size);
323 if (!m->data)
324 goto fail;
325
326 detach_location(j);
327
328 return 0;
329
330fail:
c5a10d9c
ZJS
331 match_free_if_empty(add_here);
332 match_free_if_empty(j->level2);
333 match_free_if_empty(j->level1);
334 match_free_if_empty(j->level0);
cbdca852
LP
335
336 return -ENOMEM;
337}
338
cd34b3c6 339_public_ int sd_journal_add_conjunction(sd_journal *j) {
1ae464e0
TA
340 assert_return(j, -EINVAL);
341 assert_return(!journal_pid_changed(j), -ECHILD);
1cc101f1 342
cbdca852
LP
343 if (!j->level0)
344 return 0;
345
346 if (!j->level1)
347 return 0;
348
349 if (!j->level1->matches)
350 return 0;
351
cd34b3c6
HH
352 j->level1 = NULL;
353 j->level2 = NULL;
354
355 return 0;
356}
357
358_public_ int sd_journal_add_disjunction(sd_journal *j) {
1ae464e0
TA
359 assert_return(j, -EINVAL);
360 assert_return(!journal_pid_changed(j), -ECHILD);
cd34b3c6
HH
361
362 if (!j->level0)
363 return 0;
364
365 if (!j->level1)
366 return 0;
367
368 if (!j->level2)
369 return 0;
370
371 if (!j->level2->matches)
372 return 0;
cbdca852 373
cd34b3c6 374 j->level2 = NULL;
cbdca852
LP
375 return 0;
376}
377
378static char *match_make_string(Match *m) {
6b430fdb 379 char *p = NULL, *r;
cbdca852
LP
380 Match *i;
381 bool enclose = false;
382
383 if (!m)
4ad16808 384 return strdup("none");
cbdca852
LP
385
386 if (m->type == MATCH_DISCRETE)
9e8b1ec0 387 return cescape_length(m->data, m->size);
cbdca852 388
cbdca852
LP
389 LIST_FOREACH(matches, i, m->matches) {
390 char *t, *k;
391
392 t = match_make_string(i);
6b430fdb
ZJS
393 if (!t)
394 return mfree(p);
cbdca852
LP
395
396 if (p) {
605405c6 397 k = strjoin(p, m->type == MATCH_OR_TERM ? " OR " : " AND ", t);
cbdca852
LP
398 free(p);
399 free(t);
400
401 if (!k)
402 return NULL;
403
404 p = k;
405
406 enclose = true;
bc302926 407 } else
cbdca852 408 p = t;
87d2c1ff
LP
409 }
410
cbdca852 411 if (enclose) {
605405c6 412 r = strjoin("(", p, ")");
cbdca852
LP
413 free(p);
414 return r;
415 }
87d2c1ff 416
cbdca852
LP
417 return p;
418}
de7b95cd 419
cbdca852
LP
420char *journal_make_match_string(sd_journal *j) {
421 assert(j);
8f9b6cd9 422
cbdca852 423 return match_make_string(j->level0);
87d2c1ff
LP
424}
425
a5344d2c
LP
426_public_ void sd_journal_flush_matches(sd_journal *j) {
427 if (!j)
428 return;
87d2c1ff 429
cbdca852
LP
430 if (j->level0)
431 match_free(j->level0);
de7b95cd 432
cd34b3c6 433 j->level0 = j->level1 = j->level2 = NULL;
8f9b6cd9 434
de190aef 435 detach_location(j);
87d2c1ff
LP
436}
437
b6849042 438_pure_ static int compare_with_location(const JournalFile *f, const Location *l, const JournalFile *current_file) {
90c88092
YW
439 int r;
440
487d3720 441 assert(f);
de190aef 442 assert(l);
487d3720 443 assert(f->location_type == LOCATION_SEEK);
4c701096 444 assert(IN_SET(l->type, LOCATION_DISCRETE, LOCATION_SEEK));
de190aef
LP
445
446 if (l->monotonic_set &&
487d3720 447 sd_id128_equal(f->current_boot_id, l->boot_id) &&
de190aef 448 l->realtime_set &&
487d3720 449 f->current_realtime == l->realtime &&
de190aef 450 l->xor_hash_set &&
b6849042
GM
451 f->current_xor_hash == l->xor_hash &&
452 f != current_file)
de190aef
LP
453 return 0;
454
455 if (l->seqnum_set &&
487d3720 456 sd_id128_equal(f->header->seqnum_id, l->seqnum_id)) {
de190aef 457
90c88092
YW
458 r = CMP(f->current_seqnum, l->seqnum);
459 if (r != 0)
460 return r;
de190aef
LP
461 }
462
463 if (l->monotonic_set &&
487d3720 464 sd_id128_equal(f->current_boot_id, l->boot_id)) {
de190aef 465
90c88092
YW
466 r = CMP(f->current_monotonic, l->monotonic);
467 if (r != 0)
468 return r;
de190aef
LP
469 }
470
471 if (l->realtime_set) {
472
90c88092
YW
473 r = CMP(f->current_realtime, l->realtime);
474 if (r != 0)
475 return r;
de190aef
LP
476 }
477
478 if (l->xor_hash_set) {
de190aef 479
90c88092
YW
480 r = CMP(f->current_xor_hash, l->xor_hash);
481 if (r != 0)
482 return r;
de190aef
LP
483 }
484
485 return 0;
486}
487
cbdca852
LP
488static int next_for_match(
489 sd_journal *j,
490 Match *m,
491 JournalFile *f,
492 uint64_t after_offset,
493 direction_t direction,
494 Object **ret,
495 uint64_t *offset) {
496
de7b95cd 497 int r;
cbdca852
LP
498 uint64_t np = 0;
499 Object *n;
de7b95cd
LP
500
501 assert(j);
cbdca852
LP
502 assert(m);
503 assert(f);
de7b95cd 504
cbdca852 505 if (m->type == MATCH_DISCRETE) {
4ce534f4 506 uint64_t dp, hash;
de190aef 507
4ce534f4
LP
508 /* If the keyed hash logic is used, we need to calculate the hash fresh per file. Otherwise
509 * we can use what we pre-calculated. */
510 if (JOURNAL_HEADER_KEYED_HASH(f->header))
511 hash = journal_file_hash_data(f, m->data, m->size);
512 else
513 hash = m->hash;
514
515 r = journal_file_find_data_object_with_hash(f, m->data, m->size, hash, NULL, &dp);
de190aef
LP
516 if (r <= 0)
517 return r;
518
cbdca852 519 return journal_file_move_to_entry_by_offset_for_data(f, dp, after_offset, direction, ret, offset);
de190aef 520
cbdca852
LP
521 } else if (m->type == MATCH_OR_TERM) {
522 Match *i;
de7b95cd 523
cbdca852 524 /* Find the earliest match beyond after_offset */
de190aef 525
cbdca852
LP
526 LIST_FOREACH(matches, i, m->matches) {
527 uint64_t cp;
de190aef 528
cbdca852 529 r = next_for_match(j, i, f, after_offset, direction, NULL, &cp);
b4e5f920
LP
530 if (r < 0)
531 return r;
cbdca852 532 else if (r > 0) {
bc302926 533 if (np == 0 || (direction == DIRECTION_DOWN ? cp < np : cp > np))
cbdca852
LP
534 np = cp;
535 }
536 }
b4e5f920 537
bc302926
ZJS
538 if (np == 0)
539 return 0;
540
cbdca852 541 } else if (m->type == MATCH_AND_TERM) {
2bc8ca0c 542 Match *i, *last_moved;
de190aef 543
cbdca852 544 /* Always jump to the next matching entry and repeat
2bc8ca0c 545 * this until we find an offset that matches for all
cbdca852 546 * matches. */
de190aef 547
cbdca852
LP
548 if (!m->matches)
549 return 0;
de7b95cd 550
2bc8ca0c
ZJS
551 r = next_for_match(j, m->matches, f, after_offset, direction, NULL, &np);
552 if (r <= 0)
553 return r;
de190aef 554
2bc8ca0c
ZJS
555 assert(direction == DIRECTION_DOWN ? np >= after_offset : np <= after_offset);
556 last_moved = m->matches;
de190aef 557
2bc8ca0c
ZJS
558 LIST_LOOP_BUT_ONE(matches, i, m->matches, last_moved) {
559 uint64_t cp;
de190aef 560
2bc8ca0c
ZJS
561 r = next_for_match(j, i, f, np, direction, NULL, &cp);
562 if (r <= 0)
563 return r;
de190aef 564
2bc8ca0c
ZJS
565 assert(direction == DIRECTION_DOWN ? cp >= np : cp <= np);
566 if (direction == DIRECTION_DOWN ? cp > np : cp < np) {
567 np = cp;
568 last_moved = i;
de190aef 569 }
2bc8ca0c 570 }
cbdca852 571 }
de190aef 572
bc302926 573 assert(np > 0);
de190aef 574
cbdca852
LP
575 r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
576 if (r < 0)
577 return r;
de7b95cd 578
de190aef 579 if (ret)
cbdca852 580 *ret = n;
de190aef 581 if (offset)
cbdca852 582 *offset = np;
de190aef
LP
583
584 return 1;
585}
586
cbdca852
LP
587static int find_location_for_match(
588 sd_journal *j,
589 Match *m,
590 JournalFile *f,
591 direction_t direction,
592 Object **ret,
593 uint64_t *offset) {
594
de190aef 595 int r;
de190aef
LP
596
597 assert(j);
cbdca852 598 assert(m);
de190aef 599 assert(f);
de190aef 600
cbdca852 601 if (m->type == MATCH_DISCRETE) {
4ce534f4
LP
602 uint64_t dp, hash;
603
604 if (JOURNAL_HEADER_KEYED_HASH(f->header))
605 hash = journal_file_hash_data(f, m->data, m->size);
606 else
607 hash = m->hash;
de190aef 608
4ce534f4 609 r = journal_file_find_data_object_with_hash(f, m->data, m->size, hash, NULL, &dp);
de7b95cd
LP
610 if (r <= 0)
611 return r;
612
cbdca852 613 /* FIXME: missing: find by monotonic */
de7b95cd 614
cbdca852
LP
615 if (j->current_location.type == LOCATION_HEAD)
616 return journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_DOWN, ret, offset);
617 if (j->current_location.type == LOCATION_TAIL)
618 return journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_UP, ret, offset);
619 if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
620 return journal_file_move_to_entry_by_seqnum_for_data(f, dp, j->current_location.seqnum, direction, ret, offset);
621 if (j->current_location.monotonic_set) {
622 r = journal_file_move_to_entry_by_monotonic_for_data(f, dp, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset);
623 if (r != -ENOENT)
624 return r;
625 }
626 if (j->current_location.realtime_set)
627 return journal_file_move_to_entry_by_realtime_for_data(f, dp, j->current_location.realtime, direction, ret, offset);
de190aef 628
cbdca852 629 return journal_file_next_entry_for_data(f, NULL, 0, dp, direction, ret, offset);
de7b95cd 630
cbdca852
LP
631 } else if (m->type == MATCH_OR_TERM) {
632 uint64_t np = 0;
633 Object *n;
634 Match *i;
de7b95cd 635
cbdca852 636 /* Find the earliest match */
de7b95cd 637
cbdca852
LP
638 LIST_FOREACH(matches, i, m->matches) {
639 uint64_t cp;
640
641 r = find_location_for_match(j, i, f, direction, NULL, &cp);
642 if (r < 0)
643 return r;
644 else if (r > 0) {
645 if (np == 0 || (direction == DIRECTION_DOWN ? np > cp : np < cp))
646 np = cp;
de190aef 647 }
cbdca852 648 }
de190aef 649
cbdca852
LP
650 if (np == 0)
651 return 0;
de7b95cd 652
cbdca852
LP
653 r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
654 if (r < 0)
655 return r;
de7b95cd 656
cbdca852
LP
657 if (ret)
658 *ret = n;
659 if (offset)
660 *offset = np;
de190aef 661
cbdca852 662 return 1;
e892bd17 663
cbdca852
LP
664 } else {
665 Match *i;
666 uint64_t np = 0;
667
668 assert(m->type == MATCH_AND_TERM);
669
670 /* First jump to the last match, and then find the
671 * next one where all matches match */
672
673 if (!m->matches)
674 return 0;
675
676 LIST_FOREACH(matches, i, m->matches) {
677 uint64_t cp;
678
679 r = find_location_for_match(j, i, f, direction, NULL, &cp);
680 if (r <= 0)
4b067dc9
LP
681 return r;
682
bc302926 683 if (np == 0 || (direction == DIRECTION_DOWN ? cp > np : cp < np))
cbdca852 684 np = cp;
de7b95cd
LP
685 }
686
cbdca852
LP
687 return next_for_match(j, m, f, np, direction, ret, offset);
688 }
689}
de190aef 690
cbdca852
LP
691static int find_location_with_matches(
692 sd_journal *j,
693 JournalFile *f,
694 direction_t direction,
695 Object **ret,
696 uint64_t *offset) {
697
698 int r;
699
700 assert(j);
701 assert(f);
702 assert(ret);
703 assert(offset);
704
705 if (!j->level0) {
706 /* No matches is simple */
707
708 if (j->current_location.type == LOCATION_HEAD)
f534928a 709 return journal_file_next_entry(f, 0, DIRECTION_DOWN, ret, offset);
cbdca852 710 if (j->current_location.type == LOCATION_TAIL)
f534928a 711 return journal_file_next_entry(f, 0, DIRECTION_UP, ret, offset);
cbdca852
LP
712 if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
713 return journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, ret, offset);
714 if (j->current_location.monotonic_set) {
715 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset);
716 if (r != -ENOENT)
717 return r;
de7b95cd 718 }
cbdca852
LP
719 if (j->current_location.realtime_set)
720 return journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, ret, offset);
de7b95cd 721
f534928a 722 return journal_file_next_entry(f, 0, direction, ret, offset);
cbdca852
LP
723 } else
724 return find_location_for_match(j, j->level0, f, direction, ret, offset);
725}
de7b95cd 726
cbdca852
LP
727static int next_with_matches(
728 sd_journal *j,
729 JournalFile *f,
730 direction_t direction,
731 Object **ret,
732 uint64_t *offset) {
733
cbdca852
LP
734 assert(j);
735 assert(f);
736 assert(ret);
737 assert(offset);
738
cbdca852
LP
739 /* No matches is easy. We simple advance the file
740 * pointer by one. */
741 if (!j->level0)
b29ddfcb 742 return journal_file_next_entry(f, f->current_offset, direction, ret, offset);
cbdca852
LP
743
744 /* If we have a match then we look for the next matching entry
49f43d5f 745 * with an offset at least one step larger */
b29ddfcb
MS
746 return next_for_match(j, j->level0, f,
747 direction == DIRECTION_DOWN ? f->current_offset + 1
748 : f->current_offset - 1,
749 direction, ret, offset);
de7b95cd
LP
750}
751
58439db4 752static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction) {
de190aef 753 Object *c;
6e693b42 754 uint64_t cp, n_entries;
cbdca852 755 int r;
de190aef
LP
756
757 assert(j);
758 assert(f);
759
950c07d4
MS
760 n_entries = le64toh(f->header->n_entries);
761
762 /* If we hit EOF before, we don't need to look into this file again
763 * unless direction changed or new entries appeared. */
764 if (f->last_direction == direction && f->location_type == LOCATION_TAIL &&
765 n_entries == f->last_n_entries)
766 return 0;
668c965a 767
950c07d4
MS
768 f->last_n_entries = n_entries;
769
770 if (f->last_direction == direction && f->current_offset > 0) {
7943f422
MS
771 /* LOCATION_SEEK here means we did the work in a previous
772 * iteration and the current location already points to a
773 * candidate entry. */
487d3720
MS
774 if (f->location_type != LOCATION_SEEK) {
775 r = next_with_matches(j, f, direction, &c, &cp);
776 if (r <= 0)
777 return r;
466ccd92 778
950c07d4 779 journal_file_save_location(f, c, cp);
487d3720 780 }
de190aef 781 } else {
950c07d4
MS
782 f->last_direction = direction;
783
cbdca852 784 r = find_location_with_matches(j, f, direction, &c, &cp);
de190aef
LP
785 if (r <= 0)
786 return r;
487d3720 787
950c07d4 788 journal_file_save_location(f, c, cp);
de190aef
LP
789 }
790
bc302926 791 /* OK, we found the spot, now let's advance until an entry
cbdca852
LP
792 * that is actually different from what we were previously
793 * looking at. This is necessary to handle entries which exist
794 * in two (or more) journal files, and which shall all be
795 * suppressed but one. */
796
de190aef
LP
797 for (;;) {
798 bool found;
799
800 if (j->current_location.type == LOCATION_DISCRETE) {
801 int k;
802
b6849042 803 k = compare_with_location(f, &j->current_location, j->current_file);
1cdf7175
CH
804
805 found = direction == DIRECTION_DOWN ? k > 0 : k < 0;
de190aef
LP
806 } else
807 found = true;
808
487d3720 809 if (found)
de190aef 810 return 1;
de190aef
LP
811
812 r = next_with_matches(j, f, direction, &c, &cp);
813 if (r <= 0)
814 return r;
487d3720 815
950c07d4 816 journal_file_save_location(f, c, cp);
de190aef
LP
817 }
818}
819
e892bd17 820static int real_journal_next(sd_journal *j, direction_t direction) {
5d4ba7f2
VC
821 JournalFile *new_file = NULL;
822 unsigned i, n_files;
823 const void **files;
a002d44b 824 Object *o;
87d2c1ff
LP
825 int r;
826
1ae464e0
TA
827 assert_return(j, -EINVAL);
828 assert_return(!journal_pid_changed(j), -ECHILD);
87d2c1ff 829
5d4ba7f2
VC
830 r = iterated_cache_get(j->files_cache, NULL, &files, &n_files);
831 if (r < 0)
832 return r;
833
834 for (i = 0; i < n_files; i++) {
835 JournalFile *f = (JournalFile *)files[i];
de190aef 836 bool found;
87d2c1ff 837
58439db4 838 r = next_beyond_location(j, f, direction);
e590af26 839 if (r < 0) {
da927ba9 840 log_debug_errno(r, "Can't iterate through %s, ignoring: %m", f->path);
a9a245c1 841 remove_file_real(j, f);
e590af26 842 continue;
6573ef05
MS
843 } else if (r == 0) {
844 f->location_type = LOCATION_TAIL;
cec736d2 845 continue;
6573ef05 846 }
87d2c1ff 847
468b21de 848 if (!new_file)
de190aef
LP
849 found = true;
850 else {
851 int k;
852
d8ae66d7 853 k = journal_file_compare_locations(f, new_file);
de190aef 854
bc302926 855 found = direction == DIRECTION_DOWN ? k < 0 : k > 0;
de190aef
LP
856 }
857
e499c999 858 if (found)
468b21de 859 new_file = f;
87d2c1ff
LP
860 }
861
468b21de 862 if (!new_file)
de190aef 863 return 0;
ae2cc8ef 864
e499c999 865 r = journal_file_move_to_object(new_file, OBJECT_ENTRY, new_file->current_offset, &o);
468b21de
LP
866 if (r < 0)
867 return r;
868
1eb6332d 869 set_location(j, new_file, o);
ae2cc8ef 870
de190aef
LP
871 return 1;
872}
ae2cc8ef 873
a5344d2c 874_public_ int sd_journal_next(sd_journal *j) {
de190aef
LP
875 return real_journal_next(j, DIRECTION_DOWN);
876}
ae2cc8ef 877
a5344d2c 878_public_ int sd_journal_previous(sd_journal *j) {
de190aef
LP
879 return real_journal_next(j, DIRECTION_UP);
880}
ae2cc8ef 881
6f003b43 882static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) {
de190aef 883 int c = 0, r;
ae2cc8ef 884
1ae464e0
TA
885 assert_return(j, -EINVAL);
886 assert_return(!journal_pid_changed(j), -ECHILD);
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
ZJS
1856static int add_current_paths(sd_journal *j) {
1857 Iterator i;
1858 JournalFile *f;
1859
1860 assert(j);
1861 assert(j->no_new_files);
1862
5d1ce257 1863 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
5302ebe1
ZJS
1864 * treat them as fatal. */
1865
c1f906bd 1866 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
5302ebe1 1867 _cleanup_free_ char *dir;
e9174f29 1868 int r;
5302ebe1
ZJS
1869
1870 dir = dirname_malloc(f->path);
1871 if (!dir)
1872 return -ENOMEM;
1873
5d1ce257 1874 r = add_directory(j, dir, NULL);
d617408e 1875 if (r < 0)
5302ebe1 1876 return r;
5302ebe1
ZJS
1877 }
1878
1879 return 0;
1880}
1881
a963990f 1882static int allocate_inotify(sd_journal *j) {
50f20cfd 1883 assert(j);
50f20cfd 1884
a963990f
LP
1885 if (j->inotify_fd < 0) {
1886 j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1887 if (j->inotify_fd < 0)
1888 return -errno;
1889 }
50f20cfd 1890
cb306f5d 1891 return hashmap_ensure_allocated(&j->directories_by_wd, NULL);
50f20cfd
LP
1892}
1893
456aa879 1894static sd_journal *journal_new(int flags, const char *path, const char *namespace) {
17c9aff8 1895 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
50f20cfd 1896
a963990f
LP
1897 j = new0(sd_journal, 1);
1898 if (!j)
1899 return NULL;
50f20cfd 1900
df0ff127 1901 j->original_pid = getpid_cached();
5d1ce257 1902 j->toplevel_fd = -1;
a963990f
LP
1903 j->inotify_fd = -1;
1904 j->flags = flags;
93b73b06 1905 j->data_threshold = DEFAULT_DATA_THRESHOLD;
50f20cfd 1906
7827b1a1 1907 if (path) {
16fefe90
ZJS
1908 char *t;
1909
1910 t = strdup(path);
1911 if (!t)
17c9aff8 1912 return NULL;
16fefe90
ZJS
1913
1914 if (flags & SD_JOURNAL_OS_ROOT)
1915 j->prefix = t;
1916 else
1917 j->path = t;
7827b1a1
LP
1918 }
1919
456aa879
LP
1920 if (namespace) {
1921 j->namespace = strdup(namespace);
1922 if (!j->namespace)
1923 return NULL;
1924 }
1925
548f6937 1926 j->files = ordered_hashmap_new(&path_hash_ops);
5d4ba7f2 1927 if (!j->files)
17c9aff8 1928 return NULL;
5d4ba7f2
VC
1929
1930 j->files_cache = ordered_hashmap_iterated_cache_new(j->files);
548f6937 1931 j->directories_by_path = hashmap_new(&path_hash_ops);
84168d80 1932 j->mmap = mmap_cache_new();
5d4ba7f2 1933 if (!j->files_cache || !j->directories_by_path || !j->mmap)
17c9aff8 1934 return NULL;
6180fc61 1935
17c9aff8 1936 return TAKE_PTR(j);
50f20cfd
LP
1937}
1938
1aaa68f5
ZJS
1939#define OPEN_ALLOWED_FLAGS \
1940 (SD_JOURNAL_LOCAL_ONLY | \
1941 SD_JOURNAL_RUNTIME_ONLY | \
456aa879
LP
1942 SD_JOURNAL_SYSTEM | \
1943 SD_JOURNAL_CURRENT_USER | \
1944 SD_JOURNAL_ALL_NAMESPACES | \
1945 SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE)
1aaa68f5 1946
456aa879 1947_public_ int sd_journal_open_namespace(sd_journal **ret, const char *namespace, int flags) {
17c9aff8 1948 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
3fbf9cbb 1949 int r;
87d2c1ff 1950
1ae464e0 1951 assert_return(ret, -EINVAL);
1aaa68f5 1952 assert_return((flags & ~OPEN_ALLOWED_FLAGS) == 0, -EINVAL);
87d2c1ff 1953
456aa879 1954 j = journal_new(flags, NULL, namespace);
87d2c1ff
LP
1955 if (!j)
1956 return -ENOMEM;
1957
89739579 1958 r = add_search_paths(j);
a963990f 1959 if (r < 0)
17c9aff8 1960 return r;
50f20cfd 1961
17c9aff8 1962 *ret = TAKE_PTR(j);
a963990f 1963 return 0;
a963990f 1964}
50f20cfd 1965
456aa879
LP
1966_public_ int sd_journal_open(sd_journal **ret, int flags) {
1967 return sd_journal_open_namespace(ret, NULL, flags);
1968}
1969
1aaa68f5
ZJS
1970#define OPEN_CONTAINER_ALLOWED_FLAGS \
1971 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1972
b6741478
LP
1973_public_ int sd_journal_open_container(sd_journal **ret, const char *machine, int flags) {
1974 _cleanup_free_ char *root = NULL, *class = NULL;
17c9aff8 1975 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
b6741478
LP
1976 char *p;
1977 int r;
1978
68312977 1979 /* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in
2daa9cbd
LP
1980 * combination with sd_journal_open_directory_fd(). */
1981
b6741478
LP
1982 assert_return(machine, -EINVAL);
1983 assert_return(ret, -EINVAL);
1aaa68f5 1984 assert_return((flags & ~OPEN_CONTAINER_ALLOWED_FLAGS) == 0, -EINVAL);
affcf189 1985 assert_return(machine_name_is_valid(machine), -EINVAL);
b6741478 1986
63c372cb 1987 p = strjoina("/run/systemd/machines/", machine);
13df9c39
LP
1988 r = parse_env_file(NULL, p,
1989 "ROOT", &root,
1990 "CLASS", &class);
b6741478
LP
1991 if (r == -ENOENT)
1992 return -EHOSTDOWN;
1993 if (r < 0)
1994 return r;
1995 if (!root)
1996 return -ENODATA;
1997
1998 if (!streq_ptr(class, "container"))
1999 return -EIO;
2000
456aa879 2001 j = journal_new(flags, root, NULL);
b6741478
LP
2002 if (!j)
2003 return -ENOMEM;
2004
89739579 2005 r = add_search_paths(j);
b6741478 2006 if (r < 0)
17c9aff8 2007 return r;
b6741478 2008
17c9aff8 2009 *ret = TAKE_PTR(j);
b6741478 2010 return 0;
b6741478
LP
2011}
2012
1aaa68f5 2013#define OPEN_DIRECTORY_ALLOWED_FLAGS \
10752e82
ZJS
2014 (SD_JOURNAL_OS_ROOT | \
2015 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1aaa68f5 2016
a963990f 2017_public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) {
17c9aff8 2018 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
a963990f 2019 int r;
87d2c1ff 2020
1ae464e0
TA
2021 assert_return(ret, -EINVAL);
2022 assert_return(path, -EINVAL);
1aaa68f5 2023 assert_return((flags & ~OPEN_DIRECTORY_ALLOWED_FLAGS) == 0, -EINVAL);
87d2c1ff 2024
456aa879 2025 j = journal_new(flags, path, NULL);
a963990f
LP
2026 if (!j)
2027 return -ENOMEM;
3fbf9cbb 2028
d077390c
LP
2029 if (flags & SD_JOURNAL_OS_ROOT)
2030 r = add_search_paths(j);
2031 else
2032 r = add_root_directory(j, path, false);
d617408e 2033 if (r < 0)
17c9aff8 2034 return r;
87d2c1ff 2035
17c9aff8 2036 *ret = TAKE_PTR(j);
87d2c1ff 2037 return 0;
a963990f 2038}
87d2c1ff 2039
5302ebe1 2040_public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int flags) {
17c9aff8 2041 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
5302ebe1
ZJS
2042 const char **path;
2043 int r;
2044
1ae464e0
TA
2045 assert_return(ret, -EINVAL);
2046 assert_return(flags == 0, -EINVAL);
5302ebe1 2047
456aa879 2048 j = journal_new(flags, NULL, NULL);
5302ebe1
ZJS
2049 if (!j)
2050 return -ENOMEM;
2051
2052 STRV_FOREACH(path, paths) {
5d1ce257 2053 r = add_any_file(j, -1, *path);
d617408e 2054 if (r < 0)
17c9aff8 2055 return r;
5302ebe1
ZJS
2056 }
2057
2058 j->no_new_files = true;
2059
17c9aff8 2060 *ret = TAKE_PTR(j);
5302ebe1 2061 return 0;
5d1ce257
LP
2062}
2063
1aaa68f5 2064#define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
10752e82
ZJS
2065 (SD_JOURNAL_OS_ROOT | \
2066 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1aaa68f5 2067
5d1ce257 2068_public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) {
17c9aff8 2069 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
5d1ce257
LP
2070 struct stat st;
2071 int r;
2072
2073 assert_return(ret, -EINVAL);
2074 assert_return(fd >= 0, -EBADF);
1aaa68f5 2075 assert_return((flags & ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS) == 0, -EINVAL);
5d1ce257
LP
2076
2077 if (fstat(fd, &st) < 0)
2078 return -errno;
2079
2080 if (!S_ISDIR(st.st_mode))
2081 return -EBADFD;
2082
456aa879 2083 j = journal_new(flags, NULL, NULL);
5d1ce257
LP
2084 if (!j)
2085 return -ENOMEM;
2086
2087 j->toplevel_fd = fd;
2088
d077390c
LP
2089 if (flags & SD_JOURNAL_OS_ROOT)
2090 r = add_search_paths(j);
2091 else
2092 r = add_root_directory(j, NULL, false);
5d1ce257 2093 if (r < 0)
17c9aff8 2094 return r;
5d1ce257 2095
17c9aff8 2096 *ret = TAKE_PTR(j);
5d1ce257 2097 return 0;
5d1ce257
LP
2098}
2099
2100_public_ int sd_journal_open_files_fd(sd_journal **ret, int fds[], unsigned n_fds, int flags) {
2101 Iterator iterator;
2102 JournalFile *f;
17c9aff8 2103 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
5d1ce257
LP
2104 unsigned i;
2105 int r;
2106
2107 assert_return(ret, -EINVAL);
2108 assert_return(n_fds > 0, -EBADF);
2109 assert_return(flags == 0, -EINVAL);
2110
456aa879 2111 j = journal_new(flags, NULL, NULL);
5d1ce257
LP
2112 if (!j)
2113 return -ENOMEM;
2114
2115 for (i = 0; i < n_fds; i++) {
2116 struct stat st;
2117
2118 if (fds[i] < 0) {
2119 r = -EBADF;
2120 goto fail;
2121 }
2122
2123 if (fstat(fds[i], &st) < 0) {
2124 r = -errno;
2125 goto fail;
2126 }
2127
3cc44114
LP
2128 r = stat_verify_regular(&st);
2129 if (r < 0)
5d1ce257 2130 goto fail;
5d1ce257
LP
2131
2132 r = add_any_file(j, fds[i], NULL);
2133 if (r < 0)
2134 goto fail;
2135 }
2136
2137 j->no_new_files = true;
2138 j->no_inotify = true;
5302ebe1 2139
17c9aff8 2140 *ret = TAKE_PTR(j);
5d1ce257
LP
2141 return 0;
2142
2143fail:
f8e2f4d6 2144 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
5d1ce257
LP
2145 * remain open */
2146 ORDERED_HASHMAP_FOREACH(f, j->files, iterator)
2147 f->close_fd = false;
2148
5302ebe1
ZJS
2149 return r;
2150}
2151
a5344d2c 2152_public_ void sd_journal_close(sd_journal *j) {
a963990f 2153 Directory *d;
a963990f 2154
a5344d2c
LP
2155 if (!j)
2156 return;
87d2c1ff 2157
54b1da83
LP
2158 sd_journal_flush_matches(j);
2159
f9168190 2160 ordered_hashmap_free_with_destructor(j->files, journal_file_close);
5d4ba7f2 2161 iterated_cache_free(j->files_cache);
260a2be4 2162
a963990f
LP
2163 while ((d = hashmap_first(j->directories_by_path)))
2164 remove_directory(j, d);
260a2be4 2165
a963990f
LP
2166 while ((d = hashmap_first(j->directories_by_wd)))
2167 remove_directory(j, d);
87d2c1ff 2168
a963990f
LP
2169 hashmap_free(j->directories_by_path);
2170 hashmap_free(j->directories_by_wd);
1cc101f1 2171
03e334a1 2172 safe_close(j->inotify_fd);
50f20cfd 2173
bf807d4d
LP
2174 if (j->mmap) {
2175 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j->mmap), mmap_cache_get_missed(j->mmap));
16e9f408 2176 mmap_cache_unref(j->mmap);
bf807d4d 2177 }
16e9f408 2178
ec1d2909 2179 hashmap_free_free(j->errors);
5768d259 2180
7827b1a1 2181 free(j->path);
89739579 2182 free(j->prefix);
456aa879 2183 free(j->namespace);
3c1668da 2184 free(j->unique_field);
eb86030e 2185 free(j->fields_buffer);
87d2c1ff
LP
2186 free(j);
2187}
3fbf9cbb 2188
a5344d2c 2189_public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
3fbf9cbb
LP
2190 Object *o;
2191 JournalFile *f;
2192 int r;
2193
1ae464e0
TA
2194 assert_return(j, -EINVAL);
2195 assert_return(!journal_pid_changed(j), -ECHILD);
2196 assert_return(ret, -EINVAL);
3fbf9cbb
LP
2197
2198 f = j->current_file;
2199 if (!f)
de190aef 2200 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2201
2202 if (f->current_offset <= 0)
de190aef 2203 return -EADDRNOTAVAIL;
3fbf9cbb 2204
de190aef 2205 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2206 if (r < 0)
2207 return r;
2208
2209 *ret = le64toh(o->entry.realtime);
de190aef 2210 return 0;
3fbf9cbb
LP
2211}
2212
a5344d2c 2213_public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
3fbf9cbb
LP
2214 Object *o;
2215 JournalFile *f;
2216 int r;
3fbf9cbb 2217
1ae464e0
TA
2218 assert_return(j, -EINVAL);
2219 assert_return(!journal_pid_changed(j), -ECHILD);
3fbf9cbb
LP
2220
2221 f = j->current_file;
2222 if (!f)
de190aef 2223 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2224
2225 if (f->current_offset <= 0)
de190aef 2226 return -EADDRNOTAVAIL;
3fbf9cbb 2227
de190aef 2228 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2229 if (r < 0)
2230 return r;
2231
de190aef
LP
2232 if (ret_boot_id)
2233 *ret_boot_id = o->entry.boot_id;
2234 else {
d4739bc4
VC
2235 sd_id128_t id;
2236
de190aef
LP
2237 r = sd_id128_get_boot(&id);
2238 if (r < 0)
2239 return r;
3fbf9cbb 2240
de190aef 2241 if (!sd_id128_equal(id, o->entry.boot_id))
df50185b 2242 return -ESTALE;
de190aef 2243 }
3fbf9cbb 2244
14a65d65
LP
2245 if (ret)
2246 *ret = le64toh(o->entry.monotonic);
2247
de190aef 2248 return 0;
3fbf9cbb
LP
2249}
2250
362a3f81
LP
2251static bool field_is_valid(const char *field) {
2252 const char *p;
2253
2254 assert(field);
2255
2256 if (isempty(field))
2257 return false;
2258
2259 if (startswith(field, "__"))
2260 return false;
2261
2262 for (p = field; *p; p++) {
2263
2264 if (*p == '_')
2265 continue;
2266
2267 if (*p >= 'A' && *p <= 'Z')
2268 continue;
2269
2270 if (*p >= '0' && *p <= '9')
2271 continue;
2272
2273 return false;
2274 }
2275
2276 return true;
2277}
2278
a5344d2c 2279_public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
3fbf9cbb
LP
2280 JournalFile *f;
2281 uint64_t i, n;
2282 size_t field_length;
2283 int r;
2284 Object *o;
2285
1ae464e0
TA
2286 assert_return(j, -EINVAL);
2287 assert_return(!journal_pid_changed(j), -ECHILD);
2288 assert_return(field, -EINVAL);
2289 assert_return(data, -EINVAL);
2290 assert_return(size, -EINVAL);
2291 assert_return(field_is_valid(field), -EINVAL);
3fbf9cbb
LP
2292
2293 f = j->current_file;
2294 if (!f)
de190aef 2295 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2296
2297 if (f->current_offset <= 0)
de190aef 2298 return -EADDRNOTAVAIL;
3fbf9cbb 2299
de190aef 2300 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2301 if (r < 0)
2302 return r;
2303
2304 field_length = strlen(field);
2305
2306 n = journal_file_entry_n_items(o);
2307 for (i = 0; i < n; i++) {
4fd052ae
FC
2308 uint64_t p, l;
2309 le64_t le_hash;
3fbf9cbb 2310 size_t t;
1ec7120e 2311 int compression;
3fbf9cbb
LP
2312
2313 p = le64toh(o->entry.items[i].object_offset);
807e17f0 2314 le_hash = o->entry.items[i].hash;
de190aef 2315 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
3fbf9cbb
LP
2316 if (r < 0)
2317 return r;
2318
de190aef 2319 if (le_hash != o->data.hash)
de7b95cd
LP
2320 return -EBADMSG;
2321
3fbf9cbb
LP
2322 l = le64toh(o->object.size) - offsetof(Object, data.payload);
2323
1ec7120e 2324 compression = o->object.flags & OBJECT_COMPRESSION_MASK;
3b1a55e1 2325 if (compression) {
d80b051c 2326#if HAVE_COMPRESSION
2aaec9b4 2327 r = decompress_startswith(compression,
3b1a55e1
ZJS
2328 o->data.payload, l,
2329 &f->compress_buffer, &f->compress_buffer_size,
2aaec9b4
ZJS
2330 field, field_length, '=');
2331 if (r < 0)
82e24b00 2332 log_debug_errno(r, "Cannot decompress %s object of length %"PRIu64" at offset "OFSfmt": %m",
2aaec9b4
ZJS
2333 object_compressed_to_string(compression), l, p);
2334 else if (r > 0) {
3b1a55e1 2335
fa1c4b51 2336 size_t rsize;
3b1a55e1
ZJS
2337
2338 r = decompress_blob(compression,
2339 o->data.payload, l,
2340 &f->compress_buffer, &f->compress_buffer_size, &rsize,
2341 j->data_threshold);
2342 if (r < 0)
2343 return r;
807e17f0 2344
3b1a55e1
ZJS
2345 *data = f->compress_buffer;
2346 *size = (size_t) rsize;
807e17f0 2347
3b1a55e1
ZJS
2348 return 0;
2349 }
2350#else
2351 return -EPROTONOSUPPORT;
2352#endif
807e17f0
LP
2353 } else if (l >= field_length+1 &&
2354 memcmp(o->data.payload, field, field_length) == 0 &&
2355 o->data.payload[field_length] == '=') {
3fbf9cbb 2356
161e54f8 2357 t = (size_t) l;
3fbf9cbb 2358
161e54f8
LP
2359 if ((uint64_t) t != l)
2360 return -E2BIG;
3fbf9cbb 2361
161e54f8
LP
2362 *data = o->data.payload;
2363 *size = t;
3fbf9cbb 2364
99613ec5 2365 return 0;
161e54f8 2366 }
3fbf9cbb 2367
de190aef 2368 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
161e54f8
LP
2369 if (r < 0)
2370 return r;
3fbf9cbb
LP
2371 }
2372
de190aef 2373 return -ENOENT;
3fbf9cbb
LP
2374}
2375
93b73b06 2376static int return_data(sd_journal *j, JournalFile *f, Object *o, const void **data, size_t *size) {
3c1668da
LP
2377 size_t t;
2378 uint64_t l;
3b1a55e1 2379 int compression;
3c1668da 2380
893e0f8f
LP
2381 l = le64toh(READ_NOW(o->object.size));
2382 if (l < offsetof(Object, data.payload))
2383 return -EBADMSG;
2384 l -= offsetof(Object, data.payload);
3c1668da
LP
2385 t = (size_t) l;
2386
2387 /* We can't read objects larger than 4G on a 32bit machine */
2388 if ((uint64_t) t != l)
2389 return -E2BIG;
2390
1ec7120e
ZJS
2391 compression = o->object.flags & OBJECT_COMPRESSION_MASK;
2392 if (compression) {
d80b051c 2393#if HAVE_COMPRESSION
fa1c4b51 2394 size_t rsize;
3b1a55e1 2395 int r;
3c1668da 2396
1ec7120e
ZJS
2397 r = decompress_blob(compression,
2398 o->data.payload, l, &f->compress_buffer,
2399 &f->compress_buffer_size, &rsize, j->data_threshold);
2400 if (r < 0)
2401 return r;
3c1668da
LP
2402
2403 *data = f->compress_buffer;
2404 *size = (size_t) rsize;
3b1a55e1
ZJS
2405#else
2406 return -EPROTONOSUPPORT;
2407#endif
3c1668da
LP
2408 } else {
2409 *data = o->data.payload;
2410 *size = t;
2411 }
2412
2413 return 0;
2414}
2415
a5344d2c 2416_public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
3fbf9cbb 2417 JournalFile *f;
3c1668da 2418 uint64_t p, n;
4fd052ae 2419 le64_t le_hash;
3fbf9cbb
LP
2420 int r;
2421 Object *o;
2422
1ae464e0
TA
2423 assert_return(j, -EINVAL);
2424 assert_return(!journal_pid_changed(j), -ECHILD);
2425 assert_return(data, -EINVAL);
2426 assert_return(size, -EINVAL);
3fbf9cbb
LP
2427
2428 f = j->current_file;
2429 if (!f)
de190aef 2430 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2431
2432 if (f->current_offset <= 0)
de190aef 2433 return -EADDRNOTAVAIL;
3fbf9cbb 2434
de190aef 2435 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2436 if (r < 0)
2437 return r;
2438
2439 n = journal_file_entry_n_items(o);
7210bfb3 2440 if (j->current_field >= n)
3fbf9cbb
LP
2441 return 0;
2442
7210bfb3 2443 p = le64toh(o->entry.items[j->current_field].object_offset);
de190aef
LP
2444 le_hash = o->entry.items[j->current_field].hash;
2445 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
3fbf9cbb
LP
2446 if (r < 0)
2447 return r;
2448
de190aef 2449 if (le_hash != o->data.hash)
de7b95cd
LP
2450 return -EBADMSG;
2451
93b73b06 2452 r = return_data(j, f, o, data, size);
3c1668da
LP
2453 if (r < 0)
2454 return r;
3fbf9cbb 2455
313cefa1 2456 j->current_field++;
3fbf9cbb
LP
2457
2458 return 1;
2459}
c2373f84 2460
76cbafcd
ZJS
2461_public_ int sd_journal_enumerate_available_data(sd_journal *j, const void **data, size_t *size) {
2462 for (;;) {
2463 int r;
2464
2465 r = sd_journal_enumerate_data(j, data, size);
2466 if (r >= 0)
2467 return r;
2468 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r))
2469 return r;
2470 j->current_field++; /* Try with the next field */
2471 }
2472}
2473
a5344d2c
LP
2474_public_ void sd_journal_restart_data(sd_journal *j) {
2475 if (!j)
2476 return;
8725d60a
LP
2477
2478 j->current_field = 0;
c2373f84 2479}
50f20cfd 2480
858749f7
LP
2481static int reiterate_all_paths(sd_journal *j) {
2482 assert(j);
2483
2484 if (j->no_new_files)
2485 return add_current_paths(j);
2486
2487 if (j->flags & SD_JOURNAL_OS_ROOT)
2488 return add_search_paths(j);
2489
2490 if (j->toplevel_fd >= 0)
2491 return add_root_directory(j, NULL, false);
2492
2493 if (j->path)
2494 return add_root_directory(j, j->path, true);
2495
2496 return add_search_paths(j);
2497}
2498
a5344d2c 2499_public_ int sd_journal_get_fd(sd_journal *j) {
a963990f
LP
2500 int r;
2501
1ae464e0
TA
2502 assert_return(j, -EINVAL);
2503 assert_return(!journal_pid_changed(j), -ECHILD);
50f20cfd 2504
5d1ce257
LP
2505 if (j->no_inotify)
2506 return -EMEDIUMTYPE;
2507
a963990f
LP
2508 if (j->inotify_fd >= 0)
2509 return j->inotify_fd;
2510
2511 r = allocate_inotify(j);
2512 if (r < 0)
2513 return r;
2514
858749f7 2515 log_debug("Reiterating files to get inotify watches established.");
5d1ce257 2516
858749f7
LP
2517 /* Iterate through all dirs again, to add them to the inotify */
2518 r = reiterate_all_paths(j);
a963990f
LP
2519 if (r < 0)
2520 return r;
2521
50f20cfd
LP
2522 return j->inotify_fd;
2523}
2524
ee531d94
LP
2525_public_ int sd_journal_get_events(sd_journal *j) {
2526 int fd;
2527
1ae464e0
TA
2528 assert_return(j, -EINVAL);
2529 assert_return(!journal_pid_changed(j), -ECHILD);
ee531d94
LP
2530
2531 fd = sd_journal_get_fd(j);
2532 if (fd < 0)
2533 return fd;
2534
2535 return POLLIN;
2536}
2537
39c155ea
LP
2538_public_ int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec) {
2539 int fd;
2540
1ae464e0
TA
2541 assert_return(j, -EINVAL);
2542 assert_return(!journal_pid_changed(j), -ECHILD);
2543 assert_return(timeout_usec, -EINVAL);
39c155ea
LP
2544
2545 fd = sd_journal_get_fd(j);
2546 if (fd < 0)
2547 return fd;
2548
2549 if (!j->on_network) {
2550 *timeout_usec = (uint64_t) -1;
2551 return 0;
2552 }
2553
2554 /* If we are on the network we need to regularly check for
2555 * changes manually */
2556
2557 *timeout_usec = j->last_process_usec + JOURNAL_FILES_RECHECK_USEC;
2558 return 1;
2559}
2560
858749f7
LP
2561static void process_q_overflow(sd_journal *j) {
2562 JournalFile *f;
2563 Directory *m;
2564 Iterator i;
2565
2566 assert(j);
2567
2568 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2569 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2570 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2571 * are subject for unloading. */
2572
2573 log_debug("Inotify queue overrun, reiterating everything.");
2574
2575 j->generation++;
2576 (void) reiterate_all_paths(j);
2577
2578 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
2579
2580 if (f->last_seen_generation == j->generation)
2581 continue;
2582
2583 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f->path);
2584 remove_file_real(j, f);
2585 }
2586
2587 HASHMAP_FOREACH(m, j->directories_by_path, i) {
2588
2589 if (m->last_seen_generation == j->generation)
2590 continue;
2591
2592 if (m->is_root) /* Never GC root directories */
2593 continue;
2594
2595 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f->path);
2596 remove_directory(j, m);
2597 }
2598
2599 log_debug("Reiteration complete.");
2600}
2601
31e99dd2 2602static void process_inotify_event(sd_journal *j, const struct inotify_event *e) {
a963990f 2603 Directory *d;
50f20cfd
LP
2604
2605 assert(j);
2606 assert(e);
2607
858749f7
LP
2608 if (e->mask & IN_Q_OVERFLOW) {
2609 process_q_overflow(j);
2610 return;
2611 }
2612
50f20cfd 2613 /* Is this a subdirectory we watch? */
a963990f
LP
2614 d = hashmap_get(j->directories_by_wd, INT_TO_PTR(e->wd));
2615 if (d) {
de2c3907
LP
2616 if (!(e->mask & IN_ISDIR) && e->len > 0 &&
2617 (endswith(e->name, ".journal") ||
2618 endswith(e->name, ".journal~"))) {
50f20cfd
LP
2619
2620 /* Event for a journal file */
2621
d617408e 2622 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
fc1813c0 2623 (void) add_file_by_name(j, d->path, e->name);
d617408e 2624 else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT))
fc1813c0 2625 remove_file_by_name(j, d->path, e->name);
50f20cfd 2626
a963990f 2627 } else if (!d->is_root && e->len == 0) {
50f20cfd 2628
a963990f 2629 /* Event for a subdirectory */
50f20cfd 2630
b2b46f91
TA
2631 if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT))
2632 remove_directory(j, d);
50f20cfd 2633
a9be0692 2634 } else if (d->is_root && (e->mask & IN_ISDIR) && e->len > 0 && id128_is_valid(e->name)) {
50f20cfd 2635
a963990f 2636 /* Event for root directory */
50f20cfd 2637
d617408e
LP
2638 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
2639 (void) add_directory(j, d->path, e->name);
50f20cfd
LP
2640 }
2641
2642 return;
2643 }
2644
2645 if (e->mask & IN_IGNORED)
2646 return;
2647
a9be0692 2648 log_debug("Unexpected inotify event.");
50f20cfd
LP
2649}
2650
a963990f
LP
2651static int determine_change(sd_journal *j) {
2652 bool b;
2653
2654 assert(j);
2655
2656 b = j->current_invalidate_counter != j->last_invalidate_counter;
2657 j->last_invalidate_counter = j->current_invalidate_counter;
2658
2659 return b ? SD_JOURNAL_INVALIDATE : SD_JOURNAL_APPEND;
2660}
2661
a5344d2c 2662_public_ int sd_journal_process(sd_journal *j) {
a963990f 2663 bool got_something = false;
50f20cfd 2664
1ae464e0
TA
2665 assert_return(j, -EINVAL);
2666 assert_return(!journal_pid_changed(j), -ECHILD);
50f20cfd 2667
10c4d640
LP
2668 if (j->inotify_fd < 0) /* We have no inotify fd yet? Then there's noting to process. */
2669 return 0;
2670
39c155ea 2671 j->last_process_usec = now(CLOCK_MONOTONIC);
f9346444 2672 j->last_invalidate_counter = j->current_invalidate_counter;
39c155ea 2673
50f20cfd 2674 for (;;) {
0254e944 2675 union inotify_event_buffer buffer;
50f20cfd
LP
2676 struct inotify_event *e;
2677 ssize_t l;
2678
0254e944 2679 l = read(j->inotify_fd, &buffer, sizeof(buffer));
50f20cfd 2680 if (l < 0) {
3742095b 2681 if (IN_SET(errno, EAGAIN, EINTR))
a963990f 2682 return got_something ? determine_change(j) : SD_JOURNAL_NOP;
50f20cfd
LP
2683
2684 return -errno;
2685 }
2686
a963990f
LP
2687 got_something = true;
2688
f7c1ad4f 2689 FOREACH_INOTIFY_EVENT(e, buffer, l)
50f20cfd 2690 process_inotify_event(j, e);
50f20cfd
LP
2691 }
2692}
6ad1d1c3 2693
e02d1cf7 2694_public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
a963990f 2695 int r;
39c155ea 2696 uint64_t t;
e02d1cf7 2697
1ae464e0
TA
2698 assert_return(j, -EINVAL);
2699 assert_return(!journal_pid_changed(j), -ECHILD);
e02d1cf7 2700
a963990f 2701 if (j->inotify_fd < 0) {
28ca867a
MS
2702 Iterator i;
2703 JournalFile *f;
a963990f
LP
2704
2705 /* This is the first invocation, hence create the
2706 * inotify watch */
2707 r = sd_journal_get_fd(j);
2708 if (r < 0)
2709 return r;
2710
28ca867a
MS
2711 /* Server might have done some vacuuming while we weren't watching.
2712 Get rid of the deleted files now so they don't stay around indefinitely. */
2713 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
2714 r = journal_file_fstat(f);
8581b9f9
MS
2715 if (r == -EIDRM)
2716 remove_file_real(j, f);
2717 else if (r < 0) {
28ca867a
MS
2718 log_debug_errno(r,"Failed to fstat() journal file '%s' : %m", f->path);
2719 continue;
2720 }
28ca867a
MS
2721 }
2722
a963990f
LP
2723 /* The journal might have changed since the context
2724 * object was created and we weren't watching before,
2725 * hence don't wait for anything, and return
2726 * immediately. */
2727 return determine_change(j);
2728 }
2729
39c155ea
LP
2730 r = sd_journal_get_timeout(j, &t);
2731 if (r < 0)
2732 return r;
2733
2734 if (t != (uint64_t) -1) {
2735 usec_t n;
2736
2737 n = now(CLOCK_MONOTONIC);
2738 t = t > n ? t - n : 0;
85210bff 2739
39c155ea
LP
2740 if (timeout_usec == (uint64_t) -1 || timeout_usec > t)
2741 timeout_usec = t;
85210bff
LP
2742 }
2743
a963990f
LP
2744 do {
2745 r = fd_wait_for_event(j->inotify_fd, POLLIN, timeout_usec);
2746 } while (r == -EINTR);
e02d1cf7
LP
2747
2748 if (r < 0)
2749 return r;
2750
a963990f 2751 return sd_journal_process(j);
e02d1cf7
LP
2752}
2753
08984293
LP
2754_public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to) {
2755 Iterator i;
2756 JournalFile *f;
2757 bool first = true;
581483bf 2758 uint64_t fmin = 0, tmax = 0;
08984293
LP
2759 int r;
2760
1ae464e0
TA
2761 assert_return(j, -EINVAL);
2762 assert_return(!journal_pid_changed(j), -ECHILD);
2763 assert_return(from || to, -EINVAL);
2764 assert_return(from != to, -EINVAL);
08984293 2765
c1f906bd 2766 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
08984293
LP
2767 usec_t fr, t;
2768
2769 r = journal_file_get_cutoff_realtime_usec(f, &fr, &t);
9f8d2983
LP
2770 if (r == -ENOENT)
2771 continue;
08984293
LP
2772 if (r < 0)
2773 return r;
2774 if (r == 0)
2775 continue;
2776
2777 if (first) {
581483bf
LP
2778 fmin = fr;
2779 tmax = t;
08984293
LP
2780 first = false;
2781 } else {
581483bf
LP
2782 fmin = MIN(fr, fmin);
2783 tmax = MAX(t, tmax);
08984293
LP
2784 }
2785 }
2786
581483bf
LP
2787 if (from)
2788 *from = fmin;
2789 if (to)
2790 *to = tmax;
2791
08984293
LP
2792 return first ? 0 : 1;
2793}
2794
2795_public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t *from, uint64_t *to) {
2796 Iterator i;
2797 JournalFile *f;
1651e2c6 2798 bool found = false;
08984293
LP
2799 int r;
2800
1ae464e0
TA
2801 assert_return(j, -EINVAL);
2802 assert_return(!journal_pid_changed(j), -ECHILD);
2803 assert_return(from || to, -EINVAL);
2804 assert_return(from != to, -EINVAL);
08984293 2805
c1f906bd 2806 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
08984293
LP
2807 usec_t fr, t;
2808
2809 r = journal_file_get_cutoff_monotonic_usec(f, boot_id, &fr, &t);
9f8d2983
LP
2810 if (r == -ENOENT)
2811 continue;
08984293
LP
2812 if (r < 0)
2813 return r;
2814 if (r == 0)
2815 continue;
2816
1651e2c6 2817 if (found) {
08984293 2818 if (from)
1651e2c6 2819 *from = MIN(fr, *from);
08984293 2820 if (to)
1651e2c6 2821 *to = MAX(t, *to);
08984293
LP
2822 } else {
2823 if (from)
1651e2c6 2824 *from = fr;
08984293 2825 if (to)
1651e2c6
ZJS
2826 *to = t;
2827 found = true;
08984293
LP
2828 }
2829 }
2830
1651e2c6 2831 return found;
08984293
LP
2832}
2833
dca6219e
LP
2834void journal_print_header(sd_journal *j) {
2835 Iterator i;
2836 JournalFile *f;
2837 bool newline = false;
2838
2839 assert(j);
2840
c1f906bd 2841 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
dca6219e
LP
2842 if (newline)
2843 putchar('\n');
2844 else
2845 newline = true;
2846
2847 journal_file_print_header(f);
2848 }
2849}
08984293 2850
a1a03e30
LP
2851_public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) {
2852 Iterator i;
2853 JournalFile *f;
2854 uint64_t sum = 0;
2855
1ae464e0
TA
2856 assert_return(j, -EINVAL);
2857 assert_return(!journal_pid_changed(j), -ECHILD);
2858 assert_return(bytes, -EINVAL);
a1a03e30 2859
c1f906bd 2860 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
a1a03e30
LP
2861 struct stat st;
2862
2863 if (fstat(f->fd, &st) < 0)
2864 return -errno;
2865
2866 sum += (uint64_t) st.st_blocks * 512ULL;
2867 }
2868
2869 *bytes = sum;
2870 return 0;
2871}
2872
3c1668da
LP
2873_public_ int sd_journal_query_unique(sd_journal *j, const char *field) {
2874 char *f;
2875
1ae464e0
TA
2876 assert_return(j, -EINVAL);
2877 assert_return(!journal_pid_changed(j), -ECHILD);
2878 assert_return(!isempty(field), -EINVAL);
2879 assert_return(field_is_valid(field), -EINVAL);
3c1668da
LP
2880
2881 f = strdup(field);
2882 if (!f)
2883 return -ENOMEM;
2884
2885 free(j->unique_field);
2886 j->unique_field = f;
2887 j->unique_file = NULL;
2888 j->unique_offset = 0;
360af4cf 2889 j->unique_file_lost = false;
3c1668da
LP
2890
2891 return 0;
2892}
2893
2894_public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l) {
3c1668da 2895 size_t k;
19a2bd80 2896
1ae464e0
TA
2897 assert_return(j, -EINVAL);
2898 assert_return(!journal_pid_changed(j), -ECHILD);
2899 assert_return(data, -EINVAL);
2900 assert_return(l, -EINVAL);
2901 assert_return(j->unique_field, -EINVAL);
19a2bd80 2902
3c1668da 2903 k = strlen(j->unique_field);
19a2bd80 2904
3c1668da 2905 if (!j->unique_file) {
360af4cf
ZJS
2906 if (j->unique_file_lost)
2907 return 0;
2908
c1f906bd 2909 j->unique_file = ordered_hashmap_first(j->files);
3c1668da
LP
2910 if (!j->unique_file)
2911 return 0;
360af4cf 2912
3c1668da
LP
2913 j->unique_offset = 0;
2914 }
19a2bd80 2915
3c1668da
LP
2916 for (;;) {
2917 JournalFile *of;
2918 Iterator i;
ae97089d 2919 Object *o;
3c1668da
LP
2920 const void *odata;
2921 size_t ol;
2922 bool found;
ae97089d 2923 int r;
3c1668da 2924
bdc02927 2925 /* Proceed to next data object in the field's linked list */
3c1668da
LP
2926 if (j->unique_offset == 0) {
2927 r = journal_file_find_field_object(j->unique_file, j->unique_field, k, &o, NULL);
2928 if (r < 0)
2929 return r;
2930
2931 j->unique_offset = r > 0 ? le64toh(o->field.head_data_offset) : 0;
2932 } else {
2933 r = journal_file_move_to_object(j->unique_file, OBJECT_DATA, j->unique_offset, &o);
2934 if (r < 0)
2935 return r;
2936
2937 j->unique_offset = le64toh(o->data.next_field_offset);
2938 }
2939
2940 /* We reached the end of the list? Then start again, with the next file */
2941 if (j->unique_offset == 0) {
c1f906bd 2942 j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
360af4cf 2943 if (!j->unique_file)
3c1668da
LP
2944 return 0;
2945
3c1668da
LP
2946 continue;
2947 }
2948
d05089d8
MS
2949 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2950 * instead, so that we can look at this data object at the same
3c1668da 2951 * time as one on another file */
d05089d8 2952 r = journal_file_move_to_object(j->unique_file, OBJECT_UNUSED, j->unique_offset, &o);
3c1668da
LP
2953 if (r < 0)
2954 return r;
2955
2956 /* Let's do the type check by hand, since we used 0 context above. */
baaa35ad
ZJS
2957 if (o->object.type != OBJECT_DATA)
2958 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
2959 "%s:offset " OFSfmt ": object has type %d, expected %d",
2960 j->unique_file->path,
2961 j->unique_offset,
2962 o->object.type, OBJECT_DATA);
ae97089d 2963
93b73b06 2964 r = return_data(j, j->unique_file, o, &odata, &ol);
3c1668da
LP
2965 if (r < 0)
2966 return r;
2967
0f99f74a 2968 /* Check if we have at least the field name and "=". */
baaa35ad
ZJS
2969 if (ol <= k)
2970 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
2971 "%s:offset " OFSfmt ": object has size %zu, expected at least %zu",
2972 j->unique_file->path,
2973 j->unique_offset, ol, k + 1);
2974
2975 if (memcmp(odata, j->unique_field, k) || ((const char*) odata)[k] != '=')
2976 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
2977 "%s:offset " OFSfmt ": object does not start with \"%s=\"",
2978 j->unique_file->path,
2979 j->unique_offset,
2980 j->unique_field);
0f99f74a 2981
3c1668da
LP
2982 /* OK, now let's see if we already returned this data
2983 * object by checking if it exists in the earlier
2984 * traversed files. */
2985 found = false;
c1f906bd 2986 ORDERED_HASHMAP_FOREACH(of, j->files, i) {
3c1668da
LP
2987 if (of == j->unique_file)
2988 break;
2989
ed71f956
LP
2990 /* Skip this file it didn't have any fields indexed */
2991 if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
3c1668da
LP
2992 continue;
2993
ed71f956 2994 r = journal_file_find_data_object_with_hash(of, odata, ol, le64toh(o->data.hash), NULL, NULL);
3c1668da
LP
2995 if (r < 0)
2996 return r;
ed71f956 2997 if (r > 0) {
3c1668da 2998 found = true;
ed71f956
LP
2999 break;
3000 }
3c1668da
LP
3001 }
3002
06cc69d4
JJ
3003 if (found)
3004 continue;
3005
93b73b06 3006 r = return_data(j, j->unique_file, o, data, l);
3c1668da
LP
3007 if (r < 0)
3008 return r;
3009
3010 return 1;
3011 }
3012}
3013
76cbafcd
ZJS
3014_public_ int sd_journal_enumerate_available_unique(sd_journal *j, const void **data, size_t *size) {
3015 for (;;) {
3016 int r;
3017
3018 r = sd_journal_enumerate_unique(j, data, size);
3019 if (r >= 0)
3020 return r;
3021 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r))
3022 return r;
3023 /* Try with the next field. sd_journal_enumerate_unique() modifies state, so on the next try
3024 * we will access the next field. */
3025 }
3026}
3027
115646c7 3028_public_ void sd_journal_restart_unique(sd_journal *j) {
3c1668da
LP
3029 if (!j)
3030 return;
3031
3032 j->unique_file = NULL;
3033 j->unique_offset = 0;
360af4cf 3034 j->unique_file_lost = false;
3c1668da 3035}
85210bff 3036
eb86030e
LP
3037_public_ int sd_journal_enumerate_fields(sd_journal *j, const char **field) {
3038 int r;
3039
3040 assert_return(j, -EINVAL);
3041 assert_return(!journal_pid_changed(j), -ECHILD);
3042 assert_return(field, -EINVAL);
3043
3044 if (!j->fields_file) {
3045 if (j->fields_file_lost)
3046 return 0;
3047
3048 j->fields_file = ordered_hashmap_first(j->files);
3049 if (!j->fields_file)
3050 return 0;
3051
3052 j->fields_hash_table_index = 0;
3053 j->fields_offset = 0;
3054 }
3055
3056 for (;;) {
3057 JournalFile *f, *of;
3058 Iterator i;
3059 uint64_t m;
3060 Object *o;
3061 size_t sz;
3062 bool found;
3063
3064 f = j->fields_file;
3065
3066 if (j->fields_offset == 0) {
3067 bool eof = false;
3068
3069 /* We are not yet positioned at any field. Let's pick the first one */
3070 r = journal_file_map_field_hash_table(f);
3071 if (r < 0)
3072 return r;
3073
3074 m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem);
3075 for (;;) {
3076 if (j->fields_hash_table_index >= m) {
3077 /* Reached the end of the hash table, go to the next file. */
3078 eof = true;
3079 break;
3080 }
3081
3082 j->fields_offset = le64toh(f->field_hash_table[j->fields_hash_table_index].head_hash_offset);
3083
3084 if (j->fields_offset != 0)
3085 break;
3086
3087 /* Empty hash table bucket, go to next one */
3088 j->fields_hash_table_index++;
3089 }
3090
3091 if (eof) {
3092 /* Proceed with next file */
3093 j->fields_file = ordered_hashmap_next(j->files, f->path);
3094 if (!j->fields_file) {
3095 *field = NULL;
3096 return 0;
3097 }
3098
3099 j->fields_offset = 0;
3100 j->fields_hash_table_index = 0;
3101 continue;
3102 }
3103
3104 } else {
3105 /* We are already positioned at a field. If so, let's figure out the next field from it */
3106
3107 r = journal_file_move_to_object(f, OBJECT_FIELD, j->fields_offset, &o);
3108 if (r < 0)
3109 return r;
3110
3111 j->fields_offset = le64toh(o->field.next_hash_offset);
3112 if (j->fields_offset == 0) {
3113 /* Reached the end of the hash table chain */
3114 j->fields_hash_table_index++;
3115 continue;
3116 }
3117 }
3118
1f133e0d 3119 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
eb86030e
LP
3120 r = journal_file_move_to_object(f, OBJECT_UNUSED, j->fields_offset, &o);
3121 if (r < 0)
3122 return r;
3123
3124 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
baaa35ad
ZJS
3125 if (o->object.type != OBJECT_FIELD)
3126 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
3127 "%s:offset " OFSfmt ": object has type %i, expected %i",
3128 f->path, j->fields_offset,
3129 o->object.type, OBJECT_FIELD);
eb86030e
LP
3130
3131 sz = le64toh(o->object.size) - offsetof(Object, field.payload);
3132
3133 /* Let's see if we already returned this field name before. */
3134 found = false;
3135 ORDERED_HASHMAP_FOREACH(of, j->files, i) {
3136 if (of == f)
3137 break;
3138
3139 /* Skip this file it didn't have any fields indexed */
3140 if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
3141 continue;
3142
3143 r = journal_file_find_field_object_with_hash(of, o->field.payload, sz, le64toh(o->field.hash), NULL, NULL);
3144 if (r < 0)
3145 return r;
3146 if (r > 0) {
3147 found = true;
3148 break;
3149 }
3150 }
3151
3152 if (found)
3153 continue;
3154
3155 /* Check if this is really a valid string containing no NUL byte */
3156 if (memchr(o->field.payload, 0, sz))
3157 return -EBADMSG;
3158
3159 if (sz > j->data_threshold)
3160 sz = j->data_threshold;
3161
3162 if (!GREEDY_REALLOC(j->fields_buffer, j->fields_buffer_allocated, sz + 1))
3163 return -ENOMEM;
3164
3165 memcpy(j->fields_buffer, o->field.payload, sz);
3166 j->fields_buffer[sz] = 0;
3167
3168 if (!field_is_valid(j->fields_buffer))
3169 return -EBADMSG;
3170
3171 *field = j->fields_buffer;
3172 return 1;
3173 }
3174}
3175
3176_public_ void sd_journal_restart_fields(sd_journal *j) {
3177 if (!j)
3178 return;
3179
3180 j->fields_file = NULL;
3181 j->fields_hash_table_index = 0;
3182 j->fields_offset = 0;
3183 j->fields_file_lost = false;
3184}
3185
85210bff 3186_public_ int sd_journal_reliable_fd(sd_journal *j) {
1ae464e0
TA
3187 assert_return(j, -EINVAL);
3188 assert_return(!journal_pid_changed(j), -ECHILD);
85210bff
LP
3189
3190 return !j->on_network;
3191}
d4205751
LP
3192
3193static char *lookup_field(const char *field, void *userdata) {
3194 sd_journal *j = userdata;
3195 const void *data;
3196 size_t size, d;
3197 int r;
3198
3199 assert(field);
3200 assert(j);
3201
3202 r = sd_journal_get_data(j, field, &data, &size);
3203 if (r < 0 ||
3204 size > REPLACE_VAR_MAX)
3205 return strdup(field);
3206
3207 d = strlen(field) + 1;
3208
3209 return strndup((const char*) data + d, size - d);
3210}
3211
3212_public_ int sd_journal_get_catalog(sd_journal *j, char **ret) {
3213 const void *data;
3214 size_t size;
3215 sd_id128_t id;
3216 _cleanup_free_ char *text = NULL, *cid = NULL;
3217 char *t;
3218 int r;
3219
1ae464e0
TA
3220 assert_return(j, -EINVAL);
3221 assert_return(!journal_pid_changed(j), -ECHILD);
3222 assert_return(ret, -EINVAL);
d4205751
LP
3223
3224 r = sd_journal_get_data(j, "MESSAGE_ID", &data, &size);
3225 if (r < 0)
3226 return r;
3227
3228 cid = strndup((const char*) data + 11, size - 11);
3229 if (!cid)
3230 return -ENOMEM;
3231
3232 r = sd_id128_from_string(cid, &id);
3233 if (r < 0)
3234 return r;
3235
844ec79b 3236 r = catalog_get(CATALOG_DATABASE, id, &text);
d4205751
LP
3237 if (r < 0)
3238 return r;
3239
3240 t = replace_var(text, lookup_field, j);
3241 if (!t)
3242 return -ENOMEM;
3243
3244 *ret = t;
3245 return 0;
3246}
8f1e860f
LP
3247
3248_public_ int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **ret) {
1ae464e0 3249 assert_return(ret, -EINVAL);
8f1e860f 3250
844ec79b 3251 return catalog_get(CATALOG_DATABASE, id, ret);
8f1e860f 3252}
93b73b06
LP
3253
3254_public_ int sd_journal_set_data_threshold(sd_journal *j, size_t sz) {
1ae464e0
TA
3255 assert_return(j, -EINVAL);
3256 assert_return(!journal_pid_changed(j), -ECHILD);
93b73b06
LP
3257
3258 j->data_threshold = sz;
3259 return 0;
3260}
3261
3262_public_ int sd_journal_get_data_threshold(sd_journal *j, size_t *sz) {
1ae464e0
TA
3263 assert_return(j, -EINVAL);
3264 assert_return(!journal_pid_changed(j), -ECHILD);
3265 assert_return(sz, -EINVAL);
93b73b06
LP
3266
3267 *sz = j->data_threshold;
3268 return 0;
3269}
39fd5b08
JS
3270
3271_public_ int sd_journal_has_runtime_files(sd_journal *j) {
3272 assert_return(j, -EINVAL);
3273
3274 return j->has_runtime_files;
3275}
3276
3277_public_ int sd_journal_has_persistent_files(sd_journal *j) {
3278 assert_return(j, -EINVAL);
3279
3280 return j->has_persistent_files;
3281}