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