]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-journal/sd-journal.c
sd-journal: always fallback to find entry by realtime
[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
4cbabc19 453 * prioq again and only use the information once we reached an equilibrium or hit a limit */
262299dc
LP
454 }
455}
456
457static int compare_boot_ids(sd_journal *j, sd_id128_t a, sd_id128_t b) {
458 JournalFile *x, *y;
459
460 assert(j);
461
462 /* Try to find the newest open journal file for the two boot ids */
463 if (journal_file_find_newest_for_boot_id(j, a, &x) < 0 ||
464 journal_file_find_newest_for_boot_id(j, b, &y) < 0)
465 return 0;
466
467 /* Only compare the boot id timestamps if they originate from the same machine. If they are from
468 * different machines, then we timestamps of the boot ids might be as off as the timestamps on the
469 * entries and hence not useful for comparing. */
470 if (!sd_id128_equal(x->newest_machine_id, y->newest_machine_id))
471 return 0;
472
473 return CMP(x->newest_realtime_usec, y->newest_realtime_usec);
474}
475
476static int compare_with_location(
477 sd_journal *j,
478 const JournalFile *f,
479 const Location *l,
480 const JournalFile *current_file) {
90c88092
YW
481 int r;
482
262299dc 483 assert(j);
487d3720 484 assert(f);
de190aef 485 assert(l);
487d3720 486 assert(f->location_type == LOCATION_SEEK);
4c701096 487 assert(IN_SET(l->type, LOCATION_DISCRETE, LOCATION_SEEK));
de190aef
LP
488
489 if (l->monotonic_set &&
487d3720 490 sd_id128_equal(f->current_boot_id, l->boot_id) &&
de190aef 491 l->realtime_set &&
487d3720 492 f->current_realtime == l->realtime &&
de190aef 493 l->xor_hash_set &&
b6849042 494 f->current_xor_hash == l->xor_hash &&
b17f651a 495 l->seqnum_set &&
496 sd_id128_equal(f->header->seqnum_id, l->seqnum_id) &&
497 f->current_seqnum == l->seqnum &&
b6849042 498 f != current_file)
de190aef
LP
499 return 0;
500
501 if (l->seqnum_set &&
487d3720 502 sd_id128_equal(f->header->seqnum_id, l->seqnum_id)) {
90c88092
YW
503 r = CMP(f->current_seqnum, l->seqnum);
504 if (r != 0)
505 return r;
de190aef
LP
506 }
507
262299dc
LP
508 if (l->monotonic_set) {
509 /* If both arguments have the same boot ID, then we can compare the monotonic timestamps. If
510 * they are distinct, then we might able to lookup the timestamps of those boot IDs (if they
511 * are from the same machine) and order by that. */
512 if (sd_id128_equal(f->current_boot_id, l->boot_id))
513 r = CMP(f->current_monotonic, l->monotonic);
514 else
515 r = compare_boot_ids(j, f->current_boot_id, l->boot_id);
90c88092
YW
516 if (r != 0)
517 return r;
de190aef
LP
518 }
519
520 if (l->realtime_set) {
90c88092
YW
521 r = CMP(f->current_realtime, l->realtime);
522 if (r != 0)
523 return r;
de190aef
LP
524 }
525
526 if (l->xor_hash_set) {
90c88092
YW
527 r = CMP(f->current_xor_hash, l->xor_hash);
528 if (r != 0)
529 return r;
de190aef
LP
530 }
531
532 return 0;
533}
534
cbdca852
LP
535static int next_for_match(
536 sd_journal *j,
537 Match *m,
538 JournalFile *f,
539 uint64_t after_offset,
540 direction_t direction,
541 Object **ret,
542 uint64_t *offset) {
543
de7b95cd 544 int r;
cbdca852 545 uint64_t np = 0;
de7b95cd
LP
546
547 assert(j);
cbdca852
LP
548 assert(m);
549 assert(f);
de7b95cd 550
cbdca852 551 if (m->type == MATCH_DISCRETE) {
ec50313d
DDM
552 Object *d;
553 uint64_t hash;
de190aef 554
4ce534f4
LP
555 /* If the keyed hash logic is used, we need to calculate the hash fresh per file. Otherwise
556 * we can use what we pre-calculated. */
557 if (JOURNAL_HEADER_KEYED_HASH(f->header))
558 hash = journal_file_hash_data(f, m->data, m->size);
559 else
560 hash = m->hash;
561
ec50313d 562 r = journal_file_find_data_object_with_hash(f, m->data, m->size, hash, &d, NULL);
de190aef
LP
563 if (r <= 0)
564 return r;
565
ec50313d 566 return journal_file_move_to_entry_by_offset_for_data(f, d, after_offset, direction, ret, offset);
de190aef 567
cbdca852 568 } else if (m->type == MATCH_OR_TERM) {
de7b95cd 569
cbdca852 570 /* Find the earliest match beyond after_offset */
de190aef 571
cbdca852
LP
572 LIST_FOREACH(matches, i, m->matches) {
573 uint64_t cp;
de190aef 574
cbdca852 575 r = next_for_match(j, i, f, after_offset, direction, NULL, &cp);
b4e5f920
LP
576 if (r < 0)
577 return r;
cbdca852 578 else if (r > 0) {
bc302926 579 if (np == 0 || (direction == DIRECTION_DOWN ? cp < np : cp > np))
cbdca852
LP
580 np = cp;
581 }
582 }
b4e5f920 583
bc302926
ZJS
584 if (np == 0)
585 return 0;
586
cbdca852 587 } else if (m->type == MATCH_AND_TERM) {
03677889 588 Match *last_moved;
de190aef 589
cbdca852 590 /* Always jump to the next matching entry and repeat
2bc8ca0c 591 * this until we find an offset that matches for all
cbdca852 592 * matches. */
de190aef 593
cbdca852
LP
594 if (!m->matches)
595 return 0;
de7b95cd 596
2bc8ca0c
ZJS
597 r = next_for_match(j, m->matches, f, after_offset, direction, NULL, &np);
598 if (r <= 0)
599 return r;
de190aef 600
2bc8ca0c
ZJS
601 assert(direction == DIRECTION_DOWN ? np >= after_offset : np <= after_offset);
602 last_moved = m->matches;
de190aef 603
2bc8ca0c
ZJS
604 LIST_LOOP_BUT_ONE(matches, i, m->matches, last_moved) {
605 uint64_t cp;
de190aef 606
2bc8ca0c
ZJS
607 r = next_for_match(j, i, f, np, direction, NULL, &cp);
608 if (r <= 0)
609 return r;
de190aef 610
2bc8ca0c
ZJS
611 assert(direction == DIRECTION_DOWN ? cp >= np : cp <= np);
612 if (direction == DIRECTION_DOWN ? cp > np : cp < np) {
613 np = cp;
614 last_moved = i;
de190aef 615 }
2bc8ca0c 616 }
cbdca852 617 }
de190aef 618
bc302926 619 assert(np > 0);
de190aef 620
4d8b09e4
DDM
621 if (ret) {
622 r = journal_file_move_to_object(f, OBJECT_ENTRY, np, ret);
623 if (r < 0)
624 return r;
625 }
de7b95cd 626
de190aef 627 if (offset)
cbdca852 628 *offset = np;
de190aef
LP
629
630 return 1;
631}
632
cbdca852
LP
633static int find_location_for_match(
634 sd_journal *j,
635 Match *m,
636 JournalFile *f,
637 direction_t direction,
638 Object **ret,
639 uint64_t *offset) {
640
de190aef 641 int r;
de190aef
LP
642
643 assert(j);
cbdca852 644 assert(m);
de190aef 645 assert(f);
de190aef 646
cbdca852 647 if (m->type == MATCH_DISCRETE) {
ec50313d 648 Object *d;
4ce534f4
LP
649 uint64_t dp, hash;
650
651 if (JOURNAL_HEADER_KEYED_HASH(f->header))
652 hash = journal_file_hash_data(f, m->data, m->size);
653 else
654 hash = m->hash;
de190aef 655
ec50313d 656 r = journal_file_find_data_object_with_hash(f, m->data, m->size, hash, &d, &dp);
de7b95cd
LP
657 if (r <= 0)
658 return r;
659
cbdca852 660 /* FIXME: missing: find by monotonic */
de7b95cd 661
cbdca852 662 if (j->current_location.type == LOCATION_HEAD)
7a4ee861 663 return direction == DIRECTION_DOWN ? journal_file_next_entry_for_data(f, d, DIRECTION_DOWN, ret, offset) : 0;
cbdca852 664 if (j->current_location.type == LOCATION_TAIL)
7a4ee861 665 return direction == DIRECTION_UP ? journal_file_next_entry_for_data(f, d, DIRECTION_UP, ret, offset) : 0;
cbdca852 666 if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
ec50313d 667 return journal_file_move_to_entry_by_seqnum_for_data(f, d, j->current_location.seqnum, direction, ret, offset);
cbdca852 668 if (j->current_location.monotonic_set) {
ec50313d 669 r = journal_file_move_to_entry_by_monotonic_for_data(f, d, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset);
304cb08f 670 if (r != 0)
cbdca852 671 return r;
ec50313d
DDM
672
673 /* The data object might have been invalidated. */
674 r = journal_file_move_to_object(f, OBJECT_DATA, dp, &d);
675 if (r < 0)
676 return r;
cbdca852
LP
677 }
678 if (j->current_location.realtime_set)
ec50313d 679 return journal_file_move_to_entry_by_realtime_for_data(f, d, j->current_location.realtime, direction, ret, offset);
de190aef 680
ec50313d 681 return journal_file_next_entry_for_data(f, d, direction, ret, offset);
de7b95cd 682
cbdca852
LP
683 } else if (m->type == MATCH_OR_TERM) {
684 uint64_t np = 0;
de7b95cd 685
cbdca852 686 /* Find the earliest match */
de7b95cd 687
cbdca852
LP
688 LIST_FOREACH(matches, i, m->matches) {
689 uint64_t cp;
690
691 r = find_location_for_match(j, i, f, direction, NULL, &cp);
692 if (r < 0)
693 return r;
694 else if (r > 0) {
695 if (np == 0 || (direction == DIRECTION_DOWN ? np > cp : np < cp))
696 np = cp;
de190aef 697 }
cbdca852 698 }
de190aef 699
cbdca852
LP
700 if (np == 0)
701 return 0;
de7b95cd 702
4d8b09e4
DDM
703 if (ret) {
704 r = journal_file_move_to_object(f, OBJECT_ENTRY, np, ret);
705 if (r < 0)
706 return r;
707 }
de7b95cd 708
cbdca852
LP
709 if (offset)
710 *offset = np;
de190aef 711
cbdca852 712 return 1;
e892bd17 713
cbdca852 714 } else {
cbdca852
LP
715 uint64_t np = 0;
716
717 assert(m->type == MATCH_AND_TERM);
718
719 /* First jump to the last match, and then find the
720 * next one where all matches match */
721
722 if (!m->matches)
723 return 0;
724
725 LIST_FOREACH(matches, i, m->matches) {
726 uint64_t cp;
727
728 r = find_location_for_match(j, i, f, direction, NULL, &cp);
729 if (r <= 0)
4b067dc9
LP
730 return r;
731
bc302926 732 if (np == 0 || (direction == DIRECTION_DOWN ? cp > np : cp < np))
cbdca852 733 np = cp;
de7b95cd
LP
734 }
735
cbdca852
LP
736 return next_for_match(j, m, f, np, direction, ret, offset);
737 }
738}
de190aef 739
cbdca852
LP
740static int find_location_with_matches(
741 sd_journal *j,
742 JournalFile *f,
743 direction_t direction,
744 Object **ret,
745 uint64_t *offset) {
746
747 int r;
748
749 assert(j);
750 assert(f);
751 assert(ret);
752 assert(offset);
753
754 if (!j->level0) {
755 /* No matches is simple */
756
757 if (j->current_location.type == LOCATION_HEAD)
7a4ee861 758 return direction == DIRECTION_DOWN ? journal_file_next_entry(f, 0, DIRECTION_DOWN, ret, offset) : 0;
cbdca852 759 if (j->current_location.type == LOCATION_TAIL)
7a4ee861 760 return direction == DIRECTION_UP ? journal_file_next_entry(f, 0, DIRECTION_UP, ret, offset) : 0;
cbdca852
LP
761 if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
762 return journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, ret, offset);
763 if (j->current_location.monotonic_set) {
764 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset);
304cb08f 765 if (r != 0)
cbdca852 766 return r;
de7b95cd 767 }
cbdca852
LP
768 if (j->current_location.realtime_set)
769 return journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, ret, offset);
de7b95cd 770
f534928a 771 return journal_file_next_entry(f, 0, direction, ret, offset);
cbdca852
LP
772 } else
773 return find_location_for_match(j, j->level0, f, direction, ret, offset);
774}
de7b95cd 775
cbdca852
LP
776static int next_with_matches(
777 sd_journal *j,
778 JournalFile *f,
779 direction_t direction,
780 Object **ret,
781 uint64_t *offset) {
782
cbdca852
LP
783 assert(j);
784 assert(f);
785 assert(ret);
786 assert(offset);
787
cbdca852
LP
788 /* No matches is easy. We simple advance the file
789 * pointer by one. */
790 if (!j->level0)
b29ddfcb 791 return journal_file_next_entry(f, f->current_offset, direction, ret, offset);
cbdca852
LP
792
793 /* If we have a match then we look for the next matching entry
49f43d5f 794 * with an offset at least one step larger */
b29ddfcb
MS
795 return next_for_match(j, j->level0, f,
796 direction == DIRECTION_DOWN ? f->current_offset + 1
797 : f->current_offset - 1,
798 direction, ret, offset);
de7b95cd
LP
799}
800
58439db4 801static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction) {
de190aef 802 Object *c;
6e693b42 803 uint64_t cp, n_entries;
cbdca852 804 int r;
de190aef
LP
805
806 assert(j);
807 assert(f);
808
34af7494
LP
809 (void) journal_file_read_tail_timestamp(j, f);
810
950c07d4
MS
811 n_entries = le64toh(f->header->n_entries);
812
813 /* If we hit EOF before, we don't need to look into this file again
814 * unless direction changed or new entries appeared. */
a9414960
YW
815 if (f->last_direction == direction &&
816 f->location_type == (direction == DIRECTION_DOWN ? LOCATION_TAIL : LOCATION_HEAD) &&
950c07d4
MS
817 n_entries == f->last_n_entries)
818 return 0;
668c965a 819
950c07d4
MS
820 f->last_n_entries = n_entries;
821
822 if (f->last_direction == direction && f->current_offset > 0) {
7943f422
MS
823 /* LOCATION_SEEK here means we did the work in a previous
824 * iteration and the current location already points to a
825 * candidate entry. */
487d3720
MS
826 if (f->location_type != LOCATION_SEEK) {
827 r = next_with_matches(j, f, direction, &c, &cp);
828 if (r <= 0)
829 return r;
466ccd92 830
950c07d4 831 journal_file_save_location(f, c, cp);
487d3720 832 }
de190aef 833 } else {
950c07d4
MS
834 f->last_direction = direction;
835
cbdca852 836 r = find_location_with_matches(j, f, direction, &c, &cp);
de190aef
LP
837 if (r <= 0)
838 return r;
487d3720 839
950c07d4 840 journal_file_save_location(f, c, cp);
de190aef
LP
841 }
842
bc302926 843 /* OK, we found the spot, now let's advance until an entry
cbdca852
LP
844 * that is actually different from what we were previously
845 * looking at. This is necessary to handle entries which exist
846 * in two (or more) journal files, and which shall all be
847 * suppressed but one. */
848
de190aef
LP
849 for (;;) {
850 bool found;
851
852 if (j->current_location.type == LOCATION_DISCRETE) {
853 int k;
854
262299dc 855 k = compare_with_location(j, f, &j->current_location, j->current_file);
1cdf7175
CH
856
857 found = direction == DIRECTION_DOWN ? k > 0 : k < 0;
de190aef
LP
858 } else
859 found = true;
860
487d3720 861 if (found)
de190aef 862 return 1;
de190aef
LP
863
864 r = next_with_matches(j, f, direction, &c, &cp);
865 if (r <= 0)
866 return r;
487d3720 867
950c07d4 868 journal_file_save_location(f, c, cp);
de190aef
LP
869 }
870}
871
262299dc 872static int compare_locations(sd_journal *j, JournalFile *af, JournalFile *bf) {
01e4f03f
LP
873 int r;
874
262299dc 875 assert(j);
01e4f03f
LP
876 assert(af);
877 assert(af->header);
878 assert(bf);
879 assert(bf->header);
880 assert(af->location_type == LOCATION_SEEK);
881 assert(bf->location_type == LOCATION_SEEK);
882
883 /* If contents, timestamps and seqnum match, these entries are identical. */
884 if (sd_id128_equal(af->current_boot_id, bf->current_boot_id) &&
885 af->current_monotonic == bf->current_monotonic &&
886 af->current_realtime == bf->current_realtime &&
887 af->current_xor_hash == bf->current_xor_hash &&
888 sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id) &&
889 af->current_seqnum == bf->current_seqnum)
890 return 0;
891
892 if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
893 /* If this is from the same seqnum source, compare seqnums */
894 r = CMP(af->current_seqnum, bf->current_seqnum);
895 if (r != 0)
896 return r;
897
898 /* Wow! This is weird, different data but the same seqnums? Something is borked, but let's
899 * make the best of it and compare by time. */
900 }
901
262299dc 902 if (sd_id128_equal(af->current_boot_id, bf->current_boot_id))
01e4f03f
LP
903 /* If the boot id matches, compare monotonic time */
904 r = CMP(af->current_monotonic, bf->current_monotonic);
262299dc
LP
905 else
906 /* If they don't match try to compare boot IDs */
907 r = compare_boot_ids(j, af->current_boot_id, bf->current_boot_id);
908 if (r != 0)
909 return r;
01e4f03f
LP
910
911 /* Otherwise, compare UTC time */
912 r = CMP(af->current_realtime, bf->current_realtime);
913 if (r != 0)
914 return r;
915
916 /* Finally, compare by contents */
917 return CMP(af->current_xor_hash, bf->current_xor_hash);
918}
919
e892bd17 920static int real_journal_next(sd_journal *j, direction_t direction) {
5d4ba7f2 921 JournalFile *new_file = NULL;
abcdc02c 922 unsigned n_files;
5d4ba7f2 923 const void **files;
a002d44b 924 Object *o;
87d2c1ff
LP
925 int r;
926
1ae464e0 927 assert_return(j, -EINVAL);
e046719b 928 assert_return(!journal_origin_changed(j), -ECHILD);
87d2c1ff 929
5d4ba7f2
VC
930 r = iterated_cache_get(j->files_cache, NULL, &files, &n_files);
931 if (r < 0)
932 return r;
933
abcdc02c 934 for (unsigned i = 0; i < n_files; i++) {
5d4ba7f2 935 JournalFile *f = (JournalFile *)files[i];
de190aef 936 bool found;
87d2c1ff 937
58439db4 938 r = next_beyond_location(j, f, direction);
e590af26 939 if (r < 0) {
da927ba9 940 log_debug_errno(r, "Can't iterate through %s, ignoring: %m", f->path);
a9a245c1 941 remove_file_real(j, f);
e590af26 942 continue;
6573ef05 943 } else if (r == 0) {
a9414960 944 f->location_type = direction == DIRECTION_DOWN ? LOCATION_TAIL : LOCATION_HEAD;
cec736d2 945 continue;
6573ef05 946 }
87d2c1ff 947
468b21de 948 if (!new_file)
de190aef
LP
949 found = true;
950 else {
951 int k;
952
262299dc 953 k = compare_locations(j, f, new_file);
de190aef 954
bc302926 955 found = direction == DIRECTION_DOWN ? k < 0 : k > 0;
de190aef
LP
956 }
957
e499c999 958 if (found)
468b21de 959 new_file = f;
87d2c1ff
LP
960 }
961
468b21de 962 if (!new_file)
de190aef 963 return 0;
ae2cc8ef 964
e499c999 965 r = journal_file_move_to_object(new_file, OBJECT_ENTRY, new_file->current_offset, &o);
468b21de
LP
966 if (r < 0)
967 return r;
968
1eb6332d 969 set_location(j, new_file, o);
ae2cc8ef 970
de190aef
LP
971 return 1;
972}
ae2cc8ef 973
a5344d2c 974_public_ int sd_journal_next(sd_journal *j) {
de190aef
LP
975 return real_journal_next(j, DIRECTION_DOWN);
976}
ae2cc8ef 977
a5344d2c 978_public_ int sd_journal_previous(sd_journal *j) {
de190aef
LP
979 return real_journal_next(j, DIRECTION_UP);
980}
ae2cc8ef 981
b78f9481
YW
982_public_ int sd_journal_step_one(sd_journal *j, int advanced) {
983 assert_return(j, -EINVAL);
984
985 if (j->current_location.type == LOCATION_HEAD)
986 return sd_journal_next(j);
987 if (j->current_location.type == LOCATION_TAIL)
988 return sd_journal_previous(j);
989 return real_journal_next(j, advanced ? DIRECTION_DOWN : DIRECTION_UP);
990}
991
6f003b43 992static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) {
de190aef 993 int c = 0, r;
ae2cc8ef 994
1ae464e0 995 assert_return(j, -EINVAL);
e046719b 996 assert_return(!journal_origin_changed(j), -ECHILD);
41d544a1 997 assert_return(skip <= INT_MAX, -ERANGE);
de190aef 998
6f003b43
LP
999 if (skip == 0) {
1000 /* If this is not a discrete skip, then at least
1001 * resolve the current location */
04884354
LK
1002 if (j->current_location.type != LOCATION_DISCRETE) {
1003 r = real_journal_next(j, direction);
1004 if (r < 0)
1005 return r;
1006 }
6f003b43
LP
1007
1008 return 0;
1009 }
1010
1011 do {
1012 r = real_journal_next(j, direction);
de190aef
LP
1013 if (r < 0)
1014 return r;
1015
1016 if (r == 0)
1017 return c;
1018
1019 skip--;
1020 c++;
6f003b43 1021 } while (skip > 0);
87d2c1ff 1022
de190aef 1023 return c;
87d2c1ff
LP
1024}
1025
6f003b43
LP
1026_public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
1027 return real_journal_next_skip(j, DIRECTION_DOWN, skip);
1028}
de190aef 1029
6f003b43
LP
1030_public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
1031 return real_journal_next_skip(j, DIRECTION_UP, skip);
87d2c1ff
LP
1032}
1033
a5344d2c 1034_public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) {
cec736d2 1035 Object *o;
87d2c1ff
LP
1036 int r;
1037
1ae464e0 1038 assert_return(j, -EINVAL);
e046719b 1039 assert_return(!journal_origin_changed(j), -ECHILD);
1ae464e0 1040 assert_return(cursor, -EINVAL);
87d2c1ff 1041
3fbf9cbb
LP
1042 if (!j->current_file || j->current_file->current_offset <= 0)
1043 return -EADDRNOTAVAIL;
87d2c1ff 1044
de190aef 1045 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
87d2c1ff
LP
1046 if (r < 0)
1047 return r;
1048
3fbf9cbb 1049 if (asprintf(cursor,
507f22bd 1050 "s=%s;i=%"PRIx64";b=%s;m=%"PRIx64";t=%"PRIx64";x=%"PRIx64,
85b55869
LP
1051 SD_ID128_TO_STRING(j->current_file->header->seqnum_id), le64toh(o->entry.seqnum),
1052 SD_ID128_TO_STRING(o->entry.boot_id), le64toh(o->entry.monotonic),
507f22bd
ZJS
1053 le64toh(o->entry.realtime),
1054 le64toh(o->entry.xor_hash)) < 0)
3fbf9cbb 1055 return -ENOMEM;
87d2c1ff 1056
6f47ad30 1057 return 0;
87d2c1ff
LP
1058}
1059
a5344d2c 1060_public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
de190aef 1061 unsigned long long seqnum, monotonic, realtime, xor_hash;
da277e90
ZJS
1062 bool seqnum_id_set = false,
1063 seqnum_set = false,
1064 boot_id_set = false,
1065 monotonic_set = false,
1066 realtime_set = false,
1067 xor_hash_set = false;
de190aef 1068 sd_id128_t seqnum_id, boot_id;
da277e90 1069 int r;
de190aef 1070
1ae464e0 1071 assert_return(j, -EINVAL);
e046719b 1072 assert_return(!journal_origin_changed(j), -ECHILD);
1ae464e0 1073 assert_return(!isempty(cursor), -EINVAL);
de190aef 1074
da277e90
ZJS
1075 for (const char *p = cursor;;) {
1076 _cleanup_free_ char *word = NULL;
de190aef 1077
da277e90
ZJS
1078 r = extract_first_word(&p, &word, ";", EXTRACT_DONT_COALESCE_SEPARATORS);
1079 if (r < 0)
1080 return r;
1081 if (r == 0)
1082 break;
de190aef 1083
da277e90
ZJS
1084 if (word[0] == '\0' || word[1] != '=')
1085 return -EINVAL;
de190aef 1086
a2a5291b 1087 switch (word[0]) {
de190aef
LP
1088 case 's':
1089 seqnum_id_set = true;
da277e90
ZJS
1090 r = sd_id128_from_string(word + 2, &seqnum_id);
1091 if (r < 0)
1092 return r;
de190aef
LP
1093 break;
1094
1095 case 'i':
1096 seqnum_set = true;
da277e90
ZJS
1097 if (sscanf(word + 2, "%llx", &seqnum) != 1)
1098 return -EINVAL;
de190aef
LP
1099 break;
1100
1101 case 'b':
1102 boot_id_set = true;
da277e90 1103 r = sd_id128_from_string(word + 2, &boot_id);
c9f5ac09
SS
1104 if (r < 0)
1105 return r;
de190aef
LP
1106 break;
1107
1108 case 'm':
1109 monotonic_set = true;
da277e90
ZJS
1110 if (sscanf(word + 2, "%llx", &monotonic) != 1)
1111 return -EINVAL;
de190aef
LP
1112 break;
1113
1114 case 't':
1115 realtime_set = true;
da277e90
ZJS
1116 if (sscanf(word + 2, "%llx", &realtime) != 1)
1117 return -EINVAL;
de190aef
LP
1118 break;
1119
1120 case 'x':
1121 xor_hash_set = true;
da277e90
ZJS
1122 if (sscanf(word + 2, "%llx", &xor_hash) != 1)
1123 return -EINVAL;
de190aef
LP
1124 break;
1125 }
de190aef
LP
1126 }
1127
1128 if ((!seqnum_set || !seqnum_id_set) &&
1129 (!monotonic_set || !boot_id_set) &&
1130 !realtime_set)
1131 return -EINVAL;
1132
bba6e4ae
LP
1133 detach_location(j);
1134 j->current_location = (Location) {
1135 .type = LOCATION_SEEK,
1136 };
de190aef
LP
1137
1138 if (realtime_set) {
1139 j->current_location.realtime = (uint64_t) realtime;
1140 j->current_location.realtime_set = true;
1141 }
1142
1143 if (seqnum_set && seqnum_id_set) {
1144 j->current_location.seqnum = (uint64_t) seqnum;
1145 j->current_location.seqnum_id = seqnum_id;
1146 j->current_location.seqnum_set = true;
1147 }
1148
1149 if (monotonic_set && boot_id_set) {
1150 j->current_location.monotonic = (uint64_t) monotonic;
1151 j->current_location.boot_id = boot_id;
1152 j->current_location.monotonic_set = true;
1153 }
1154
1155 if (xor_hash_set) {
1156 j->current_location.xor_hash = (uint64_t) xor_hash;
1157 j->current_location.xor_hash_set = true;
1158 }
1159
1160 return 0;
1161}
1162
c6511e85
LP
1163_public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) {
1164 int r;
c6511e85
LP
1165 Object *o;
1166
1ae464e0 1167 assert_return(j, -EINVAL);
e046719b 1168 assert_return(!journal_origin_changed(j), -ECHILD);
1ae464e0 1169 assert_return(!isempty(cursor), -EINVAL);
c6511e85
LP
1170
1171 if (!j->current_file || j->current_file->current_offset <= 0)
1172 return -EADDRNOTAVAIL;
1173
1174 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
1175 if (r < 0)
1176 return r;
1177
9ed794a3 1178 for (;;) {
c6511e85 1179 _cleanup_free_ char *item = NULL;
c6511e85 1180 unsigned long long ll;
7294bb5b 1181 sd_id128_t id;
c6511e85
LP
1182 int k = 0;
1183
7294bb5b
SS
1184 r = extract_first_word(&cursor, &item, ";", EXTRACT_DONT_COALESCE_SEPARATORS);
1185 if (r < 0)
1186 return r;
c6511e85 1187
7294bb5b
SS
1188 if (r == 0)
1189 break;
c6511e85 1190
7294bb5b
SS
1191 if (strlen(item) < 2 || item[1] != '=')
1192 return -EINVAL;
1193
1194 switch (item[0]) {
c6511e85
LP
1195
1196 case 's':
1197 k = sd_id128_from_string(item+2, &id);
1198 if (k < 0)
1199 return k;
1200 if (!sd_id128_equal(id, j->current_file->header->seqnum_id))
1201 return 0;
1202 break;
1203
1204 case 'i':
1205 if (sscanf(item+2, "%llx", &ll) != 1)
1206 return -EINVAL;
1207 if (ll != le64toh(o->entry.seqnum))
1208 return 0;
1209 break;
1210
1211 case 'b':
1212 k = sd_id128_from_string(item+2, &id);
1213 if (k < 0)
1214 return k;
1215 if (!sd_id128_equal(id, o->entry.boot_id))
1216 return 0;
1217 break;
1218
1219 case 'm':
1220 if (sscanf(item+2, "%llx", &ll) != 1)
1221 return -EINVAL;
1222 if (ll != le64toh(o->entry.monotonic))
1223 return 0;
1224 break;
1225
1226 case 't':
1227 if (sscanf(item+2, "%llx", &ll) != 1)
1228 return -EINVAL;
1229 if (ll != le64toh(o->entry.realtime))
1230 return 0;
1231 break;
1232
1233 case 'x':
1234 if (sscanf(item+2, "%llx", &ll) != 1)
1235 return -EINVAL;
1236 if (ll != le64toh(o->entry.xor_hash))
1237 return 0;
1238 break;
1239 }
1240 }
1241
1242 return 1;
1243}
1244
a5344d2c 1245_public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
1ae464e0 1246 assert_return(j, -EINVAL);
e046719b 1247 assert_return(!journal_origin_changed(j), -ECHILD);
de190aef 1248
bba6e4ae
LP
1249 detach_location(j);
1250
1251 j->current_location = (Location) {
1252 .type = LOCATION_SEEK,
1253 .boot_id = boot_id,
1254 .monotonic = usec,
1255 .monotonic_set = true,
1256 };
de190aef
LP
1257
1258 return 0;
1259}
1260
a5344d2c 1261_public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
1ae464e0 1262 assert_return(j, -EINVAL);
e046719b 1263 assert_return(!journal_origin_changed(j), -ECHILD);
de190aef 1264
bba6e4ae
LP
1265 detach_location(j);
1266
1267 j->current_location = (Location) {
1268 .type = LOCATION_SEEK,
1269 .realtime = usec,
1270 .realtime_set = true,
1271 };
de190aef
LP
1272
1273 return 0;
1274}
1275
a5344d2c 1276_public_ int sd_journal_seek_head(sd_journal *j) {
1ae464e0 1277 assert_return(j, -EINVAL);
e046719b 1278 assert_return(!journal_origin_changed(j), -ECHILD);
de190aef 1279
bba6e4ae
LP
1280 detach_location(j);
1281
1282 j->current_location = (Location) {
1283 .type = LOCATION_HEAD,
1284 };
de190aef
LP
1285
1286 return 0;
1287}
1288
a5344d2c 1289_public_ int sd_journal_seek_tail(sd_journal *j) {
1ae464e0 1290 assert_return(j, -EINVAL);
e046719b 1291 assert_return(!journal_origin_changed(j), -ECHILD);
de190aef 1292
bba6e4ae
LP
1293 detach_location(j);
1294
1295 j->current_location = (Location) {
1296 .type = LOCATION_TAIL,
1297 };
de190aef
LP
1298
1299 return 0;
87d2c1ff
LP
1300}
1301
85210bff 1302static void check_network(sd_journal *j, int fd) {
85210bff
LP
1303 assert(j);
1304
1305 if (j->on_network)
1306 return;
1307
77f9fa3b 1308 j->on_network = fd_is_network_fs(fd);
85210bff
LP
1309}
1310
a688baa8
ZJS
1311static bool file_has_type_prefix(const char *prefix, const char *filename) {
1312 const char *full, *tilded, *atted;
1313
63c372cb
LP
1314 full = strjoina(prefix, ".journal");
1315 tilded = strjoina(full, "~");
1316 atted = strjoina(prefix, "@");
a688baa8 1317
0cbd293e 1318 return STR_IN_SET(filename, full, tilded) ||
a688baa8
ZJS
1319 startswith(filename, atted);
1320}
1321
1322static bool file_type_wanted(int flags, const char *filename) {
d617408e
LP
1323 assert(filename);
1324
97c621b7 1325 if (!ENDSWITH_SET(filename, ".journal", ".journal~"))
a688baa8
ZJS
1326 return false;
1327
1328 /* no flags set → every type is OK */
1329 if (!(flags & (SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER)))
1330 return true;
1331
97c621b7 1332 if (FLAGS_SET(flags, SD_JOURNAL_CURRENT_USER)) {
a688baa8
ZJS
1333 char prefix[5 + DECIMAL_STR_MAX(uid_t) + 1];
1334
97c621b7 1335 xsprintf(prefix, "user-" UID_FMT, getuid());
a688baa8
ZJS
1336
1337 if (file_has_type_prefix(prefix, filename))
1338 return true;
97c621b7
LP
1339
1340 /* If SD_JOURNAL_CURRENT_USER is specified and we are invoked under a system UID, then
1341 * automatically enable SD_JOURNAL_SYSTEM too, because journald will actually put system user
1342 * data into the system journal. */
1343
1344 if (uid_for_system_journal(getuid()))
1345 flags |= SD_JOURNAL_SYSTEM;
a688baa8
ZJS
1346 }
1347
97c621b7
LP
1348 if (FLAGS_SET(flags, SD_JOURNAL_SYSTEM) && file_has_type_prefix("system", filename))
1349 return true;
1350
a688baa8
ZJS
1351 return false;
1352}
1353
5d1ce257
LP
1354static bool path_has_prefix(sd_journal *j, const char *path, const char *prefix) {
1355 assert(j);
1356 assert(path);
1357 assert(prefix);
1358
1359 if (j->toplevel_fd >= 0)
1360 return false;
1361
1362 return path_startswith(path, prefix);
1363}
1364
9c66f528
LP
1365static void track_file_disposition(sd_journal *j, JournalFile *f) {
1366 assert(j);
1367 assert(f);
1368
1369 if (!j->has_runtime_files && path_has_prefix(j, f->path, "/run"))
1370 j->has_runtime_files = true;
1371 else if (!j->has_persistent_files && path_has_prefix(j, f->path, "/var"))
1372 j->has_persistent_files = true;
1373}
1374
5d1ce257
LP
1375static const char *skip_slash(const char *p) {
1376
1377 if (!p)
1378 return NULL;
1379
1380 while (*p == '/')
1381 p++;
1382
1383 return p;
1384}
1385
9c66f528
LP
1386static int add_any_file(
1387 sd_journal *j,
1388 int fd,
1389 const char *path) {
1390
254d1313 1391 _cleanup_close_ int our_fd = -EBADF;
9c66f528
LP
1392 JournalFile *f;
1393 struct stat st;
70f50a47 1394 int r;
3fbf9cbb
LP
1395
1396 assert(j);
5d1ce257 1397 assert(fd >= 0 || path);
3fbf9cbb 1398
9c66f528 1399 if (fd < 0) {
85b0ff8a 1400 assert(path); /* For gcc. */
9c66f528
LP
1401 if (j->toplevel_fd >= 0)
1402 /* If there's a top-level fd defined make the path relative, explicitly, since otherwise
1403 * openat() ignores the first argument. */
1404
70f50a47 1405 fd = our_fd = openat(j->toplevel_fd, skip_slash(path), O_RDONLY|O_CLOEXEC|O_NONBLOCK);
9c66f528 1406 else
70f50a47 1407 fd = our_fd = open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
9c66f528
LP
1408 if (fd < 0) {
1409 r = log_debug_errno(errno, "Failed to open journal file %s: %m", path);
70f50a47 1410 goto error;
9c66f528
LP
1411 }
1412
9c66f528
LP
1413 r = fd_nonblock(fd, false);
1414 if (r < 0) {
1415 r = log_debug_errno(errno, "Failed to turn off O_NONBLOCK for %s: %m", path);
70f50a47 1416 goto error;
858749f7
LP
1417 }
1418 }
50f20cfd 1419
9c66f528 1420 if (fstat(fd, &st) < 0) {
85b0ff8a 1421 r = log_debug_errno(errno, "Failed to fstat %s: %m", path ?: "fd");
70f50a47 1422 goto error;
9c66f528 1423 }
3cc44114
LP
1424
1425 r = stat_verify_regular(&st);
1426 if (r < 0) {
85b0ff8a 1427 log_debug_errno(r, "Refusing to open %s: %m", path ?: "fd");
70f50a47 1428 goto error;
9e8abdf0
LP
1429 }
1430
85b0ff8a
ZJS
1431 if (path) {
1432 f = ordered_hashmap_get(j->files, path);
1433 if (f) {
1434 if (stat_inode_same(&f->last_stat, &st)) {
1435 /* We already track this file, under the same path and with the same
1436 * device/inode numbers, it's hence really the same. Mark this file as seen
1437 * in this generation. This is used to GC old files in process_q_overflow()
1438 * to detect journal files that are still there and discern them from those
1439 * which are gone. */
1440
1441 f->last_seen_generation = j->generation;
34af7494 1442 (void) journal_file_read_tail_timestamp(j, f);
70f50a47 1443 return 0;
85b0ff8a 1444 }
5d1ce257 1445
85b0ff8a
ZJS
1446 /* So we tracked a file under this name, but it has a different inode/device. In that
1447 * case, it got replaced (probably due to rotation?), let's drop it hence from our
1448 * list. */
1449 remove_file_real(j, f);
1450 f = NULL;
5d1ce257 1451 }
9c66f528
LP
1452 }
1453
1454 if (ordered_hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
70f50a47
ZJS
1455 r = log_debug_errno(SYNTHETIC_ERRNO(ETOOMANYREFS),
1456 "Too many open journal files, not adding %s.", path ?: "fd");
1457 goto error;
5d1ce257
LP
1458 }
1459
49615dbd 1460 r = journal_file_open(fd, path, O_RDONLY, 0, 0, 0, NULL, j->mmap, NULL, &f);
d617408e 1461 if (r < 0) {
85b0ff8a 1462 log_debug_errno(r, "Failed to open journal file %s: %m", path ?: "from fd");
70f50a47 1463 goto error;
d617408e 1464 }
3fbf9cbb 1465
72f59706 1466 /* journal_file_dump(f); */
de190aef 1467
85b0ff8a 1468 /* journal_file_open() generates an replacement fname if necessary, so we can use f->path. */
c1f906bd 1469 r = ordered_hashmap_put(j->files, f->path, f);
3fbf9cbb 1470 if (r < 0) {
85b0ff8a
ZJS
1471 f->close_fd = false; /* Make sure journal_file_close() doesn't close the caller's fd
1472 * (or our own). The caller or we will do that ourselves. */
69a3a6fd 1473 (void) journal_file_close(f);
70f50a47 1474 goto error;
3fbf9cbb
LP
1475 }
1476
70f50a47 1477 TAKE_FD(our_fd); /* the fd is now owned by the JournalFile object */
5d1ce257 1478
9c66f528 1479 f->last_seen_generation = j->generation;
a50d7d43 1480
9c66f528 1481 track_file_disposition(j, f);
85210bff 1482 check_network(j, f->fd);
34af7494 1483 (void) journal_file_read_tail_timestamp(j, f);
85210bff 1484
313cefa1 1485 j->current_invalidate_counter++;
a963990f 1486
9c66f528 1487 log_debug("File %s added.", f->path);
d617408e 1488
70f50a47 1489 return 0;
d617408e 1490
70f50a47
ZJS
1491error:
1492 (void) journal_put_error(j, r, path); /* path==NULL is OK. */
d617408e 1493 return r;
50f20cfd
LP
1494}
1495
fc1813c0
LP
1496static int add_file_by_name(
1497 sd_journal *j,
1498 const char *prefix,
1499 const char *filename) {
1500
8e7e4a73 1501 _cleanup_free_ char *path = NULL;
5302ebe1
ZJS
1502
1503 assert(j);
1504 assert(prefix);
1505 assert(filename);
1506
5d1ce257 1507 if (j->no_new_files)
5302ebe1
ZJS
1508 return 0;
1509
5d1ce257
LP
1510 if (!file_type_wanted(j->flags, filename))
1511 return 0;
39fd5b08 1512
8e7e4a73
LP
1513 path = path_join(prefix, filename);
1514 if (!path)
1515 return -ENOMEM;
1516
5d1ce257 1517 return add_any_file(j, -1, path);
5302ebe1
ZJS
1518}
1519
8e7e4a73 1520static int remove_file_by_name(
fc1813c0
LP
1521 sd_journal *j,
1522 const char *prefix,
1523 const char *filename) {
1524
8e7e4a73 1525 _cleanup_free_ char *path = NULL;
50f20cfd
LP
1526 JournalFile *f;
1527
1528 assert(j);
1529 assert(prefix);
1530 assert(filename);
1531
8e7e4a73
LP
1532 path = path_join(prefix, filename);
1533 if (!path)
1534 return -ENOMEM;
1535
c1f906bd 1536 f = ordered_hashmap_get(j->files, path);
50f20cfd 1537 if (!f)
8e7e4a73 1538 return 0;
50f20cfd 1539
a9a245c1 1540 remove_file_real(j, f);
8e7e4a73 1541 return 1;
a9a245c1
ZJS
1542}
1543
1544static void remove_file_real(sd_journal *j, JournalFile *f) {
1545 assert(j);
1546 assert(f);
1547
fc1813c0 1548 (void) ordered_hashmap_remove(j->files, f->path);
44a5fa34 1549
5ec76417 1550 log_debug("File %s removed.", f->path);
44a5fa34 1551
3c1668da
LP
1552 if (j->current_file == f) {
1553 j->current_file = NULL;
1554 j->current_field = 0;
1555 }
1556
1557 if (j->unique_file == f) {
360af4cf 1558 /* Jump to the next unique_file or NULL if that one was last */
c1f906bd 1559 j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
3c1668da 1560 j->unique_offset = 0;
360af4cf
ZJS
1561 if (!j->unique_file)
1562 j->unique_file_lost = true;
3c1668da
LP
1563 }
1564
eb86030e
LP
1565 if (j->fields_file == f) {
1566 j->fields_file = ordered_hashmap_next(j->files, j->fields_file->path);
1567 j->fields_offset = 0;
1568 if (!j->fields_file)
1569 j->fields_file_lost = true;
1570 }
1571
34af7494 1572 journal_file_unlink_newest_by_bood_id(j, f);
69a3a6fd 1573 (void) journal_file_close(f);
50f20cfd 1574
313cefa1 1575 j->current_invalidate_counter++;
3fbf9cbb
LP
1576}
1577
d617408e
LP
1578static int dirname_is_machine_id(const char *fn) {
1579 sd_id128_t id, machine;
456aa879 1580 const char *e;
d617408e
LP
1581 int r;
1582
456aa879
LP
1583 /* Returns true if the specified directory name matches the local machine ID */
1584
d617408e
LP
1585 r = sd_id128_get_machine(&machine);
1586 if (r < 0)
1587 return r;
1588
456aa879
LP
1589 e = strchr(fn, '.');
1590 if (e) {
1591 const char *k;
1592
1593 /* Looks like it has a namespace suffix. Verify that. */
1594 if (!log_namespace_name_valid(e + 1))
1595 return false;
1596
2f82562b 1597 k = strndupa_safe(fn, e - fn);
456aa879
LP
1598 r = sd_id128_from_string(k, &id);
1599 } else
1600 r = sd_id128_from_string(fn, &id);
d617408e
LP
1601 if (r < 0)
1602 return r;
1603
1604 return sd_id128_equal(id, machine);
1605}
1606
456aa879
LP
1607static int dirname_has_namespace(const char *fn, const char *namespace) {
1608 const char *e;
1609
1610 /* Returns true if the specified directory name matches the specified namespace */
1611
1612 e = strchr(fn, '.');
1613 if (e) {
1614 const char *k;
1615
1616 if (!namespace)
1617 return false;
1618
1619 if (!streq(e + 1, namespace))
1620 return false;
1621
2f82562b 1622 k = strndupa_safe(fn, e - fn);
456aa879
LP
1623 return id128_is_valid(k);
1624 }
1625
1626 if (namespace)
1627 return false;
1628
1629 return id128_is_valid(fn);
1630}
1631
858749f7
LP
1632static bool dirent_is_journal_file(const struct dirent *de) {
1633 assert(de);
1634
456aa879
LP
1635 /* Returns true if the specified directory entry looks like a journal file we might be interested in */
1636
858749f7
LP
1637 if (!IN_SET(de->d_type, DT_REG, DT_LNK, DT_UNKNOWN))
1638 return false;
1639
1640 return endswith(de->d_name, ".journal") ||
1641 endswith(de->d_name, ".journal~");
1642}
1643
456aa879
LP
1644static bool dirent_is_journal_subdir(const struct dirent *de) {
1645 const char *e, *n;
858749f7
LP
1646 assert(de);
1647
456aa879 1648 /* returns true if the specified directory entry looks like a directory that might contain journal
da890466 1649 * files we might be interested in, i.e. is either a 128-bit ID or a 128-bit ID suffixed by a
456aa879
LP
1650 * namespace. */
1651
858749f7
LP
1652 if (!IN_SET(de->d_type, DT_DIR, DT_LNK, DT_UNKNOWN))
1653 return false;
1654
456aa879
LP
1655 e = strchr(de->d_name, '.');
1656 if (!e)
1657 return id128_is_valid(de->d_name); /* No namespace */
1658
2f82562b 1659 n = strndupa_safe(de->d_name, e - de->d_name);
456aa879
LP
1660 if (!id128_is_valid(n))
1661 return false;
1662
1663 return log_namespace_name_valid(e + 1);
858749f7
LP
1664}
1665
1666static int directory_open(sd_journal *j, const char *path, DIR **ret) {
1667 DIR *d;
1668
1669 assert(j);
1670 assert(path);
1671 assert(ret);
1672
1673 if (j->toplevel_fd < 0)
1674 d = opendir(path);
1675 else
1676 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1677 * relative, by dropping the initial slash */
1678 d = xopendirat(j->toplevel_fd, skip_slash(path), 0);
1679 if (!d)
1680 return -errno;
1681
1682 *ret = d;
1683 return 0;
1684}
1685
1686static int add_directory(sd_journal *j, const char *prefix, const char *dirname);
1687
1688static void directory_enumerate(sd_journal *j, Directory *m, DIR *d) {
858749f7
LP
1689 assert(j);
1690 assert(m);
1691 assert(d);
1692
1693 FOREACH_DIRENT_ALL(de, d, goto fail) {
1694 if (dirent_is_journal_file(de))
fc1813c0 1695 (void) add_file_by_name(j, m->path, de->d_name);
858749f7 1696
456aa879 1697 if (m->is_root && dirent_is_journal_subdir(de))
858749f7
LP
1698 (void) add_directory(j, m->path, de->d_name);
1699 }
1700
1701 return;
858749f7
LP
1702fail:
1703 log_debug_errno(errno, "Failed to enumerate directory %s, ignoring: %m", m->path);
1704}
1705
1706static void directory_watch(sd_journal *j, Directory *m, int fd, uint32_t mask) {
1707 int r;
1708
1709 assert(j);
1710 assert(m);
1711 assert(fd >= 0);
1712
1713 /* Watch this directory if that's enabled and if it not being watched yet. */
1714
1715 if (m->wd > 0) /* Already have a watch? */
1716 return;
1717 if (j->inotify_fd < 0) /* Not watching at all? */
1718 return;
1719
1720 m->wd = inotify_add_watch_fd(j->inotify_fd, fd, mask);
1721 if (m->wd < 0) {
1722 log_debug_errno(errno, "Failed to watch journal directory '%s', ignoring: %m", m->path);
1723 return;
1724 }
1725
1726 r = hashmap_put(j->directories_by_wd, INT_TO_PTR(m->wd), m);
1727 if (r == -EEXIST)
1728 log_debug_errno(r, "Directory '%s' already being watched under a different path, ignoring: %m", m->path);
1729 if (r < 0) {
1730 log_debug_errno(r, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m->path);
1731 (void) inotify_rm_watch(j->inotify_fd, m->wd);
1732 m->wd = -1;
1733 }
1734}
1735
456aa879
LP
1736static int add_directory(
1737 sd_journal *j,
1738 const char *prefix,
1739 const char *dirname) {
1740
7fd1b19b 1741 _cleanup_free_ char *path = NULL;
7fd1b19b 1742 _cleanup_closedir_ DIR *d = NULL;
a963990f 1743 Directory *m;
d617408e 1744 int r, k;
3fbf9cbb
LP
1745
1746 assert(j);
1747 assert(prefix);
3fbf9cbb 1748
5d1ce257
LP
1749 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1750 * and reenumerates directory contents */
d95b1fb3 1751
657ee2d8 1752 path = path_join(prefix, dirname);
d617408e
LP
1753 if (!path) {
1754 r = -ENOMEM;
1755 goto fail;
1756 }
3fbf9cbb 1757
858749f7 1758 log_debug("Considering directory '%s'.", path);
5d1ce257
LP
1759
1760 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1761 if ((j->flags & SD_JOURNAL_LOCAL_ONLY) &&
1762 !((dirname && dirname_is_machine_id(dirname) > 0) || path_has_prefix(j, path, "/run")))
858749f7 1763 return 0;
5d1ce257 1764
2b6df46d
LP
1765 if (dirname &&
1766 (!(FLAGS_SET(j->flags, SD_JOURNAL_ALL_NAMESPACES) ||
1767 dirname_has_namespace(dirname, j->namespace) > 0 ||
1768 (FLAGS_SET(j->flags, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE) && dirname_has_namespace(dirname, NULL) > 0))))
456aa879
LP
1769 return 0;
1770
858749f7
LP
1771 r = directory_open(j, path, &d);
1772 if (r < 0) {
1773 log_debug_errno(r, "Failed to open directory '%s': %m", path);
d617408e 1774 goto fail;
3fbf9cbb
LP
1775 }
1776
a963990f
LP
1777 m = hashmap_get(j->directories_by_path, path);
1778 if (!m) {
2f5435a1 1779 m = new(Directory, 1);
d617408e
LP
1780 if (!m) {
1781 r = -ENOMEM;
1782 goto fail;
1783 }
a963990f 1784
2f5435a1
LP
1785 *m = (Directory) {
1786 .is_root = false,
1787 .path = path,
1788 };
a963990f
LP
1789
1790 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
a963990f 1791 free(m);
d617408e
LP
1792 r = -ENOMEM;
1793 goto fail;
a963990f
LP
1794 }
1795
a50d7d43 1796 path = NULL; /* avoid freeing in cleanup */
313cefa1 1797 j->current_invalidate_counter++;
a963990f 1798
5ec76417 1799 log_debug("Directory %s added.", m->path);
a963990f 1800
a50d7d43 1801 } else if (m->is_root)
858749f7 1802 return 0; /* Don't 'downgrade' from root directory */
a963990f 1803
858749f7 1804 m->last_seen_generation = j->generation;
a963990f 1805
858749f7
LP
1806 directory_watch(j, m, dirfd(d),
1807 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1808 IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|IN_MOVED_FROM|
1809 IN_ONLYDIR);
a963990f 1810
858749f7
LP
1811 if (!j->no_new_files)
1812 directory_enumerate(j, m, d);
a963990f 1813
85210bff
LP
1814 check_network(j, dirfd(d));
1815
a963990f 1816 return 0;
d617408e
LP
1817
1818fail:
5d1ce257 1819 k = journal_put_error(j, r, path ?: prefix);
d617408e
LP
1820 if (k < 0)
1821 return k;
1822
1823 return r;
a963990f
LP
1824}
1825
d617408e 1826static int add_root_directory(sd_journal *j, const char *p, bool missing_ok) {
5d1ce257 1827
7fd1b19b 1828 _cleanup_closedir_ DIR *d = NULL;
a963990f 1829 Directory *m;
d617408e 1830 int r, k;
a963990f
LP
1831
1832 assert(j);
a963990f 1833
5d1ce257
LP
1834 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
1835 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
1836 * populate the set, as well as to update it later. */
a963990f 1837
5d1ce257
LP
1838 if (p) {
1839 /* If there's a path specified, use it. */
b6741478 1840
858749f7
LP
1841 log_debug("Considering root directory '%s'.", p);
1842
5d1ce257
LP
1843 if ((j->flags & SD_JOURNAL_RUNTIME_ONLY) &&
1844 !path_has_prefix(j, p, "/run"))
1845 return -EINVAL;
d617408e 1846
5d1ce257
LP
1847 if (j->prefix)
1848 p = strjoina(j->prefix, p);
1849
858749f7
LP
1850 r = directory_open(j, p, &d);
1851 if (r == -ENOENT && missing_ok)
1852 return 0;
1853 if (r < 0) {
1854 log_debug_errno(r, "Failed to open root directory %s: %m", p);
5d1ce257
LP
1855 goto fail;
1856 }
1857 } else {
254d1313 1858 _cleanup_close_ int dfd = -EBADF;
5d1ce257
LP
1859
1860 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
1861 * opendir() will take possession of the fd, and close it, which we don't want. */
1862
1863 p = "."; /* store this as "." in the directories hashmap */
1864
1865 dfd = fcntl(j->toplevel_fd, F_DUPFD_CLOEXEC, 3);
1866 if (dfd < 0) {
1867 r = -errno;
1868 goto fail;
1869 }
1870
8e06af80 1871 d = take_fdopendir(&dfd);
5d1ce257
LP
1872 if (!d) {
1873 r = -errno;
5d1ce257
LP
1874 goto fail;
1875 }
1876
1877 rewinddir(d);
d617408e 1878 }
a963990f
LP
1879
1880 m = hashmap_get(j->directories_by_path, p);
1881 if (!m) {
1882 m = new0(Directory, 1);
d617408e
LP
1883 if (!m) {
1884 r = -ENOMEM;
1885 goto fail;
1886 }
a963990f
LP
1887
1888 m->is_root = true;
5d1ce257 1889
a963990f
LP
1890 m->path = strdup(p);
1891 if (!m->path) {
a963990f 1892 free(m);
d617408e
LP
1893 r = -ENOMEM;
1894 goto fail;
a963990f
LP
1895 }
1896
1897 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
a963990f
LP
1898 free(m->path);
1899 free(m);
d617408e
LP
1900 r = -ENOMEM;
1901 goto fail;
a963990f
LP
1902 }
1903
313cefa1 1904 j->current_invalidate_counter++;
a963990f 1905
5ec76417 1906 log_debug("Root directory %s added.", m->path);
a963990f 1907
a50d7d43 1908 } else if (!m->is_root)
a963990f 1909 return 0;
50f20cfd 1910
858749f7
LP
1911 directory_watch(j, m, dirfd(d),
1912 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1913 IN_ONLYDIR);
a963990f 1914
858749f7
LP
1915 if (!j->no_new_files)
1916 directory_enumerate(j, m, d);
3fbf9cbb 1917
85210bff
LP
1918 check_network(j, dirfd(d));
1919
a963990f 1920 return 0;
d617408e
LP
1921
1922fail:
5768d259 1923 k = journal_put_error(j, r, p);
d617408e
LP
1924 if (k < 0)
1925 return k;
1926
1927 return r;
a963990f
LP
1928}
1929
b2b46f91 1930static void remove_directory(sd_journal *j, Directory *d) {
a963990f
LP
1931 assert(j);
1932
1933 if (d->wd > 0) {
1934 hashmap_remove(j->directories_by_wd, INT_TO_PTR(d->wd));
1935
1936 if (j->inotify_fd >= 0)
98d4a4df 1937 (void) inotify_rm_watch(j->inotify_fd, d->wd);
a963990f
LP
1938 }
1939
1940 hashmap_remove(j->directories_by_path, d->path);
1941
1942 if (d->is_root)
5ec76417 1943 log_debug("Root directory %s removed.", d->path);
a963990f 1944 else
5ec76417 1945 log_debug("Directory %s removed.", d->path);
a963990f
LP
1946
1947 free(d->path);
1948 free(d);
3fbf9cbb
LP
1949}
1950
89739579 1951static int add_search_paths(sd_journal *j) {
d617408e
LP
1952
1953 static const char search_paths[] =
a963990f
LP
1954 "/run/log/journal\0"
1955 "/var/log/journal\0";
50f20cfd
LP
1956
1957 assert(j);
50f20cfd 1958
a963990f
LP
1959 /* We ignore most errors here, since the idea is to only open
1960 * what's actually accessible, and ignore the rest. */
50f20cfd 1961
d617408e
LP
1962 NULSTR_FOREACH(p, search_paths)
1963 (void) add_root_directory(j, p, true);
50f20cfd 1964
574b77ef
MB
1965 if (!(j->flags & SD_JOURNAL_LOCAL_ONLY))
1966 (void) add_root_directory(j, "/var/log/journal/remote", true);
1967
a963990f 1968 return 0;
50f20cfd
LP
1969}
1970
5302ebe1 1971static int add_current_paths(sd_journal *j) {
5302ebe1
ZJS
1972 JournalFile *f;
1973
1974 assert(j);
1975 assert(j->no_new_files);
1976
5d1ce257 1977 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
5302ebe1
ZJS
1978 * treat them as fatal. */
1979
90e74a66 1980 ORDERED_HASHMAP_FOREACH(f, j->files) {
c2b2df60 1981 _cleanup_free_ char *dir = NULL;
e9174f29 1982 int r;
5302ebe1 1983
45519d13
LP
1984 r = path_extract_directory(f->path, &dir);
1985 if (r < 0)
1986 return r;
5302ebe1 1987
5d1ce257 1988 r = add_directory(j, dir, NULL);
d617408e 1989 if (r < 0)
5302ebe1 1990 return r;
5302ebe1
ZJS
1991 }
1992
1993 return 0;
1994}
1995
a963990f 1996static int allocate_inotify(sd_journal *j) {
50f20cfd 1997 assert(j);
50f20cfd 1998
a963990f
LP
1999 if (j->inotify_fd < 0) {
2000 j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
2001 if (j->inotify_fd < 0)
2002 return -errno;
2003 }
50f20cfd 2004
cb306f5d 2005 return hashmap_ensure_allocated(&j->directories_by_wd, NULL);
50f20cfd
LP
2006}
2007
456aa879 2008static sd_journal *journal_new(int flags, const char *path, const char *namespace) {
17c9aff8 2009 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
50f20cfd 2010
6f30a67a 2011 j = new(sd_journal, 1);
a963990f
LP
2012 if (!j)
2013 return NULL;
50f20cfd 2014
6f30a67a 2015 *j = (sd_journal) {
e046719b 2016 .origin_id = origin_id_query(),
6f30a67a
LP
2017 .toplevel_fd = -EBADF,
2018 .inotify_fd = -EBADF,
2019 .flags = flags,
2020 .data_threshold = DEFAULT_DATA_THRESHOLD,
2021 };
50f20cfd 2022
7827b1a1 2023 if (path) {
16fefe90
ZJS
2024 char *t;
2025
2026 t = strdup(path);
2027 if (!t)
17c9aff8 2028 return NULL;
16fefe90
ZJS
2029
2030 if (flags & SD_JOURNAL_OS_ROOT)
2031 j->prefix = t;
2032 else
2033 j->path = t;
7827b1a1
LP
2034 }
2035
456aa879
LP
2036 if (namespace) {
2037 j->namespace = strdup(namespace);
2038 if (!j->namespace)
2039 return NULL;
2040 }
2041
548f6937 2042 j->files = ordered_hashmap_new(&path_hash_ops);
5d4ba7f2 2043 if (!j->files)
17c9aff8 2044 return NULL;
5d4ba7f2
VC
2045
2046 j->files_cache = ordered_hashmap_iterated_cache_new(j->files);
548f6937 2047 j->directories_by_path = hashmap_new(&path_hash_ops);
84168d80 2048 j->mmap = mmap_cache_new();
5d4ba7f2 2049 if (!j->files_cache || !j->directories_by_path || !j->mmap)
17c9aff8 2050 return NULL;
6180fc61 2051
17c9aff8 2052 return TAKE_PTR(j);
50f20cfd
LP
2053}
2054
1aaa68f5
ZJS
2055#define OPEN_ALLOWED_FLAGS \
2056 (SD_JOURNAL_LOCAL_ONLY | \
2057 SD_JOURNAL_RUNTIME_ONLY | \
456aa879
LP
2058 SD_JOURNAL_SYSTEM | \
2059 SD_JOURNAL_CURRENT_USER | \
2060 SD_JOURNAL_ALL_NAMESPACES | \
2061 SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE)
1aaa68f5 2062
456aa879 2063_public_ int sd_journal_open_namespace(sd_journal **ret, const char *namespace, int flags) {
17c9aff8 2064 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
3fbf9cbb 2065 int r;
87d2c1ff 2066
1ae464e0 2067 assert_return(ret, -EINVAL);
1aaa68f5 2068 assert_return((flags & ~OPEN_ALLOWED_FLAGS) == 0, -EINVAL);
87d2c1ff 2069
456aa879 2070 j = journal_new(flags, NULL, namespace);
87d2c1ff
LP
2071 if (!j)
2072 return -ENOMEM;
2073
89739579 2074 r = add_search_paths(j);
a963990f 2075 if (r < 0)
17c9aff8 2076 return r;
50f20cfd 2077
17c9aff8 2078 *ret = TAKE_PTR(j);
a963990f 2079 return 0;
a963990f 2080}
50f20cfd 2081
456aa879
LP
2082_public_ int sd_journal_open(sd_journal **ret, int flags) {
2083 return sd_journal_open_namespace(ret, NULL, flags);
2084}
2085
1aaa68f5
ZJS
2086#define OPEN_CONTAINER_ALLOWED_FLAGS \
2087 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
2088
b6741478
LP
2089_public_ int sd_journal_open_container(sd_journal **ret, const char *machine, int flags) {
2090 _cleanup_free_ char *root = NULL, *class = NULL;
17c9aff8 2091 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
b6741478
LP
2092 char *p;
2093 int r;
2094
68312977 2095 /* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in
2daa9cbd
LP
2096 * combination with sd_journal_open_directory_fd(). */
2097
b6741478
LP
2098 assert_return(machine, -EINVAL);
2099 assert_return(ret, -EINVAL);
1aaa68f5 2100 assert_return((flags & ~OPEN_CONTAINER_ALLOWED_FLAGS) == 0, -EINVAL);
52ef5dd7 2101 assert_return(hostname_is_valid(machine, 0), -EINVAL);
b6741478 2102
63c372cb 2103 p = strjoina("/run/systemd/machines/", machine);
13df9c39
LP
2104 r = parse_env_file(NULL, p,
2105 "ROOT", &root,
2106 "CLASS", &class);
b6741478
LP
2107 if (r == -ENOENT)
2108 return -EHOSTDOWN;
2109 if (r < 0)
2110 return r;
2111 if (!root)
2112 return -ENODATA;
2113
2114 if (!streq_ptr(class, "container"))
2115 return -EIO;
2116
456aa879 2117 j = journal_new(flags, root, NULL);
b6741478
LP
2118 if (!j)
2119 return -ENOMEM;
2120
89739579 2121 r = add_search_paths(j);
b6741478 2122 if (r < 0)
17c9aff8 2123 return r;
b6741478 2124
17c9aff8 2125 *ret = TAKE_PTR(j);
b6741478 2126 return 0;
b6741478
LP
2127}
2128
1aaa68f5 2129#define OPEN_DIRECTORY_ALLOWED_FLAGS \
10752e82
ZJS
2130 (SD_JOURNAL_OS_ROOT | \
2131 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1aaa68f5 2132
a963990f 2133_public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) {
17c9aff8 2134 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
a963990f 2135 int r;
87d2c1ff 2136
1ae464e0
TA
2137 assert_return(ret, -EINVAL);
2138 assert_return(path, -EINVAL);
1aaa68f5 2139 assert_return((flags & ~OPEN_DIRECTORY_ALLOWED_FLAGS) == 0, -EINVAL);
87d2c1ff 2140
456aa879 2141 j = journal_new(flags, path, NULL);
a963990f
LP
2142 if (!j)
2143 return -ENOMEM;
3fbf9cbb 2144
d077390c
LP
2145 if (flags & SD_JOURNAL_OS_ROOT)
2146 r = add_search_paths(j);
2147 else
2148 r = add_root_directory(j, path, false);
d617408e 2149 if (r < 0)
17c9aff8 2150 return r;
87d2c1ff 2151
17c9aff8 2152 *ret = TAKE_PTR(j);
87d2c1ff 2153 return 0;
a963990f 2154}
87d2c1ff 2155
5302ebe1 2156_public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int flags) {
17c9aff8 2157 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
5302ebe1
ZJS
2158 int r;
2159
1ae464e0
TA
2160 assert_return(ret, -EINVAL);
2161 assert_return(flags == 0, -EINVAL);
5302ebe1 2162
456aa879 2163 j = journal_new(flags, NULL, NULL);
5302ebe1
ZJS
2164 if (!j)
2165 return -ENOMEM;
2166
2167 STRV_FOREACH(path, paths) {
5d1ce257 2168 r = add_any_file(j, -1, *path);
d617408e 2169 if (r < 0)
17c9aff8 2170 return r;
5302ebe1
ZJS
2171 }
2172
2173 j->no_new_files = true;
2174
17c9aff8 2175 *ret = TAKE_PTR(j);
5302ebe1 2176 return 0;
5d1ce257
LP
2177}
2178
4a45a2e0 2179#define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
10752e82 2180 (SD_JOURNAL_OS_ROOT | \
4a45a2e0
YW
2181 SD_JOURNAL_SYSTEM | \
2182 SD_JOURNAL_CURRENT_USER | \
2183 SD_JOURNAL_TAKE_DIRECTORY_FD)
1aaa68f5 2184
5d1ce257 2185_public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) {
17c9aff8 2186 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
5d1ce257 2187 struct stat st;
4a45a2e0 2188 bool take_fd;
5d1ce257
LP
2189 int r;
2190
2191 assert_return(ret, -EINVAL);
2192 assert_return(fd >= 0, -EBADF);
1aaa68f5 2193 assert_return((flags & ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS) == 0, -EINVAL);
5d1ce257
LP
2194
2195 if (fstat(fd, &st) < 0)
2196 return -errno;
2197
2198 if (!S_ISDIR(st.st_mode))
2199 return -EBADFD;
2200
4a45a2e0
YW
2201 take_fd = FLAGS_SET(flags, SD_JOURNAL_TAKE_DIRECTORY_FD);
2202 j = journal_new(flags & ~SD_JOURNAL_TAKE_DIRECTORY_FD, NULL, NULL);
5d1ce257
LP
2203 if (!j)
2204 return -ENOMEM;
2205
2206 j->toplevel_fd = fd;
2207
d077390c
LP
2208 if (flags & SD_JOURNAL_OS_ROOT)
2209 r = add_search_paths(j);
2210 else
2211 r = add_root_directory(j, NULL, false);
5d1ce257 2212 if (r < 0)
17c9aff8 2213 return r;
5d1ce257 2214
4a45a2e0
YW
2215 SET_FLAG(j->flags, SD_JOURNAL_TAKE_DIRECTORY_FD, take_fd);
2216
17c9aff8 2217 *ret = TAKE_PTR(j);
5d1ce257 2218 return 0;
5d1ce257
LP
2219}
2220
2221_public_ int sd_journal_open_files_fd(sd_journal **ret, int fds[], unsigned n_fds, int flags) {
5d1ce257 2222 JournalFile *f;
17c9aff8 2223 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
5d1ce257
LP
2224 int r;
2225
2226 assert_return(ret, -EINVAL);
2227 assert_return(n_fds > 0, -EBADF);
2228 assert_return(flags == 0, -EINVAL);
2229
456aa879 2230 j = journal_new(flags, NULL, NULL);
5d1ce257
LP
2231 if (!j)
2232 return -ENOMEM;
2233
abcdc02c 2234 for (unsigned i = 0; i < n_fds; i++) {
5d1ce257
LP
2235 struct stat st;
2236
2237 if (fds[i] < 0) {
2238 r = -EBADF;
2239 goto fail;
2240 }
2241
2242 if (fstat(fds[i], &st) < 0) {
2243 r = -errno;
2244 goto fail;
2245 }
2246
3cc44114
LP
2247 r = stat_verify_regular(&st);
2248 if (r < 0)
5d1ce257 2249 goto fail;
5d1ce257
LP
2250
2251 r = add_any_file(j, fds[i], NULL);
2252 if (r < 0)
2253 goto fail;
2254 }
2255
2256 j->no_new_files = true;
2257 j->no_inotify = true;
5302ebe1 2258
17c9aff8 2259 *ret = TAKE_PTR(j);
5d1ce257
LP
2260 return 0;
2261
2262fail:
f8e2f4d6 2263 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
5d1ce257 2264 * remain open */
90e74a66 2265 ORDERED_HASHMAP_FOREACH(f, j->files)
5d1ce257
LP
2266 f->close_fd = false;
2267
5302ebe1
ZJS
2268 return r;
2269}
2270
a5344d2c 2271_public_ void sd_journal_close(sd_journal *j) {
a963990f 2272 Directory *d;
34af7494 2273 Prioq *p;
a963990f 2274
e046719b 2275 if (!j || journal_origin_changed(j))
a5344d2c 2276 return;
87d2c1ff 2277
34af7494
LP
2278 while ((p = hashmap_first(j->newest_by_boot_id)))
2279 journal_file_unlink_newest_by_bood_id(j, prioq_peek(p));
2280 hashmap_free(j->newest_by_boot_id);
2281
54b1da83
LP
2282 sd_journal_flush_matches(j);
2283
f9168190 2284 ordered_hashmap_free_with_destructor(j->files, journal_file_close);
5d4ba7f2 2285 iterated_cache_free(j->files_cache);
260a2be4 2286
a963990f
LP
2287 while ((d = hashmap_first(j->directories_by_path)))
2288 remove_directory(j, d);
260a2be4 2289
a963990f
LP
2290 while ((d = hashmap_first(j->directories_by_wd)))
2291 remove_directory(j, d);
87d2c1ff 2292
a963990f
LP
2293 hashmap_free(j->directories_by_path);
2294 hashmap_free(j->directories_by_wd);
1cc101f1 2295
4a45a2e0
YW
2296 if (FLAGS_SET(j->flags, SD_JOURNAL_TAKE_DIRECTORY_FD))
2297 safe_close(j->toplevel_fd);
2298
03e334a1 2299 safe_close(j->inotify_fd);
50f20cfd 2300
bf807d4d 2301 if (j->mmap) {
3a595c59 2302 mmap_cache_stats_log_debug(j->mmap);
16e9f408 2303 mmap_cache_unref(j->mmap);
bf807d4d 2304 }
16e9f408 2305
ec1d2909 2306 hashmap_free_free(j->errors);
5768d259 2307
7827b1a1 2308 free(j->path);
89739579 2309 free(j->prefix);
456aa879 2310 free(j->namespace);
3c1668da 2311 free(j->unique_field);
eb86030e 2312 free(j->fields_buffer);
87d2c1ff
LP
2313 free(j);
2314}
3fbf9cbb 2315
34af7494
LP
2316static void journal_file_unlink_newest_by_bood_id(sd_journal *j, JournalFile *f) {
2317 JournalFile *nf;
2318 Prioq *p;
2319
2320 assert(j);
2321 assert(f);
2322
2323 if (f->newest_boot_id_prioq_idx == PRIOQ_IDX_NULL) /* not linked currently, hence this is a NOP */
2324 return;
2325
2326 assert_se(p = hashmap_get(j->newest_by_boot_id, &f->newest_boot_id));
2327 assert_se(prioq_remove(p, f, &f->newest_boot_id_prioq_idx) > 0);
2328
2329 nf = prioq_peek(p);
2330 if (nf)
2331 /* There's still a member in the prioq? Then make sure the hashmap key now points to its
2332 * .newest_boot_id field (and not ours!). Not we only replace the memory of the key here, the
2333 * value of the key (and the data associated with it) remain the same. */
e28d8251 2334 assert_se(hashmap_replace(j->newest_by_boot_id, &nf->newest_boot_id, p) >= 0);
34af7494
LP
2335 else {
2336 assert_se(hashmap_remove(j->newest_by_boot_id, &f->newest_boot_id) == p);
2337 prioq_free(p);
2338 }
2339
2340 f->newest_boot_id_prioq_idx = PRIOQ_IDX_NULL;
2341}
2342
2343static int journal_file_newest_monotonic_compare(const void *a, const void *b) {
2344 const JournalFile *x = a, *y = b;
2345
2346 return -CMP(x->newest_monotonic_usec, y->newest_monotonic_usec); /* Invert order, we want newest first! */
2347}
2348
2349static int journal_file_reshuffle_newest_by_boot_id(sd_journal *j, JournalFile *f) {
2350 Prioq *p;
2351 int r;
2352
2353 assert(j);
2354 assert(f);
2355
2356 p = hashmap_get(j->newest_by_boot_id, &f->newest_boot_id);
2357 if (p) {
2358 /* There's already a priority queue for this boot ID */
2359
2360 if (f->newest_boot_id_prioq_idx == PRIOQ_IDX_NULL) {
2361 r = prioq_put(p, f, &f->newest_boot_id_prioq_idx); /* Insert if we aren't in there yet */
2362 if (r < 0)
2363 return r;
2364 } else
2365 prioq_reshuffle(p, f, &f->newest_boot_id_prioq_idx); /* Reshuffle otherwise */
2366
2367 } else {
2368 _cleanup_(prioq_freep) Prioq *q = NULL;
2369
2370 /* No priority queue yet, then allocate one */
2371
2372 assert(f->newest_boot_id_prioq_idx == PRIOQ_IDX_NULL); /* we can't be a member either */
2373
2374 q = prioq_new(journal_file_newest_monotonic_compare);
2375 if (!q)
2376 return -ENOMEM;
2377
2378 r = prioq_put(q, f, &f->newest_boot_id_prioq_idx);
2379 if (r < 0)
2380 return r;
2381
2382 r = hashmap_ensure_put(&j->newest_by_boot_id, &id128_hash_ops, &f->newest_boot_id, q);
f11de491
YW
2383 if (r < 0) {
2384 f->newest_boot_id_prioq_idx = PRIOQ_IDX_NULL;
34af7494 2385 return r;
f11de491 2386 }
34af7494
LP
2387
2388 TAKE_PTR(q);
2389 }
2390
2391 return 0;
2392}
2393
2394static int journal_file_read_tail_timestamp(sd_journal *j, JournalFile *f) {
2395 uint64_t offset, mo, rt;
2396 sd_id128_t id;
2397 ObjectType type;
2398 Object *o;
2399 int r;
2400
2401 assert(j);
2402 assert(f);
2403 assert(f->header);
2404
2405 /* Tries to read the timestamp of the most recently written entry. */
2406
2407 r = journal_file_fstat(f);
2408 if (r < 0)
2409 return r;
2410 if (f->newest_mtime == timespec_load(&f->last_stat.st_mtim))
2411 return 0; /* mtime didn't change since last time, don't bother */
2412
2413 if (JOURNAL_HEADER_CONTAINS(f->header, tail_entry_offset)) {
2414 offset = le64toh(READ_NOW(f->header->tail_entry_offset));
2415 type = OBJECT_ENTRY;
2416 } else {
2417 offset = le64toh(READ_NOW(f->header->tail_object_offset));
2418 type = OBJECT_UNUSED;
2419 }
2420 if (offset == 0)
2421 return -ENODATA; /* not a single object/entry, hence no tail timestamp */
2422
2423 /* Move to the last object in the journal file, in the hope it is an entry (which it usually will
2424 * be). If we lack the "tail_entry_offset" field in the header, we specify the type as OBJECT_UNUSED
2425 * here, since we cannot be sure what the last object will be, and want no noisy logging if it isn't
2426 * an entry. We instead check after figuring out the pointer. */
2427 r = journal_file_move_to_object(f, type, offset, &o);
2428 if (r < 0) {
2429 log_debug_errno(r, "Failed to move to last object in journal file, ignoring: %m");
2430 o = NULL;
2431 }
2432 if (o && o->object.type == OBJECT_ENTRY) {
2433 /* Yay, last object is an entry, let's use the data. */
2434 id = o->entry.boot_id;
2435 mo = le64toh(o->entry.monotonic);
2436 rt = le64toh(o->entry.realtime);
2437 } else {
2438 /* So the object is not an entry or we couldn't access it? In that case, let's read the most
2439 * recent entry timestamps from the header. It's equally good. Unfortunately though, in old
2440 * versions of the journal the boot ID in the header doesn't have to match the monotonic
2441 * timestamp of the header. Let's check the header flag that indicates whether this strictly
2442 * matches first hence, before using the data. */
2443
2444 if (JOURNAL_HEADER_TAIL_ENTRY_BOOT_ID(f->header) && f->header->state == STATE_ARCHIVED) {
2445 mo = le64toh(f->header->tail_entry_monotonic);
2446 rt = le64toh(f->header->tail_entry_realtime);
2447 id = f->header->tail_entry_boot_id;
34af7494
LP
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}