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