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