]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/sd-journal.c
sd-journal: when enumerating, continue even after an inaccessible field
[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) {
a2a5291b 954 const char *word, *state;
de190aef 955 size_t l;
de190aef
LP
956 unsigned long long seqnum, monotonic, realtime, xor_hash;
957 bool
958 seqnum_id_set = false,
959 seqnum_set = false,
960 boot_id_set = false,
961 monotonic_set = false,
962 realtime_set = false,
963 xor_hash_set = false;
964 sd_id128_t seqnum_id, boot_id;
965
1ae464e0
TA
966 assert_return(j, -EINVAL);
967 assert_return(!journal_pid_changed(j), -ECHILD);
968 assert_return(!isempty(cursor), -EINVAL);
de190aef 969
a2a5291b 970 FOREACH_WORD_SEPARATOR(word, l, cursor, ";", state) {
de190aef
LP
971 char *item;
972 int k = 0;
973
a2a5291b 974 if (l < 2 || word[1] != '=')
de190aef
LP
975 return -EINVAL;
976
a2a5291b 977 item = strndup(word, l);
de190aef
LP
978 if (!item)
979 return -ENOMEM;
980
a2a5291b 981 switch (word[0]) {
de190aef
LP
982
983 case 's':
984 seqnum_id_set = true;
be3ea5ea 985 k = sd_id128_from_string(item+2, &seqnum_id);
de190aef
LP
986 break;
987
988 case 'i':
989 seqnum_set = true;
be3ea5ea 990 if (sscanf(item+2, "%llx", &seqnum) != 1)
de190aef
LP
991 k = -EINVAL;
992 break;
993
994 case 'b':
995 boot_id_set = true;
be3ea5ea 996 k = sd_id128_from_string(item+2, &boot_id);
de190aef
LP
997 break;
998
999 case 'm':
1000 monotonic_set = true;
be3ea5ea 1001 if (sscanf(item+2, "%llx", &monotonic) != 1)
de190aef
LP
1002 k = -EINVAL;
1003 break;
1004
1005 case 't':
1006 realtime_set = true;
be3ea5ea 1007 if (sscanf(item+2, "%llx", &realtime) != 1)
de190aef
LP
1008 k = -EINVAL;
1009 break;
1010
1011 case 'x':
1012 xor_hash_set = true;
be3ea5ea 1013 if (sscanf(item+2, "%llx", &xor_hash) != 1)
de190aef
LP
1014 k = -EINVAL;
1015 break;
1016 }
1017
1018 free(item);
1019
1020 if (k < 0)
1021 return k;
1022 }
1023
1024 if ((!seqnum_set || !seqnum_id_set) &&
1025 (!monotonic_set || !boot_id_set) &&
1026 !realtime_set)
1027 return -EINVAL;
1028
bba6e4ae
LP
1029 detach_location(j);
1030 j->current_location = (Location) {
1031 .type = LOCATION_SEEK,
1032 };
de190aef
LP
1033
1034 if (realtime_set) {
1035 j->current_location.realtime = (uint64_t) realtime;
1036 j->current_location.realtime_set = true;
1037 }
1038
1039 if (seqnum_set && seqnum_id_set) {
1040 j->current_location.seqnum = (uint64_t) seqnum;
1041 j->current_location.seqnum_id = seqnum_id;
1042 j->current_location.seqnum_set = true;
1043 }
1044
1045 if (monotonic_set && boot_id_set) {
1046 j->current_location.monotonic = (uint64_t) monotonic;
1047 j->current_location.boot_id = boot_id;
1048 j->current_location.monotonic_set = true;
1049 }
1050
1051 if (xor_hash_set) {
1052 j->current_location.xor_hash = (uint64_t) xor_hash;
1053 j->current_location.xor_hash_set = true;
1054 }
1055
1056 return 0;
1057}
1058
c6511e85
LP
1059_public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) {
1060 int r;
c6511e85
LP
1061 Object *o;
1062
1ae464e0
TA
1063 assert_return(j, -EINVAL);
1064 assert_return(!journal_pid_changed(j), -ECHILD);
1065 assert_return(!isempty(cursor), -EINVAL);
c6511e85
LP
1066
1067 if (!j->current_file || j->current_file->current_offset <= 0)
1068 return -EADDRNOTAVAIL;
1069
1070 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
1071 if (r < 0)
1072 return r;
1073
9ed794a3 1074 for (;;) {
c6511e85 1075 _cleanup_free_ char *item = NULL;
c6511e85 1076 unsigned long long ll;
7294bb5b 1077 sd_id128_t id;
c6511e85
LP
1078 int k = 0;
1079
7294bb5b
SS
1080 r = extract_first_word(&cursor, &item, ";", EXTRACT_DONT_COALESCE_SEPARATORS);
1081 if (r < 0)
1082 return r;
c6511e85 1083
7294bb5b
SS
1084 if (r == 0)
1085 break;
c6511e85 1086
7294bb5b
SS
1087 if (strlen(item) < 2 || item[1] != '=')
1088 return -EINVAL;
1089
1090 switch (item[0]) {
c6511e85
LP
1091
1092 case 's':
1093 k = sd_id128_from_string(item+2, &id);
1094 if (k < 0)
1095 return k;
1096 if (!sd_id128_equal(id, j->current_file->header->seqnum_id))
1097 return 0;
1098 break;
1099
1100 case 'i':
1101 if (sscanf(item+2, "%llx", &ll) != 1)
1102 return -EINVAL;
1103 if (ll != le64toh(o->entry.seqnum))
1104 return 0;
1105 break;
1106
1107 case 'b':
1108 k = sd_id128_from_string(item+2, &id);
1109 if (k < 0)
1110 return k;
1111 if (!sd_id128_equal(id, o->entry.boot_id))
1112 return 0;
1113 break;
1114
1115 case 'm':
1116 if (sscanf(item+2, "%llx", &ll) != 1)
1117 return -EINVAL;
1118 if (ll != le64toh(o->entry.monotonic))
1119 return 0;
1120 break;
1121
1122 case 't':
1123 if (sscanf(item+2, "%llx", &ll) != 1)
1124 return -EINVAL;
1125 if (ll != le64toh(o->entry.realtime))
1126 return 0;
1127 break;
1128
1129 case 'x':
1130 if (sscanf(item+2, "%llx", &ll) != 1)
1131 return -EINVAL;
1132 if (ll != le64toh(o->entry.xor_hash))
1133 return 0;
1134 break;
1135 }
1136 }
1137
1138 return 1;
1139}
1140
a5344d2c 1141_public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
1ae464e0
TA
1142 assert_return(j, -EINVAL);
1143 assert_return(!journal_pid_changed(j), -ECHILD);
de190aef 1144
bba6e4ae
LP
1145 detach_location(j);
1146
1147 j->current_location = (Location) {
1148 .type = LOCATION_SEEK,
1149 .boot_id = boot_id,
1150 .monotonic = usec,
1151 .monotonic_set = true,
1152 };
de190aef
LP
1153
1154 return 0;
1155}
1156
a5344d2c 1157_public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
1ae464e0
TA
1158 assert_return(j, -EINVAL);
1159 assert_return(!journal_pid_changed(j), -ECHILD);
de190aef 1160
bba6e4ae
LP
1161 detach_location(j);
1162
1163 j->current_location = (Location) {
1164 .type = LOCATION_SEEK,
1165 .realtime = usec,
1166 .realtime_set = true,
1167 };
de190aef
LP
1168
1169 return 0;
1170}
1171
a5344d2c 1172_public_ int sd_journal_seek_head(sd_journal *j) {
1ae464e0
TA
1173 assert_return(j, -EINVAL);
1174 assert_return(!journal_pid_changed(j), -ECHILD);
de190aef 1175
bba6e4ae
LP
1176 detach_location(j);
1177
1178 j->current_location = (Location) {
1179 .type = LOCATION_HEAD,
1180 };
de190aef
LP
1181
1182 return 0;
1183}
1184
a5344d2c 1185_public_ int sd_journal_seek_tail(sd_journal *j) {
1ae464e0
TA
1186 assert_return(j, -EINVAL);
1187 assert_return(!journal_pid_changed(j), -ECHILD);
de190aef 1188
bba6e4ae
LP
1189 detach_location(j);
1190
1191 j->current_location = (Location) {
1192 .type = LOCATION_TAIL,
1193 };
de190aef
LP
1194
1195 return 0;
87d2c1ff
LP
1196}
1197
85210bff 1198static void check_network(sd_journal *j, int fd) {
85210bff
LP
1199 assert(j);
1200
1201 if (j->on_network)
1202 return;
1203
77f9fa3b 1204 j->on_network = fd_is_network_fs(fd);
85210bff
LP
1205}
1206
a688baa8
ZJS
1207static bool file_has_type_prefix(const char *prefix, const char *filename) {
1208 const char *full, *tilded, *atted;
1209
63c372cb
LP
1210 full = strjoina(prefix, ".journal");
1211 tilded = strjoina(full, "~");
1212 atted = strjoina(prefix, "@");
a688baa8 1213
0cbd293e 1214 return STR_IN_SET(filename, full, tilded) ||
a688baa8
ZJS
1215 startswith(filename, atted);
1216}
1217
1218static bool file_type_wanted(int flags, const char *filename) {
d617408e
LP
1219 assert(filename);
1220
a688baa8
ZJS
1221 if (!endswith(filename, ".journal") && !endswith(filename, ".journal~"))
1222 return false;
1223
1224 /* no flags set → every type is OK */
1225 if (!(flags & (SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER)))
1226 return true;
1227
1228 if (flags & SD_JOURNAL_SYSTEM && file_has_type_prefix("system", filename))
1229 return true;
1230
1231 if (flags & SD_JOURNAL_CURRENT_USER) {
1232 char prefix[5 + DECIMAL_STR_MAX(uid_t) + 1];
1233
5ffa8c81 1234 xsprintf(prefix, "user-"UID_FMT, getuid());
a688baa8
ZJS
1235
1236 if (file_has_type_prefix(prefix, filename))
1237 return true;
1238 }
1239
1240 return false;
1241}
1242
5d1ce257
LP
1243static bool path_has_prefix(sd_journal *j, const char *path, const char *prefix) {
1244 assert(j);
1245 assert(path);
1246 assert(prefix);
1247
1248 if (j->toplevel_fd >= 0)
1249 return false;
1250
1251 return path_startswith(path, prefix);
1252}
1253
9c66f528
LP
1254static void track_file_disposition(sd_journal *j, JournalFile *f) {
1255 assert(j);
1256 assert(f);
1257
1258 if (!j->has_runtime_files && path_has_prefix(j, f->path, "/run"))
1259 j->has_runtime_files = true;
1260 else if (!j->has_persistent_files && path_has_prefix(j, f->path, "/var"))
1261 j->has_persistent_files = true;
1262}
1263
5d1ce257
LP
1264static const char *skip_slash(const char *p) {
1265
1266 if (!p)
1267 return NULL;
1268
1269 while (*p == '/')
1270 p++;
1271
1272 return p;
1273}
1274
9c66f528
LP
1275static int add_any_file(
1276 sd_journal *j,
1277 int fd,
1278 const char *path) {
1279
5d1ce257 1280 bool close_fd = false;
9c66f528
LP
1281 JournalFile *f;
1282 struct stat st;
d617408e 1283 int r, k;
3fbf9cbb
LP
1284
1285 assert(j);
5d1ce257 1286 assert(fd >= 0 || path);
3fbf9cbb 1287
9c66f528
LP
1288 if (fd < 0) {
1289 if (j->toplevel_fd >= 0)
1290 /* If there's a top-level fd defined make the path relative, explicitly, since otherwise
1291 * openat() ignores the first argument. */
1292
1293 fd = openat(j->toplevel_fd, skip_slash(path), O_RDONLY|O_CLOEXEC|O_NONBLOCK);
1294 else
1295 fd = open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
1296 if (fd < 0) {
1297 r = log_debug_errno(errno, "Failed to open journal file %s: %m", path);
1298 goto finish;
1299 }
1300
1301 close_fd = true;
1302
1303 r = fd_nonblock(fd, false);
1304 if (r < 0) {
1305 r = log_debug_errno(errno, "Failed to turn off O_NONBLOCK for %s: %m", path);
1306 goto finish;
858749f7
LP
1307 }
1308 }
50f20cfd 1309
9c66f528
LP
1310 if (fstat(fd, &st) < 0) {
1311 r = log_debug_errno(errno, "Failed to fstat file '%s': %m", path);
1312 goto finish;
1313 }
3cc44114
LP
1314
1315 r = stat_verify_regular(&st);
1316 if (r < 0) {
1317 log_debug_errno(r, "Refusing to open '%s', as it is not a regular file.", path);
9c66f528 1318 goto finish;
9e8abdf0
LP
1319 }
1320
9c66f528
LP
1321 f = ordered_hashmap_get(j->files, path);
1322 if (f) {
1323 if (f->last_stat.st_dev == st.st_dev &&
1324 f->last_stat.st_ino == st.st_ino) {
5d1ce257 1325
9c66f528
LP
1326 /* We already track this file, under the same path and with the same device/inode numbers, it's
1327 * hence really the same. Mark this file as seen in this generation. This is used to GC old
1328 * files in process_q_overflow() to detect journal files that are still there and discern them
1329 * from those which are gone. */
5d1ce257 1330
9c66f528
LP
1331 f->last_seen_generation = j->generation;
1332 r = 0;
1333 goto finish;
5d1ce257
LP
1334 }
1335
9c66f528
LP
1336 /* So we tracked a file under this name, but it has a different inode/device. In that case, it got
1337 * replaced (probably due to rotation?), let's drop it hence from our list. */
1338 remove_file_real(j, f);
1339 f = NULL;
1340 }
1341
1342 if (ordered_hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
1343 log_debug("Too many open journal files, not adding %s.", path);
1344 r = -ETOOMANYREFS;
1345 goto finish;
5d1ce257
LP
1346 }
1347
57850536 1348 r = journal_file_open(fd, path, O_RDONLY, 0, false, 0, false, NULL, j->mmap, NULL, NULL, &f);
d617408e
LP
1349 if (r < 0) {
1350 log_debug_errno(r, "Failed to open journal file %s: %m", path);
9c66f528 1351 goto finish;
d617408e 1352 }
3fbf9cbb 1353
72f59706 1354 /* journal_file_dump(f); */
de190aef 1355
c1f906bd 1356 r = ordered_hashmap_put(j->files, f->path, f);
3fbf9cbb 1357 if (r < 0) {
9c66f528 1358 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 1359 (void) journal_file_close(f);
9c66f528 1360 goto finish;
3fbf9cbb
LP
1361 }
1362
9c66f528 1363 close_fd = false; /* the fd is now owned by the JournalFile object */
5d1ce257 1364
9c66f528 1365 f->last_seen_generation = j->generation;
a50d7d43 1366
9c66f528 1367 track_file_disposition(j, f);
85210bff
LP
1368 check_network(j, f->fd);
1369
313cefa1 1370 j->current_invalidate_counter++;
a963990f 1371
9c66f528 1372 log_debug("File %s added.", f->path);
d617408e 1373
9c66f528
LP
1374 r = 0;
1375
1376finish:
1377 if (close_fd)
1378 safe_close(fd);
1379
1380 if (r < 0) {
1381 k = journal_put_error(j, r, path);
1382 if (k < 0)
1383 return k;
1384 }
d617408e
LP
1385
1386 return r;
50f20cfd
LP
1387}
1388
fc1813c0
LP
1389static int add_file_by_name(
1390 sd_journal *j,
1391 const char *prefix,
1392 const char *filename) {
1393
d617408e 1394 const char *path;
5302ebe1
ZJS
1395
1396 assert(j);
1397 assert(prefix);
1398 assert(filename);
1399
5d1ce257 1400 if (j->no_new_files)
5302ebe1
ZJS
1401 return 0;
1402
5d1ce257
LP
1403 if (!file_type_wanted(j->flags, filename))
1404 return 0;
39fd5b08 1405
270384b2 1406 path = prefix_roota(prefix, filename);
5d1ce257 1407 return add_any_file(j, -1, path);
5302ebe1
ZJS
1408}
1409
fc1813c0
LP
1410static void remove_file_by_name(
1411 sd_journal *j,
1412 const char *prefix,
1413 const char *filename) {
1414
d617408e 1415 const char *path;
50f20cfd
LP
1416 JournalFile *f;
1417
1418 assert(j);
1419 assert(prefix);
1420 assert(filename);
1421
270384b2 1422 path = prefix_roota(prefix, filename);
c1f906bd 1423 f = ordered_hashmap_get(j->files, path);
50f20cfd 1424 if (!f)
d617408e 1425 return;
50f20cfd 1426
a9a245c1 1427 remove_file_real(j, f);
a9a245c1
ZJS
1428}
1429
1430static void remove_file_real(sd_journal *j, JournalFile *f) {
1431 assert(j);
1432 assert(f);
1433
fc1813c0 1434 (void) ordered_hashmap_remove(j->files, f->path);
44a5fa34 1435
5ec76417 1436 log_debug("File %s removed.", f->path);
44a5fa34 1437
3c1668da
LP
1438 if (j->current_file == f) {
1439 j->current_file = NULL;
1440 j->current_field = 0;
1441 }
1442
1443 if (j->unique_file == f) {
360af4cf 1444 /* Jump to the next unique_file or NULL if that one was last */
c1f906bd 1445 j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
3c1668da 1446 j->unique_offset = 0;
360af4cf
ZJS
1447 if (!j->unique_file)
1448 j->unique_file_lost = true;
3c1668da
LP
1449 }
1450
eb86030e
LP
1451 if (j->fields_file == f) {
1452 j->fields_file = ordered_hashmap_next(j->files, j->fields_file->path);
1453 j->fields_offset = 0;
1454 if (!j->fields_file)
1455 j->fields_file_lost = true;
1456 }
1457
69a3a6fd 1458 (void) journal_file_close(f);
50f20cfd 1459
313cefa1 1460 j->current_invalidate_counter++;
3fbf9cbb
LP
1461}
1462
d617408e
LP
1463static int dirname_is_machine_id(const char *fn) {
1464 sd_id128_t id, machine;
456aa879 1465 const char *e;
d617408e
LP
1466 int r;
1467
456aa879
LP
1468 /* Returns true if the specified directory name matches the local machine ID */
1469
d617408e
LP
1470 r = sd_id128_get_machine(&machine);
1471 if (r < 0)
1472 return r;
1473
456aa879
LP
1474 e = strchr(fn, '.');
1475 if (e) {
1476 const char *k;
1477
1478 /* Looks like it has a namespace suffix. Verify that. */
1479 if (!log_namespace_name_valid(e + 1))
1480 return false;
1481
1482 k = strndupa(fn, e - fn);
1483 r = sd_id128_from_string(k, &id);
1484 } else
1485 r = sd_id128_from_string(fn, &id);
d617408e
LP
1486 if (r < 0)
1487 return r;
1488
1489 return sd_id128_equal(id, machine);
1490}
1491
456aa879
LP
1492static int dirname_has_namespace(const char *fn, const char *namespace) {
1493 const char *e;
1494
1495 /* Returns true if the specified directory name matches the specified namespace */
1496
1497 e = strchr(fn, '.');
1498 if (e) {
1499 const char *k;
1500
1501 if (!namespace)
1502 return false;
1503
1504 if (!streq(e + 1, namespace))
1505 return false;
1506
1507 k = strndupa(fn, e - fn);
1508 return id128_is_valid(k);
1509 }
1510
1511 if (namespace)
1512 return false;
1513
1514 return id128_is_valid(fn);
1515}
1516
858749f7
LP
1517static bool dirent_is_journal_file(const struct dirent *de) {
1518 assert(de);
1519
456aa879
LP
1520 /* Returns true if the specified directory entry looks like a journal file we might be interested in */
1521
858749f7
LP
1522 if (!IN_SET(de->d_type, DT_REG, DT_LNK, DT_UNKNOWN))
1523 return false;
1524
1525 return endswith(de->d_name, ".journal") ||
1526 endswith(de->d_name, ".journal~");
1527}
1528
456aa879
LP
1529static bool dirent_is_journal_subdir(const struct dirent *de) {
1530 const char *e, *n;
858749f7
LP
1531 assert(de);
1532
456aa879
LP
1533 /* returns true if the specified directory entry looks like a directory that might contain journal
1534 * files we might be interested in, i.e. is either a 128bit ID or a 128bit ID suffixed by a
1535 * namespace. */
1536
858749f7
LP
1537 if (!IN_SET(de->d_type, DT_DIR, DT_LNK, DT_UNKNOWN))
1538 return false;
1539
456aa879
LP
1540 e = strchr(de->d_name, '.');
1541 if (!e)
1542 return id128_is_valid(de->d_name); /* No namespace */
1543
1544 n = strndupa(de->d_name, e - de->d_name);
1545 if (!id128_is_valid(n))
1546 return false;
1547
1548 return log_namespace_name_valid(e + 1);
858749f7
LP
1549}
1550
1551static int directory_open(sd_journal *j, const char *path, DIR **ret) {
1552 DIR *d;
1553
1554 assert(j);
1555 assert(path);
1556 assert(ret);
1557
1558 if (j->toplevel_fd < 0)
1559 d = opendir(path);
1560 else
1561 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1562 * relative, by dropping the initial slash */
1563 d = xopendirat(j->toplevel_fd, skip_slash(path), 0);
1564 if (!d)
1565 return -errno;
1566
1567 *ret = d;
1568 return 0;
1569}
1570
1571static int add_directory(sd_journal *j, const char *prefix, const char *dirname);
1572
1573static void directory_enumerate(sd_journal *j, Directory *m, DIR *d) {
1574 struct dirent *de;
1575
1576 assert(j);
1577 assert(m);
1578 assert(d);
1579
1580 FOREACH_DIRENT_ALL(de, d, goto fail) {
fc1813c0 1581
858749f7 1582 if (dirent_is_journal_file(de))
fc1813c0 1583 (void) add_file_by_name(j, m->path, de->d_name);
858749f7 1584
456aa879 1585 if (m->is_root && dirent_is_journal_subdir(de))
858749f7
LP
1586 (void) add_directory(j, m->path, de->d_name);
1587 }
1588
1589 return;
1590
1591fail:
1592 log_debug_errno(errno, "Failed to enumerate directory %s, ignoring: %m", m->path);
1593}
1594
1595static void directory_watch(sd_journal *j, Directory *m, int fd, uint32_t mask) {
1596 int r;
1597
1598 assert(j);
1599 assert(m);
1600 assert(fd >= 0);
1601
1602 /* Watch this directory if that's enabled and if it not being watched yet. */
1603
1604 if (m->wd > 0) /* Already have a watch? */
1605 return;
1606 if (j->inotify_fd < 0) /* Not watching at all? */
1607 return;
1608
1609 m->wd = inotify_add_watch_fd(j->inotify_fd, fd, mask);
1610 if (m->wd < 0) {
1611 log_debug_errno(errno, "Failed to watch journal directory '%s', ignoring: %m", m->path);
1612 return;
1613 }
1614
1615 r = hashmap_put(j->directories_by_wd, INT_TO_PTR(m->wd), m);
1616 if (r == -EEXIST)
1617 log_debug_errno(r, "Directory '%s' already being watched under a different path, ignoring: %m", m->path);
1618 if (r < 0) {
1619 log_debug_errno(r, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m->path);
1620 (void) inotify_rm_watch(j->inotify_fd, m->wd);
1621 m->wd = -1;
1622 }
1623}
1624
456aa879
LP
1625static int add_directory(
1626 sd_journal *j,
1627 const char *prefix,
1628 const char *dirname) {
1629
7fd1b19b 1630 _cleanup_free_ char *path = NULL;
7fd1b19b 1631 _cleanup_closedir_ DIR *d = NULL;
a963990f 1632 Directory *m;
d617408e 1633 int r, k;
3fbf9cbb
LP
1634
1635 assert(j);
1636 assert(prefix);
3fbf9cbb 1637
5d1ce257
LP
1638 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1639 * and reenumerates directory contents */
d95b1fb3 1640
657ee2d8 1641 path = path_join(prefix, dirname);
d617408e
LP
1642 if (!path) {
1643 r = -ENOMEM;
1644 goto fail;
1645 }
3fbf9cbb 1646
858749f7 1647 log_debug("Considering directory '%s'.", path);
5d1ce257
LP
1648
1649 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1650 if ((j->flags & SD_JOURNAL_LOCAL_ONLY) &&
1651 !((dirname && dirname_is_machine_id(dirname) > 0) || path_has_prefix(j, path, "/run")))
858749f7 1652 return 0;
5d1ce257 1653
2b6df46d
LP
1654 if (dirname &&
1655 (!(FLAGS_SET(j->flags, SD_JOURNAL_ALL_NAMESPACES) ||
1656 dirname_has_namespace(dirname, j->namespace) > 0 ||
1657 (FLAGS_SET(j->flags, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE) && dirname_has_namespace(dirname, NULL) > 0))))
456aa879
LP
1658 return 0;
1659
858749f7
LP
1660 r = directory_open(j, path, &d);
1661 if (r < 0) {
1662 log_debug_errno(r, "Failed to open directory '%s': %m", path);
d617408e 1663 goto fail;
3fbf9cbb
LP
1664 }
1665
a963990f
LP
1666 m = hashmap_get(j->directories_by_path, path);
1667 if (!m) {
2f5435a1 1668 m = new(Directory, 1);
d617408e
LP
1669 if (!m) {
1670 r = -ENOMEM;
1671 goto fail;
1672 }
a963990f 1673
2f5435a1
LP
1674 *m = (Directory) {
1675 .is_root = false,
1676 .path = path,
1677 };
a963990f
LP
1678
1679 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
a963990f 1680 free(m);
d617408e
LP
1681 r = -ENOMEM;
1682 goto fail;
a963990f
LP
1683 }
1684
a50d7d43 1685 path = NULL; /* avoid freeing in cleanup */
313cefa1 1686 j->current_invalidate_counter++;
a963990f 1687
5ec76417 1688 log_debug("Directory %s added.", m->path);
a963990f 1689
a50d7d43 1690 } else if (m->is_root)
858749f7 1691 return 0; /* Don't 'downgrade' from root directory */
a963990f 1692
858749f7 1693 m->last_seen_generation = j->generation;
a963990f 1694
858749f7
LP
1695 directory_watch(j, m, dirfd(d),
1696 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1697 IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|IN_MOVED_FROM|
1698 IN_ONLYDIR);
a963990f 1699
858749f7
LP
1700 if (!j->no_new_files)
1701 directory_enumerate(j, m, d);
a963990f 1702
85210bff
LP
1703 check_network(j, dirfd(d));
1704
a963990f 1705 return 0;
d617408e
LP
1706
1707fail:
5d1ce257 1708 k = journal_put_error(j, r, path ?: prefix);
d617408e
LP
1709 if (k < 0)
1710 return k;
1711
1712 return r;
a963990f
LP
1713}
1714
d617408e 1715static int add_root_directory(sd_journal *j, const char *p, bool missing_ok) {
5d1ce257 1716
7fd1b19b 1717 _cleanup_closedir_ DIR *d = NULL;
a963990f 1718 Directory *m;
d617408e 1719 int r, k;
a963990f
LP
1720
1721 assert(j);
a963990f 1722
5d1ce257
LP
1723 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
1724 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
1725 * populate the set, as well as to update it later. */
a963990f 1726
5d1ce257
LP
1727 if (p) {
1728 /* If there's a path specified, use it. */
b6741478 1729
858749f7
LP
1730 log_debug("Considering root directory '%s'.", p);
1731
5d1ce257
LP
1732 if ((j->flags & SD_JOURNAL_RUNTIME_ONLY) &&
1733 !path_has_prefix(j, p, "/run"))
1734 return -EINVAL;
d617408e 1735
5d1ce257
LP
1736 if (j->prefix)
1737 p = strjoina(j->prefix, p);
1738
858749f7
LP
1739 r = directory_open(j, p, &d);
1740 if (r == -ENOENT && missing_ok)
1741 return 0;
1742 if (r < 0) {
1743 log_debug_errno(r, "Failed to open root directory %s: %m", p);
5d1ce257
LP
1744 goto fail;
1745 }
1746 } else {
8e06af80 1747 _cleanup_close_ int dfd = -1;
5d1ce257
LP
1748
1749 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
1750 * opendir() will take possession of the fd, and close it, which we don't want. */
1751
1752 p = "."; /* store this as "." in the directories hashmap */
1753
1754 dfd = fcntl(j->toplevel_fd, F_DUPFD_CLOEXEC, 3);
1755 if (dfd < 0) {
1756 r = -errno;
1757 goto fail;
1758 }
1759
8e06af80 1760 d = take_fdopendir(&dfd);
5d1ce257
LP
1761 if (!d) {
1762 r = -errno;
5d1ce257
LP
1763 goto fail;
1764 }
1765
1766 rewinddir(d);
d617408e 1767 }
a963990f
LP
1768
1769 m = hashmap_get(j->directories_by_path, p);
1770 if (!m) {
1771 m = new0(Directory, 1);
d617408e
LP
1772 if (!m) {
1773 r = -ENOMEM;
1774 goto fail;
1775 }
a963990f
LP
1776
1777 m->is_root = true;
5d1ce257 1778
a963990f
LP
1779 m->path = strdup(p);
1780 if (!m->path) {
a963990f 1781 free(m);
d617408e
LP
1782 r = -ENOMEM;
1783 goto fail;
a963990f
LP
1784 }
1785
1786 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
a963990f
LP
1787 free(m->path);
1788 free(m);
d617408e
LP
1789 r = -ENOMEM;
1790 goto fail;
a963990f
LP
1791 }
1792
313cefa1 1793 j->current_invalidate_counter++;
a963990f 1794
5ec76417 1795 log_debug("Root directory %s added.", m->path);
a963990f 1796
a50d7d43 1797 } else if (!m->is_root)
a963990f 1798 return 0;
50f20cfd 1799
858749f7
LP
1800 directory_watch(j, m, dirfd(d),
1801 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1802 IN_ONLYDIR);
a963990f 1803
858749f7
LP
1804 if (!j->no_new_files)
1805 directory_enumerate(j, m, d);
3fbf9cbb 1806
85210bff
LP
1807 check_network(j, dirfd(d));
1808
a963990f 1809 return 0;
d617408e
LP
1810
1811fail:
5768d259 1812 k = journal_put_error(j, r, p);
d617408e
LP
1813 if (k < 0)
1814 return k;
1815
1816 return r;
a963990f
LP
1817}
1818
b2b46f91 1819static void remove_directory(sd_journal *j, Directory *d) {
a963990f
LP
1820 assert(j);
1821
1822 if (d->wd > 0) {
1823 hashmap_remove(j->directories_by_wd, INT_TO_PTR(d->wd));
1824
1825 if (j->inotify_fd >= 0)
98d4a4df 1826 (void) inotify_rm_watch(j->inotify_fd, d->wd);
a963990f
LP
1827 }
1828
1829 hashmap_remove(j->directories_by_path, d->path);
1830
1831 if (d->is_root)
5ec76417 1832 log_debug("Root directory %s removed.", d->path);
a963990f 1833 else
5ec76417 1834 log_debug("Directory %s removed.", d->path);
a963990f
LP
1835
1836 free(d->path);
1837 free(d);
3fbf9cbb
LP
1838}
1839
89739579 1840static int add_search_paths(sd_journal *j) {
d617408e
LP
1841
1842 static const char search_paths[] =
a963990f
LP
1843 "/run/log/journal\0"
1844 "/var/log/journal\0";
1845 const char *p;
50f20cfd
LP
1846
1847 assert(j);
50f20cfd 1848
a963990f
LP
1849 /* We ignore most errors here, since the idea is to only open
1850 * what's actually accessible, and ignore the rest. */
50f20cfd 1851
d617408e
LP
1852 NULSTR_FOREACH(p, search_paths)
1853 (void) add_root_directory(j, p, true);
50f20cfd 1854
574b77ef
MB
1855 if (!(j->flags & SD_JOURNAL_LOCAL_ONLY))
1856 (void) add_root_directory(j, "/var/log/journal/remote", true);
1857
a963990f 1858 return 0;
50f20cfd
LP
1859}
1860
5302ebe1
ZJS
1861static int add_current_paths(sd_journal *j) {
1862 Iterator i;
1863 JournalFile *f;
1864
1865 assert(j);
1866 assert(j->no_new_files);
1867
5d1ce257 1868 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
5302ebe1
ZJS
1869 * treat them as fatal. */
1870
c1f906bd 1871 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
5302ebe1 1872 _cleanup_free_ char *dir;
e9174f29 1873 int r;
5302ebe1
ZJS
1874
1875 dir = dirname_malloc(f->path);
1876 if (!dir)
1877 return -ENOMEM;
1878
5d1ce257 1879 r = add_directory(j, dir, NULL);
d617408e 1880 if (r < 0)
5302ebe1 1881 return r;
5302ebe1
ZJS
1882 }
1883
1884 return 0;
1885}
1886
a963990f 1887static int allocate_inotify(sd_journal *j) {
50f20cfd 1888 assert(j);
50f20cfd 1889
a963990f
LP
1890 if (j->inotify_fd < 0) {
1891 j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1892 if (j->inotify_fd < 0)
1893 return -errno;
1894 }
50f20cfd 1895
cb306f5d 1896 return hashmap_ensure_allocated(&j->directories_by_wd, NULL);
50f20cfd
LP
1897}
1898
456aa879 1899static sd_journal *journal_new(int flags, const char *path, const char *namespace) {
17c9aff8 1900 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
50f20cfd 1901
a963990f
LP
1902 j = new0(sd_journal, 1);
1903 if (!j)
1904 return NULL;
50f20cfd 1905
df0ff127 1906 j->original_pid = getpid_cached();
5d1ce257 1907 j->toplevel_fd = -1;
a963990f
LP
1908 j->inotify_fd = -1;
1909 j->flags = flags;
93b73b06 1910 j->data_threshold = DEFAULT_DATA_THRESHOLD;
50f20cfd 1911
7827b1a1 1912 if (path) {
16fefe90
ZJS
1913 char *t;
1914
1915 t = strdup(path);
1916 if (!t)
17c9aff8 1917 return NULL;
16fefe90
ZJS
1918
1919 if (flags & SD_JOURNAL_OS_ROOT)
1920 j->prefix = t;
1921 else
1922 j->path = t;
7827b1a1
LP
1923 }
1924
456aa879
LP
1925 if (namespace) {
1926 j->namespace = strdup(namespace);
1927 if (!j->namespace)
1928 return NULL;
1929 }
1930
548f6937 1931 j->files = ordered_hashmap_new(&path_hash_ops);
5d4ba7f2 1932 if (!j->files)
17c9aff8 1933 return NULL;
5d4ba7f2
VC
1934
1935 j->files_cache = ordered_hashmap_iterated_cache_new(j->files);
548f6937 1936 j->directories_by_path = hashmap_new(&path_hash_ops);
84168d80 1937 j->mmap = mmap_cache_new();
5d4ba7f2 1938 if (!j->files_cache || !j->directories_by_path || !j->mmap)
17c9aff8 1939 return NULL;
6180fc61 1940
17c9aff8 1941 return TAKE_PTR(j);
50f20cfd
LP
1942}
1943
1aaa68f5
ZJS
1944#define OPEN_ALLOWED_FLAGS \
1945 (SD_JOURNAL_LOCAL_ONLY | \
1946 SD_JOURNAL_RUNTIME_ONLY | \
456aa879
LP
1947 SD_JOURNAL_SYSTEM | \
1948 SD_JOURNAL_CURRENT_USER | \
1949 SD_JOURNAL_ALL_NAMESPACES | \
1950 SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE)
1aaa68f5 1951
456aa879 1952_public_ int sd_journal_open_namespace(sd_journal **ret, const char *namespace, int flags) {
17c9aff8 1953 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
3fbf9cbb 1954 int r;
87d2c1ff 1955
1ae464e0 1956 assert_return(ret, -EINVAL);
1aaa68f5 1957 assert_return((flags & ~OPEN_ALLOWED_FLAGS) == 0, -EINVAL);
87d2c1ff 1958
456aa879 1959 j = journal_new(flags, NULL, namespace);
87d2c1ff
LP
1960 if (!j)
1961 return -ENOMEM;
1962
89739579 1963 r = add_search_paths(j);
a963990f 1964 if (r < 0)
17c9aff8 1965 return r;
50f20cfd 1966
17c9aff8 1967 *ret = TAKE_PTR(j);
a963990f 1968 return 0;
a963990f 1969}
50f20cfd 1970
456aa879
LP
1971_public_ int sd_journal_open(sd_journal **ret, int flags) {
1972 return sd_journal_open_namespace(ret, NULL, flags);
1973}
1974
1aaa68f5
ZJS
1975#define OPEN_CONTAINER_ALLOWED_FLAGS \
1976 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1977
b6741478
LP
1978_public_ int sd_journal_open_container(sd_journal **ret, const char *machine, int flags) {
1979 _cleanup_free_ char *root = NULL, *class = NULL;
17c9aff8 1980 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
b6741478
LP
1981 char *p;
1982 int r;
1983
68312977 1984 /* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in
2daa9cbd
LP
1985 * combination with sd_journal_open_directory_fd(). */
1986
b6741478
LP
1987 assert_return(machine, -EINVAL);
1988 assert_return(ret, -EINVAL);
1aaa68f5 1989 assert_return((flags & ~OPEN_CONTAINER_ALLOWED_FLAGS) == 0, -EINVAL);
affcf189 1990 assert_return(machine_name_is_valid(machine), -EINVAL);
b6741478 1991
63c372cb 1992 p = strjoina("/run/systemd/machines/", machine);
13df9c39
LP
1993 r = parse_env_file(NULL, p,
1994 "ROOT", &root,
1995 "CLASS", &class);
b6741478
LP
1996 if (r == -ENOENT)
1997 return -EHOSTDOWN;
1998 if (r < 0)
1999 return r;
2000 if (!root)
2001 return -ENODATA;
2002
2003 if (!streq_ptr(class, "container"))
2004 return -EIO;
2005
456aa879 2006 j = journal_new(flags, root, NULL);
b6741478
LP
2007 if (!j)
2008 return -ENOMEM;
2009
89739579 2010 r = add_search_paths(j);
b6741478 2011 if (r < 0)
17c9aff8 2012 return r;
b6741478 2013
17c9aff8 2014 *ret = TAKE_PTR(j);
b6741478 2015 return 0;
b6741478
LP
2016}
2017
1aaa68f5 2018#define OPEN_DIRECTORY_ALLOWED_FLAGS \
10752e82
ZJS
2019 (SD_JOURNAL_OS_ROOT | \
2020 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1aaa68f5 2021
a963990f 2022_public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) {
17c9aff8 2023 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
a963990f 2024 int r;
87d2c1ff 2025
1ae464e0
TA
2026 assert_return(ret, -EINVAL);
2027 assert_return(path, -EINVAL);
1aaa68f5 2028 assert_return((flags & ~OPEN_DIRECTORY_ALLOWED_FLAGS) == 0, -EINVAL);
87d2c1ff 2029
456aa879 2030 j = journal_new(flags, path, NULL);
a963990f
LP
2031 if (!j)
2032 return -ENOMEM;
3fbf9cbb 2033
d077390c
LP
2034 if (flags & SD_JOURNAL_OS_ROOT)
2035 r = add_search_paths(j);
2036 else
2037 r = add_root_directory(j, path, false);
d617408e 2038 if (r < 0)
17c9aff8 2039 return r;
87d2c1ff 2040
17c9aff8 2041 *ret = TAKE_PTR(j);
87d2c1ff 2042 return 0;
a963990f 2043}
87d2c1ff 2044
5302ebe1 2045_public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int flags) {
17c9aff8 2046 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
5302ebe1
ZJS
2047 const char **path;
2048 int r;
2049
1ae464e0
TA
2050 assert_return(ret, -EINVAL);
2051 assert_return(flags == 0, -EINVAL);
5302ebe1 2052
456aa879 2053 j = journal_new(flags, NULL, NULL);
5302ebe1
ZJS
2054 if (!j)
2055 return -ENOMEM;
2056
2057 STRV_FOREACH(path, paths) {
5d1ce257 2058 r = add_any_file(j, -1, *path);
d617408e 2059 if (r < 0)
17c9aff8 2060 return r;
5302ebe1
ZJS
2061 }
2062
2063 j->no_new_files = true;
2064
17c9aff8 2065 *ret = TAKE_PTR(j);
5302ebe1 2066 return 0;
5d1ce257
LP
2067}
2068
1aaa68f5 2069#define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
10752e82
ZJS
2070 (SD_JOURNAL_OS_ROOT | \
2071 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1aaa68f5 2072
5d1ce257 2073_public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) {
17c9aff8 2074 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
5d1ce257
LP
2075 struct stat st;
2076 int r;
2077
2078 assert_return(ret, -EINVAL);
2079 assert_return(fd >= 0, -EBADF);
1aaa68f5 2080 assert_return((flags & ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS) == 0, -EINVAL);
5d1ce257
LP
2081
2082 if (fstat(fd, &st) < 0)
2083 return -errno;
2084
2085 if (!S_ISDIR(st.st_mode))
2086 return -EBADFD;
2087
456aa879 2088 j = journal_new(flags, NULL, NULL);
5d1ce257
LP
2089 if (!j)
2090 return -ENOMEM;
2091
2092 j->toplevel_fd = fd;
2093
d077390c
LP
2094 if (flags & SD_JOURNAL_OS_ROOT)
2095 r = add_search_paths(j);
2096 else
2097 r = add_root_directory(j, NULL, false);
5d1ce257 2098 if (r < 0)
17c9aff8 2099 return r;
5d1ce257 2100
17c9aff8 2101 *ret = TAKE_PTR(j);
5d1ce257 2102 return 0;
5d1ce257
LP
2103}
2104
2105_public_ int sd_journal_open_files_fd(sd_journal **ret, int fds[], unsigned n_fds, int flags) {
2106 Iterator iterator;
2107 JournalFile *f;
17c9aff8 2108 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
5d1ce257
LP
2109 unsigned i;
2110 int r;
2111
2112 assert_return(ret, -EINVAL);
2113 assert_return(n_fds > 0, -EBADF);
2114 assert_return(flags == 0, -EINVAL);
2115
456aa879 2116 j = journal_new(flags, NULL, NULL);
5d1ce257
LP
2117 if (!j)
2118 return -ENOMEM;
2119
2120 for (i = 0; i < n_fds; i++) {
2121 struct stat st;
2122
2123 if (fds[i] < 0) {
2124 r = -EBADF;
2125 goto fail;
2126 }
2127
2128 if (fstat(fds[i], &st) < 0) {
2129 r = -errno;
2130 goto fail;
2131 }
2132
3cc44114
LP
2133 r = stat_verify_regular(&st);
2134 if (r < 0)
5d1ce257 2135 goto fail;
5d1ce257
LP
2136
2137 r = add_any_file(j, fds[i], NULL);
2138 if (r < 0)
2139 goto fail;
2140 }
2141
2142 j->no_new_files = true;
2143 j->no_inotify = true;
5302ebe1 2144
17c9aff8 2145 *ret = TAKE_PTR(j);
5d1ce257
LP
2146 return 0;
2147
2148fail:
f8e2f4d6 2149 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
5d1ce257
LP
2150 * remain open */
2151 ORDERED_HASHMAP_FOREACH(f, j->files, iterator)
2152 f->close_fd = false;
2153
5302ebe1
ZJS
2154 return r;
2155}
2156
a5344d2c 2157_public_ void sd_journal_close(sd_journal *j) {
a963990f 2158 Directory *d;
a963990f 2159
a5344d2c
LP
2160 if (!j)
2161 return;
87d2c1ff 2162
54b1da83
LP
2163 sd_journal_flush_matches(j);
2164
f9168190 2165 ordered_hashmap_free_with_destructor(j->files, journal_file_close);
5d4ba7f2 2166 iterated_cache_free(j->files_cache);
260a2be4 2167
a963990f
LP
2168 while ((d = hashmap_first(j->directories_by_path)))
2169 remove_directory(j, d);
260a2be4 2170
a963990f
LP
2171 while ((d = hashmap_first(j->directories_by_wd)))
2172 remove_directory(j, d);
87d2c1ff 2173
a963990f
LP
2174 hashmap_free(j->directories_by_path);
2175 hashmap_free(j->directories_by_wd);
1cc101f1 2176
03e334a1 2177 safe_close(j->inotify_fd);
50f20cfd 2178
bf807d4d
LP
2179 if (j->mmap) {
2180 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j->mmap), mmap_cache_get_missed(j->mmap));
16e9f408 2181 mmap_cache_unref(j->mmap);
bf807d4d 2182 }
16e9f408 2183
ec1d2909 2184 hashmap_free_free(j->errors);
5768d259 2185
7827b1a1 2186 free(j->path);
89739579 2187 free(j->prefix);
456aa879 2188 free(j->namespace);
3c1668da 2189 free(j->unique_field);
eb86030e 2190 free(j->fields_buffer);
87d2c1ff
LP
2191 free(j);
2192}
3fbf9cbb 2193
a5344d2c 2194_public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
3fbf9cbb
LP
2195 Object *o;
2196 JournalFile *f;
2197 int r;
2198
1ae464e0
TA
2199 assert_return(j, -EINVAL);
2200 assert_return(!journal_pid_changed(j), -ECHILD);
2201 assert_return(ret, -EINVAL);
3fbf9cbb
LP
2202
2203 f = j->current_file;
2204 if (!f)
de190aef 2205 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2206
2207 if (f->current_offset <= 0)
de190aef 2208 return -EADDRNOTAVAIL;
3fbf9cbb 2209
de190aef 2210 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2211 if (r < 0)
2212 return r;
2213
2214 *ret = le64toh(o->entry.realtime);
de190aef 2215 return 0;
3fbf9cbb
LP
2216}
2217
a5344d2c 2218_public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
3fbf9cbb
LP
2219 Object *o;
2220 JournalFile *f;
2221 int r;
2222 sd_id128_t id;
2223
1ae464e0
TA
2224 assert_return(j, -EINVAL);
2225 assert_return(!journal_pid_changed(j), -ECHILD);
3fbf9cbb
LP
2226
2227 f = j->current_file;
2228 if (!f)
de190aef 2229 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2230
2231 if (f->current_offset <= 0)
de190aef 2232 return -EADDRNOTAVAIL;
3fbf9cbb 2233
de190aef 2234 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2235 if (r < 0)
2236 return r;
2237
de190aef
LP
2238 if (ret_boot_id)
2239 *ret_boot_id = o->entry.boot_id;
2240 else {
2241 r = sd_id128_get_boot(&id);
2242 if (r < 0)
2243 return r;
3fbf9cbb 2244
de190aef 2245 if (!sd_id128_equal(id, o->entry.boot_id))
df50185b 2246 return -ESTALE;
de190aef 2247 }
3fbf9cbb 2248
14a65d65
LP
2249 if (ret)
2250 *ret = le64toh(o->entry.monotonic);
2251
de190aef 2252 return 0;
3fbf9cbb
LP
2253}
2254
362a3f81
LP
2255static bool field_is_valid(const char *field) {
2256 const char *p;
2257
2258 assert(field);
2259
2260 if (isempty(field))
2261 return false;
2262
2263 if (startswith(field, "__"))
2264 return false;
2265
2266 for (p = field; *p; p++) {
2267
2268 if (*p == '_')
2269 continue;
2270
2271 if (*p >= 'A' && *p <= 'Z')
2272 continue;
2273
2274 if (*p >= '0' && *p <= '9')
2275 continue;
2276
2277 return false;
2278 }
2279
2280 return true;
2281}
2282
a5344d2c 2283_public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
3fbf9cbb
LP
2284 JournalFile *f;
2285 uint64_t i, n;
2286 size_t field_length;
2287 int r;
2288 Object *o;
2289
1ae464e0
TA
2290 assert_return(j, -EINVAL);
2291 assert_return(!journal_pid_changed(j), -ECHILD);
2292 assert_return(field, -EINVAL);
2293 assert_return(data, -EINVAL);
2294 assert_return(size, -EINVAL);
2295 assert_return(field_is_valid(field), -EINVAL);
3fbf9cbb
LP
2296
2297 f = j->current_file;
2298 if (!f)
de190aef 2299 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2300
2301 if (f->current_offset <= 0)
de190aef 2302 return -EADDRNOTAVAIL;
3fbf9cbb 2303
de190aef 2304 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2305 if (r < 0)
2306 return r;
2307
2308 field_length = strlen(field);
2309
2310 n = journal_file_entry_n_items(o);
2311 for (i = 0; i < n; i++) {
4fd052ae
FC
2312 uint64_t p, l;
2313 le64_t le_hash;
3fbf9cbb 2314 size_t t;
1ec7120e 2315 int compression;
3fbf9cbb
LP
2316
2317 p = le64toh(o->entry.items[i].object_offset);
807e17f0 2318 le_hash = o->entry.items[i].hash;
de190aef 2319 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
3fbf9cbb
LP
2320 if (r < 0)
2321 return r;
2322
de190aef 2323 if (le_hash != o->data.hash)
de7b95cd
LP
2324 return -EBADMSG;
2325
3fbf9cbb
LP
2326 l = le64toh(o->object.size) - offsetof(Object, data.payload);
2327
1ec7120e 2328 compression = o->object.flags & OBJECT_COMPRESSION_MASK;
3b1a55e1 2329 if (compression) {
d80b051c 2330#if HAVE_COMPRESSION
2aaec9b4 2331 r = decompress_startswith(compression,
3b1a55e1
ZJS
2332 o->data.payload, l,
2333 &f->compress_buffer, &f->compress_buffer_size,
2aaec9b4
ZJS
2334 field, field_length, '=');
2335 if (r < 0)
82e24b00 2336 log_debug_errno(r, "Cannot decompress %s object of length %"PRIu64" at offset "OFSfmt": %m",
2aaec9b4
ZJS
2337 object_compressed_to_string(compression), l, p);
2338 else if (r > 0) {
3b1a55e1 2339
fa1c4b51 2340 size_t rsize;
3b1a55e1
ZJS
2341
2342 r = decompress_blob(compression,
2343 o->data.payload, l,
2344 &f->compress_buffer, &f->compress_buffer_size, &rsize,
2345 j->data_threshold);
2346 if (r < 0)
2347 return r;
807e17f0 2348
3b1a55e1
ZJS
2349 *data = f->compress_buffer;
2350 *size = (size_t) rsize;
807e17f0 2351
3b1a55e1
ZJS
2352 return 0;
2353 }
2354#else
2355 return -EPROTONOSUPPORT;
2356#endif
807e17f0
LP
2357 } else if (l >= field_length+1 &&
2358 memcmp(o->data.payload, field, field_length) == 0 &&
2359 o->data.payload[field_length] == '=') {
3fbf9cbb 2360
161e54f8 2361 t = (size_t) l;
3fbf9cbb 2362
161e54f8
LP
2363 if ((uint64_t) t != l)
2364 return -E2BIG;
3fbf9cbb 2365
161e54f8
LP
2366 *data = o->data.payload;
2367 *size = t;
3fbf9cbb 2368
99613ec5 2369 return 0;
161e54f8 2370 }
3fbf9cbb 2371
de190aef 2372 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
161e54f8
LP
2373 if (r < 0)
2374 return r;
3fbf9cbb
LP
2375 }
2376
de190aef 2377 return -ENOENT;
3fbf9cbb
LP
2378}
2379
93b73b06 2380static int return_data(sd_journal *j, JournalFile *f, Object *o, const void **data, size_t *size) {
3c1668da
LP
2381 size_t t;
2382 uint64_t l;
3b1a55e1 2383 int compression;
3c1668da 2384
893e0f8f
LP
2385 l = le64toh(READ_NOW(o->object.size));
2386 if (l < offsetof(Object, data.payload))
2387 return -EBADMSG;
2388 l -= offsetof(Object, data.payload);
3c1668da
LP
2389 t = (size_t) l;
2390
2391 /* We can't read objects larger than 4G on a 32bit machine */
2392 if ((uint64_t) t != l)
2393 return -E2BIG;
2394
1ec7120e
ZJS
2395 compression = o->object.flags & OBJECT_COMPRESSION_MASK;
2396 if (compression) {
d80b051c 2397#if HAVE_COMPRESSION
fa1c4b51 2398 size_t rsize;
3b1a55e1 2399 int r;
3c1668da 2400
1ec7120e
ZJS
2401 r = decompress_blob(compression,
2402 o->data.payload, l, &f->compress_buffer,
2403 &f->compress_buffer_size, &rsize, j->data_threshold);
2404 if (r < 0)
2405 return r;
3c1668da
LP
2406
2407 *data = f->compress_buffer;
2408 *size = (size_t) rsize;
3b1a55e1
ZJS
2409#else
2410 return -EPROTONOSUPPORT;
2411#endif
3c1668da
LP
2412 } else {
2413 *data = o->data.payload;
2414 *size = t;
2415 }
2416
2417 return 0;
2418}
2419
a5344d2c 2420_public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
3fbf9cbb 2421 JournalFile *f;
3c1668da 2422 uint64_t p, n;
4fd052ae 2423 le64_t le_hash;
3fbf9cbb
LP
2424 int r;
2425 Object *o;
2426
1ae464e0
TA
2427 assert_return(j, -EINVAL);
2428 assert_return(!journal_pid_changed(j), -ECHILD);
2429 assert_return(data, -EINVAL);
2430 assert_return(size, -EINVAL);
3fbf9cbb
LP
2431
2432 f = j->current_file;
2433 if (!f)
de190aef 2434 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2435
2436 if (f->current_offset <= 0)
de190aef 2437 return -EADDRNOTAVAIL;
3fbf9cbb 2438
de190aef 2439 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2440 if (r < 0)
2441 return r;
2442
2443 n = journal_file_entry_n_items(o);
7210bfb3 2444 if (j->current_field >= n)
3fbf9cbb
LP
2445 return 0;
2446
7210bfb3 2447 p = le64toh(o->entry.items[j->current_field].object_offset);
de190aef
LP
2448 le_hash = o->entry.items[j->current_field].hash;
2449 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
3fbf9cbb
LP
2450 if (r < 0)
2451 return r;
2452
de190aef 2453 if (le_hash != o->data.hash)
de7b95cd
LP
2454 return -EBADMSG;
2455
93b73b06 2456 r = return_data(j, f, o, data, size);
3c1668da
LP
2457 if (r < 0)
2458 return r;
3fbf9cbb 2459
313cefa1 2460 j->current_field++;
3fbf9cbb
LP
2461
2462 return 1;
2463}
c2373f84 2464
76cbafcd
ZJS
2465_public_ int sd_journal_enumerate_available_data(sd_journal *j, const void **data, size_t *size) {
2466 for (;;) {
2467 int r;
2468
2469 r = sd_journal_enumerate_data(j, data, size);
2470 if (r >= 0)
2471 return r;
2472 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r))
2473 return r;
2474 j->current_field++; /* Try with the next field */
2475 }
2476}
2477
a5344d2c
LP
2478_public_ void sd_journal_restart_data(sd_journal *j) {
2479 if (!j)
2480 return;
8725d60a
LP
2481
2482 j->current_field = 0;
c2373f84 2483}
50f20cfd 2484
858749f7
LP
2485static int reiterate_all_paths(sd_journal *j) {
2486 assert(j);
2487
2488 if (j->no_new_files)
2489 return add_current_paths(j);
2490
2491 if (j->flags & SD_JOURNAL_OS_ROOT)
2492 return add_search_paths(j);
2493
2494 if (j->toplevel_fd >= 0)
2495 return add_root_directory(j, NULL, false);
2496
2497 if (j->path)
2498 return add_root_directory(j, j->path, true);
2499
2500 return add_search_paths(j);
2501}
2502
a5344d2c 2503_public_ int sd_journal_get_fd(sd_journal *j) {
a963990f
LP
2504 int r;
2505
1ae464e0
TA
2506 assert_return(j, -EINVAL);
2507 assert_return(!journal_pid_changed(j), -ECHILD);
50f20cfd 2508
5d1ce257
LP
2509 if (j->no_inotify)
2510 return -EMEDIUMTYPE;
2511
a963990f
LP
2512 if (j->inotify_fd >= 0)
2513 return j->inotify_fd;
2514
2515 r = allocate_inotify(j);
2516 if (r < 0)
2517 return r;
2518
858749f7 2519 log_debug("Reiterating files to get inotify watches established.");
5d1ce257 2520
858749f7
LP
2521 /* Iterate through all dirs again, to add them to the inotify */
2522 r = reiterate_all_paths(j);
a963990f
LP
2523 if (r < 0)
2524 return r;
2525
50f20cfd
LP
2526 return j->inotify_fd;
2527}
2528
ee531d94
LP
2529_public_ int sd_journal_get_events(sd_journal *j) {
2530 int fd;
2531
1ae464e0
TA
2532 assert_return(j, -EINVAL);
2533 assert_return(!journal_pid_changed(j), -ECHILD);
ee531d94
LP
2534
2535 fd = sd_journal_get_fd(j);
2536 if (fd < 0)
2537 return fd;
2538
2539 return POLLIN;
2540}
2541
39c155ea
LP
2542_public_ int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec) {
2543 int fd;
2544
1ae464e0
TA
2545 assert_return(j, -EINVAL);
2546 assert_return(!journal_pid_changed(j), -ECHILD);
2547 assert_return(timeout_usec, -EINVAL);
39c155ea
LP
2548
2549 fd = sd_journal_get_fd(j);
2550 if (fd < 0)
2551 return fd;
2552
2553 if (!j->on_network) {
2554 *timeout_usec = (uint64_t) -1;
2555 return 0;
2556 }
2557
2558 /* If we are on the network we need to regularly check for
2559 * changes manually */
2560
2561 *timeout_usec = j->last_process_usec + JOURNAL_FILES_RECHECK_USEC;
2562 return 1;
2563}
2564
858749f7
LP
2565static void process_q_overflow(sd_journal *j) {
2566 JournalFile *f;
2567 Directory *m;
2568 Iterator i;
2569
2570 assert(j);
2571
2572 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2573 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2574 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2575 * are subject for unloading. */
2576
2577 log_debug("Inotify queue overrun, reiterating everything.");
2578
2579 j->generation++;
2580 (void) reiterate_all_paths(j);
2581
2582 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
2583
2584 if (f->last_seen_generation == j->generation)
2585 continue;
2586
2587 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f->path);
2588 remove_file_real(j, f);
2589 }
2590
2591 HASHMAP_FOREACH(m, j->directories_by_path, i) {
2592
2593 if (m->last_seen_generation == j->generation)
2594 continue;
2595
2596 if (m->is_root) /* Never GC root directories */
2597 continue;
2598
2599 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f->path);
2600 remove_directory(j, m);
2601 }
2602
2603 log_debug("Reiteration complete.");
2604}
2605
31e99dd2 2606static void process_inotify_event(sd_journal *j, const struct inotify_event *e) {
a963990f 2607 Directory *d;
50f20cfd
LP
2608
2609 assert(j);
2610 assert(e);
2611
858749f7
LP
2612 if (e->mask & IN_Q_OVERFLOW) {
2613 process_q_overflow(j);
2614 return;
2615 }
2616
50f20cfd 2617 /* Is this a subdirectory we watch? */
a963990f
LP
2618 d = hashmap_get(j->directories_by_wd, INT_TO_PTR(e->wd));
2619 if (d) {
de2c3907
LP
2620 if (!(e->mask & IN_ISDIR) && e->len > 0 &&
2621 (endswith(e->name, ".journal") ||
2622 endswith(e->name, ".journal~"))) {
50f20cfd
LP
2623
2624 /* Event for a journal file */
2625
d617408e 2626 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
fc1813c0 2627 (void) add_file_by_name(j, d->path, e->name);
d617408e 2628 else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT))
fc1813c0 2629 remove_file_by_name(j, d->path, e->name);
50f20cfd 2630
a963990f 2631 } else if (!d->is_root && e->len == 0) {
50f20cfd 2632
a963990f 2633 /* Event for a subdirectory */
50f20cfd 2634
b2b46f91
TA
2635 if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT))
2636 remove_directory(j, d);
50f20cfd 2637
a9be0692 2638 } else if (d->is_root && (e->mask & IN_ISDIR) && e->len > 0 && id128_is_valid(e->name)) {
50f20cfd 2639
a963990f 2640 /* Event for root directory */
50f20cfd 2641
d617408e
LP
2642 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
2643 (void) add_directory(j, d->path, e->name);
50f20cfd
LP
2644 }
2645
2646 return;
2647 }
2648
2649 if (e->mask & IN_IGNORED)
2650 return;
2651
a9be0692 2652 log_debug("Unexpected inotify event.");
50f20cfd
LP
2653}
2654
a963990f
LP
2655static int determine_change(sd_journal *j) {
2656 bool b;
2657
2658 assert(j);
2659
2660 b = j->current_invalidate_counter != j->last_invalidate_counter;
2661 j->last_invalidate_counter = j->current_invalidate_counter;
2662
2663 return b ? SD_JOURNAL_INVALIDATE : SD_JOURNAL_APPEND;
2664}
2665
a5344d2c 2666_public_ int sd_journal_process(sd_journal *j) {
a963990f 2667 bool got_something = false;
50f20cfd 2668
1ae464e0
TA
2669 assert_return(j, -EINVAL);
2670 assert_return(!journal_pid_changed(j), -ECHILD);
50f20cfd 2671
10c4d640
LP
2672 if (j->inotify_fd < 0) /* We have no inotify fd yet? Then there's noting to process. */
2673 return 0;
2674
39c155ea 2675 j->last_process_usec = now(CLOCK_MONOTONIC);
f9346444 2676 j->last_invalidate_counter = j->current_invalidate_counter;
39c155ea 2677
50f20cfd 2678 for (;;) {
0254e944 2679 union inotify_event_buffer buffer;
50f20cfd
LP
2680 struct inotify_event *e;
2681 ssize_t l;
2682
0254e944 2683 l = read(j->inotify_fd, &buffer, sizeof(buffer));
50f20cfd 2684 if (l < 0) {
3742095b 2685 if (IN_SET(errno, EAGAIN, EINTR))
a963990f 2686 return got_something ? determine_change(j) : SD_JOURNAL_NOP;
50f20cfd
LP
2687
2688 return -errno;
2689 }
2690
a963990f
LP
2691 got_something = true;
2692
f7c1ad4f 2693 FOREACH_INOTIFY_EVENT(e, buffer, l)
50f20cfd 2694 process_inotify_event(j, e);
50f20cfd
LP
2695 }
2696}
6ad1d1c3 2697
e02d1cf7 2698_public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
a963990f 2699 int r;
39c155ea 2700 uint64_t t;
e02d1cf7 2701
1ae464e0
TA
2702 assert_return(j, -EINVAL);
2703 assert_return(!journal_pid_changed(j), -ECHILD);
e02d1cf7 2704
a963990f 2705 if (j->inotify_fd < 0) {
28ca867a
MS
2706 Iterator i;
2707 JournalFile *f;
a963990f
LP
2708
2709 /* This is the first invocation, hence create the
2710 * inotify watch */
2711 r = sd_journal_get_fd(j);
2712 if (r < 0)
2713 return r;
2714
28ca867a
MS
2715 /* Server might have done some vacuuming while we weren't watching.
2716 Get rid of the deleted files now so they don't stay around indefinitely. */
2717 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
2718 r = journal_file_fstat(f);
8581b9f9
MS
2719 if (r == -EIDRM)
2720 remove_file_real(j, f);
2721 else if (r < 0) {
28ca867a
MS
2722 log_debug_errno(r,"Failed to fstat() journal file '%s' : %m", f->path);
2723 continue;
2724 }
28ca867a
MS
2725 }
2726
a963990f
LP
2727 /* The journal might have changed since the context
2728 * object was created and we weren't watching before,
2729 * hence don't wait for anything, and return
2730 * immediately. */
2731 return determine_change(j);
2732 }
2733
39c155ea
LP
2734 r = sd_journal_get_timeout(j, &t);
2735 if (r < 0)
2736 return r;
2737
2738 if (t != (uint64_t) -1) {
2739 usec_t n;
2740
2741 n = now(CLOCK_MONOTONIC);
2742 t = t > n ? t - n : 0;
85210bff 2743
39c155ea
LP
2744 if (timeout_usec == (uint64_t) -1 || timeout_usec > t)
2745 timeout_usec = t;
85210bff
LP
2746 }
2747
a963990f
LP
2748 do {
2749 r = fd_wait_for_event(j->inotify_fd, POLLIN, timeout_usec);
2750 } while (r == -EINTR);
e02d1cf7
LP
2751
2752 if (r < 0)
2753 return r;
2754
a963990f 2755 return sd_journal_process(j);
e02d1cf7
LP
2756}
2757
08984293
LP
2758_public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to) {
2759 Iterator i;
2760 JournalFile *f;
2761 bool first = true;
581483bf 2762 uint64_t fmin = 0, tmax = 0;
08984293
LP
2763 int r;
2764
1ae464e0
TA
2765 assert_return(j, -EINVAL);
2766 assert_return(!journal_pid_changed(j), -ECHILD);
2767 assert_return(from || to, -EINVAL);
2768 assert_return(from != to, -EINVAL);
08984293 2769
c1f906bd 2770 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
08984293
LP
2771 usec_t fr, t;
2772
2773 r = journal_file_get_cutoff_realtime_usec(f, &fr, &t);
9f8d2983
LP
2774 if (r == -ENOENT)
2775 continue;
08984293
LP
2776 if (r < 0)
2777 return r;
2778 if (r == 0)
2779 continue;
2780
2781 if (first) {
581483bf
LP
2782 fmin = fr;
2783 tmax = t;
08984293
LP
2784 first = false;
2785 } else {
581483bf
LP
2786 fmin = MIN(fr, fmin);
2787 tmax = MAX(t, tmax);
08984293
LP
2788 }
2789 }
2790
581483bf
LP
2791 if (from)
2792 *from = fmin;
2793 if (to)
2794 *to = tmax;
2795
08984293
LP
2796 return first ? 0 : 1;
2797}
2798
2799_public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t *from, uint64_t *to) {
2800 Iterator i;
2801 JournalFile *f;
1651e2c6 2802 bool found = false;
08984293
LP
2803 int r;
2804
1ae464e0
TA
2805 assert_return(j, -EINVAL);
2806 assert_return(!journal_pid_changed(j), -ECHILD);
2807 assert_return(from || to, -EINVAL);
2808 assert_return(from != to, -EINVAL);
08984293 2809
c1f906bd 2810 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
08984293
LP
2811 usec_t fr, t;
2812
2813 r = journal_file_get_cutoff_monotonic_usec(f, boot_id, &fr, &t);
9f8d2983
LP
2814 if (r == -ENOENT)
2815 continue;
08984293
LP
2816 if (r < 0)
2817 return r;
2818 if (r == 0)
2819 continue;
2820
1651e2c6 2821 if (found) {
08984293 2822 if (from)
1651e2c6 2823 *from = MIN(fr, *from);
08984293 2824 if (to)
1651e2c6 2825 *to = MAX(t, *to);
08984293
LP
2826 } else {
2827 if (from)
1651e2c6 2828 *from = fr;
08984293 2829 if (to)
1651e2c6
ZJS
2830 *to = t;
2831 found = true;
08984293
LP
2832 }
2833 }
2834
1651e2c6 2835 return found;
08984293
LP
2836}
2837
dca6219e
LP
2838void journal_print_header(sd_journal *j) {
2839 Iterator i;
2840 JournalFile *f;
2841 bool newline = false;
2842
2843 assert(j);
2844
c1f906bd 2845 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
dca6219e
LP
2846 if (newline)
2847 putchar('\n');
2848 else
2849 newline = true;
2850
2851 journal_file_print_header(f);
2852 }
2853}
08984293 2854
a1a03e30
LP
2855_public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) {
2856 Iterator i;
2857 JournalFile *f;
2858 uint64_t sum = 0;
2859
1ae464e0
TA
2860 assert_return(j, -EINVAL);
2861 assert_return(!journal_pid_changed(j), -ECHILD);
2862 assert_return(bytes, -EINVAL);
a1a03e30 2863
c1f906bd 2864 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
a1a03e30
LP
2865 struct stat st;
2866
2867 if (fstat(f->fd, &st) < 0)
2868 return -errno;
2869
2870 sum += (uint64_t) st.st_blocks * 512ULL;
2871 }
2872
2873 *bytes = sum;
2874 return 0;
2875}
2876
3c1668da
LP
2877_public_ int sd_journal_query_unique(sd_journal *j, const char *field) {
2878 char *f;
2879
1ae464e0
TA
2880 assert_return(j, -EINVAL);
2881 assert_return(!journal_pid_changed(j), -ECHILD);
2882 assert_return(!isempty(field), -EINVAL);
2883 assert_return(field_is_valid(field), -EINVAL);
3c1668da
LP
2884
2885 f = strdup(field);
2886 if (!f)
2887 return -ENOMEM;
2888
2889 free(j->unique_field);
2890 j->unique_field = f;
2891 j->unique_file = NULL;
2892 j->unique_offset = 0;
360af4cf 2893 j->unique_file_lost = false;
3c1668da
LP
2894
2895 return 0;
2896}
2897
2898_public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l) {
3c1668da 2899 size_t k;
19a2bd80 2900
1ae464e0
TA
2901 assert_return(j, -EINVAL);
2902 assert_return(!journal_pid_changed(j), -ECHILD);
2903 assert_return(data, -EINVAL);
2904 assert_return(l, -EINVAL);
2905 assert_return(j->unique_field, -EINVAL);
19a2bd80 2906
3c1668da 2907 k = strlen(j->unique_field);
19a2bd80 2908
3c1668da 2909 if (!j->unique_file) {
360af4cf
ZJS
2910 if (j->unique_file_lost)
2911 return 0;
2912
c1f906bd 2913 j->unique_file = ordered_hashmap_first(j->files);
3c1668da
LP
2914 if (!j->unique_file)
2915 return 0;
360af4cf 2916
3c1668da
LP
2917 j->unique_offset = 0;
2918 }
19a2bd80 2919
3c1668da
LP
2920 for (;;) {
2921 JournalFile *of;
2922 Iterator i;
ae97089d 2923 Object *o;
3c1668da
LP
2924 const void *odata;
2925 size_t ol;
2926 bool found;
ae97089d 2927 int r;
3c1668da 2928
bdc02927 2929 /* Proceed to next data object in the field's linked list */
3c1668da
LP
2930 if (j->unique_offset == 0) {
2931 r = journal_file_find_field_object(j->unique_file, j->unique_field, k, &o, NULL);
2932 if (r < 0)
2933 return r;
2934
2935 j->unique_offset = r > 0 ? le64toh(o->field.head_data_offset) : 0;
2936 } else {
2937 r = journal_file_move_to_object(j->unique_file, OBJECT_DATA, j->unique_offset, &o);
2938 if (r < 0)
2939 return r;
2940
2941 j->unique_offset = le64toh(o->data.next_field_offset);
2942 }
2943
2944 /* We reached the end of the list? Then start again, with the next file */
2945 if (j->unique_offset == 0) {
c1f906bd 2946 j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
360af4cf 2947 if (!j->unique_file)
3c1668da
LP
2948 return 0;
2949
3c1668da
LP
2950 continue;
2951 }
2952
d05089d8
MS
2953 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2954 * instead, so that we can look at this data object at the same
3c1668da 2955 * time as one on another file */
d05089d8 2956 r = journal_file_move_to_object(j->unique_file, OBJECT_UNUSED, j->unique_offset, &o);
3c1668da
LP
2957 if (r < 0)
2958 return r;
2959
2960 /* Let's do the type check by hand, since we used 0 context above. */
baaa35ad
ZJS
2961 if (o->object.type != OBJECT_DATA)
2962 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
2963 "%s:offset " OFSfmt ": object has type %d, expected %d",
2964 j->unique_file->path,
2965 j->unique_offset,
2966 o->object.type, OBJECT_DATA);
ae97089d 2967
93b73b06 2968 r = return_data(j, j->unique_file, o, &odata, &ol);
3c1668da
LP
2969 if (r < 0)
2970 return r;
2971
0f99f74a 2972 /* Check if we have at least the field name and "=". */
baaa35ad
ZJS
2973 if (ol <= k)
2974 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
2975 "%s:offset " OFSfmt ": object has size %zu, expected at least %zu",
2976 j->unique_file->path,
2977 j->unique_offset, ol, k + 1);
2978
2979 if (memcmp(odata, j->unique_field, k) || ((const char*) odata)[k] != '=')
2980 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
2981 "%s:offset " OFSfmt ": object does not start with \"%s=\"",
2982 j->unique_file->path,
2983 j->unique_offset,
2984 j->unique_field);
0f99f74a 2985
3c1668da
LP
2986 /* OK, now let's see if we already returned this data
2987 * object by checking if it exists in the earlier
2988 * traversed files. */
2989 found = false;
c1f906bd 2990 ORDERED_HASHMAP_FOREACH(of, j->files, i) {
3c1668da
LP
2991 if (of == j->unique_file)
2992 break;
2993
ed71f956
LP
2994 /* Skip this file it didn't have any fields indexed */
2995 if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
3c1668da
LP
2996 continue;
2997
ed71f956 2998 r = journal_file_find_data_object_with_hash(of, odata, ol, le64toh(o->data.hash), NULL, NULL);
3c1668da
LP
2999 if (r < 0)
3000 return r;
ed71f956 3001 if (r > 0) {
3c1668da 3002 found = true;
ed71f956
LP
3003 break;
3004 }
3c1668da
LP
3005 }
3006
06cc69d4
JJ
3007 if (found)
3008 continue;
3009
93b73b06 3010 r = return_data(j, j->unique_file, o, data, l);
3c1668da
LP
3011 if (r < 0)
3012 return r;
3013
3014 return 1;
3015 }
3016}
3017
76cbafcd
ZJS
3018_public_ int sd_journal_enumerate_available_unique(sd_journal *j, const void **data, size_t *size) {
3019 for (;;) {
3020 int r;
3021
3022 r = sd_journal_enumerate_unique(j, data, size);
3023 if (r >= 0)
3024 return r;
3025 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r))
3026 return r;
3027 /* Try with the next field. sd_journal_enumerate_unique() modifies state, so on the next try
3028 * we will access the next field. */
3029 }
3030}
3031
115646c7 3032_public_ void sd_journal_restart_unique(sd_journal *j) {
3c1668da
LP
3033 if (!j)
3034 return;
3035
3036 j->unique_file = NULL;
3037 j->unique_offset = 0;
360af4cf 3038 j->unique_file_lost = false;
3c1668da 3039}
85210bff 3040
eb86030e
LP
3041_public_ int sd_journal_enumerate_fields(sd_journal *j, const char **field) {
3042 int r;
3043
3044 assert_return(j, -EINVAL);
3045 assert_return(!journal_pid_changed(j), -ECHILD);
3046 assert_return(field, -EINVAL);
3047
3048 if (!j->fields_file) {
3049 if (j->fields_file_lost)
3050 return 0;
3051
3052 j->fields_file = ordered_hashmap_first(j->files);
3053 if (!j->fields_file)
3054 return 0;
3055
3056 j->fields_hash_table_index = 0;
3057 j->fields_offset = 0;
3058 }
3059
3060 for (;;) {
3061 JournalFile *f, *of;
3062 Iterator i;
3063 uint64_t m;
3064 Object *o;
3065 size_t sz;
3066 bool found;
3067
3068 f = j->fields_file;
3069
3070 if (j->fields_offset == 0) {
3071 bool eof = false;
3072
3073 /* We are not yet positioned at any field. Let's pick the first one */
3074 r = journal_file_map_field_hash_table(f);
3075 if (r < 0)
3076 return r;
3077
3078 m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem);
3079 for (;;) {
3080 if (j->fields_hash_table_index >= m) {
3081 /* Reached the end of the hash table, go to the next file. */
3082 eof = true;
3083 break;
3084 }
3085
3086 j->fields_offset = le64toh(f->field_hash_table[j->fields_hash_table_index].head_hash_offset);
3087
3088 if (j->fields_offset != 0)
3089 break;
3090
3091 /* Empty hash table bucket, go to next one */
3092 j->fields_hash_table_index++;
3093 }
3094
3095 if (eof) {
3096 /* Proceed with next file */
3097 j->fields_file = ordered_hashmap_next(j->files, f->path);
3098 if (!j->fields_file) {
3099 *field = NULL;
3100 return 0;
3101 }
3102
3103 j->fields_offset = 0;
3104 j->fields_hash_table_index = 0;
3105 continue;
3106 }
3107
3108 } else {
3109 /* We are already positioned at a field. If so, let's figure out the next field from it */
3110
3111 r = journal_file_move_to_object(f, OBJECT_FIELD, j->fields_offset, &o);
3112 if (r < 0)
3113 return r;
3114
3115 j->fields_offset = le64toh(o->field.next_hash_offset);
3116 if (j->fields_offset == 0) {
3117 /* Reached the end of the hash table chain */
3118 j->fields_hash_table_index++;
3119 continue;
3120 }
3121 }
3122
1f133e0d 3123 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
eb86030e
LP
3124 r = journal_file_move_to_object(f, OBJECT_UNUSED, j->fields_offset, &o);
3125 if (r < 0)
3126 return r;
3127
3128 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
baaa35ad
ZJS
3129 if (o->object.type != OBJECT_FIELD)
3130 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
3131 "%s:offset " OFSfmt ": object has type %i, expected %i",
3132 f->path, j->fields_offset,
3133 o->object.type, OBJECT_FIELD);
eb86030e
LP
3134
3135 sz = le64toh(o->object.size) - offsetof(Object, field.payload);
3136
3137 /* Let's see if we already returned this field name before. */
3138 found = false;
3139 ORDERED_HASHMAP_FOREACH(of, j->files, i) {
3140 if (of == f)
3141 break;
3142
3143 /* Skip this file it didn't have any fields indexed */
3144 if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
3145 continue;
3146
3147 r = journal_file_find_field_object_with_hash(of, o->field.payload, sz, le64toh(o->field.hash), NULL, NULL);
3148 if (r < 0)
3149 return r;
3150 if (r > 0) {
3151 found = true;
3152 break;
3153 }
3154 }
3155
3156 if (found)
3157 continue;
3158
3159 /* Check if this is really a valid string containing no NUL byte */
3160 if (memchr(o->field.payload, 0, sz))
3161 return -EBADMSG;
3162
3163 if (sz > j->data_threshold)
3164 sz = j->data_threshold;
3165
3166 if (!GREEDY_REALLOC(j->fields_buffer, j->fields_buffer_allocated, sz + 1))
3167 return -ENOMEM;
3168
3169 memcpy(j->fields_buffer, o->field.payload, sz);
3170 j->fields_buffer[sz] = 0;
3171
3172 if (!field_is_valid(j->fields_buffer))
3173 return -EBADMSG;
3174
3175 *field = j->fields_buffer;
3176 return 1;
3177 }
3178}
3179
3180_public_ void sd_journal_restart_fields(sd_journal *j) {
3181 if (!j)
3182 return;
3183
3184 j->fields_file = NULL;
3185 j->fields_hash_table_index = 0;
3186 j->fields_offset = 0;
3187 j->fields_file_lost = false;
3188}
3189
85210bff 3190_public_ int sd_journal_reliable_fd(sd_journal *j) {
1ae464e0
TA
3191 assert_return(j, -EINVAL);
3192 assert_return(!journal_pid_changed(j), -ECHILD);
85210bff
LP
3193
3194 return !j->on_network;
3195}
d4205751
LP
3196
3197static char *lookup_field(const char *field, void *userdata) {
3198 sd_journal *j = userdata;
3199 const void *data;
3200 size_t size, d;
3201 int r;
3202
3203 assert(field);
3204 assert(j);
3205
3206 r = sd_journal_get_data(j, field, &data, &size);
3207 if (r < 0 ||
3208 size > REPLACE_VAR_MAX)
3209 return strdup(field);
3210
3211 d = strlen(field) + 1;
3212
3213 return strndup((const char*) data + d, size - d);
3214}
3215
3216_public_ int sd_journal_get_catalog(sd_journal *j, char **ret) {
3217 const void *data;
3218 size_t size;
3219 sd_id128_t id;
3220 _cleanup_free_ char *text = NULL, *cid = NULL;
3221 char *t;
3222 int r;
3223
1ae464e0
TA
3224 assert_return(j, -EINVAL);
3225 assert_return(!journal_pid_changed(j), -ECHILD);
3226 assert_return(ret, -EINVAL);
d4205751
LP
3227
3228 r = sd_journal_get_data(j, "MESSAGE_ID", &data, &size);
3229 if (r < 0)
3230 return r;
3231
3232 cid = strndup((const char*) data + 11, size - 11);
3233 if (!cid)
3234 return -ENOMEM;
3235
3236 r = sd_id128_from_string(cid, &id);
3237 if (r < 0)
3238 return r;
3239
844ec79b 3240 r = catalog_get(CATALOG_DATABASE, id, &text);
d4205751
LP
3241 if (r < 0)
3242 return r;
3243
3244 t = replace_var(text, lookup_field, j);
3245 if (!t)
3246 return -ENOMEM;
3247
3248 *ret = t;
3249 return 0;
3250}
8f1e860f
LP
3251
3252_public_ int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **ret) {
1ae464e0 3253 assert_return(ret, -EINVAL);
8f1e860f 3254
844ec79b 3255 return catalog_get(CATALOG_DATABASE, id, ret);
8f1e860f 3256}
93b73b06
LP
3257
3258_public_ int sd_journal_set_data_threshold(sd_journal *j, size_t sz) {
1ae464e0
TA
3259 assert_return(j, -EINVAL);
3260 assert_return(!journal_pid_changed(j), -ECHILD);
93b73b06
LP
3261
3262 j->data_threshold = sz;
3263 return 0;
3264}
3265
3266_public_ int sd_journal_get_data_threshold(sd_journal *j, size_t *sz) {
1ae464e0
TA
3267 assert_return(j, -EINVAL);
3268 assert_return(!journal_pid_changed(j), -ECHILD);
3269 assert_return(sz, -EINVAL);
93b73b06
LP
3270
3271 *sz = j->data_threshold;
3272 return 0;
3273}
39fd5b08
JS
3274
3275_public_ int sd_journal_has_runtime_files(sd_journal *j) {
3276 assert_return(j, -EINVAL);
3277
3278 return j->has_runtime_files;
3279}
3280
3281_public_ int sd_journal_has_persistent_files(sd_journal *j) {
3282 assert_return(j, -EINVAL);
3283
3284 return j->has_persistent_files;
3285}