]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-journal/sd-journal.c
journal-upload: make --namespace=* work
[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
LP
59static int journal_file_read_tail_timestamp(sd_journal *j, JournalFile *f);
60static void journal_file_unlink_newest_by_bood_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
1ae464e0 238 assert_return(match_is_valid(data, size), -EINVAL);
1cc101f1 239
cd34b3c6
HH
240 /* level 0: AND term
241 * level 1: OR terms
242 * level 2: AND terms
243 * level 3: OR terms
244 * level 4: concrete matches */
cbdca852
LP
245
246 if (!j->level0) {
cd34b3c6 247 j->level0 = match_new(NULL, MATCH_AND_TERM);
cbdca852
LP
248 if (!j->level0)
249 return -ENOMEM;
250 }
251
252 if (!j->level1) {
cd34b3c6 253 j->level1 = match_new(j->level0, MATCH_OR_TERM);
cbdca852
LP
254 if (!j->level1)
255 return -ENOMEM;
256 }
257
cd34b3c6
HH
258 if (!j->level2) {
259 j->level2 = match_new(j->level1, MATCH_AND_TERM);
260 if (!j->level2)
261 return -ENOMEM;
262 }
263
264 assert(j->level0->type == MATCH_AND_TERM);
265 assert(j->level1->type == MATCH_OR_TERM);
266 assert(j->level2->type == MATCH_AND_TERM);
ab4979d2 267
4ce534f4
LP
268 /* Old-style Jenkins (unkeyed) hashing only here. We do not cover new-style siphash (keyed) hashing
269 * here, since it's different for each file, and thus can't be pre-calculated in the Match object. */
20b0acfa 270 hash = jenkins_hash64(data, size);
de190aef 271
cd34b3c6
HH
272 LIST_FOREACH(matches, l3, j->level2->matches) {
273 assert(l3->type == MATCH_OR_TERM);
de190aef 274
cd34b3c6
HH
275 LIST_FOREACH(matches, l4, l3->matches) {
276 assert(l4->type == MATCH_DISCRETE);
de190aef 277
cbdca852
LP
278 /* Exactly the same match already? Then ignore
279 * this addition */
cde8c5f7 280 if (l4->hash == hash &&
cd34b3c6
HH
281 l4->size == size &&
282 memcmp(l4->data, data, size) == 0)
cbdca852
LP
283 return 0;
284
285 /* Same field? Then let's add this to this OR term */
cd34b3c6
HH
286 if (same_field(data, size, l4->data, l4->size)) {
287 add_here = l3;
cbdca852
LP
288 break;
289 }
290 }
291
292 if (add_here)
293 break;
de190aef
LP
294 }
295
cbdca852 296 if (!add_here) {
cd34b3c6 297 add_here = match_new(j->level2, MATCH_OR_TERM);
cbdca852
LP
298 if (!add_here)
299 goto fail;
300 }
301
302 m = match_new(add_here, MATCH_DISCRETE);
cec736d2 303 if (!m)
cbdca852 304 goto fail;
87d2c1ff 305
cde8c5f7 306 m->hash = hash;
1cc101f1 307 m->size = size;
cbdca852
LP
308 m->data = memdup(data, size);
309 if (!m->data)
310 goto fail;
311
312 detach_location(j);
313
314 return 0;
315
316fail:
418cce62 317 match_free(m);
c5a10d9c 318 match_free_if_empty(add_here);
39dfc0de
YW
319 j->level2 = match_free_if_empty(j->level2);
320 j->level1 = match_free_if_empty(j->level1);
321 j->level0 = match_free_if_empty(j->level0);
cbdca852
LP
322
323 return -ENOMEM;
324}
325
cd34b3c6 326_public_ int sd_journal_add_conjunction(sd_journal *j) {
1ae464e0 327 assert_return(j, -EINVAL);
e046719b 328 assert_return(!journal_origin_changed(j), -ECHILD);
1cc101f1 329
cbdca852
LP
330 if (!j->level0)
331 return 0;
332
333 if (!j->level1)
334 return 0;
335
336 if (!j->level1->matches)
337 return 0;
338
cd34b3c6
HH
339 j->level1 = NULL;
340 j->level2 = NULL;
341
342 return 0;
343}
344
345_public_ int sd_journal_add_disjunction(sd_journal *j) {
1ae464e0 346 assert_return(j, -EINVAL);
e046719b 347 assert_return(!journal_origin_changed(j), -ECHILD);
cd34b3c6
HH
348
349 if (!j->level0)
350 return 0;
351
352 if (!j->level1)
353 return 0;
354
355 if (!j->level2)
356 return 0;
357
358 if (!j->level2->matches)
359 return 0;
cbdca852 360
cd34b3c6 361 j->level2 = NULL;
cbdca852
LP
362 return 0;
363}
364
365static char *match_make_string(Match *m) {
6320409c 366 _cleanup_free_ char *p = NULL;
cbdca852
LP
367 bool enclose = false;
368
369 if (!m)
4ad16808 370 return strdup("none");
cbdca852
LP
371
372 if (m->type == MATCH_DISCRETE)
9e8b1ec0 373 return cescape_length(m->data, m->size);
cbdca852 374
cbdca852 375 LIST_FOREACH(matches, i, m->matches) {
6320409c 376 _cleanup_free_ char *t = NULL;
cbdca852
LP
377
378 t = match_make_string(i);
6b430fdb 379 if (!t)
6320409c 380 return NULL;
cbdca852
LP
381
382 if (p) {
6320409c 383 if (!strextend(&p, m->type == MATCH_OR_TERM ? " OR " : " AND ", t))
cbdca852
LP
384 return NULL;
385
cbdca852 386 enclose = true;
bc302926 387 } else
6320409c 388 p = TAKE_PTR(t);
87d2c1ff
LP
389 }
390
6320409c
LP
391 if (enclose)
392 return strjoin("(", p, ")");
87d2c1ff 393
6320409c 394 return TAKE_PTR(p);
cbdca852 395}
de7b95cd 396
cbdca852
LP
397char *journal_make_match_string(sd_journal *j) {
398 assert(j);
8f9b6cd9 399
cbdca852 400 return match_make_string(j->level0);
87d2c1ff
LP
401}
402
a5344d2c 403_public_ void sd_journal_flush_matches(sd_journal *j) {
e046719b 404 if (!j || journal_origin_changed(j))
a5344d2c 405 return;
87d2c1ff 406
cbdca852
LP
407 if (j->level0)
408 match_free(j->level0);
de7b95cd 409
cd34b3c6 410 j->level0 = j->level1 = j->level2 = NULL;
8f9b6cd9 411
de190aef 412 detach_location(j);
87d2c1ff
LP
413}
414
262299dc
LP
415static int journal_file_find_newest_for_boot_id(
416 sd_journal *j,
417 sd_id128_t id,
418 JournalFile **ret) {
419
420 JournalFile *prev = NULL;
421 int r;
422
423 assert(j);
424 assert(ret);
425
426 /* Before we use it, let's refresh the timestamp from the header, and reshuffle our prioq
427 * accordingly. We do this only a bunch of times, to not be caught in some update loop. */
428 for (unsigned n_tries = 0;; n_tries++) {
429 JournalFile *f;
430 Prioq *q;
431
432 q = hashmap_get(j->newest_by_boot_id, &id);
433 if (!q)
434 return log_debug_errno(SYNTHETIC_ERRNO(ENODATA),
435 "Requested delta for boot ID %s, but we have no information about that boot ID.", SD_ID128_TO_STRING(id));
436
437 assert_se(f = prioq_peek(q)); /* we delete hashmap entries once the prioq is empty, so this must hold */
438
439 if (f == prev || n_tries >= 5) {
440 /* This was already the best answer in the previous run, or we tried too often, use it */
441 *ret = f;
442 return 0;
443 }
444
445 prev = f;
446
447 /* Let's read the journal file's current timestamp once, before we return it, maybe it has changed. */
448 r = journal_file_read_tail_timestamp(j, f);
449 if (r < 0)
450 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));
451
452 /* Refreshing the timestamp we read might have reshuffled the prioq, hence let's check the
453 * prioq again and only use the the information once we reached an equilibrium or hit a
454 * limit */
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)
7a4ee861 664 return direction == DIRECTION_DOWN ? journal_file_next_entry_for_data(f, d, DIRECTION_DOWN, ret, offset) : 0;
cbdca852 665 if (j->current_location.type == LOCATION_TAIL)
7a4ee861 666 return direction == DIRECTION_UP ? journal_file_next_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);
cbdca852
LP
671 if (r != -ENOENT)
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
ec50313d 682 return journal_file_next_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);
766 if (r != -ENOENT)
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
abcdc02c 935 for (unsigned i = 0; i < n_files; i++) {
5d4ba7f2 936 JournalFile *f = (JournalFile *)files[i];
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
34af7494 1573 journal_file_unlink_newest_by_bood_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
1aaa68f5 2180#define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
10752e82
ZJS
2181 (SD_JOURNAL_OS_ROOT | \
2182 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1aaa68f5 2183
5d1ce257 2184_public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) {
17c9aff8 2185 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
5d1ce257
LP
2186 struct stat st;
2187 int r;
2188
2189 assert_return(ret, -EINVAL);
2190 assert_return(fd >= 0, -EBADF);
1aaa68f5 2191 assert_return((flags & ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS) == 0, -EINVAL);
5d1ce257
LP
2192
2193 if (fstat(fd, &st) < 0)
2194 return -errno;
2195
2196 if (!S_ISDIR(st.st_mode))
2197 return -EBADFD;
2198
456aa879 2199 j = journal_new(flags, NULL, NULL);
5d1ce257
LP
2200 if (!j)
2201 return -ENOMEM;
2202
2203 j->toplevel_fd = fd;
2204
d077390c
LP
2205 if (flags & SD_JOURNAL_OS_ROOT)
2206 r = add_search_paths(j);
2207 else
2208 r = add_root_directory(j, NULL, false);
5d1ce257 2209 if (r < 0)
17c9aff8 2210 return r;
5d1ce257 2211
17c9aff8 2212 *ret = TAKE_PTR(j);
5d1ce257 2213 return 0;
5d1ce257
LP
2214}
2215
2216_public_ int sd_journal_open_files_fd(sd_journal **ret, int fds[], unsigned n_fds, int flags) {
5d1ce257 2217 JournalFile *f;
17c9aff8 2218 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
5d1ce257
LP
2219 int r;
2220
2221 assert_return(ret, -EINVAL);
2222 assert_return(n_fds > 0, -EBADF);
2223 assert_return(flags == 0, -EINVAL);
2224
456aa879 2225 j = journal_new(flags, NULL, NULL);
5d1ce257
LP
2226 if (!j)
2227 return -ENOMEM;
2228
abcdc02c 2229 for (unsigned i = 0; i < n_fds; i++) {
5d1ce257
LP
2230 struct stat st;
2231
2232 if (fds[i] < 0) {
2233 r = -EBADF;
2234 goto fail;
2235 }
2236
2237 if (fstat(fds[i], &st) < 0) {
2238 r = -errno;
2239 goto fail;
2240 }
2241
3cc44114
LP
2242 r = stat_verify_regular(&st);
2243 if (r < 0)
5d1ce257 2244 goto fail;
5d1ce257
LP
2245
2246 r = add_any_file(j, fds[i], NULL);
2247 if (r < 0)
2248 goto fail;
2249 }
2250
2251 j->no_new_files = true;
2252 j->no_inotify = true;
5302ebe1 2253
17c9aff8 2254 *ret = TAKE_PTR(j);
5d1ce257
LP
2255 return 0;
2256
2257fail:
f8e2f4d6 2258 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
5d1ce257 2259 * remain open */
90e74a66 2260 ORDERED_HASHMAP_FOREACH(f, j->files)
5d1ce257
LP
2261 f->close_fd = false;
2262
5302ebe1
ZJS
2263 return r;
2264}
2265
a5344d2c 2266_public_ void sd_journal_close(sd_journal *j) {
a963990f 2267 Directory *d;
34af7494 2268 Prioq *p;
a963990f 2269
e046719b 2270 if (!j || journal_origin_changed(j))
a5344d2c 2271 return;
87d2c1ff 2272
34af7494
LP
2273 while ((p = hashmap_first(j->newest_by_boot_id)))
2274 journal_file_unlink_newest_by_bood_id(j, prioq_peek(p));
2275 hashmap_free(j->newest_by_boot_id);
2276
54b1da83
LP
2277 sd_journal_flush_matches(j);
2278
f9168190 2279 ordered_hashmap_free_with_destructor(j->files, journal_file_close);
5d4ba7f2 2280 iterated_cache_free(j->files_cache);
260a2be4 2281
a963990f
LP
2282 while ((d = hashmap_first(j->directories_by_path)))
2283 remove_directory(j, d);
260a2be4 2284
a963990f
LP
2285 while ((d = hashmap_first(j->directories_by_wd)))
2286 remove_directory(j, d);
87d2c1ff 2287
a963990f
LP
2288 hashmap_free(j->directories_by_path);
2289 hashmap_free(j->directories_by_wd);
1cc101f1 2290
03e334a1 2291 safe_close(j->inotify_fd);
50f20cfd 2292
bf807d4d 2293 if (j->mmap) {
3a595c59 2294 mmap_cache_stats_log_debug(j->mmap);
16e9f408 2295 mmap_cache_unref(j->mmap);
bf807d4d 2296 }
16e9f408 2297
ec1d2909 2298 hashmap_free_free(j->errors);
5768d259 2299
7827b1a1 2300 free(j->path);
89739579 2301 free(j->prefix);
456aa879 2302 free(j->namespace);
3c1668da 2303 free(j->unique_field);
eb86030e 2304 free(j->fields_buffer);
87d2c1ff
LP
2305 free(j);
2306}
3fbf9cbb 2307
34af7494
LP
2308static void journal_file_unlink_newest_by_bood_id(sd_journal *j, JournalFile *f) {
2309 JournalFile *nf;
2310 Prioq *p;
2311
2312 assert(j);
2313 assert(f);
2314
2315 if (f->newest_boot_id_prioq_idx == PRIOQ_IDX_NULL) /* not linked currently, hence this is a NOP */
2316 return;
2317
2318 assert_se(p = hashmap_get(j->newest_by_boot_id, &f->newest_boot_id));
2319 assert_se(prioq_remove(p, f, &f->newest_boot_id_prioq_idx) > 0);
2320
2321 nf = prioq_peek(p);
2322 if (nf)
2323 /* There's still a member in the prioq? Then make sure the hashmap key now points to its
2324 * .newest_boot_id field (and not ours!). Not we only replace the memory of the key here, the
2325 * value of the key (and the data associated with it) remain the same. */
e28d8251 2326 assert_se(hashmap_replace(j->newest_by_boot_id, &nf->newest_boot_id, p) >= 0);
34af7494
LP
2327 else {
2328 assert_se(hashmap_remove(j->newest_by_boot_id, &f->newest_boot_id) == p);
2329 prioq_free(p);
2330 }
2331
2332 f->newest_boot_id_prioq_idx = PRIOQ_IDX_NULL;
2333}
2334
2335static int journal_file_newest_monotonic_compare(const void *a, const void *b) {
2336 const JournalFile *x = a, *y = b;
2337
2338 return -CMP(x->newest_monotonic_usec, y->newest_monotonic_usec); /* Invert order, we want newest first! */
2339}
2340
2341static int journal_file_reshuffle_newest_by_boot_id(sd_journal *j, JournalFile *f) {
2342 Prioq *p;
2343 int r;
2344
2345 assert(j);
2346 assert(f);
2347
2348 p = hashmap_get(j->newest_by_boot_id, &f->newest_boot_id);
2349 if (p) {
2350 /* There's already a priority queue for this boot ID */
2351
2352 if (f->newest_boot_id_prioq_idx == PRIOQ_IDX_NULL) {
2353 r = prioq_put(p, f, &f->newest_boot_id_prioq_idx); /* Insert if we aren't in there yet */
2354 if (r < 0)
2355 return r;
2356 } else
2357 prioq_reshuffle(p, f, &f->newest_boot_id_prioq_idx); /* Reshuffle otherwise */
2358
2359 } else {
2360 _cleanup_(prioq_freep) Prioq *q = NULL;
2361
2362 /* No priority queue yet, then allocate one */
2363
2364 assert(f->newest_boot_id_prioq_idx == PRIOQ_IDX_NULL); /* we can't be a member either */
2365
2366 q = prioq_new(journal_file_newest_monotonic_compare);
2367 if (!q)
2368 return -ENOMEM;
2369
2370 r = prioq_put(q, f, &f->newest_boot_id_prioq_idx);
2371 if (r < 0)
2372 return r;
2373
2374 r = hashmap_ensure_put(&j->newest_by_boot_id, &id128_hash_ops, &f->newest_boot_id, q);
f11de491
YW
2375 if (r < 0) {
2376 f->newest_boot_id_prioq_idx = PRIOQ_IDX_NULL;
34af7494 2377 return r;
f11de491 2378 }
34af7494
LP
2379
2380 TAKE_PTR(q);
2381 }
2382
2383 return 0;
2384}
2385
2386static int journal_file_read_tail_timestamp(sd_journal *j, JournalFile *f) {
2387 uint64_t offset, mo, rt;
2388 sd_id128_t id;
2389 ObjectType type;
2390 Object *o;
2391 int r;
2392
2393 assert(j);
2394 assert(f);
2395 assert(f->header);
2396
2397 /* Tries to read the timestamp of the most recently written entry. */
2398
2399 r = journal_file_fstat(f);
2400 if (r < 0)
2401 return r;
2402 if (f->newest_mtime == timespec_load(&f->last_stat.st_mtim))
2403 return 0; /* mtime didn't change since last time, don't bother */
2404
2405 if (JOURNAL_HEADER_CONTAINS(f->header, tail_entry_offset)) {
2406 offset = le64toh(READ_NOW(f->header->tail_entry_offset));
2407 type = OBJECT_ENTRY;
2408 } else {
2409 offset = le64toh(READ_NOW(f->header->tail_object_offset));
2410 type = OBJECT_UNUSED;
2411 }
2412 if (offset == 0)
2413 return -ENODATA; /* not a single object/entry, hence no tail timestamp */
2414
2415 /* Move to the last object in the journal file, in the hope it is an entry (which it usually will
2416 * be). If we lack the "tail_entry_offset" field in the header, we specify the type as OBJECT_UNUSED
2417 * here, since we cannot be sure what the last object will be, and want no noisy logging if it isn't
2418 * an entry. We instead check after figuring out the pointer. */
2419 r = journal_file_move_to_object(f, type, offset, &o);
2420 if (r < 0) {
2421 log_debug_errno(r, "Failed to move to last object in journal file, ignoring: %m");
2422 o = NULL;
2423 }
2424 if (o && o->object.type == OBJECT_ENTRY) {
2425 /* Yay, last object is an entry, let's use the data. */
2426 id = o->entry.boot_id;
2427 mo = le64toh(o->entry.monotonic);
2428 rt = le64toh(o->entry.realtime);
2429 } else {
2430 /* So the object is not an entry or we couldn't access it? In that case, let's read the most
2431 * recent entry timestamps from the header. It's equally good. Unfortunately though, in old
2432 * versions of the journal the boot ID in the header doesn't have to match the monotonic
2433 * timestamp of the header. Let's check the header flag that indicates whether this strictly
2434 * matches first hence, before using the data. */
2435
2436 if (JOURNAL_HEADER_TAIL_ENTRY_BOOT_ID(f->header) && f->header->state == STATE_ARCHIVED) {
2437 mo = le64toh(f->header->tail_entry_monotonic);
2438 rt = le64toh(f->header->tail_entry_realtime);
2439 id = f->header->tail_entry_boot_id;
2440
2441 /* Some superficial checking if what we read makes sense. Note that we only do this
2442 * when reading the timestamps from the Header object, but not when reading them from
2443 * the most recent entry object, because in that case journal_file_move_to_object()
2444 * already validated them. */
2445 if (!VALID_MONOTONIC(mo) || !VALID_REALTIME(rt))
2446 return -ENODATA;
2447
2448 } else {
2449 /* Otherwise let's find the last entry manually (this possibly means traversing the
2450 * chain of entry arrays, till the end */
2451 r = journal_file_next_entry(f, 0, DIRECTION_UP, &o, NULL);
2452 if (r < 0)
2453 return r;
3b1b0f1a
YW
2454 if (r == 0)
2455 return -ENODATA;
34af7494
LP
2456
2457 id = o->entry.boot_id;
2458 mo = le64toh(o->entry.monotonic);
2459 rt = le64toh(o->entry.realtime);
2460 }
2461 }
2462
2463 if (mo > rt) /* monotonic clock is further ahead than realtime? that's weird, refuse to use the data */
2464 return -ENODATA;
2465
2466 if (!sd_id128_equal(f->newest_boot_id, id))
2467 journal_file_unlink_newest_by_bood_id(j, f);
2468
2469 f->newest_boot_id = id;
2470 f->newest_monotonic_usec = mo;
2471 f->newest_realtime_usec = rt;
2472 f->newest_machine_id = f->header->machine_id;
2473 f->newest_mtime = timespec_load(&f->last_stat.st_mtim);
2474
2475 r = journal_file_reshuffle_newest_by_boot_id(j, f);
2476 if (r < 0)
2477 return r;
2478
2479 return 0;
2480}
2481
a5344d2c 2482_public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
3fbf9cbb 2483 JournalFile *f;
834f759c 2484 Object *o;
3fbf9cbb
LP
2485 int r;
2486
1ae464e0 2487 assert_return(j, -EINVAL);
e046719b 2488 assert_return(!journal_origin_changed(j), -ECHILD);
3fbf9cbb
LP
2489
2490 f = j->current_file;
2491 if (!f)
de190aef 2492 return -EADDRNOTAVAIL;
3fbf9cbb 2493 if (f->current_offset <= 0)
de190aef 2494 return -EADDRNOTAVAIL;
3fbf9cbb 2495
de190aef 2496 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2497 if (r < 0)
2498 return r;
2499
834f759c
LP
2500 uint64_t t = le64toh(o->entry.realtime);
2501 if (!VALID_REALTIME(t))
2502 return -EBADMSG;
2503
2504 if (ret)
2505 *ret = t;
2506
de190aef 2507 return 0;
3fbf9cbb
LP
2508}
2509
a5344d2c 2510_public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
3fbf9cbb 2511 JournalFile *f;
404803e6 2512 Object *o;
3fbf9cbb 2513 int r;
3fbf9cbb 2514
1ae464e0 2515 assert_return(j, -EINVAL);
e046719b 2516 assert_return(!journal_origin_changed(j), -ECHILD);
3fbf9cbb
LP
2517
2518 f = j->current_file;
2519 if (!f)
de190aef 2520 return -EADDRNOTAVAIL;
3fbf9cbb 2521 if (f->current_offset <= 0)
de190aef 2522 return -EADDRNOTAVAIL;
3fbf9cbb 2523
de190aef 2524 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2525 if (r < 0)
2526 return r;
2527
de190aef
LP
2528 if (ret_boot_id)
2529 *ret_boot_id = o->entry.boot_id;
2530 else {
d4739bc4
VC
2531 sd_id128_t id;
2532
de190aef
LP
2533 r = sd_id128_get_boot(&id);
2534 if (r < 0)
2535 return r;
3fbf9cbb 2536
de190aef 2537 if (!sd_id128_equal(id, o->entry.boot_id))
df50185b 2538 return -ESTALE;
de190aef 2539 }
3fbf9cbb 2540
404803e6
LP
2541 uint64_t t = le64toh(o->entry.monotonic);
2542 if (!VALID_MONOTONIC(t))
2543 return -EBADMSG;
2544
14a65d65 2545 if (ret)
404803e6 2546 *ret = t;
14a65d65 2547
de190aef 2548 return 0;
3fbf9cbb
LP
2549}
2550
b1712fab
LP
2551_public_ int sd_journal_get_seqnum(
2552 sd_journal *j,
2553 uint64_t *ret_seqnum,
2554 sd_id128_t *ret_seqnum_id) {
2555
2556 JournalFile *f;
2557 Object *o;
2558 int r;
2559
2560 assert_return(j, -EINVAL);
e046719b 2561 assert_return(!journal_origin_changed(j), -ECHILD);
b1712fab
LP
2562
2563 f = j->current_file;
2564 if (!f)
2565 return -EADDRNOTAVAIL;
2566
2567 if (f->current_offset <= 0)
2568 return -EADDRNOTAVAIL;
2569
2570 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2571 if (r < 0)
2572 return r;
2573
2574 if (ret_seqnum_id)
2575 *ret_seqnum_id = f->header->seqnum_id;
2576 if (ret_seqnum)
2577 *ret_seqnum = le64toh(o->entry.seqnum);
2578
2579 return 0;
2580}
2581
362a3f81 2582static bool field_is_valid(const char *field) {
362a3f81
LP
2583 assert(field);
2584
2585 if (isempty(field))
2586 return false;
2587
2588 if (startswith(field, "__"))
2589 return false;
2590
abcdc02c 2591 for (const char *p = field; *p; p++) {
362a3f81
LP
2592
2593 if (*p == '_')
2594 continue;
2595
2596 if (*p >= 'A' && *p <= 'Z')
2597 continue;
2598
ff25d338 2599 if (ascii_isdigit(*p))
362a3f81
LP
2600 continue;
2601
2602 return false;
2603 }
2604
2605 return true;
2606}
2607
a5344d2c 2608_public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
3fbf9cbb 2609 JournalFile *f;
3fbf9cbb 2610 size_t field_length;
3fbf9cbb 2611 Object *o;
a1640191 2612 int r;
3fbf9cbb 2613
1ae464e0 2614 assert_return(j, -EINVAL);
e046719b 2615 assert_return(!journal_origin_changed(j), -ECHILD);
1ae464e0
TA
2616 assert_return(field, -EINVAL);
2617 assert_return(data, -EINVAL);
2618 assert_return(size, -EINVAL);
2619 assert_return(field_is_valid(field), -EINVAL);
3fbf9cbb
LP
2620
2621 f = j->current_file;
2622 if (!f)
de190aef 2623 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2624
2625 if (f->current_offset <= 0)
de190aef 2626 return -EADDRNOTAVAIL;
3fbf9cbb 2627
de190aef 2628 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2629 if (r < 0)
2630 return r;
2631
2632 field_length = strlen(field);
2633
a9089a66 2634 uint64_t n = journal_file_entry_n_items(f, o);
abcdc02c 2635 for (uint64_t i = 0; i < n; i++) {
0e35afff
DDM
2636 uint64_t p;
2637 void *d;
2638 size_t l;
3fbf9cbb 2639
a9089a66 2640 p = journal_file_entry_item_object_offset(f, o, i);
0e35afff
DDM
2641 r = journal_file_data_payload(f, NULL, p, field, field_length, j->data_threshold, &d, &l);
2642 if (r == 0)
a1640191 2643 goto next;
df207ccb
DDM
2644 if (IN_SET(r, -EADDRNOTAVAIL, -EBADMSG)) {
2645 log_debug_errno(r, "Entry item %"PRIu64" data object is bad, skipping over it: %m", i);
a1640191 2646 goto next;
8a799bed 2647 }
3fbf9cbb
LP
2648 if (r < 0)
2649 return r;
2650
0e35afff
DDM
2651 *data = d;
2652 *size = l;
3fbf9cbb 2653
0e35afff 2654 return 0;
a1640191
YW
2655
2656 next:
2657 /* journal_file_data_payload() may clear or overwrite cached object. */
2658 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2659 if (r < 0)
2660 return r;
3fbf9cbb
LP
2661 }
2662
de190aef 2663 return -ENOENT;
3fbf9cbb
LP
2664}
2665
a5344d2c 2666_public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
3fbf9cbb 2667 JournalFile *f;
3fbf9cbb 2668 Object *o;
5a94a2bf 2669 int r;
3fbf9cbb 2670
1ae464e0 2671 assert_return(j, -EINVAL);
e046719b 2672 assert_return(!journal_origin_changed(j), -ECHILD);
1ae464e0
TA
2673 assert_return(data, -EINVAL);
2674 assert_return(size, -EINVAL);
3fbf9cbb
LP
2675
2676 f = j->current_file;
2677 if (!f)
de190aef 2678 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2679
2680 if (f->current_offset <= 0)
de190aef 2681 return -EADDRNOTAVAIL;
3fbf9cbb 2682
de190aef 2683 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2684 if (r < 0)
2685 return r;
2686
a9089a66 2687 for (uint64_t n = journal_file_entry_n_items(f, o); j->current_field < n; j->current_field++) {
5a94a2bf 2688 uint64_t p;
0e35afff
DDM
2689 void *d;
2690 size_t l;
3fbf9cbb 2691
a9089a66 2692 p = journal_file_entry_item_object_offset(f, o, j->current_field);
0e35afff 2693 r = journal_file_data_payload(f, NULL, p, NULL, 0, j->data_threshold, &d, &l);
df207ccb
DDM
2694 if (IN_SET(r, -EADDRNOTAVAIL, -EBADMSG)) {
2695 log_debug_errno(r, "Entry item %"PRIu64" data object is bad, skipping over it: %m", j->current_field);
a1640191 2696 goto next;
5a94a2bf
DDM
2697 }
2698 if (r < 0)
2699 return r;
0e35afff 2700 assert(r > 0);
3fbf9cbb 2701
0e35afff
DDM
2702 *data = d;
2703 *size = l;
3fbf9cbb 2704
5a94a2bf 2705 j->current_field++;
3fbf9cbb 2706
5a94a2bf 2707 return 1;
a1640191
YW
2708
2709 next:
2710 /* journal_file_data_payload() may clear or overwrite cached object. */
2711 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2712 if (r < 0)
2713 return r;
5a94a2bf
DDM
2714 }
2715
2716 return 0;
3fbf9cbb 2717}
c2373f84 2718
76cbafcd
ZJS
2719_public_ int sd_journal_enumerate_available_data(sd_journal *j, const void **data, size_t *size) {
2720 for (;;) {
2721 int r;
2722
2723 r = sd_journal_enumerate_data(j, data, size);
2724 if (r >= 0)
2725 return r;
2726 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r))
2727 return r;
2728 j->current_field++; /* Try with the next field */
2729 }
2730}
2731
a5344d2c 2732_public_ void sd_journal_restart_data(sd_journal *j) {
e046719b 2733 if (!j || journal_origin_changed(j))
a5344d2c 2734 return;
8725d60a
LP
2735
2736 j->current_field = 0;
c2373f84 2737}
50f20cfd 2738
858749f7
LP
2739static int reiterate_all_paths(sd_journal *j) {
2740 assert(j);
2741
2742 if (j->no_new_files)
2743 return add_current_paths(j);
2744
2745 if (j->flags & SD_JOURNAL_OS_ROOT)
2746 return add_search_paths(j);
2747
2748 if (j->toplevel_fd >= 0)
2749 return add_root_directory(j, NULL, false);
2750
2751 if (j->path)
2752 return add_root_directory(j, j->path, true);
2753
2754 return add_search_paths(j);
2755}
2756
a5344d2c 2757_public_ int sd_journal_get_fd(sd_journal *j) {
a963990f
LP
2758 int r;
2759
1ae464e0 2760 assert_return(j, -EINVAL);
e046719b 2761 assert_return(!journal_origin_changed(j), -ECHILD);
50f20cfd 2762
5d1ce257
LP
2763 if (j->no_inotify)
2764 return -EMEDIUMTYPE;
2765
a963990f
LP
2766 if (j->inotify_fd >= 0)
2767 return j->inotify_fd;
2768
2769 r = allocate_inotify(j);
2770 if (r < 0)
2771 return r;
2772
858749f7 2773 log_debug("Reiterating files to get inotify watches established.");
5d1ce257 2774
858749f7
LP
2775 /* Iterate through all dirs again, to add them to the inotify */
2776 r = reiterate_all_paths(j);
a963990f
LP
2777 if (r < 0)
2778 return r;
2779
50f20cfd
LP
2780 return j->inotify_fd;
2781}
2782
ee531d94
LP
2783_public_ int sd_journal_get_events(sd_journal *j) {
2784 int fd;
2785
1ae464e0 2786 assert_return(j, -EINVAL);
e046719b 2787 assert_return(!journal_origin_changed(j), -ECHILD);
ee531d94
LP
2788
2789 fd = sd_journal_get_fd(j);
2790 if (fd < 0)
2791 return fd;
2792
2793 return POLLIN;
2794}
2795
39c155ea
LP
2796_public_ int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec) {
2797 int fd;
2798
1ae464e0 2799 assert_return(j, -EINVAL);
e046719b 2800 assert_return(!journal_origin_changed(j), -ECHILD);
1ae464e0 2801 assert_return(timeout_usec, -EINVAL);
39c155ea
LP
2802
2803 fd = sd_journal_get_fd(j);
2804 if (fd < 0)
2805 return fd;
2806
2807 if (!j->on_network) {
f5fbe71d 2808 *timeout_usec = UINT64_MAX;
39c155ea
LP
2809 return 0;
2810 }
2811
2812 /* If we are on the network we need to regularly check for
2813 * changes manually */
2814
2815 *timeout_usec = j->last_process_usec + JOURNAL_FILES_RECHECK_USEC;
2816 return 1;
2817}
2818
858749f7
LP
2819static void process_q_overflow(sd_journal *j) {
2820 JournalFile *f;
2821 Directory *m;
858749f7
LP
2822
2823 assert(j);
2824
2825 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2826 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2827 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2828 * are subject for unloading. */
2829
2830 log_debug("Inotify queue overrun, reiterating everything.");
2831
2832 j->generation++;
2833 (void) reiterate_all_paths(j);
2834
90e74a66 2835 ORDERED_HASHMAP_FOREACH(f, j->files) {
858749f7
LP
2836
2837 if (f->last_seen_generation == j->generation)
2838 continue;
2839
2840 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f->path);
2841 remove_file_real(j, f);
2842 }
2843
90e74a66 2844 HASHMAP_FOREACH(m, j->directories_by_path) {
858749f7
LP
2845
2846 if (m->last_seen_generation == j->generation)
2847 continue;
2848
2849 if (m->is_root) /* Never GC root directories */
2850 continue;
2851
2852 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f->path);
2853 remove_directory(j, m);
2854 }
2855
2856 log_debug("Reiteration complete.");
2857}
2858
31e99dd2 2859static void process_inotify_event(sd_journal *j, const struct inotify_event *e) {
a963990f 2860 Directory *d;
50f20cfd
LP
2861
2862 assert(j);
2863 assert(e);
2864
858749f7
LP
2865 if (e->mask & IN_Q_OVERFLOW) {
2866 process_q_overflow(j);
2867 return;
2868 }
2869
50f20cfd 2870 /* Is this a subdirectory we watch? */
a963990f
LP
2871 d = hashmap_get(j->directories_by_wd, INT_TO_PTR(e->wd));
2872 if (d) {
de2c3907
LP
2873 if (!(e->mask & IN_ISDIR) && e->len > 0 &&
2874 (endswith(e->name, ".journal") ||
2875 endswith(e->name, ".journal~"))) {
50f20cfd
LP
2876
2877 /* Event for a journal file */
2878
d617408e 2879 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
fc1813c0 2880 (void) add_file_by_name(j, d->path, e->name);
d617408e 2881 else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT))
8e7e4a73 2882 (void) remove_file_by_name(j, d->path, e->name);
50f20cfd 2883
a963990f 2884 } else if (!d->is_root && e->len == 0) {
50f20cfd 2885
a963990f 2886 /* Event for a subdirectory */
50f20cfd 2887
b2b46f91
TA
2888 if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT))
2889 remove_directory(j, d);
50f20cfd 2890
a9be0692 2891 } else if (d->is_root && (e->mask & IN_ISDIR) && e->len > 0 && id128_is_valid(e->name)) {
50f20cfd 2892
a963990f 2893 /* Event for root directory */
50f20cfd 2894
d617408e
LP
2895 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
2896 (void) add_directory(j, d->path, e->name);
50f20cfd
LP
2897 }
2898
2899 return;
2900 }
2901
2902 if (e->mask & IN_IGNORED)
2903 return;
2904
a9be0692 2905 log_debug("Unexpected inotify event.");
50f20cfd
LP
2906}
2907
a963990f
LP
2908static int determine_change(sd_journal *j) {
2909 bool b;
2910
2911 assert(j);
2912
2913 b = j->current_invalidate_counter != j->last_invalidate_counter;
2914 j->last_invalidate_counter = j->current_invalidate_counter;
2915
2916 return b ? SD_JOURNAL_INVALIDATE : SD_JOURNAL_APPEND;
2917}
2918
a5344d2c 2919_public_ int sd_journal_process(sd_journal *j) {
a963990f 2920 bool got_something = false;
50f20cfd 2921
1ae464e0 2922 assert_return(j, -EINVAL);
e046719b 2923 assert_return(!journal_origin_changed(j), -ECHILD);
50f20cfd 2924
10c4d640
LP
2925 if (j->inotify_fd < 0) /* We have no inotify fd yet? Then there's noting to process. */
2926 return 0;
2927
39c155ea 2928 j->last_process_usec = now(CLOCK_MONOTONIC);
f9346444 2929 j->last_invalidate_counter = j->current_invalidate_counter;
39c155ea 2930
50f20cfd 2931 for (;;) {
0254e944 2932 union inotify_event_buffer buffer;
50f20cfd
LP
2933 ssize_t l;
2934
0254e944 2935 l = read(j->inotify_fd, &buffer, sizeof(buffer));
50f20cfd 2936 if (l < 0) {
8add30a0 2937 if (ERRNO_IS_TRANSIENT(errno))
a963990f 2938 return got_something ? determine_change(j) : SD_JOURNAL_NOP;
50f20cfd
LP
2939
2940 return -errno;
2941 }
2942
a963990f
LP
2943 got_something = true;
2944
f7c1ad4f 2945 FOREACH_INOTIFY_EVENT(e, buffer, l)
50f20cfd 2946 process_inotify_event(j, e);
50f20cfd
LP
2947 }
2948}
6ad1d1c3 2949
e02d1cf7 2950_public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
a963990f 2951 int r;
39c155ea 2952 uint64_t t;
e02d1cf7 2953
1ae464e0 2954 assert_return(j, -EINVAL);
e046719b 2955 assert_return(!journal_origin_changed(j), -ECHILD);
e02d1cf7 2956
a963990f 2957 if (j->inotify_fd < 0) {
28ca867a 2958 JournalFile *f;
a963990f 2959
9eba03c7 2960 /* This is the first invocation, hence create the inotify watch */
a963990f
LP
2961 r = sd_journal_get_fd(j);
2962 if (r < 0)
2963 return r;
2964
9eba03c7
LP
2965 /* Server might have done some vacuuming while we weren't watching. Get rid of the deleted
2966 * files now so they don't stay around indefinitely. */
90e74a66 2967 ORDERED_HASHMAP_FOREACH(f, j->files) {
28ca867a 2968 r = journal_file_fstat(f);
8581b9f9
MS
2969 if (r == -EIDRM)
2970 remove_file_real(j, f);
9eba03c7
LP
2971 else if (r < 0)
2972 log_debug_errno(r, "Failed to fstat() journal file '%s', ignoring: %m", f->path);
28ca867a
MS
2973 }
2974
9eba03c7
LP
2975 /* The journal might have changed since the context object was created and we weren't
2976 * watching before, hence don't wait for anything, and return immediately. */
a963990f
LP
2977 return determine_change(j);
2978 }
2979
39c155ea
LP
2980 r = sd_journal_get_timeout(j, &t);
2981 if (r < 0)
2982 return r;
2983
f5fbe71d 2984 if (t != UINT64_MAX) {
496db330 2985 t = usec_sub_unsigned(t, now(CLOCK_MONOTONIC));
85210bff 2986
f5fbe71d 2987 if (timeout_usec == UINT64_MAX || timeout_usec > t)
39c155ea 2988 timeout_usec = t;
85210bff
LP
2989 }
2990
a963990f
LP
2991 do {
2992 r = fd_wait_for_event(j->inotify_fd, POLLIN, timeout_usec);
2993 } while (r == -EINTR);
e02d1cf7
LP
2994
2995 if (r < 0)
2996 return r;
2997
a963990f 2998 return sd_journal_process(j);
e02d1cf7
LP
2999}
3000
08984293 3001_public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to) {
08984293
LP
3002 JournalFile *f;
3003 bool first = true;
581483bf 3004 uint64_t fmin = 0, tmax = 0;
08984293
LP
3005 int r;
3006
1ae464e0 3007 assert_return(j, -EINVAL);
e046719b 3008 assert_return(!journal_origin_changed(j), -ECHILD);
1ae464e0
TA
3009 assert_return(from || to, -EINVAL);
3010 assert_return(from != to, -EINVAL);
08984293 3011
90e74a66 3012 ORDERED_HASHMAP_FOREACH(f, j->files) {
08984293
LP
3013 usec_t fr, t;
3014
3015 r = journal_file_get_cutoff_realtime_usec(f, &fr, &t);
9f8d2983
LP
3016 if (r == -ENOENT)
3017 continue;
08984293
LP
3018 if (r < 0)
3019 return r;
3020 if (r == 0)
3021 continue;
3022
3023 if (first) {
581483bf
LP
3024 fmin = fr;
3025 tmax = t;
08984293
LP
3026 first = false;
3027 } else {
581483bf
LP
3028 fmin = MIN(fr, fmin);
3029 tmax = MAX(t, tmax);
08984293
LP
3030 }
3031 }
3032
581483bf
LP
3033 if (from)
3034 *from = fmin;
3035 if (to)
3036 *to = tmax;
3037
08984293
LP
3038 return first ? 0 : 1;
3039}
3040
f4cb1bfd
LP
3041_public_ int sd_journal_get_cutoff_monotonic_usec(
3042 sd_journal *j,
3043 sd_id128_t boot_id,
3044 uint64_t *ret_from,
3045 uint64_t *ret_to) {
3046
3047 uint64_t from = UINT64_MAX, to = UINT64_MAX;
1651e2c6 3048 bool found = false;
f4cb1bfd 3049 JournalFile *f;
08984293
LP
3050 int r;
3051
1ae464e0 3052 assert_return(j, -EINVAL);
e046719b 3053 assert_return(!journal_origin_changed(j), -ECHILD);
f4cb1bfd 3054 assert_return(ret_from != ret_to, -EINVAL);
08984293 3055
90e74a66 3056 ORDERED_HASHMAP_FOREACH(f, j->files) {
f4cb1bfd 3057 usec_t ff, tt;
08984293 3058
f4cb1bfd 3059 r = journal_file_get_cutoff_monotonic_usec(f, boot_id, &ff, &tt);
9f8d2983
LP
3060 if (r == -ENOENT)
3061 continue;
08984293
LP
3062 if (r < 0)
3063 return r;
3064 if (r == 0)
3065 continue;
3066
1651e2c6 3067 if (found) {
f4cb1bfd
LP
3068 from = MIN(ff, from);
3069 to = MAX(tt, to);
08984293 3070 } else {
f4cb1bfd
LP
3071 from = ff;
3072 to = tt;
1651e2c6 3073 found = true;
08984293
LP
3074 }
3075 }
3076
f4cb1bfd
LP
3077 if (ret_from)
3078 *ret_from = from;
3079 if (ret_to)
3080 *ret_to = to;
3081
1651e2c6 3082 return found;
08984293
LP
3083}
3084
dca6219e 3085void journal_print_header(sd_journal *j) {
dca6219e
LP
3086 JournalFile *f;
3087 bool newline = false;
3088
3089 assert(j);
3090
90e74a66 3091 ORDERED_HASHMAP_FOREACH(f, j->files) {
dca6219e
LP
3092 if (newline)
3093 putchar('\n');
3094 else
3095 newline = true;
3096
3097 journal_file_print_header(f);
3098 }
3099}
08984293 3100
d8671b1c 3101_public_ int sd_journal_get_usage(sd_journal *j, uint64_t *ret) {
a1a03e30
LP
3102 JournalFile *f;
3103 uint64_t sum = 0;
3104
1ae464e0 3105 assert_return(j, -EINVAL);
e046719b 3106 assert_return(!journal_origin_changed(j), -ECHILD);
d8671b1c 3107 assert_return(ret, -EINVAL);
a1a03e30 3108
90e74a66 3109 ORDERED_HASHMAP_FOREACH(f, j->files) {
a1a03e30 3110 struct stat st;
d8671b1c 3111 uint64_t b;
a1a03e30
LP
3112
3113 if (fstat(f->fd, &st) < 0)
3114 return -errno;
3115
d8671b1c
LP
3116 b = (uint64_t) st.st_blocks;
3117 if (b > UINT64_MAX / 512)
3118 return -EOVERFLOW;
3119 b *= 512;
3120
3121 if (sum > UINT64_MAX - b)
3122 return -EOVERFLOW;
3123 sum += b;
a1a03e30
LP
3124 }
3125
d8671b1c 3126 *ret = sum;
a1a03e30
LP
3127 return 0;
3128}
3129
3c1668da 3130_public_ int sd_journal_query_unique(sd_journal *j, const char *field) {
900952ec 3131 int r;
3c1668da 3132
1ae464e0 3133 assert_return(j, -EINVAL);
e046719b 3134 assert_return(!journal_origin_changed(j), -ECHILD);
1ae464e0
TA
3135 assert_return(!isempty(field), -EINVAL);
3136 assert_return(field_is_valid(field), -EINVAL);
3c1668da 3137
900952ec
LP
3138 r = free_and_strdup(&j->unique_field, field);
3139 if (r < 0)
3140 return r;
3c1668da 3141
3c1668da
LP
3142 j->unique_file = NULL;
3143 j->unique_offset = 0;
360af4cf 3144 j->unique_file_lost = false;
3c1668da
LP
3145
3146 return 0;
3147}
3148
0e0b0529
LP
3149_public_ int sd_journal_enumerate_unique(
3150 sd_journal *j,
3151 const void **ret_data,
3152 size_t *ret_size) {
3153
3c1668da 3154 size_t k;
19a2bd80 3155
1ae464e0 3156 assert_return(j, -EINVAL);
e046719b 3157 assert_return(!journal_origin_changed(j), -ECHILD);
1ae464e0 3158 assert_return(j->unique_field, -EINVAL);
19a2bd80 3159
3c1668da 3160 k = strlen(j->unique_field);
19a2bd80 3161
3c1668da 3162 if (!j->unique_file) {
360af4cf
ZJS
3163 if (j->unique_file_lost)
3164 return 0;
3165
c1f906bd 3166 j->unique_file = ordered_hashmap_first(j->files);
3c1668da
LP
3167 if (!j->unique_file)
3168 return 0;
360af4cf 3169
3c1668da
LP
3170 j->unique_offset = 0;
3171 }
19a2bd80 3172
3c1668da
LP
3173 for (;;) {
3174 JournalFile *of;
ae97089d 3175 Object *o;
0e35afff 3176 void *odata;
3c1668da
LP
3177 size_t ol;
3178 bool found;
ae97089d 3179 int r;
3c1668da 3180
bdc02927 3181 /* Proceed to next data object in the field's linked list */
3c1668da
LP
3182 if (j->unique_offset == 0) {
3183 r = journal_file_find_field_object(j->unique_file, j->unique_field, k, &o, NULL);
3184 if (r < 0)
3185 return r;
3186
3187 j->unique_offset = r > 0 ? le64toh(o->field.head_data_offset) : 0;
3188 } else {
3189 r = journal_file_move_to_object(j->unique_file, OBJECT_DATA, j->unique_offset, &o);
3190 if (r < 0)
3191 return r;
3192
3193 j->unique_offset = le64toh(o->data.next_field_offset);
3194 }
3195
3196 /* We reached the end of the list? Then start again, with the next file */
3197 if (j->unique_offset == 0) {
c1f906bd 3198 j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
360af4cf 3199 if (!j->unique_file)
3c1668da
LP
3200 return 0;
3201
3c1668da
LP
3202 continue;
3203 }
3204
d05089d8
MS
3205 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
3206 * instead, so that we can look at this data object at the same
3c1668da 3207 * time as one on another file */
d05089d8 3208 r = journal_file_move_to_object(j->unique_file, OBJECT_UNUSED, j->unique_offset, &o);
3c1668da
LP
3209 if (r < 0)
3210 return r;
3211
3212 /* Let's do the type check by hand, since we used 0 context above. */
baaa35ad
ZJS
3213 if (o->object.type != OBJECT_DATA)
3214 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
3215 "%s:offset " OFSfmt ": object has type %d, expected %d",
3216 j->unique_file->path,
3217 j->unique_offset,
3218 o->object.type, OBJECT_DATA);
ae97089d 3219
0e35afff
DDM
3220 r = journal_file_data_payload(j->unique_file, o, j->unique_offset, NULL, 0,
3221 j->data_threshold, &odata, &ol);
3c1668da
LP
3222 if (r < 0)
3223 return r;
3224
0f99f74a 3225 /* Check if we have at least the field name and "=". */
baaa35ad
ZJS
3226 if (ol <= k)
3227 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
3228 "%s:offset " OFSfmt ": object has size %zu, expected at least %zu",
3229 j->unique_file->path,
3230 j->unique_offset, ol, k + 1);
3231
0e0b0529 3232 if (memcmp(odata, j->unique_field, k) != 0 || ((const char*) odata)[k] != '=')
baaa35ad
ZJS
3233 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
3234 "%s:offset " OFSfmt ": object does not start with \"%s=\"",
3235 j->unique_file->path,
3236 j->unique_offset,
3237 j->unique_field);
0f99f74a 3238
0e0b0529
LP
3239 /* OK, now let's see if we already returned this data object by checking if it exists in the
3240 * earlier traversed files. */
3c1668da 3241 found = false;
90e74a66 3242 ORDERED_HASHMAP_FOREACH(of, j->files) {
3c1668da
LP
3243 if (of == j->unique_file)
3244 break;
3245
ed71f956
LP
3246 /* Skip this file it didn't have any fields indexed */
3247 if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
3c1668da
LP
3248 continue;
3249
2e1a8a5d
LP
3250 /* We can reuse the hash from our current file only on old-style journal files
3251 * without keyed hashes. On new-style files we have to calculate the hash anew, to
3252 * take the per-file hash seed into consideration. */
3253 if (!JOURNAL_HEADER_KEYED_HASH(j->unique_file->header) && !JOURNAL_HEADER_KEYED_HASH(of->header))
3254 r = journal_file_find_data_object_with_hash(of, odata, ol, le64toh(o->data.hash), NULL, NULL);
3255 else
3256 r = journal_file_find_data_object(of, odata, ol, NULL, NULL);
3c1668da
LP
3257 if (r < 0)
3258 return r;
ed71f956 3259 if (r > 0) {
3c1668da 3260 found = true;
ed71f956
LP
3261 break;
3262 }
3c1668da
LP
3263 }
3264
06cc69d4
JJ
3265 if (found)
3266 continue;
3267
0e35afff
DDM
3268 *ret_data = odata;
3269 *ret_size = ol;
3c1668da
LP
3270
3271 return 1;
3272 }
3273}
3274
76cbafcd
ZJS
3275_public_ int sd_journal_enumerate_available_unique(sd_journal *j, const void **data, size_t *size) {
3276 for (;;) {
3277 int r;
3278
3279 r = sd_journal_enumerate_unique(j, data, size);
3280 if (r >= 0)
3281 return r;
3282 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r))
3283 return r;
3284 /* Try with the next field. sd_journal_enumerate_unique() modifies state, so on the next try
3285 * we will access the next field. */
3286 }
3287}
3288
115646c7 3289_public_ void sd_journal_restart_unique(sd_journal *j) {
e046719b 3290 if (!j || journal_origin_changed(j))
3c1668da
LP
3291 return;
3292
3293 j->unique_file = NULL;
3294 j->unique_offset = 0;
360af4cf 3295 j->unique_file_lost = false;
3c1668da 3296}
85210bff 3297
eb86030e
LP
3298_public_ int sd_journal_enumerate_fields(sd_journal *j, const char **field) {
3299 int r;
3300
3301 assert_return(j, -EINVAL);
e046719b 3302 assert_return(!journal_origin_changed(j), -ECHILD);
eb86030e
LP
3303 assert_return(field, -EINVAL);
3304
3305 if (!j->fields_file) {
3306 if (j->fields_file_lost)
3307 return 0;
3308
3309 j->fields_file = ordered_hashmap_first(j->files);
3310 if (!j->fields_file)
3311 return 0;
3312
3313 j->fields_hash_table_index = 0;
3314 j->fields_offset = 0;
3315 }
3316
3317 for (;;) {
3318 JournalFile *f, *of;
eb86030e
LP
3319 uint64_t m;
3320 Object *o;
3321 size_t sz;
3322 bool found;
3323
3324 f = j->fields_file;
3325
3326 if (j->fields_offset == 0) {
3327 bool eof = false;
3328
3329 /* We are not yet positioned at any field. Let's pick the first one */
3330 r = journal_file_map_field_hash_table(f);
3331 if (r < 0)
3332 return r;
3333
3334 m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem);
3335 for (;;) {
3336 if (j->fields_hash_table_index >= m) {
3337 /* Reached the end of the hash table, go to the next file. */
3338 eof = true;
3339 break;
3340 }
3341
3342 j->fields_offset = le64toh(f->field_hash_table[j->fields_hash_table_index].head_hash_offset);
3343
3344 if (j->fields_offset != 0)
3345 break;
3346
3347 /* Empty hash table bucket, go to next one */
3348 j->fields_hash_table_index++;
3349 }
3350
3351 if (eof) {
3352 /* Proceed with next file */
3353 j->fields_file = ordered_hashmap_next(j->files, f->path);
3354 if (!j->fields_file) {
3355 *field = NULL;
3356 return 0;
3357 }
3358
3359 j->fields_offset = 0;
3360 j->fields_hash_table_index = 0;
3361 continue;
3362 }
3363
3364 } else {
3365 /* We are already positioned at a field. If so, let's figure out the next field from it */
3366
3367 r = journal_file_move_to_object(f, OBJECT_FIELD, j->fields_offset, &o);
3368 if (r < 0)
3369 return r;
3370
3371 j->fields_offset = le64toh(o->field.next_hash_offset);
3372 if (j->fields_offset == 0) {
3373 /* Reached the end of the hash table chain */
3374 j->fields_hash_table_index++;
3375 continue;
3376 }
3377 }
3378
1f133e0d 3379 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
eb86030e
LP
3380 r = journal_file_move_to_object(f, OBJECT_UNUSED, j->fields_offset, &o);
3381 if (r < 0)
3382 return r;
3383
3384 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
baaa35ad
ZJS
3385 if (o->object.type != OBJECT_FIELD)
3386 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
3387 "%s:offset " OFSfmt ": object has type %i, expected %i",
3388 f->path, j->fields_offset,
3389 o->object.type, OBJECT_FIELD);
eb86030e
LP
3390
3391 sz = le64toh(o->object.size) - offsetof(Object, field.payload);
3392
3393 /* Let's see if we already returned this field name before. */
3394 found = false;
90e74a66 3395 ORDERED_HASHMAP_FOREACH(of, j->files) {
eb86030e
LP
3396 if (of == f)
3397 break;
3398
3399 /* Skip this file it didn't have any fields indexed */
3400 if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
3401 continue;
3402
27bf0ab7
DDM
3403 if (!JOURNAL_HEADER_KEYED_HASH(f->header) && !JOURNAL_HEADER_KEYED_HASH(of->header))
3404 r = journal_file_find_field_object_with_hash(of, o->field.payload, sz,
3405 le64toh(o->field.hash), NULL, NULL);
3406 else
3407 r = journal_file_find_field_object(of, o->field.payload, sz, NULL, NULL);
eb86030e
LP
3408 if (r < 0)
3409 return r;
3410 if (r > 0) {
3411 found = true;
3412 break;
3413 }
3414 }
3415
3416 if (found)
3417 continue;
3418
3419 /* Check if this is really a valid string containing no NUL byte */
3420 if (memchr(o->field.payload, 0, sz))
3421 return -EBADMSG;
3422
adbd80f5 3423 if (j->data_threshold > 0 && sz > j->data_threshold)
eb86030e
LP
3424 sz = j->data_threshold;
3425
319a4f4b 3426 if (!GREEDY_REALLOC(j->fields_buffer, sz + 1))
eb86030e
LP
3427 return -ENOMEM;
3428
3429 memcpy(j->fields_buffer, o->field.payload, sz);
3430 j->fields_buffer[sz] = 0;
3431
3432 if (!field_is_valid(j->fields_buffer))
3433 return -EBADMSG;
3434
3435 *field = j->fields_buffer;
3436 return 1;
3437 }
3438}
3439
3440_public_ void sd_journal_restart_fields(sd_journal *j) {
e046719b 3441 if (!j || journal_origin_changed(j))
eb86030e
LP
3442 return;
3443
3444 j->fields_file = NULL;
3445 j->fields_hash_table_index = 0;
3446 j->fields_offset = 0;
3447 j->fields_file_lost = false;
3448}
3449
85210bff 3450_public_ int sd_journal_reliable_fd(sd_journal *j) {
1ae464e0 3451 assert_return(j, -EINVAL);
e046719b 3452 assert_return(!journal_origin_changed(j), -ECHILD);
85210bff
LP
3453
3454 return !j->on_network;
3455}
d4205751
LP
3456
3457static char *lookup_field(const char *field, void *userdata) {
99534007 3458 sd_journal *j = ASSERT_PTR(userdata);
d4205751
LP
3459 const void *data;
3460 size_t size, d;
3461 int r;
3462
3463 assert(field);
d4205751
LP
3464
3465 r = sd_journal_get_data(j, field, &data, &size);
3466 if (r < 0 ||
3467 size > REPLACE_VAR_MAX)
3468 return strdup(field);
3469
3470 d = strlen(field) + 1;
3471
3472 return strndup((const char*) data + d, size - d);
3473}
3474
3475_public_ int sd_journal_get_catalog(sd_journal *j, char **ret) {
3476 const void *data;
3477 size_t size;
3478 sd_id128_t id;
3479 _cleanup_free_ char *text = NULL, *cid = NULL;
3480 char *t;
3481 int r;
3482
1ae464e0 3483 assert_return(j, -EINVAL);
e046719b 3484 assert_return(!journal_origin_changed(j), -ECHILD);
1ae464e0 3485 assert_return(ret, -EINVAL);
d4205751
LP
3486
3487 r = sd_journal_get_data(j, "MESSAGE_ID", &data, &size);
3488 if (r < 0)
3489 return r;
3490
3491 cid = strndup((const char*) data + 11, size - 11);
3492 if (!cid)
3493 return -ENOMEM;
3494
3495 r = sd_id128_from_string(cid, &id);
3496 if (r < 0)
3497 return r;
3498
fba84e12 3499 r = catalog_get(secure_getenv("SYSTEMD_CATALOG") ?: CATALOG_DATABASE, id, &text);
d4205751
LP
3500 if (r < 0)
3501 return r;
3502
3503 t = replace_var(text, lookup_field, j);
3504 if (!t)
3505 return -ENOMEM;
3506
3507 *ret = t;
3508 return 0;
3509}
8f1e860f
LP
3510
3511_public_ int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **ret) {
1ae464e0 3512 assert_return(ret, -EINVAL);
8f1e860f 3513
844ec79b 3514 return catalog_get(CATALOG_DATABASE, id, ret);
8f1e860f 3515}
93b73b06
LP
3516
3517_public_ int sd_journal_set_data_threshold(sd_journal *j, size_t sz) {
1ae464e0 3518 assert_return(j, -EINVAL);
e046719b 3519 assert_return(!journal_origin_changed(j), -ECHILD);
93b73b06
LP
3520
3521 j->data_threshold = sz;
3522 return 0;
3523}
3524
3525_public_ int sd_journal_get_data_threshold(sd_journal *j, size_t *sz) {
1ae464e0 3526 assert_return(j, -EINVAL);
e046719b 3527 assert_return(!journal_origin_changed(j), -ECHILD);
1ae464e0 3528 assert_return(sz, -EINVAL);
93b73b06
LP
3529
3530 *sz = j->data_threshold;
3531 return 0;
3532}
39fd5b08
JS
3533
3534_public_ int sd_journal_has_runtime_files(sd_journal *j) {
3535 assert_return(j, -EINVAL);
3536
3537 return j->has_runtime_files;
3538}
3539
3540_public_ int sd_journal_has_persistent_files(sd_journal *j) {
3541 assert_return(j, -EINVAL);
3542
3543 return j->has_persistent_files;
3544}