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