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