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