]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/sd-journal.c
sd-journal: allow SYSTEM and CURRENT_USER flags with sd_j_open_directory[_fd]
[thirdparty/systemd.git] / src / journal / sd-journal.c
CommitLineData
87d2c1ff
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2011 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
87d2c1ff
LP
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 14 Lesser General Public License for more details.
87d2c1ff 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
87d2c1ff
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
87d2c1ff 20#include <errno.h>
87d2c1ff 21#include <fcntl.h>
82e24b00 22#include <inttypes.h>
07630cea
LP
23#include <linux/magic.h>
24#include <poll.h>
3fbf9cbb 25#include <stddef.h>
50f20cfd 26#include <sys/inotify.h>
85210bff 27#include <sys/vfs.h>
07630cea 28#include <unistd.h>
87d2c1ff
LP
29
30#include "sd-journal.h"
c004493c 31
b5efdb8a 32#include "alloc-util.h"
07630cea
LP
33#include "catalog.h"
34#include "compress.h"
a0956174 35#include "dirent-util.h"
3ffd4af2 36#include "fd-util.h"
07630cea
LP
37#include "fileio.h"
38#include "formats-util.h"
77601719 39#include "fs-util.h"
07630cea
LP
40#include "hashmap.h"
41#include "hostname-util.h"
c004493c 42#include "io-util.h"
87d2c1ff 43#include "journal-def.h"
cec736d2 44#include "journal-file.h"
07630cea 45#include "journal-internal.h"
cec736d2 46#include "list.h"
de7b95cd 47#include "lookup3.h"
85210bff 48#include "missing.h"
07630cea 49#include "path-util.h"
d4205751 50#include "replace-var.h"
15a5e950
LP
51#include "stat-util.h"
52#include "stdio-util.h"
07630cea
LP
53#include "string-util.h"
54#include "strv.h"
87d2c1ff 55
9e8abdf0
LP
56#define JOURNAL_FILES_MAX 7168
57
85210bff
LP
58#define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
59
d4205751
LP
60#define REPLACE_VAR_MAX 256
61
93b73b06
LP
62#define DEFAULT_DATA_THRESHOLD (64*1024)
63
a9a245c1
ZJS
64static void remove_file_real(sd_journal *j, JournalFile *f);
65
a65f06bb
ZJS
66static bool journal_pid_changed(sd_journal *j) {
67 assert(j);
68
69 /* We don't support people creating a journal object and
70 * keeping it around over a fork(). Let's complain. */
71
72 return j->original_pid != getpid();
73}
74
5768d259
LP
75static int journal_put_error(sd_journal *j, int r, const char *path) {
76 char *copy;
3ac251b8
LP
77 int k;
78
5768d259
LP
79 /* Memorize an error we encountered, and store which
80 * file/directory it was generated from. Note that we store
81 * only *one* path per error code, as the error code is the
82 * key into the hashmap, and the path is the value. This means
83 * we keep track only of all error kinds, but not of all error
84 * locations. This has the benefit that the hashmap cannot
85 * grow beyond bounds.
86 *
87 * We return an error here only if we didn't manage to
88 * memorize the real error. */
89
6fe391c5
ZJS
90 if (r >= 0)
91 return r;
92
5768d259 93 k = hashmap_ensure_allocated(&j->errors, NULL);
3ac251b8
LP
94 if (k < 0)
95 return k;
96
5768d259
LP
97 if (path) {
98 copy = strdup(path);
99 if (!copy)
100 return -ENOMEM;
101 } else
102 copy = NULL;
103
104 k = hashmap_put(j->errors, INT_TO_PTR(r), copy);
105 if (k < 0) {
106 free(copy);
107
108 if (k == -EEXIST)
109 return 0;
110
111 return k;
112 }
113
114 return 0;
6fe391c5
ZJS
115}
116
de190aef 117static void detach_location(sd_journal *j) {
8f9b6cd9
LP
118 Iterator i;
119 JournalFile *f;
120
121 assert(j);
122
123 j->current_file = NULL;
124 j->current_field = 0;
125
c1f906bd 126 ORDERED_HASHMAP_FOREACH(f, j->files, i)
1fc605b0 127 journal_file_reset_location(f);
8f9b6cd9
LP
128}
129
de190aef
LP
130static void reset_location(sd_journal *j) {
131 assert(j);
132
133 detach_location(j);
134 zero(j->current_location);
135}
136
a87247dd 137static void init_location(Location *l, LocationType type, JournalFile *f, Object *o) {
de190aef 138 assert(l);
a87247dd 139 assert(type == LOCATION_DISCRETE || type == LOCATION_SEEK);
de190aef
LP
140 assert(f);
141 assert(o->object.type == OBJECT_ENTRY);
142
a87247dd 143 l->type = type;
de190aef
LP
144 l->seqnum = le64toh(o->entry.seqnum);
145 l->seqnum_id = f->header->seqnum_id;
146 l->realtime = le64toh(o->entry.realtime);
147 l->monotonic = le64toh(o->entry.monotonic);
ce3fd7e7 148 l->boot_id = o->entry.boot_id;
de190aef
LP
149 l->xor_hash = le64toh(o->entry.xor_hash);
150
151 l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true;
152}
153
1eb6332d 154static void set_location(sd_journal *j, JournalFile *f, Object *o) {
de190aef
LP
155 assert(j);
156 assert(f);
157 assert(o);
158
1eb6332d 159 init_location(&j->current_location, LOCATION_DISCRETE, f, o);
de190aef
LP
160
161 j->current_file = f;
162 j->current_field = 0;
163
6573ef05
MS
164 /* Let f know its candidate entry was picked. */
165 assert(f->location_type == LOCATION_SEEK);
166 f->location_type = LOCATION_DISCRETE;
de190aef
LP
167}
168
cbdca852
LP
169static int match_is_valid(const void *data, size_t size) {
170 const char *b, *p;
171
172 assert(data);
173
174 if (size < 2)
175 return false;
176
177 if (startswith(data, "__"))
178 return false;
179
180 b = data;
181 for (p = b; p < b + size; p++) {
182
183 if (*p == '=')
184 return p > b;
185
186 if (*p == '_')
187 continue;
188
189 if (*p >= 'A' && *p <= 'Z')
190 continue;
191
192 if (*p >= '0' && *p <= '9')
193 continue;
194
195 return false;
196 }
197
198 return false;
199}
200
201static bool same_field(const void *_a, size_t s, const void *_b, size_t t) {
de190aef
LP
202 const uint8_t *a = _a, *b = _b;
203 size_t j;
de190aef
LP
204
205 for (j = 0; j < s && j < t; j++) {
206
de190aef 207 if (a[j] != b[j])
cbdca852 208 return false;
de190aef 209
cbdca852
LP
210 if (a[j] == '=')
211 return true;
de190aef
LP
212 }
213
bc302926 214 assert_not_reached("\"=\" not found");
cbdca852
LP
215}
216
217static Match *match_new(Match *p, MatchType t) {
218 Match *m;
219
220 m = new0(Match, 1);
221 if (!m)
222 return NULL;
223
224 m->type = t;
225
226 if (p) {
227 m->parent = p;
71fda00f 228 LIST_PREPEND(matches, p->matches, m);
cbdca852
LP
229 }
230
231 return m;
232}
233
234static void match_free(Match *m) {
235 assert(m);
236
237 while (m->matches)
238 match_free(m->matches);
239
240 if (m->parent)
71fda00f 241 LIST_REMOVE(matches, m->parent->matches, m);
cbdca852
LP
242
243 free(m->data);
244 free(m);
245}
246
247static void match_free_if_empty(Match *m) {
c5a10d9c 248 if (!m || m->matches)
cbdca852
LP
249 return;
250
251 match_free(m);
de190aef
LP
252}
253
a5344d2c 254_public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
cd34b3c6 255 Match *l3, *l4, *add_here = NULL, *m;
4fd052ae 256 le64_t le_hash;
87d2c1ff 257
1ae464e0
TA
258 assert_return(j, -EINVAL);
259 assert_return(!journal_pid_changed(j), -ECHILD);
260 assert_return(data, -EINVAL);
cbdca852
LP
261
262 if (size == 0)
263 size = strlen(data);
264
1ae464e0 265 assert_return(match_is_valid(data, size), -EINVAL);
1cc101f1 266
cd34b3c6
HH
267 /* level 0: AND term
268 * level 1: OR terms
269 * level 2: AND terms
270 * level 3: OR terms
271 * level 4: concrete matches */
cbdca852
LP
272
273 if (!j->level0) {
cd34b3c6 274 j->level0 = match_new(NULL, MATCH_AND_TERM);
cbdca852
LP
275 if (!j->level0)
276 return -ENOMEM;
277 }
278
279 if (!j->level1) {
cd34b3c6 280 j->level1 = match_new(j->level0, MATCH_OR_TERM);
cbdca852
LP
281 if (!j->level1)
282 return -ENOMEM;
283 }
284
cd34b3c6
HH
285 if (!j->level2) {
286 j->level2 = match_new(j->level1, MATCH_AND_TERM);
287 if (!j->level2)
288 return -ENOMEM;
289 }
290
291 assert(j->level0->type == MATCH_AND_TERM);
292 assert(j->level1->type == MATCH_OR_TERM);
293 assert(j->level2->type == MATCH_AND_TERM);
ab4979d2 294
de190aef
LP
295 le_hash = htole64(hash64(data, size));
296
cd34b3c6
HH
297 LIST_FOREACH(matches, l3, j->level2->matches) {
298 assert(l3->type == MATCH_OR_TERM);
de190aef 299
cd34b3c6
HH
300 LIST_FOREACH(matches, l4, l3->matches) {
301 assert(l4->type == MATCH_DISCRETE);
de190aef 302
cbdca852
LP
303 /* Exactly the same match already? Then ignore
304 * this addition */
cd34b3c6
HH
305 if (l4->le_hash == le_hash &&
306 l4->size == size &&
307 memcmp(l4->data, data, size) == 0)
cbdca852
LP
308 return 0;
309
310 /* Same field? Then let's add this to this OR term */
cd34b3c6
HH
311 if (same_field(data, size, l4->data, l4->size)) {
312 add_here = l3;
cbdca852
LP
313 break;
314 }
315 }
316
317 if (add_here)
318 break;
de190aef
LP
319 }
320
cbdca852 321 if (!add_here) {
cd34b3c6 322 add_here = match_new(j->level2, MATCH_OR_TERM);
cbdca852
LP
323 if (!add_here)
324 goto fail;
325 }
326
327 m = match_new(add_here, MATCH_DISCRETE);
cec736d2 328 if (!m)
cbdca852 329 goto fail;
87d2c1ff 330
cbdca852 331 m->le_hash = le_hash;
1cc101f1 332 m->size = size;
cbdca852
LP
333 m->data = memdup(data, size);
334 if (!m->data)
335 goto fail;
336
337 detach_location(j);
338
339 return 0;
340
341fail:
c5a10d9c
ZJS
342 match_free_if_empty(add_here);
343 match_free_if_empty(j->level2);
344 match_free_if_empty(j->level1);
345 match_free_if_empty(j->level0);
cbdca852
LP
346
347 return -ENOMEM;
348}
349
cd34b3c6 350_public_ int sd_journal_add_conjunction(sd_journal *j) {
1ae464e0
TA
351 assert_return(j, -EINVAL);
352 assert_return(!journal_pid_changed(j), -ECHILD);
1cc101f1 353
cbdca852
LP
354 if (!j->level0)
355 return 0;
356
357 if (!j->level1)
358 return 0;
359
360 if (!j->level1->matches)
361 return 0;
362
cd34b3c6
HH
363 j->level1 = NULL;
364 j->level2 = NULL;
365
366 return 0;
367}
368
369_public_ int sd_journal_add_disjunction(sd_journal *j) {
1ae464e0
TA
370 assert_return(j, -EINVAL);
371 assert_return(!journal_pid_changed(j), -ECHILD);
cd34b3c6
HH
372
373 if (!j->level0)
374 return 0;
375
376 if (!j->level1)
377 return 0;
378
379 if (!j->level2)
380 return 0;
381
382 if (!j->level2->matches)
383 return 0;
cbdca852 384
cd34b3c6 385 j->level2 = NULL;
cbdca852
LP
386 return 0;
387}
388
389static char *match_make_string(Match *m) {
390 char *p, *r;
391 Match *i;
392 bool enclose = false;
393
394 if (!m)
4ad16808 395 return strdup("none");
cbdca852
LP
396
397 if (m->type == MATCH_DISCRETE)
398 return strndup(m->data, m->size);
399
400 p = NULL;
401 LIST_FOREACH(matches, i, m->matches) {
402 char *t, *k;
403
404 t = match_make_string(i);
405 if (!t) {
406 free(p);
407 return NULL;
408 }
409
410 if (p) {
b7def684 411 k = strjoin(p, m->type == MATCH_OR_TERM ? " OR " : " AND ", t, NULL);
cbdca852
LP
412 free(p);
413 free(t);
414
415 if (!k)
416 return NULL;
417
418 p = k;
419
420 enclose = true;
bc302926 421 } else
cbdca852 422 p = t;
87d2c1ff
LP
423 }
424
cbdca852 425 if (enclose) {
b7def684 426 r = strjoin("(", p, ")", NULL);
cbdca852
LP
427 free(p);
428 return r;
429 }
87d2c1ff 430
cbdca852
LP
431 return p;
432}
de7b95cd 433
cbdca852
LP
434char *journal_make_match_string(sd_journal *j) {
435 assert(j);
8f9b6cd9 436
cbdca852 437 return match_make_string(j->level0);
87d2c1ff
LP
438}
439
a5344d2c
LP
440_public_ void sd_journal_flush_matches(sd_journal *j) {
441 if (!j)
442 return;
87d2c1ff 443
cbdca852
LP
444 if (j->level0)
445 match_free(j->level0);
de7b95cd 446
cd34b3c6 447 j->level0 = j->level1 = j->level2 = NULL;
8f9b6cd9 448
de190aef 449 detach_location(j);
87d2c1ff
LP
450}
451
487d3720
MS
452_pure_ static int compare_with_location(JournalFile *f, Location *l) {
453 assert(f);
de190aef 454 assert(l);
487d3720 455 assert(f->location_type == LOCATION_SEEK);
a87247dd 456 assert(l->type == LOCATION_DISCRETE || l->type == LOCATION_SEEK);
de190aef
LP
457
458 if (l->monotonic_set &&
487d3720 459 sd_id128_equal(f->current_boot_id, l->boot_id) &&
de190aef 460 l->realtime_set &&
487d3720 461 f->current_realtime == l->realtime &&
de190aef 462 l->xor_hash_set &&
487d3720 463 f->current_xor_hash == l->xor_hash)
de190aef
LP
464 return 0;
465
466 if (l->seqnum_set &&
487d3720 467 sd_id128_equal(f->header->seqnum_id, l->seqnum_id)) {
de190aef 468
487d3720 469 if (f->current_seqnum < l->seqnum)
de190aef 470 return -1;
487d3720 471 if (f->current_seqnum > l->seqnum)
de190aef
LP
472 return 1;
473 }
474
475 if (l->monotonic_set &&
487d3720 476 sd_id128_equal(f->current_boot_id, l->boot_id)) {
de190aef 477
487d3720 478 if (f->current_monotonic < l->monotonic)
de190aef 479 return -1;
487d3720 480 if (f->current_monotonic > l->monotonic)
de190aef
LP
481 return 1;
482 }
483
484 if (l->realtime_set) {
485
487d3720 486 if (f->current_realtime < l->realtime)
de190aef 487 return -1;
487d3720 488 if (f->current_realtime > l->realtime)
de190aef
LP
489 return 1;
490 }
491
492 if (l->xor_hash_set) {
de190aef 493
487d3720 494 if (f->current_xor_hash < l->xor_hash)
de190aef 495 return -1;
487d3720 496 if (f->current_xor_hash > l->xor_hash)
de190aef
LP
497 return 1;
498 }
499
500 return 0;
501}
502
cbdca852
LP
503static int next_for_match(
504 sd_journal *j,
505 Match *m,
506 JournalFile *f,
507 uint64_t after_offset,
508 direction_t direction,
509 Object **ret,
510 uint64_t *offset) {
511
de7b95cd 512 int r;
cbdca852
LP
513 uint64_t np = 0;
514 Object *n;
de7b95cd
LP
515
516 assert(j);
cbdca852
LP
517 assert(m);
518 assert(f);
de7b95cd 519
cbdca852
LP
520 if (m->type == MATCH_DISCRETE) {
521 uint64_t dp;
de190aef 522
cbdca852 523 r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), NULL, &dp);
de190aef
LP
524 if (r <= 0)
525 return r;
526
cbdca852 527 return journal_file_move_to_entry_by_offset_for_data(f, dp, after_offset, direction, ret, offset);
de190aef 528
cbdca852
LP
529 } else if (m->type == MATCH_OR_TERM) {
530 Match *i;
de7b95cd 531
cbdca852 532 /* Find the earliest match beyond after_offset */
de190aef 533
cbdca852
LP
534 LIST_FOREACH(matches, i, m->matches) {
535 uint64_t cp;
de190aef 536
cbdca852 537 r = next_for_match(j, i, f, after_offset, direction, NULL, &cp);
b4e5f920
LP
538 if (r < 0)
539 return r;
cbdca852 540 else if (r > 0) {
bc302926 541 if (np == 0 || (direction == DIRECTION_DOWN ? cp < np : cp > np))
cbdca852
LP
542 np = cp;
543 }
544 }
b4e5f920 545
bc302926
ZJS
546 if (np == 0)
547 return 0;
548
cbdca852 549 } else if (m->type == MATCH_AND_TERM) {
2bc8ca0c 550 Match *i, *last_moved;
de190aef 551
cbdca852 552 /* Always jump to the next matching entry and repeat
2bc8ca0c 553 * this until we find an offset that matches for all
cbdca852 554 * matches. */
de190aef 555
cbdca852
LP
556 if (!m->matches)
557 return 0;
de7b95cd 558
2bc8ca0c
ZJS
559 r = next_for_match(j, m->matches, f, after_offset, direction, NULL, &np);
560 if (r <= 0)
561 return r;
de190aef 562
2bc8ca0c
ZJS
563 assert(direction == DIRECTION_DOWN ? np >= after_offset : np <= after_offset);
564 last_moved = m->matches;
de190aef 565
2bc8ca0c
ZJS
566 LIST_LOOP_BUT_ONE(matches, i, m->matches, last_moved) {
567 uint64_t cp;
de190aef 568
2bc8ca0c
ZJS
569 r = next_for_match(j, i, f, np, direction, NULL, &cp);
570 if (r <= 0)
571 return r;
de190aef 572
2bc8ca0c
ZJS
573 assert(direction == DIRECTION_DOWN ? cp >= np : cp <= np);
574 if (direction == DIRECTION_DOWN ? cp > np : cp < np) {
575 np = cp;
576 last_moved = i;
de190aef 577 }
2bc8ca0c 578 }
cbdca852 579 }
de190aef 580
bc302926 581 assert(np > 0);
de190aef 582
cbdca852
LP
583 r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
584 if (r < 0)
585 return r;
de7b95cd 586
de190aef 587 if (ret)
cbdca852 588 *ret = n;
de190aef 589 if (offset)
cbdca852 590 *offset = np;
de190aef
LP
591
592 return 1;
593}
594
cbdca852
LP
595static int find_location_for_match(
596 sd_journal *j,
597 Match *m,
598 JournalFile *f,
599 direction_t direction,
600 Object **ret,
601 uint64_t *offset) {
602
de190aef 603 int r;
de190aef
LP
604
605 assert(j);
cbdca852 606 assert(m);
de190aef 607 assert(f);
de190aef 608
cbdca852
LP
609 if (m->type == MATCH_DISCRETE) {
610 uint64_t dp;
de190aef 611
cbdca852 612 r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), NULL, &dp);
de7b95cd
LP
613 if (r <= 0)
614 return r;
615
cbdca852 616 /* FIXME: missing: find by monotonic */
de7b95cd 617
cbdca852
LP
618 if (j->current_location.type == LOCATION_HEAD)
619 return journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_DOWN, ret, offset);
620 if (j->current_location.type == LOCATION_TAIL)
621 return journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_UP, ret, offset);
622 if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
623 return journal_file_move_to_entry_by_seqnum_for_data(f, dp, j->current_location.seqnum, direction, ret, offset);
624 if (j->current_location.monotonic_set) {
625 r = journal_file_move_to_entry_by_monotonic_for_data(f, dp, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset);
626 if (r != -ENOENT)
627 return r;
628 }
629 if (j->current_location.realtime_set)
630 return journal_file_move_to_entry_by_realtime_for_data(f, dp, j->current_location.realtime, direction, ret, offset);
de190aef 631
cbdca852 632 return journal_file_next_entry_for_data(f, NULL, 0, dp, direction, ret, offset);
de7b95cd 633
cbdca852
LP
634 } else if (m->type == MATCH_OR_TERM) {
635 uint64_t np = 0;
636 Object *n;
637 Match *i;
de7b95cd 638
cbdca852 639 /* Find the earliest match */
de7b95cd 640
cbdca852
LP
641 LIST_FOREACH(matches, i, m->matches) {
642 uint64_t cp;
643
644 r = find_location_for_match(j, i, f, direction, NULL, &cp);
645 if (r < 0)
646 return r;
647 else if (r > 0) {
648 if (np == 0 || (direction == DIRECTION_DOWN ? np > cp : np < cp))
649 np = cp;
de190aef 650 }
cbdca852 651 }
de190aef 652
cbdca852
LP
653 if (np == 0)
654 return 0;
de7b95cd 655
cbdca852
LP
656 r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
657 if (r < 0)
658 return r;
de7b95cd 659
cbdca852
LP
660 if (ret)
661 *ret = n;
662 if (offset)
663 *offset = np;
de190aef 664
cbdca852 665 return 1;
e892bd17 666
cbdca852
LP
667 } else {
668 Match *i;
669 uint64_t np = 0;
670
671 assert(m->type == MATCH_AND_TERM);
672
673 /* First jump to the last match, and then find the
674 * next one where all matches match */
675
676 if (!m->matches)
677 return 0;
678
679 LIST_FOREACH(matches, i, m->matches) {
680 uint64_t cp;
681
682 r = find_location_for_match(j, i, f, direction, NULL, &cp);
683 if (r <= 0)
4b067dc9
LP
684 return r;
685
bc302926 686 if (np == 0 || (direction == DIRECTION_DOWN ? cp > np : cp < np))
cbdca852 687 np = cp;
de7b95cd
LP
688 }
689
cbdca852
LP
690 return next_for_match(j, m, f, np, direction, ret, offset);
691 }
692}
de190aef 693
cbdca852
LP
694static int find_location_with_matches(
695 sd_journal *j,
696 JournalFile *f,
697 direction_t direction,
698 Object **ret,
699 uint64_t *offset) {
700
701 int r;
702
703 assert(j);
704 assert(f);
705 assert(ret);
706 assert(offset);
707
708 if (!j->level0) {
709 /* No matches is simple */
710
711 if (j->current_location.type == LOCATION_HEAD)
f534928a 712 return journal_file_next_entry(f, 0, DIRECTION_DOWN, ret, offset);
cbdca852 713 if (j->current_location.type == LOCATION_TAIL)
f534928a 714 return journal_file_next_entry(f, 0, DIRECTION_UP, ret, offset);
cbdca852
LP
715 if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
716 return journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, ret, offset);
717 if (j->current_location.monotonic_set) {
718 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset);
719 if (r != -ENOENT)
720 return r;
de7b95cd 721 }
cbdca852
LP
722 if (j->current_location.realtime_set)
723 return journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, ret, offset);
de7b95cd 724
f534928a 725 return journal_file_next_entry(f, 0, direction, ret, offset);
cbdca852
LP
726 } else
727 return find_location_for_match(j, j->level0, f, direction, ret, offset);
728}
de7b95cd 729
cbdca852
LP
730static int next_with_matches(
731 sd_journal *j,
732 JournalFile *f,
733 direction_t direction,
734 Object **ret,
735 uint64_t *offset) {
736
cbdca852
LP
737 assert(j);
738 assert(f);
739 assert(ret);
740 assert(offset);
741
cbdca852
LP
742 /* No matches is easy. We simple advance the file
743 * pointer by one. */
744 if (!j->level0)
b29ddfcb 745 return journal_file_next_entry(f, f->current_offset, direction, ret, offset);
cbdca852
LP
746
747 /* If we have a match then we look for the next matching entry
49f43d5f 748 * with an offset at least one step larger */
b29ddfcb
MS
749 return next_for_match(j, j->level0, f,
750 direction == DIRECTION_DOWN ? f->current_offset + 1
751 : f->current_offset - 1,
752 direction, ret, offset);
de7b95cd
LP
753}
754
58439db4 755static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction) {
de190aef 756 Object *c;
6e693b42 757 uint64_t cp, n_entries;
cbdca852 758 int r;
de190aef
LP
759
760 assert(j);
761 assert(f);
762
950c07d4
MS
763 n_entries = le64toh(f->header->n_entries);
764
765 /* If we hit EOF before, we don't need to look into this file again
766 * unless direction changed or new entries appeared. */
767 if (f->last_direction == direction && f->location_type == LOCATION_TAIL &&
768 n_entries == f->last_n_entries)
769 return 0;
668c965a 770
950c07d4
MS
771 f->last_n_entries = n_entries;
772
773 if (f->last_direction == direction && f->current_offset > 0) {
7943f422
MS
774 /* LOCATION_SEEK here means we did the work in a previous
775 * iteration and the current location already points to a
776 * candidate entry. */
487d3720
MS
777 if (f->location_type != LOCATION_SEEK) {
778 r = next_with_matches(j, f, direction, &c, &cp);
779 if (r <= 0)
780 return r;
466ccd92 781
950c07d4 782 journal_file_save_location(f, c, cp);
487d3720 783 }
de190aef 784 } else {
950c07d4
MS
785 f->last_direction = direction;
786
cbdca852 787 r = find_location_with_matches(j, f, direction, &c, &cp);
de190aef
LP
788 if (r <= 0)
789 return r;
487d3720 790
950c07d4 791 journal_file_save_location(f, c, cp);
de190aef
LP
792 }
793
bc302926 794 /* OK, we found the spot, now let's advance until an entry
cbdca852
LP
795 * that is actually different from what we were previously
796 * looking at. This is necessary to handle entries which exist
797 * in two (or more) journal files, and which shall all be
798 * suppressed but one. */
799
de190aef
LP
800 for (;;) {
801 bool found;
802
803 if (j->current_location.type == LOCATION_DISCRETE) {
804 int k;
805
487d3720 806 k = compare_with_location(f, &j->current_location);
1cdf7175
CH
807
808 found = direction == DIRECTION_DOWN ? k > 0 : k < 0;
de190aef
LP
809 } else
810 found = true;
811
487d3720 812 if (found)
de190aef 813 return 1;
de190aef
LP
814
815 r = next_with_matches(j, f, direction, &c, &cp);
816 if (r <= 0)
817 return r;
487d3720 818
950c07d4 819 journal_file_save_location(f, c, cp);
de190aef
LP
820 }
821}
822
e892bd17 823static int real_journal_next(sd_journal *j, direction_t direction) {
468b21de 824 JournalFile *f, *new_file = NULL;
cec736d2 825 Iterator i;
a002d44b 826 Object *o;
87d2c1ff
LP
827 int r;
828
1ae464e0
TA
829 assert_return(j, -EINVAL);
830 assert_return(!journal_pid_changed(j), -ECHILD);
87d2c1ff 831
c1f906bd 832 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
de190aef 833 bool found;
87d2c1ff 834
58439db4 835 r = next_beyond_location(j, f, direction);
e590af26 836 if (r < 0) {
da927ba9 837 log_debug_errno(r, "Can't iterate through %s, ignoring: %m", f->path);
a9a245c1 838 remove_file_real(j, f);
e590af26 839 continue;
6573ef05
MS
840 } else if (r == 0) {
841 f->location_type = LOCATION_TAIL;
cec736d2 842 continue;
6573ef05 843 }
87d2c1ff 844
468b21de 845 if (!new_file)
de190aef
LP
846 found = true;
847 else {
848 int k;
849
d8ae66d7 850 k = journal_file_compare_locations(f, new_file);
de190aef 851
bc302926 852 found = direction == DIRECTION_DOWN ? k < 0 : k > 0;
de190aef
LP
853 }
854
e499c999 855 if (found)
468b21de 856 new_file = f;
87d2c1ff
LP
857 }
858
468b21de 859 if (!new_file)
de190aef 860 return 0;
ae2cc8ef 861
e499c999 862 r = journal_file_move_to_object(new_file, OBJECT_ENTRY, new_file->current_offset, &o);
468b21de
LP
863 if (r < 0)
864 return r;
865
1eb6332d 866 set_location(j, new_file, o);
ae2cc8ef 867
de190aef
LP
868 return 1;
869}
ae2cc8ef 870
a5344d2c 871_public_ int sd_journal_next(sd_journal *j) {
de190aef
LP
872 return real_journal_next(j, DIRECTION_DOWN);
873}
ae2cc8ef 874
a5344d2c 875_public_ int sd_journal_previous(sd_journal *j) {
de190aef
LP
876 return real_journal_next(j, DIRECTION_UP);
877}
ae2cc8ef 878
6f003b43 879static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) {
de190aef 880 int c = 0, r;
ae2cc8ef 881
1ae464e0
TA
882 assert_return(j, -EINVAL);
883 assert_return(!journal_pid_changed(j), -ECHILD);
de190aef 884
6f003b43
LP
885 if (skip == 0) {
886 /* If this is not a discrete skip, then at least
887 * resolve the current location */
888 if (j->current_location.type != LOCATION_DISCRETE)
889 return real_journal_next(j, direction);
890
891 return 0;
892 }
893
894 do {
895 r = real_journal_next(j, direction);
de190aef
LP
896 if (r < 0)
897 return r;
898
899 if (r == 0)
900 return c;
901
902 skip--;
903 c++;
6f003b43 904 } while (skip > 0);
87d2c1ff 905
de190aef 906 return c;
87d2c1ff
LP
907}
908
6f003b43
LP
909_public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
910 return real_journal_next_skip(j, DIRECTION_DOWN, skip);
911}
de190aef 912
6f003b43
LP
913_public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
914 return real_journal_next_skip(j, DIRECTION_UP, skip);
87d2c1ff
LP
915}
916
a5344d2c 917_public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) {
cec736d2 918 Object *o;
87d2c1ff 919 int r;
3fbf9cbb 920 char bid[33], sid[33];
87d2c1ff 921
1ae464e0
TA
922 assert_return(j, -EINVAL);
923 assert_return(!journal_pid_changed(j), -ECHILD);
924 assert_return(cursor, -EINVAL);
87d2c1ff 925
3fbf9cbb
LP
926 if (!j->current_file || j->current_file->current_offset <= 0)
927 return -EADDRNOTAVAIL;
87d2c1ff 928
de190aef 929 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
87d2c1ff
LP
930 if (r < 0)
931 return r;
932
3fbf9cbb
LP
933 sd_id128_to_string(j->current_file->header->seqnum_id, sid);
934 sd_id128_to_string(o->entry.boot_id, bid);
87d2c1ff 935
3fbf9cbb 936 if (asprintf(cursor,
507f22bd
ZJS
937 "s=%s;i=%"PRIx64";b=%s;m=%"PRIx64";t=%"PRIx64";x=%"PRIx64,
938 sid, le64toh(o->entry.seqnum),
939 bid, le64toh(o->entry.monotonic),
940 le64toh(o->entry.realtime),
941 le64toh(o->entry.xor_hash)) < 0)
3fbf9cbb 942 return -ENOMEM;
87d2c1ff 943
6f47ad30 944 return 0;
87d2c1ff
LP
945}
946
a5344d2c 947_public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
a2a5291b 948 const char *word, *state;
de190aef 949 size_t l;
de190aef
LP
950 unsigned long long seqnum, monotonic, realtime, xor_hash;
951 bool
952 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;
958 sd_id128_t seqnum_id, boot_id;
959
1ae464e0
TA
960 assert_return(j, -EINVAL);
961 assert_return(!journal_pid_changed(j), -ECHILD);
962 assert_return(!isempty(cursor), -EINVAL);
de190aef 963
a2a5291b 964 FOREACH_WORD_SEPARATOR(word, l, cursor, ";", state) {
de190aef
LP
965 char *item;
966 int k = 0;
967
a2a5291b 968 if (l < 2 || word[1] != '=')
de190aef
LP
969 return -EINVAL;
970
a2a5291b 971 item = strndup(word, l);
de190aef
LP
972 if (!item)
973 return -ENOMEM;
974
a2a5291b 975 switch (word[0]) {
de190aef
LP
976
977 case 's':
978 seqnum_id_set = true;
be3ea5ea 979 k = sd_id128_from_string(item+2, &seqnum_id);
de190aef
LP
980 break;
981
982 case 'i':
983 seqnum_set = true;
be3ea5ea 984 if (sscanf(item+2, "%llx", &seqnum) != 1)
de190aef
LP
985 k = -EINVAL;
986 break;
987
988 case 'b':
989 boot_id_set = true;
be3ea5ea 990 k = sd_id128_from_string(item+2, &boot_id);
de190aef
LP
991 break;
992
993 case 'm':
994 monotonic_set = true;
be3ea5ea 995 if (sscanf(item+2, "%llx", &monotonic) != 1)
de190aef
LP
996 k = -EINVAL;
997 break;
998
999 case 't':
1000 realtime_set = true;
be3ea5ea 1001 if (sscanf(item+2, "%llx", &realtime) != 1)
de190aef
LP
1002 k = -EINVAL;
1003 break;
1004
1005 case 'x':
1006 xor_hash_set = true;
be3ea5ea 1007 if (sscanf(item+2, "%llx", &xor_hash) != 1)
de190aef
LP
1008 k = -EINVAL;
1009 break;
1010 }
1011
1012 free(item);
1013
1014 if (k < 0)
1015 return k;
1016 }
1017
1018 if ((!seqnum_set || !seqnum_id_set) &&
1019 (!monotonic_set || !boot_id_set) &&
1020 !realtime_set)
1021 return -EINVAL;
1022
1023 reset_location(j);
1024
a87247dd 1025 j->current_location.type = LOCATION_SEEK;
de190aef
LP
1026
1027 if (realtime_set) {
1028 j->current_location.realtime = (uint64_t) realtime;
1029 j->current_location.realtime_set = true;
1030 }
1031
1032 if (seqnum_set && seqnum_id_set) {
1033 j->current_location.seqnum = (uint64_t) seqnum;
1034 j->current_location.seqnum_id = seqnum_id;
1035 j->current_location.seqnum_set = true;
1036 }
1037
1038 if (monotonic_set && boot_id_set) {
1039 j->current_location.monotonic = (uint64_t) monotonic;
1040 j->current_location.boot_id = boot_id;
1041 j->current_location.monotonic_set = true;
1042 }
1043
1044 if (xor_hash_set) {
1045 j->current_location.xor_hash = (uint64_t) xor_hash;
1046 j->current_location.xor_hash_set = true;
1047 }
1048
1049 return 0;
1050}
1051
c6511e85
LP
1052_public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) {
1053 int r;
c6511e85
LP
1054 Object *o;
1055
1ae464e0
TA
1056 assert_return(j, -EINVAL);
1057 assert_return(!journal_pid_changed(j), -ECHILD);
1058 assert_return(!isempty(cursor), -EINVAL);
c6511e85
LP
1059
1060 if (!j->current_file || j->current_file->current_offset <= 0)
1061 return -EADDRNOTAVAIL;
1062
1063 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
1064 if (r < 0)
1065 return r;
1066
9ed794a3 1067 for (;;) {
c6511e85 1068 _cleanup_free_ char *item = NULL;
c6511e85 1069 unsigned long long ll;
7294bb5b 1070 sd_id128_t id;
c6511e85
LP
1071 int k = 0;
1072
7294bb5b
SS
1073 r = extract_first_word(&cursor, &item, ";", EXTRACT_DONT_COALESCE_SEPARATORS);
1074 if (r < 0)
1075 return r;
c6511e85 1076
7294bb5b
SS
1077 if (r == 0)
1078 break;
c6511e85 1079
7294bb5b
SS
1080 if (strlen(item) < 2 || item[1] != '=')
1081 return -EINVAL;
1082
1083 switch (item[0]) {
c6511e85
LP
1084
1085 case 's':
1086 k = sd_id128_from_string(item+2, &id);
1087 if (k < 0)
1088 return k;
1089 if (!sd_id128_equal(id, j->current_file->header->seqnum_id))
1090 return 0;
1091 break;
1092
1093 case 'i':
1094 if (sscanf(item+2, "%llx", &ll) != 1)
1095 return -EINVAL;
1096 if (ll != le64toh(o->entry.seqnum))
1097 return 0;
1098 break;
1099
1100 case 'b':
1101 k = sd_id128_from_string(item+2, &id);
1102 if (k < 0)
1103 return k;
1104 if (!sd_id128_equal(id, o->entry.boot_id))
1105 return 0;
1106 break;
1107
1108 case 'm':
1109 if (sscanf(item+2, "%llx", &ll) != 1)
1110 return -EINVAL;
1111 if (ll != le64toh(o->entry.monotonic))
1112 return 0;
1113 break;
1114
1115 case 't':
1116 if (sscanf(item+2, "%llx", &ll) != 1)
1117 return -EINVAL;
1118 if (ll != le64toh(o->entry.realtime))
1119 return 0;
1120 break;
1121
1122 case 'x':
1123 if (sscanf(item+2, "%llx", &ll) != 1)
1124 return -EINVAL;
1125 if (ll != le64toh(o->entry.xor_hash))
1126 return 0;
1127 break;
1128 }
1129 }
1130
1131 return 1;
1132}
1133
1134
a5344d2c 1135_public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
1ae464e0
TA
1136 assert_return(j, -EINVAL);
1137 assert_return(!journal_pid_changed(j), -ECHILD);
de190aef
LP
1138
1139 reset_location(j);
a87247dd 1140 j->current_location.type = LOCATION_SEEK;
de190aef
LP
1141 j->current_location.boot_id = boot_id;
1142 j->current_location.monotonic = usec;
1143 j->current_location.monotonic_set = true;
1144
1145 return 0;
1146}
1147
a5344d2c 1148_public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
1ae464e0
TA
1149 assert_return(j, -EINVAL);
1150 assert_return(!journal_pid_changed(j), -ECHILD);
de190aef
LP
1151
1152 reset_location(j);
a87247dd 1153 j->current_location.type = LOCATION_SEEK;
de190aef
LP
1154 j->current_location.realtime = usec;
1155 j->current_location.realtime_set = true;
1156
1157 return 0;
1158}
1159
a5344d2c 1160_public_ int sd_journal_seek_head(sd_journal *j) {
1ae464e0
TA
1161 assert_return(j, -EINVAL);
1162 assert_return(!journal_pid_changed(j), -ECHILD);
de190aef
LP
1163
1164 reset_location(j);
1165 j->current_location.type = LOCATION_HEAD;
1166
1167 return 0;
1168}
1169
a5344d2c 1170_public_ int sd_journal_seek_tail(sd_journal *j) {
1ae464e0
TA
1171 assert_return(j, -EINVAL);
1172 assert_return(!journal_pid_changed(j), -ECHILD);
de190aef
LP
1173
1174 reset_location(j);
1175 j->current_location.type = LOCATION_TAIL;
1176
1177 return 0;
87d2c1ff
LP
1178}
1179
85210bff
LP
1180static void check_network(sd_journal *j, int fd) {
1181 struct statfs sfs;
1182
1183 assert(j);
1184
1185 if (j->on_network)
1186 return;
1187
1188 if (fstatfs(fd, &sfs) < 0)
1189 return;
1190
1191 j->on_network =
c51cf056
ZJS
1192 F_TYPE_EQUAL(sfs.f_type, CIFS_MAGIC_NUMBER) ||
1193 F_TYPE_EQUAL(sfs.f_type, CODA_SUPER_MAGIC) ||
1194 F_TYPE_EQUAL(sfs.f_type, NCP_SUPER_MAGIC) ||
1195 F_TYPE_EQUAL(sfs.f_type, NFS_SUPER_MAGIC) ||
1196 F_TYPE_EQUAL(sfs.f_type, SMB_SUPER_MAGIC);
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
ZJS
1205
1206 return streq(filename, full) ||
1207 streq(filename, tilded) ||
1208 startswith(filename, atted);
1209}
1210
1211static bool file_type_wanted(int flags, const char *filename) {
d617408e
LP
1212 assert(filename);
1213
a688baa8
ZJS
1214 if (!endswith(filename, ".journal") && !endswith(filename, ".journal~"))
1215 return false;
1216
1217 /* no flags set → every type is OK */
1218 if (!(flags & (SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER)))
1219 return true;
1220
1221 if (flags & SD_JOURNAL_SYSTEM && file_has_type_prefix("system", filename))
1222 return true;
1223
1224 if (flags & SD_JOURNAL_CURRENT_USER) {
1225 char prefix[5 + DECIMAL_STR_MAX(uid_t) + 1];
1226
5ffa8c81 1227 xsprintf(prefix, "user-"UID_FMT, getuid());
a688baa8
ZJS
1228
1229 if (file_has_type_prefix(prefix, filename))
1230 return true;
1231 }
1232
1233 return false;
1234}
1235
5d1ce257
LP
1236static bool path_has_prefix(sd_journal *j, const char *path, const char *prefix) {
1237 assert(j);
1238 assert(path);
1239 assert(prefix);
1240
1241 if (j->toplevel_fd >= 0)
1242 return false;
1243
1244 return path_startswith(path, prefix);
1245}
1246
1247static const char *skip_slash(const char *p) {
1248
1249 if (!p)
1250 return NULL;
1251
1252 while (*p == '/')
1253 p++;
1254
1255 return p;
1256}
1257
1258static int add_any_file(sd_journal *j, int fd, const char *path) {
39883f62 1259 JournalFile *f = NULL;
5d1ce257 1260 bool close_fd = false;
d617408e 1261 int r, k;
3fbf9cbb
LP
1262
1263 assert(j);
5d1ce257 1264 assert(fd >= 0 || path);
3fbf9cbb 1265
5d1ce257 1266 if (path && ordered_hashmap_get(j->files, path))
50f20cfd 1267 return 0;
50f20cfd 1268
9e8abdf0 1269 if (ordered_hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
d617408e
LP
1270 log_debug("Too many open journal files, not adding %s.", path);
1271 r = -ETOOMANYREFS;
1272 goto fail;
9e8abdf0
LP
1273 }
1274
5d1ce257
LP
1275 if (fd < 0 && j->toplevel_fd >= 0) {
1276
1277 /* If there's a top-level fd defined, open the file relative to this now. (Make the path relative,
1278 * explicitly, since otherwise openat() ignores the first argument.) */
1279
1280 fd = openat(j->toplevel_fd, skip_slash(path), O_RDONLY|O_CLOEXEC);
1281 if (fd < 0) {
1282 r = log_debug_errno(errno, "Failed to open journal file %s: %m", path);
1283 goto fail;
1284 }
1285
1286 close_fd = true;
1287 }
1288
1289 r = journal_file_open(fd, path, O_RDONLY, 0, false, false, NULL, j->mmap, NULL, NULL, &f);
d617408e 1290 if (r < 0) {
5d1ce257
LP
1291 if (close_fd)
1292 safe_close(fd);
d617408e
LP
1293 log_debug_errno(r, "Failed to open journal file %s: %m", path);
1294 goto fail;
1295 }
3fbf9cbb 1296
72f59706 1297 /* journal_file_dump(f); */
de190aef 1298
c1f906bd 1299 r = ordered_hashmap_put(j->files, f->path, f);
3fbf9cbb 1300 if (r < 0) {
5d1ce257 1301 f->close_fd = close_fd;
69a3a6fd 1302 (void) journal_file_close(f);
d617408e 1303 goto fail;
3fbf9cbb
LP
1304 }
1305
5d1ce257
LP
1306 if (!j->has_runtime_files && path_has_prefix(j, f->path, "/run"))
1307 j->has_runtime_files = true;
1308 else if (!j->has_persistent_files && path_has_prefix(j, f->path, "/var"))
1309 j->has_persistent_files = true;
1310
5ec76417 1311 log_debug("File %s added.", f->path);
a50d7d43 1312
85210bff
LP
1313 check_network(j, f->fd);
1314
313cefa1 1315 j->current_invalidate_counter++;
a963990f 1316
50f20cfd 1317 return 0;
d617408e
LP
1318
1319fail:
5768d259 1320 k = journal_put_error(j, r, path);
d617408e
LP
1321 if (k < 0)
1322 return k;
1323
1324 return r;
50f20cfd
LP
1325}
1326
5302ebe1 1327static int add_file(sd_journal *j, const char *prefix, const char *filename) {
d617408e 1328 const char *path;
5302ebe1
ZJS
1329
1330 assert(j);
1331 assert(prefix);
1332 assert(filename);
1333
5d1ce257 1334 if (j->no_new_files)
5302ebe1
ZJS
1335 return 0;
1336
5d1ce257
LP
1337 if (!file_type_wanted(j->flags, filename))
1338 return 0;
39fd5b08 1339
5d1ce257
LP
1340 path = strjoina(prefix, "/", filename);
1341 return add_any_file(j, -1, path);
5302ebe1
ZJS
1342}
1343
d617408e
LP
1344static void remove_file(sd_journal *j, const char *prefix, const char *filename) {
1345 const char *path;
50f20cfd
LP
1346 JournalFile *f;
1347
1348 assert(j);
1349 assert(prefix);
1350 assert(filename);
1351
d617408e 1352 path = strjoina(prefix, "/", filename);
c1f906bd 1353 f = ordered_hashmap_get(j->files, path);
50f20cfd 1354 if (!f)
d617408e 1355 return;
50f20cfd 1356
a9a245c1 1357 remove_file_real(j, f);
a9a245c1
ZJS
1358}
1359
1360static void remove_file_real(sd_journal *j, JournalFile *f) {
1361 assert(j);
1362 assert(f);
1363
c1f906bd 1364 ordered_hashmap_remove(j->files, f->path);
44a5fa34 1365
5ec76417 1366 log_debug("File %s removed.", f->path);
44a5fa34 1367
3c1668da
LP
1368 if (j->current_file == f) {
1369 j->current_file = NULL;
1370 j->current_field = 0;
1371 }
1372
1373 if (j->unique_file == f) {
360af4cf 1374 /* Jump to the next unique_file or NULL if that one was last */
c1f906bd 1375 j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
3c1668da 1376 j->unique_offset = 0;
360af4cf
ZJS
1377 if (!j->unique_file)
1378 j->unique_file_lost = true;
3c1668da
LP
1379 }
1380
eb86030e
LP
1381 if (j->fields_file == f) {
1382 j->fields_file = ordered_hashmap_next(j->files, j->fields_file->path);
1383 j->fields_offset = 0;
1384 if (!j->fields_file)
1385 j->fields_file_lost = true;
1386 }
1387
69a3a6fd 1388 (void) journal_file_close(f);
50f20cfd 1389
313cefa1 1390 j->current_invalidate_counter++;
3fbf9cbb
LP
1391}
1392
d617408e
LP
1393static int dirname_is_machine_id(const char *fn) {
1394 sd_id128_t id, machine;
1395 int r;
1396
1397 r = sd_id128_get_machine(&machine);
1398 if (r < 0)
1399 return r;
1400
1401 r = sd_id128_from_string(fn, &id);
1402 if (r < 0)
1403 return r;
1404
1405 return sd_id128_equal(id, machine);
1406}
1407
a963990f 1408static int add_directory(sd_journal *j, const char *prefix, const char *dirname) {
7fd1b19b 1409 _cleanup_free_ char *path = NULL;
7fd1b19b 1410 _cleanup_closedir_ DIR *d = NULL;
d617408e 1411 struct dirent *de = NULL;
a963990f 1412 Directory *m;
d617408e 1413 int r, k;
3fbf9cbb
LP
1414
1415 assert(j);
1416 assert(prefix);
3fbf9cbb 1417
5d1ce257
LP
1418 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1419 * and reenumerates directory contents */
d95b1fb3 1420
5d1ce257
LP
1421 if (dirname)
1422 path = strjoin(prefix, "/", dirname, NULL);
1423 else
1424 path = strdup(prefix);
d617408e
LP
1425 if (!path) {
1426 r = -ENOMEM;
1427 goto fail;
1428 }
3fbf9cbb 1429
5d1ce257
LP
1430 log_debug("Considering directory %s.", path);
1431
1432 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1433 if ((j->flags & SD_JOURNAL_LOCAL_ONLY) &&
1434 !((dirname && dirname_is_machine_id(dirname) > 0) || path_has_prefix(j, path, "/run")))
1435 return 0;
1436
1437
1438 if (j->toplevel_fd < 0)
1439 d = opendir(path);
1440 else
61233823 1441 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
5d1ce257
LP
1442 * relative, by dropping the initial slash */
1443 d = xopendirat(j->toplevel_fd, skip_slash(path), 0);
3fbf9cbb 1444 if (!d) {
d617408e
LP
1445 r = log_debug_errno(errno, "Failed to open directory %s: %m", path);
1446 goto fail;
3fbf9cbb
LP
1447 }
1448
a963990f
LP
1449 m = hashmap_get(j->directories_by_path, path);
1450 if (!m) {
1451 m = new0(Directory, 1);
d617408e
LP
1452 if (!m) {
1453 r = -ENOMEM;
1454 goto fail;
1455 }
a963990f
LP
1456
1457 m->is_root = false;
1458 m->path = path;
1459
1460 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
a963990f 1461 free(m);
d617408e
LP
1462 r = -ENOMEM;
1463 goto fail;
a963990f
LP
1464 }
1465
a50d7d43 1466 path = NULL; /* avoid freeing in cleanup */
313cefa1 1467 j->current_invalidate_counter++;
a963990f 1468
5ec76417 1469 log_debug("Directory %s added.", m->path);
a963990f 1470
a50d7d43 1471 } else if (m->is_root)
a963990f 1472 return 0;
a963990f
LP
1473
1474 if (m->wd <= 0 && j->inotify_fd >= 0) {
5d1ce257 1475 /* Watch this directory, if it not being watched yet. */
a963990f 1476
430fbf8e
LP
1477 m->wd = inotify_add_watch_fd(j->inotify_fd, dirfd(d),
1478 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1479 IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|IN_MOVED_FROM|
1480 IN_ONLYDIR);
a963990f
LP
1481
1482 if (m->wd > 0 && hashmap_put(j->directories_by_wd, INT_TO_PTR(m->wd), m) < 0)
1483 inotify_rm_watch(j->inotify_fd, m->wd);
1484 }
1485
f6377261 1486 FOREACH_DIRENT_ALL(de, d, r = log_debug_errno(errno, "Failed to read directory %s: %m", m->path); goto fail) {
a963990f 1487
de2c3907 1488 if (dirent_is_file_with_suffix(de, ".journal") ||
d617408e
LP
1489 dirent_is_file_with_suffix(de, ".journal~"))
1490 (void) add_file(j, m->path, de->d_name);
a963990f
LP
1491 }
1492
85210bff
LP
1493 check_network(j, dirfd(d));
1494
a963990f 1495 return 0;
d617408e
LP
1496
1497fail:
5d1ce257 1498 k = journal_put_error(j, r, path ?: prefix);
d617408e
LP
1499 if (k < 0)
1500 return k;
1501
1502 return r;
a963990f
LP
1503}
1504
d617408e 1505static int add_root_directory(sd_journal *j, const char *p, bool missing_ok) {
5d1ce257 1506
7fd1b19b 1507 _cleanup_closedir_ DIR *d = NULL;
d617408e 1508 struct dirent *de;
a963990f 1509 Directory *m;
d617408e 1510 int r, k;
a963990f
LP
1511
1512 assert(j);
a963990f 1513
5d1ce257
LP
1514 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
1515 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
1516 * populate the set, as well as to update it later. */
a963990f 1517
5d1ce257
LP
1518 if (p) {
1519 /* If there's a path specified, use it. */
b6741478 1520
5d1ce257
LP
1521 if ((j->flags & SD_JOURNAL_RUNTIME_ONLY) &&
1522 !path_has_prefix(j, p, "/run"))
1523 return -EINVAL;
d617408e 1524
5d1ce257
LP
1525 if (j->prefix)
1526 p = strjoina(j->prefix, p);
1527
1528 if (j->toplevel_fd < 0)
1529 d = opendir(p);
1530 else
1531 d = xopendirat(j->toplevel_fd, skip_slash(p), 0);
1532
1533 if (!d) {
1534 if (errno == ENOENT && missing_ok)
1535 return 0;
1536
1537 r = log_debug_errno(errno, "Failed to open root directory %s: %m", p);
1538 goto fail;
1539 }
1540 } else {
1541 int dfd;
1542
1543 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
1544 * opendir() will take possession of the fd, and close it, which we don't want. */
1545
1546 p = "."; /* store this as "." in the directories hashmap */
1547
1548 dfd = fcntl(j->toplevel_fd, F_DUPFD_CLOEXEC, 3);
1549 if (dfd < 0) {
1550 r = -errno;
1551 goto fail;
1552 }
1553
1554 d = fdopendir(dfd);
1555 if (!d) {
1556 r = -errno;
1557 safe_close(dfd);
1558 goto fail;
1559 }
1560
1561 rewinddir(d);
d617408e 1562 }
a963990f
LP
1563
1564 m = hashmap_get(j->directories_by_path, p);
1565 if (!m) {
1566 m = new0(Directory, 1);
d617408e
LP
1567 if (!m) {
1568 r = -ENOMEM;
1569 goto fail;
1570 }
a963990f
LP
1571
1572 m->is_root = true;
5d1ce257 1573
a963990f
LP
1574 m->path = strdup(p);
1575 if (!m->path) {
a963990f 1576 free(m);
d617408e
LP
1577 r = -ENOMEM;
1578 goto fail;
a963990f
LP
1579 }
1580
1581 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
a963990f
LP
1582 free(m->path);
1583 free(m);
d617408e
LP
1584 r = -ENOMEM;
1585 goto fail;
a963990f
LP
1586 }
1587
313cefa1 1588 j->current_invalidate_counter++;
a963990f 1589
5ec76417 1590 log_debug("Root directory %s added.", m->path);
a963990f 1591
a50d7d43 1592 } else if (!m->is_root)
a963990f 1593 return 0;
50f20cfd 1594
a963990f
LP
1595 if (m->wd <= 0 && j->inotify_fd >= 0) {
1596
430fbf8e 1597 m->wd = inotify_add_watch_fd(j->inotify_fd, dirfd(d),
a963990f 1598 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
4a842cad 1599 IN_ONLYDIR);
a963990f
LP
1600
1601 if (m->wd > 0 && hashmap_put(j->directories_by_wd, INT_TO_PTR(m->wd), m) < 0)
1602 inotify_rm_watch(j->inotify_fd, m->wd);
1603 }
50f20cfd 1604
5302ebe1
ZJS
1605 if (j->no_new_files)
1606 return 0;
1607
f6377261 1608 FOREACH_DIRENT_ALL(de, d, r = log_debug_errno(errno, "Failed to read directory %s: %m", m->path); goto fail) {
a963990f 1609 sd_id128_t id;
3fbf9cbb 1610
de2c3907 1611 if (dirent_is_file_with_suffix(de, ".journal") ||
d617408e
LP
1612 dirent_is_file_with_suffix(de, ".journal~"))
1613 (void) add_file(j, m->path, de->d_name);
1614 else if (IN_SET(de->d_type, DT_DIR, DT_LNK, DT_UNKNOWN) &&
1615 sd_id128_from_string(de->d_name, &id) >= 0)
1616 (void) add_directory(j, m->path, de->d_name);
3fbf9cbb
LP
1617 }
1618
85210bff
LP
1619 check_network(j, dirfd(d));
1620
a963990f 1621 return 0;
d617408e
LP
1622
1623fail:
5768d259 1624 k = journal_put_error(j, r, p);
d617408e
LP
1625 if (k < 0)
1626 return k;
1627
1628 return r;
a963990f
LP
1629}
1630
b2b46f91 1631static void remove_directory(sd_journal *j, Directory *d) {
a963990f
LP
1632 assert(j);
1633
1634 if (d->wd > 0) {
1635 hashmap_remove(j->directories_by_wd, INT_TO_PTR(d->wd));
1636
1637 if (j->inotify_fd >= 0)
1638 inotify_rm_watch(j->inotify_fd, d->wd);
1639 }
1640
1641 hashmap_remove(j->directories_by_path, d->path);
1642
1643 if (d->is_root)
5ec76417 1644 log_debug("Root directory %s removed.", d->path);
a963990f 1645 else
5ec76417 1646 log_debug("Directory %s removed.", d->path);
a963990f
LP
1647
1648 free(d->path);
1649 free(d);
3fbf9cbb
LP
1650}
1651
89739579 1652static int add_search_paths(sd_journal *j) {
d617408e
LP
1653
1654 static const char search_paths[] =
a963990f
LP
1655 "/run/log/journal\0"
1656 "/var/log/journal\0";
1657 const char *p;
50f20cfd
LP
1658
1659 assert(j);
50f20cfd 1660
a963990f
LP
1661 /* We ignore most errors here, since the idea is to only open
1662 * what's actually accessible, and ignore the rest. */
50f20cfd 1663
d617408e
LP
1664 NULSTR_FOREACH(p, search_paths)
1665 (void) add_root_directory(j, p, true);
50f20cfd 1666
a963990f 1667 return 0;
50f20cfd
LP
1668}
1669
5302ebe1
ZJS
1670static int add_current_paths(sd_journal *j) {
1671 Iterator i;
1672 JournalFile *f;
1673
1674 assert(j);
1675 assert(j->no_new_files);
1676
5d1ce257 1677 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
5302ebe1
ZJS
1678 * treat them as fatal. */
1679
c1f906bd 1680 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
5302ebe1 1681 _cleanup_free_ char *dir;
e9174f29 1682 int r;
5302ebe1
ZJS
1683
1684 dir = dirname_malloc(f->path);
1685 if (!dir)
1686 return -ENOMEM;
1687
5d1ce257 1688 r = add_directory(j, dir, NULL);
d617408e 1689 if (r < 0)
5302ebe1 1690 return r;
5302ebe1
ZJS
1691 }
1692
1693 return 0;
1694}
1695
a963990f 1696static int allocate_inotify(sd_journal *j) {
50f20cfd 1697 assert(j);
50f20cfd 1698
a963990f
LP
1699 if (j->inotify_fd < 0) {
1700 j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1701 if (j->inotify_fd < 0)
1702 return -errno;
1703 }
50f20cfd 1704
cb306f5d 1705 return hashmap_ensure_allocated(&j->directories_by_wd, NULL);
50f20cfd
LP
1706}
1707
7827b1a1 1708static sd_journal *journal_new(int flags, const char *path) {
a963990f 1709 sd_journal *j;
50f20cfd 1710
a963990f
LP
1711 j = new0(sd_journal, 1);
1712 if (!j)
1713 return NULL;
50f20cfd 1714
a65f06bb 1715 j->original_pid = getpid();
5d1ce257 1716 j->toplevel_fd = -1;
a963990f
LP
1717 j->inotify_fd = -1;
1718 j->flags = flags;
93b73b06 1719 j->data_threshold = DEFAULT_DATA_THRESHOLD;
50f20cfd 1720
7827b1a1
LP
1721 if (path) {
1722 j->path = strdup(path);
6180fc61
ZJS
1723 if (!j->path)
1724 goto fail;
7827b1a1
LP
1725 }
1726
c1f906bd 1727 j->files = ordered_hashmap_new(&string_hash_ops);
d5099efc 1728 j->directories_by_path = hashmap_new(&string_hash_ops);
84168d80 1729 j->mmap = mmap_cache_new();
3ac251b8 1730 if (!j->files || !j->directories_by_path || !j->mmap)
6180fc61 1731 goto fail;
16e9f408 1732
a963990f 1733 return j;
6180fc61
ZJS
1734
1735fail:
1736 sd_journal_close(j);
1737 return NULL;
50f20cfd
LP
1738}
1739
1aaa68f5
ZJS
1740#define OPEN_ALLOWED_FLAGS \
1741 (SD_JOURNAL_LOCAL_ONLY | \
1742 SD_JOURNAL_RUNTIME_ONLY | \
1743 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER)
1744
a5344d2c 1745_public_ int sd_journal_open(sd_journal **ret, int flags) {
87d2c1ff 1746 sd_journal *j;
3fbf9cbb 1747 int r;
87d2c1ff 1748
1ae464e0 1749 assert_return(ret, -EINVAL);
1aaa68f5 1750 assert_return((flags & ~OPEN_ALLOWED_FLAGS) == 0, -EINVAL);
87d2c1ff 1751
7827b1a1 1752 j = journal_new(flags, NULL);
87d2c1ff
LP
1753 if (!j)
1754 return -ENOMEM;
1755
89739579 1756 r = add_search_paths(j);
a963990f 1757 if (r < 0)
50f20cfd 1758 goto fail;
50f20cfd 1759
a963990f
LP
1760 *ret = j;
1761 return 0;
cf244689 1762
a963990f
LP
1763fail:
1764 sd_journal_close(j);
87d2c1ff 1765
a963990f
LP
1766 return r;
1767}
50f20cfd 1768
1aaa68f5
ZJS
1769#define OPEN_CONTAINER_ALLOWED_FLAGS \
1770 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1771
b6741478
LP
1772_public_ int sd_journal_open_container(sd_journal **ret, const char *machine, int flags) {
1773 _cleanup_free_ char *root = NULL, *class = NULL;
1774 sd_journal *j;
1775 char *p;
1776 int r;
1777
2daa9cbd
LP
1778 /* This is pretty much deprecated, people should use machined's OpenMachineRootDirectory() call instead in
1779 * combination with sd_journal_open_directory_fd(). */
1780
b6741478
LP
1781 assert_return(machine, -EINVAL);
1782 assert_return(ret, -EINVAL);
1aaa68f5 1783 assert_return((flags & ~OPEN_CONTAINER_ALLOWED_FLAGS) == 0, -EINVAL);
affcf189 1784 assert_return(machine_name_is_valid(machine), -EINVAL);
b6741478 1785
63c372cb 1786 p = strjoina("/run/systemd/machines/", machine);
b6741478
LP
1787 r = parse_env_file(p, NEWLINE, "ROOT", &root, "CLASS", &class, NULL);
1788 if (r == -ENOENT)
1789 return -EHOSTDOWN;
1790 if (r < 0)
1791 return r;
1792 if (!root)
1793 return -ENODATA;
1794
1795 if (!streq_ptr(class, "container"))
1796 return -EIO;
1797
1798 j = journal_new(flags, NULL);
1799 if (!j)
1800 return -ENOMEM;
1801
89739579
LP
1802 j->prefix = root;
1803 root = NULL;
1804
1805 r = add_search_paths(j);
b6741478
LP
1806 if (r < 0)
1807 goto fail;
1808
1809 *ret = j;
1810 return 0;
1811
1812fail:
1813 sd_journal_close(j);
1814 return r;
1815}
1816
1aaa68f5 1817#define OPEN_DIRECTORY_ALLOWED_FLAGS \
10752e82
ZJS
1818 (SD_JOURNAL_OS_ROOT | \
1819 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1aaa68f5 1820
a963990f
LP
1821_public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) {
1822 sd_journal *j;
1823 int r;
87d2c1ff 1824
1ae464e0
TA
1825 assert_return(ret, -EINVAL);
1826 assert_return(path, -EINVAL);
1aaa68f5 1827 assert_return((flags & ~OPEN_DIRECTORY_ALLOWED_FLAGS) == 0, -EINVAL);
87d2c1ff 1828
7827b1a1 1829 j = journal_new(flags, path);
a963990f
LP
1830 if (!j)
1831 return -ENOMEM;
3fbf9cbb 1832
d077390c
LP
1833 if (flags & SD_JOURNAL_OS_ROOT)
1834 r = add_search_paths(j);
1835 else
1836 r = add_root_directory(j, path, false);
d617408e 1837 if (r < 0)
a963990f 1838 goto fail;
87d2c1ff
LP
1839
1840 *ret = j;
1841 return 0;
1842
1843fail:
1844 sd_journal_close(j);
87d2c1ff 1845 return r;
a963990f 1846}
87d2c1ff 1847
5302ebe1
ZJS
1848_public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int flags) {
1849 sd_journal *j;
1850 const char **path;
1851 int r;
1852
1ae464e0
TA
1853 assert_return(ret, -EINVAL);
1854 assert_return(flags == 0, -EINVAL);
5302ebe1
ZJS
1855
1856 j = journal_new(flags, NULL);
1857 if (!j)
1858 return -ENOMEM;
1859
1860 STRV_FOREACH(path, paths) {
5d1ce257 1861 r = add_any_file(j, -1, *path);
d617408e 1862 if (r < 0)
5302ebe1 1863 goto fail;
5302ebe1
ZJS
1864 }
1865
1866 j->no_new_files = true;
1867
1868 *ret = j;
1869 return 0;
1870
1871fail:
1872 sd_journal_close(j);
5d1ce257
LP
1873 return r;
1874}
1875
1aaa68f5 1876#define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
10752e82
ZJS
1877 (SD_JOURNAL_OS_ROOT | \
1878 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1aaa68f5 1879
5d1ce257
LP
1880_public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) {
1881 sd_journal *j;
1882 struct stat st;
1883 int r;
1884
1885 assert_return(ret, -EINVAL);
1886 assert_return(fd >= 0, -EBADF);
1aaa68f5 1887 assert_return((flags & ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS) == 0, -EINVAL);
5d1ce257
LP
1888
1889 if (fstat(fd, &st) < 0)
1890 return -errno;
1891
1892 if (!S_ISDIR(st.st_mode))
1893 return -EBADFD;
1894
1895 j = journal_new(flags, NULL);
1896 if (!j)
1897 return -ENOMEM;
1898
1899 j->toplevel_fd = fd;
1900
d077390c
LP
1901 if (flags & SD_JOURNAL_OS_ROOT)
1902 r = add_search_paths(j);
1903 else
1904 r = add_root_directory(j, NULL, false);
5d1ce257
LP
1905 if (r < 0)
1906 goto fail;
1907
1908 *ret = j;
1909 return 0;
1910
1911fail:
1912 sd_journal_close(j);
1913 return r;
1914}
1915
1916_public_ int sd_journal_open_files_fd(sd_journal **ret, int fds[], unsigned n_fds, int flags) {
1917 Iterator iterator;
1918 JournalFile *f;
1919 sd_journal *j;
1920 unsigned i;
1921 int r;
1922
1923 assert_return(ret, -EINVAL);
1924 assert_return(n_fds > 0, -EBADF);
1925 assert_return(flags == 0, -EINVAL);
1926
1927 j = journal_new(flags, NULL);
1928 if (!j)
1929 return -ENOMEM;
1930
1931 for (i = 0; i < n_fds; i++) {
1932 struct stat st;
1933
1934 if (fds[i] < 0) {
1935 r = -EBADF;
1936 goto fail;
1937 }
1938
1939 if (fstat(fds[i], &st) < 0) {
1940 r = -errno;
1941 goto fail;
1942 }
1943
1944 if (!S_ISREG(st.st_mode)) {
1945 r = -EBADFD;
1946 goto fail;
1947 }
1948
1949 r = add_any_file(j, fds[i], NULL);
1950 if (r < 0)
1951 goto fail;
1952 }
1953
1954 j->no_new_files = true;
1955 j->no_inotify = true;
5302ebe1 1956
5d1ce257
LP
1957 *ret = j;
1958 return 0;
1959
1960fail:
f8e2f4d6 1961 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
5d1ce257
LP
1962 * remain open */
1963 ORDERED_HASHMAP_FOREACH(f, j->files, iterator)
1964 f->close_fd = false;
1965
1966 sd_journal_close(j);
5302ebe1
ZJS
1967 return r;
1968}
1969
a5344d2c 1970_public_ void sd_journal_close(sd_journal *j) {
a963990f
LP
1971 Directory *d;
1972 JournalFile *f;
5768d259 1973 char *p;
a963990f 1974
a5344d2c
LP
1975 if (!j)
1976 return;
87d2c1ff 1977
54b1da83
LP
1978 sd_journal_flush_matches(j);
1979
c1f906bd 1980 while ((f = ordered_hashmap_steal_first(j->files)))
69a3a6fd 1981 (void) journal_file_close(f);
50f20cfd 1982
c1f906bd 1983 ordered_hashmap_free(j->files);
260a2be4 1984
a963990f
LP
1985 while ((d = hashmap_first(j->directories_by_path)))
1986 remove_directory(j, d);
260a2be4 1987
a963990f
LP
1988 while ((d = hashmap_first(j->directories_by_wd)))
1989 remove_directory(j, d);
87d2c1ff 1990
a963990f
LP
1991 hashmap_free(j->directories_by_path);
1992 hashmap_free(j->directories_by_wd);
1cc101f1 1993
03e334a1 1994 safe_close(j->inotify_fd);
50f20cfd 1995
bf807d4d
LP
1996 if (j->mmap) {
1997 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j->mmap), mmap_cache_get_missed(j->mmap));
16e9f408 1998 mmap_cache_unref(j->mmap);
bf807d4d 1999 }
16e9f408 2000
5768d259
LP
2001 while ((p = hashmap_steal_first(j->errors)))
2002 free(p);
2003 hashmap_free(j->errors);
2004
7827b1a1 2005 free(j->path);
89739579 2006 free(j->prefix);
3c1668da 2007 free(j->unique_field);
eb86030e 2008 free(j->fields_buffer);
87d2c1ff
LP
2009 free(j);
2010}
3fbf9cbb 2011
a5344d2c 2012_public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
3fbf9cbb
LP
2013 Object *o;
2014 JournalFile *f;
2015 int r;
2016
1ae464e0
TA
2017 assert_return(j, -EINVAL);
2018 assert_return(!journal_pid_changed(j), -ECHILD);
2019 assert_return(ret, -EINVAL);
3fbf9cbb
LP
2020
2021 f = j->current_file;
2022 if (!f)
de190aef 2023 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2024
2025 if (f->current_offset <= 0)
de190aef 2026 return -EADDRNOTAVAIL;
3fbf9cbb 2027
de190aef 2028 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2029 if (r < 0)
2030 return r;
2031
2032 *ret = le64toh(o->entry.realtime);
de190aef 2033 return 0;
3fbf9cbb
LP
2034}
2035
a5344d2c 2036_public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
3fbf9cbb
LP
2037 Object *o;
2038 JournalFile *f;
2039 int r;
2040 sd_id128_t id;
2041
1ae464e0
TA
2042 assert_return(j, -EINVAL);
2043 assert_return(!journal_pid_changed(j), -ECHILD);
3fbf9cbb
LP
2044
2045 f = j->current_file;
2046 if (!f)
de190aef 2047 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2048
2049 if (f->current_offset <= 0)
de190aef 2050 return -EADDRNOTAVAIL;
3fbf9cbb 2051
de190aef 2052 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2053 if (r < 0)
2054 return r;
2055
de190aef
LP
2056 if (ret_boot_id)
2057 *ret_boot_id = o->entry.boot_id;
2058 else {
2059 r = sd_id128_get_boot(&id);
2060 if (r < 0)
2061 return r;
3fbf9cbb 2062
de190aef 2063 if (!sd_id128_equal(id, o->entry.boot_id))
df50185b 2064 return -ESTALE;
de190aef 2065 }
3fbf9cbb 2066
14a65d65
LP
2067 if (ret)
2068 *ret = le64toh(o->entry.monotonic);
2069
de190aef 2070 return 0;
3fbf9cbb
LP
2071}
2072
362a3f81
LP
2073static bool field_is_valid(const char *field) {
2074 const char *p;
2075
2076 assert(field);
2077
2078 if (isempty(field))
2079 return false;
2080
2081 if (startswith(field, "__"))
2082 return false;
2083
2084 for (p = field; *p; p++) {
2085
2086 if (*p == '_')
2087 continue;
2088
2089 if (*p >= 'A' && *p <= 'Z')
2090 continue;
2091
2092 if (*p >= '0' && *p <= '9')
2093 continue;
2094
2095 return false;
2096 }
2097
2098 return true;
2099}
2100
a5344d2c 2101_public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
3fbf9cbb
LP
2102 JournalFile *f;
2103 uint64_t i, n;
2104 size_t field_length;
2105 int r;
2106 Object *o;
2107
1ae464e0
TA
2108 assert_return(j, -EINVAL);
2109 assert_return(!journal_pid_changed(j), -ECHILD);
2110 assert_return(field, -EINVAL);
2111 assert_return(data, -EINVAL);
2112 assert_return(size, -EINVAL);
2113 assert_return(field_is_valid(field), -EINVAL);
3fbf9cbb
LP
2114
2115 f = j->current_file;
2116 if (!f)
de190aef 2117 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2118
2119 if (f->current_offset <= 0)
de190aef 2120 return -EADDRNOTAVAIL;
3fbf9cbb 2121
de190aef 2122 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2123 if (r < 0)
2124 return r;
2125
2126 field_length = strlen(field);
2127
2128 n = journal_file_entry_n_items(o);
2129 for (i = 0; i < n; i++) {
4fd052ae
FC
2130 uint64_t p, l;
2131 le64_t le_hash;
3fbf9cbb 2132 size_t t;
1ec7120e 2133 int compression;
3fbf9cbb
LP
2134
2135 p = le64toh(o->entry.items[i].object_offset);
807e17f0 2136 le_hash = o->entry.items[i].hash;
de190aef 2137 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
3fbf9cbb
LP
2138 if (r < 0)
2139 return r;
2140
de190aef 2141 if (le_hash != o->data.hash)
de7b95cd
LP
2142 return -EBADMSG;
2143
3fbf9cbb
LP
2144 l = le64toh(o->object.size) - offsetof(Object, data.payload);
2145
1ec7120e 2146 compression = o->object.flags & OBJECT_COMPRESSION_MASK;
3b1a55e1
ZJS
2147 if (compression) {
2148#if defined(HAVE_XZ) || defined(HAVE_LZ4)
2aaec9b4 2149 r = decompress_startswith(compression,
3b1a55e1
ZJS
2150 o->data.payload, l,
2151 &f->compress_buffer, &f->compress_buffer_size,
2aaec9b4
ZJS
2152 field, field_length, '=');
2153 if (r < 0)
82e24b00 2154 log_debug_errno(r, "Cannot decompress %s object of length %"PRIu64" at offset "OFSfmt": %m",
2aaec9b4
ZJS
2155 object_compressed_to_string(compression), l, p);
2156 else if (r > 0) {
3b1a55e1 2157
fa1c4b51 2158 size_t rsize;
3b1a55e1
ZJS
2159
2160 r = decompress_blob(compression,
2161 o->data.payload, l,
2162 &f->compress_buffer, &f->compress_buffer_size, &rsize,
2163 j->data_threshold);
2164 if (r < 0)
2165 return r;
807e17f0 2166
3b1a55e1
ZJS
2167 *data = f->compress_buffer;
2168 *size = (size_t) rsize;
807e17f0 2169
3b1a55e1
ZJS
2170 return 0;
2171 }
2172#else
2173 return -EPROTONOSUPPORT;
2174#endif
807e17f0
LP
2175 } else if (l >= field_length+1 &&
2176 memcmp(o->data.payload, field, field_length) == 0 &&
2177 o->data.payload[field_length] == '=') {
3fbf9cbb 2178
161e54f8 2179 t = (size_t) l;
3fbf9cbb 2180
161e54f8
LP
2181 if ((uint64_t) t != l)
2182 return -E2BIG;
3fbf9cbb 2183
161e54f8
LP
2184 *data = o->data.payload;
2185 *size = t;
3fbf9cbb 2186
99613ec5 2187 return 0;
161e54f8 2188 }
3fbf9cbb 2189
de190aef 2190 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
161e54f8
LP
2191 if (r < 0)
2192 return r;
3fbf9cbb
LP
2193 }
2194
de190aef 2195 return -ENOENT;
3fbf9cbb
LP
2196}
2197
93b73b06 2198static int return_data(sd_journal *j, JournalFile *f, Object *o, const void **data, size_t *size) {
3c1668da
LP
2199 size_t t;
2200 uint64_t l;
3b1a55e1 2201 int compression;
3c1668da
LP
2202
2203 l = le64toh(o->object.size) - offsetof(Object, data.payload);
2204 t = (size_t) l;
2205
2206 /* We can't read objects larger than 4G on a 32bit machine */
2207 if ((uint64_t) t != l)
2208 return -E2BIG;
2209
1ec7120e
ZJS
2210 compression = o->object.flags & OBJECT_COMPRESSION_MASK;
2211 if (compression) {
3b1a55e1 2212#if defined(HAVE_XZ) || defined(HAVE_LZ4)
fa1c4b51 2213 size_t rsize;
3b1a55e1 2214 int r;
3c1668da 2215
1ec7120e
ZJS
2216 r = decompress_blob(compression,
2217 o->data.payload, l, &f->compress_buffer,
2218 &f->compress_buffer_size, &rsize, j->data_threshold);
2219 if (r < 0)
2220 return r;
3c1668da
LP
2221
2222 *data = f->compress_buffer;
2223 *size = (size_t) rsize;
3b1a55e1
ZJS
2224#else
2225 return -EPROTONOSUPPORT;
2226#endif
3c1668da
LP
2227 } else {
2228 *data = o->data.payload;
2229 *size = t;
2230 }
2231
2232 return 0;
2233}
2234
a5344d2c 2235_public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
3fbf9cbb 2236 JournalFile *f;
3c1668da 2237 uint64_t p, n;
4fd052ae 2238 le64_t le_hash;
3fbf9cbb
LP
2239 int r;
2240 Object *o;
2241
1ae464e0
TA
2242 assert_return(j, -EINVAL);
2243 assert_return(!journal_pid_changed(j), -ECHILD);
2244 assert_return(data, -EINVAL);
2245 assert_return(size, -EINVAL);
3fbf9cbb
LP
2246
2247 f = j->current_file;
2248 if (!f)
de190aef 2249 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2250
2251 if (f->current_offset <= 0)
de190aef 2252 return -EADDRNOTAVAIL;
3fbf9cbb 2253
de190aef 2254 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2255 if (r < 0)
2256 return r;
2257
2258 n = journal_file_entry_n_items(o);
7210bfb3 2259 if (j->current_field >= n)
3fbf9cbb
LP
2260 return 0;
2261
7210bfb3 2262 p = le64toh(o->entry.items[j->current_field].object_offset);
de190aef
LP
2263 le_hash = o->entry.items[j->current_field].hash;
2264 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
3fbf9cbb
LP
2265 if (r < 0)
2266 return r;
2267
de190aef 2268 if (le_hash != o->data.hash)
de7b95cd
LP
2269 return -EBADMSG;
2270
93b73b06 2271 r = return_data(j, f, o, data, size);
3c1668da
LP
2272 if (r < 0)
2273 return r;
3fbf9cbb 2274
313cefa1 2275 j->current_field++;
3fbf9cbb
LP
2276
2277 return 1;
2278}
c2373f84 2279
a5344d2c
LP
2280_public_ void sd_journal_restart_data(sd_journal *j) {
2281 if (!j)
2282 return;
8725d60a
LP
2283
2284 j->current_field = 0;
c2373f84 2285}
50f20cfd 2286
a5344d2c 2287_public_ int sd_journal_get_fd(sd_journal *j) {
a963990f
LP
2288 int r;
2289
1ae464e0
TA
2290 assert_return(j, -EINVAL);
2291 assert_return(!journal_pid_changed(j), -ECHILD);
50f20cfd 2292
5d1ce257
LP
2293 if (j->no_inotify)
2294 return -EMEDIUMTYPE;
2295
a963990f
LP
2296 if (j->inotify_fd >= 0)
2297 return j->inotify_fd;
2298
2299 r = allocate_inotify(j);
2300 if (r < 0)
2301 return r;
2302
5d1ce257
LP
2303 log_debug("Reiterating files to get inotify watches established");
2304
a963990f
LP
2305 /* Iterate through all dirs again, to add them to the
2306 * inotify */
5302ebe1
ZJS
2307 if (j->no_new_files)
2308 r = add_current_paths(j);
e73529f9
EV
2309 else if (j->flags & SD_JOURNAL_OS_ROOT)
2310 r = add_search_paths(j);
5d1ce257
LP
2311 else if (j->toplevel_fd >= 0)
2312 r = add_root_directory(j, NULL, false);
5302ebe1 2313 else if (j->path)
d617408e 2314 r = add_root_directory(j, j->path, true);
7827b1a1 2315 else
89739579 2316 r = add_search_paths(j);
a963990f
LP
2317 if (r < 0)
2318 return r;
2319
50f20cfd
LP
2320 return j->inotify_fd;
2321}
2322
ee531d94
LP
2323_public_ int sd_journal_get_events(sd_journal *j) {
2324 int fd;
2325
1ae464e0
TA
2326 assert_return(j, -EINVAL);
2327 assert_return(!journal_pid_changed(j), -ECHILD);
ee531d94
LP
2328
2329 fd = sd_journal_get_fd(j);
2330 if (fd < 0)
2331 return fd;
2332
2333 return POLLIN;
2334}
2335
39c155ea
LP
2336_public_ int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec) {
2337 int fd;
2338
1ae464e0
TA
2339 assert_return(j, -EINVAL);
2340 assert_return(!journal_pid_changed(j), -ECHILD);
2341 assert_return(timeout_usec, -EINVAL);
39c155ea
LP
2342
2343 fd = sd_journal_get_fd(j);
2344 if (fd < 0)
2345 return fd;
2346
2347 if (!j->on_network) {
2348 *timeout_usec = (uint64_t) -1;
2349 return 0;
2350 }
2351
2352 /* If we are on the network we need to regularly check for
2353 * changes manually */
2354
2355 *timeout_usec = j->last_process_usec + JOURNAL_FILES_RECHECK_USEC;
2356 return 1;
2357}
2358
50f20cfd 2359static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
a963990f 2360 Directory *d;
50f20cfd
LP
2361
2362 assert(j);
2363 assert(e);
2364
2365 /* Is this a subdirectory we watch? */
a963990f
LP
2366 d = hashmap_get(j->directories_by_wd, INT_TO_PTR(e->wd));
2367 if (d) {
2368 sd_id128_t id;
50f20cfd 2369
de2c3907
LP
2370 if (!(e->mask & IN_ISDIR) && e->len > 0 &&
2371 (endswith(e->name, ".journal") ||
2372 endswith(e->name, ".journal~"))) {
50f20cfd
LP
2373
2374 /* Event for a journal file */
2375
d617408e
LP
2376 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
2377 (void) add_file(j, d->path, e->name);
2378 else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT))
2379 remove_file(j, d->path, e->name);
50f20cfd 2380
a963990f 2381 } else if (!d->is_root && e->len == 0) {
50f20cfd 2382
a963990f 2383 /* Event for a subdirectory */
50f20cfd 2384
b2b46f91
TA
2385 if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT))
2386 remove_directory(j, d);
50f20cfd 2387
a963990f 2388 } else if (d->is_root && (e->mask & IN_ISDIR) && e->len > 0 && sd_id128_from_string(e->name, &id) >= 0) {
50f20cfd 2389
a963990f 2390 /* Event for root directory */
50f20cfd 2391
d617408e
LP
2392 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
2393 (void) add_directory(j, d->path, e->name);
50f20cfd
LP
2394 }
2395
2396 return;
2397 }
2398
2399 if (e->mask & IN_IGNORED)
2400 return;
2401
d617408e 2402 log_debug("Unknown inotify event.");
50f20cfd
LP
2403}
2404
a963990f
LP
2405static int determine_change(sd_journal *j) {
2406 bool b;
2407
2408 assert(j);
2409
2410 b = j->current_invalidate_counter != j->last_invalidate_counter;
2411 j->last_invalidate_counter = j->current_invalidate_counter;
2412
2413 return b ? SD_JOURNAL_INVALIDATE : SD_JOURNAL_APPEND;
2414}
2415
a5344d2c 2416_public_ int sd_journal_process(sd_journal *j) {
a963990f 2417 bool got_something = false;
50f20cfd 2418
1ae464e0
TA
2419 assert_return(j, -EINVAL);
2420 assert_return(!journal_pid_changed(j), -ECHILD);
50f20cfd 2421
39c155ea
LP
2422 j->last_process_usec = now(CLOCK_MONOTONIC);
2423
50f20cfd 2424 for (;;) {
0254e944 2425 union inotify_event_buffer buffer;
50f20cfd
LP
2426 struct inotify_event *e;
2427 ssize_t l;
2428
0254e944 2429 l = read(j->inotify_fd, &buffer, sizeof(buffer));
50f20cfd 2430 if (l < 0) {
a963990f
LP
2431 if (errno == EAGAIN || errno == EINTR)
2432 return got_something ? determine_change(j) : SD_JOURNAL_NOP;
50f20cfd
LP
2433
2434 return -errno;
2435 }
2436
a963990f
LP
2437 got_something = true;
2438
f7c1ad4f 2439 FOREACH_INOTIFY_EVENT(e, buffer, l)
50f20cfd 2440 process_inotify_event(j, e);
50f20cfd
LP
2441 }
2442}
6ad1d1c3 2443
e02d1cf7 2444_public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
a963990f 2445 int r;
39c155ea 2446 uint64_t t;
e02d1cf7 2447
1ae464e0
TA
2448 assert_return(j, -EINVAL);
2449 assert_return(!journal_pid_changed(j), -ECHILD);
e02d1cf7 2450
a963990f
LP
2451 if (j->inotify_fd < 0) {
2452
2453 /* This is the first invocation, hence create the
2454 * inotify watch */
2455 r = sd_journal_get_fd(j);
2456 if (r < 0)
2457 return r;
2458
2459 /* The journal might have changed since the context
2460 * object was created and we weren't watching before,
2461 * hence don't wait for anything, and return
2462 * immediately. */
2463 return determine_change(j);
2464 }
2465
39c155ea
LP
2466 r = sd_journal_get_timeout(j, &t);
2467 if (r < 0)
2468 return r;
2469
2470 if (t != (uint64_t) -1) {
2471 usec_t n;
2472
2473 n = now(CLOCK_MONOTONIC);
2474 t = t > n ? t - n : 0;
85210bff 2475
39c155ea
LP
2476 if (timeout_usec == (uint64_t) -1 || timeout_usec > t)
2477 timeout_usec = t;
85210bff
LP
2478 }
2479
a963990f
LP
2480 do {
2481 r = fd_wait_for_event(j->inotify_fd, POLLIN, timeout_usec);
2482 } while (r == -EINTR);
e02d1cf7
LP
2483
2484 if (r < 0)
2485 return r;
2486
a963990f 2487 return sd_journal_process(j);
e02d1cf7
LP
2488}
2489
08984293
LP
2490_public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to) {
2491 Iterator i;
2492 JournalFile *f;
2493 bool first = true;
581483bf 2494 uint64_t fmin = 0, tmax = 0;
08984293
LP
2495 int r;
2496
1ae464e0
TA
2497 assert_return(j, -EINVAL);
2498 assert_return(!journal_pid_changed(j), -ECHILD);
2499 assert_return(from || to, -EINVAL);
2500 assert_return(from != to, -EINVAL);
08984293 2501
c1f906bd 2502 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
08984293
LP
2503 usec_t fr, t;
2504
2505 r = journal_file_get_cutoff_realtime_usec(f, &fr, &t);
9f8d2983
LP
2506 if (r == -ENOENT)
2507 continue;
08984293
LP
2508 if (r < 0)
2509 return r;
2510 if (r == 0)
2511 continue;
2512
2513 if (first) {
581483bf
LP
2514 fmin = fr;
2515 tmax = t;
08984293
LP
2516 first = false;
2517 } else {
581483bf
LP
2518 fmin = MIN(fr, fmin);
2519 tmax = MAX(t, tmax);
08984293
LP
2520 }
2521 }
2522
581483bf
LP
2523 if (from)
2524 *from = fmin;
2525 if (to)
2526 *to = tmax;
2527
08984293
LP
2528 return first ? 0 : 1;
2529}
2530
2531_public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t *from, uint64_t *to) {
2532 Iterator i;
2533 JournalFile *f;
1651e2c6 2534 bool found = false;
08984293
LP
2535 int r;
2536
1ae464e0
TA
2537 assert_return(j, -EINVAL);
2538 assert_return(!journal_pid_changed(j), -ECHILD);
2539 assert_return(from || to, -EINVAL);
2540 assert_return(from != to, -EINVAL);
08984293 2541
c1f906bd 2542 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
08984293
LP
2543 usec_t fr, t;
2544
2545 r = journal_file_get_cutoff_monotonic_usec(f, boot_id, &fr, &t);
9f8d2983
LP
2546 if (r == -ENOENT)
2547 continue;
08984293
LP
2548 if (r < 0)
2549 return r;
2550 if (r == 0)
2551 continue;
2552
1651e2c6 2553 if (found) {
08984293 2554 if (from)
1651e2c6 2555 *from = MIN(fr, *from);
08984293 2556 if (to)
1651e2c6 2557 *to = MAX(t, *to);
08984293
LP
2558 } else {
2559 if (from)
1651e2c6 2560 *from = fr;
08984293 2561 if (to)
1651e2c6
ZJS
2562 *to = t;
2563 found = true;
08984293
LP
2564 }
2565 }
2566
1651e2c6 2567 return found;
08984293
LP
2568}
2569
dca6219e
LP
2570void journal_print_header(sd_journal *j) {
2571 Iterator i;
2572 JournalFile *f;
2573 bool newline = false;
2574
2575 assert(j);
2576
c1f906bd 2577 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
dca6219e
LP
2578 if (newline)
2579 putchar('\n');
2580 else
2581 newline = true;
2582
2583 journal_file_print_header(f);
2584 }
2585}
08984293 2586
a1a03e30
LP
2587_public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) {
2588 Iterator i;
2589 JournalFile *f;
2590 uint64_t sum = 0;
2591
1ae464e0
TA
2592 assert_return(j, -EINVAL);
2593 assert_return(!journal_pid_changed(j), -ECHILD);
2594 assert_return(bytes, -EINVAL);
a1a03e30 2595
c1f906bd 2596 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
a1a03e30
LP
2597 struct stat st;
2598
2599 if (fstat(f->fd, &st) < 0)
2600 return -errno;
2601
2602 sum += (uint64_t) st.st_blocks * 512ULL;
2603 }
2604
2605 *bytes = sum;
2606 return 0;
2607}
2608
3c1668da
LP
2609_public_ int sd_journal_query_unique(sd_journal *j, const char *field) {
2610 char *f;
2611
1ae464e0
TA
2612 assert_return(j, -EINVAL);
2613 assert_return(!journal_pid_changed(j), -ECHILD);
2614 assert_return(!isempty(field), -EINVAL);
2615 assert_return(field_is_valid(field), -EINVAL);
3c1668da
LP
2616
2617 f = strdup(field);
2618 if (!f)
2619 return -ENOMEM;
2620
2621 free(j->unique_field);
2622 j->unique_field = f;
2623 j->unique_file = NULL;
2624 j->unique_offset = 0;
360af4cf 2625 j->unique_file_lost = false;
3c1668da
LP
2626
2627 return 0;
2628}
2629
2630_public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l) {
3c1668da 2631 size_t k;
19a2bd80 2632
1ae464e0
TA
2633 assert_return(j, -EINVAL);
2634 assert_return(!journal_pid_changed(j), -ECHILD);
2635 assert_return(data, -EINVAL);
2636 assert_return(l, -EINVAL);
2637 assert_return(j->unique_field, -EINVAL);
19a2bd80 2638
3c1668da 2639 k = strlen(j->unique_field);
19a2bd80 2640
3c1668da 2641 if (!j->unique_file) {
360af4cf
ZJS
2642 if (j->unique_file_lost)
2643 return 0;
2644
c1f906bd 2645 j->unique_file = ordered_hashmap_first(j->files);
3c1668da
LP
2646 if (!j->unique_file)
2647 return 0;
360af4cf 2648
3c1668da
LP
2649 j->unique_offset = 0;
2650 }
19a2bd80 2651
3c1668da
LP
2652 for (;;) {
2653 JournalFile *of;
2654 Iterator i;
ae97089d 2655 Object *o;
3c1668da
LP
2656 const void *odata;
2657 size_t ol;
2658 bool found;
ae97089d 2659 int r;
3c1668da 2660
bdc02927 2661 /* Proceed to next data object in the field's linked list */
3c1668da
LP
2662 if (j->unique_offset == 0) {
2663 r = journal_file_find_field_object(j->unique_file, j->unique_field, k, &o, NULL);
2664 if (r < 0)
2665 return r;
2666
2667 j->unique_offset = r > 0 ? le64toh(o->field.head_data_offset) : 0;
2668 } else {
2669 r = journal_file_move_to_object(j->unique_file, OBJECT_DATA, j->unique_offset, &o);
2670 if (r < 0)
2671 return r;
2672
2673 j->unique_offset = le64toh(o->data.next_field_offset);
2674 }
2675
2676 /* We reached the end of the list? Then start again, with the next file */
2677 if (j->unique_offset == 0) {
c1f906bd 2678 j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
360af4cf 2679 if (!j->unique_file)
3c1668da
LP
2680 return 0;
2681
3c1668da
LP
2682 continue;
2683 }
2684
d05089d8
MS
2685 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2686 * instead, so that we can look at this data object at the same
3c1668da 2687 * time as one on another file */
d05089d8 2688 r = journal_file_move_to_object(j->unique_file, OBJECT_UNUSED, j->unique_offset, &o);
3c1668da
LP
2689 if (r < 0)
2690 return r;
2691
2692 /* Let's do the type check by hand, since we used 0 context above. */
ae97089d 2693 if (o->object.type != OBJECT_DATA) {
36202fd2 2694 log_debug("%s:offset " OFSfmt ": object has type %d, expected %d",
ae97089d
ZJS
2695 j->unique_file->path, j->unique_offset,
2696 o->object.type, OBJECT_DATA);
3c1668da 2697 return -EBADMSG;
ae97089d
ZJS
2698 }
2699
93b73b06 2700 r = return_data(j, j->unique_file, o, &odata, &ol);
3c1668da
LP
2701 if (r < 0)
2702 return r;
2703
0f99f74a
ZJS
2704 /* Check if we have at least the field name and "=". */
2705 if (ol <= k) {
2706 log_debug("%s:offset " OFSfmt ": object has size %zu, expected at least %zu",
2707 j->unique_file->path, j->unique_offset,
2708 ol, k + 1);
2709 return -EBADMSG;
2710 }
2711
2712 if (memcmp(odata, j->unique_field, k) || ((const char*) odata)[k] != '=') {
2713 log_debug("%s:offset " OFSfmt ": object does not start with \"%s=\"",
2714 j->unique_file->path, j->unique_offset,
2715 j->unique_field);
2716 return -EBADMSG;
2717 }
2718
3c1668da
LP
2719 /* OK, now let's see if we already returned this data
2720 * object by checking if it exists in the earlier
2721 * traversed files. */
2722 found = false;
c1f906bd 2723 ORDERED_HASHMAP_FOREACH(of, j->files, i) {
3c1668da
LP
2724 if (of == j->unique_file)
2725 break;
2726
ed71f956
LP
2727 /* Skip this file it didn't have any fields indexed */
2728 if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
3c1668da
LP
2729 continue;
2730
ed71f956 2731 r = journal_file_find_data_object_with_hash(of, odata, ol, le64toh(o->data.hash), NULL, NULL);
3c1668da
LP
2732 if (r < 0)
2733 return r;
ed71f956 2734 if (r > 0) {
3c1668da 2735 found = true;
ed71f956
LP
2736 break;
2737 }
3c1668da
LP
2738 }
2739
06cc69d4
JJ
2740 if (found)
2741 continue;
2742
93b73b06 2743 r = return_data(j, j->unique_file, o, data, l);
3c1668da
LP
2744 if (r < 0)
2745 return r;
2746
2747 return 1;
2748 }
2749}
2750
115646c7 2751_public_ void sd_journal_restart_unique(sd_journal *j) {
3c1668da
LP
2752 if (!j)
2753 return;
2754
2755 j->unique_file = NULL;
2756 j->unique_offset = 0;
360af4cf 2757 j->unique_file_lost = false;
3c1668da 2758}
85210bff 2759
eb86030e
LP
2760_public_ int sd_journal_enumerate_fields(sd_journal *j, const char **field) {
2761 int r;
2762
2763 assert_return(j, -EINVAL);
2764 assert_return(!journal_pid_changed(j), -ECHILD);
2765 assert_return(field, -EINVAL);
2766
2767 if (!j->fields_file) {
2768 if (j->fields_file_lost)
2769 return 0;
2770
2771 j->fields_file = ordered_hashmap_first(j->files);
2772 if (!j->fields_file)
2773 return 0;
2774
2775 j->fields_hash_table_index = 0;
2776 j->fields_offset = 0;
2777 }
2778
2779 for (;;) {
2780 JournalFile *f, *of;
2781 Iterator i;
2782 uint64_t m;
2783 Object *o;
2784 size_t sz;
2785 bool found;
2786
2787 f = j->fields_file;
2788
2789 if (j->fields_offset == 0) {
2790 bool eof = false;
2791
2792 /* We are not yet positioned at any field. Let's pick the first one */
2793 r = journal_file_map_field_hash_table(f);
2794 if (r < 0)
2795 return r;
2796
2797 m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem);
2798 for (;;) {
2799 if (j->fields_hash_table_index >= m) {
2800 /* Reached the end of the hash table, go to the next file. */
2801 eof = true;
2802 break;
2803 }
2804
2805 j->fields_offset = le64toh(f->field_hash_table[j->fields_hash_table_index].head_hash_offset);
2806
2807 if (j->fields_offset != 0)
2808 break;
2809
2810 /* Empty hash table bucket, go to next one */
2811 j->fields_hash_table_index++;
2812 }
2813
2814 if (eof) {
2815 /* Proceed with next file */
2816 j->fields_file = ordered_hashmap_next(j->files, f->path);
2817 if (!j->fields_file) {
2818 *field = NULL;
2819 return 0;
2820 }
2821
2822 j->fields_offset = 0;
2823 j->fields_hash_table_index = 0;
2824 continue;
2825 }
2826
2827 } else {
2828 /* We are already positioned at a field. If so, let's figure out the next field from it */
2829
2830 r = journal_file_move_to_object(f, OBJECT_FIELD, j->fields_offset, &o);
2831 if (r < 0)
2832 return r;
2833
2834 j->fields_offset = le64toh(o->field.next_hash_offset);
2835 if (j->fields_offset == 0) {
2836 /* Reached the end of the hash table chain */
2837 j->fields_hash_table_index++;
2838 continue;
2839 }
2840 }
2841
1f133e0d 2842 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
eb86030e
LP
2843 r = journal_file_move_to_object(f, OBJECT_UNUSED, j->fields_offset, &o);
2844 if (r < 0)
2845 return r;
2846
2847 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
2848 if (o->object.type != OBJECT_FIELD) {
2849 log_debug("%s:offset " OFSfmt ": object has type %i, expected %i", f->path, j->fields_offset, o->object.type, OBJECT_FIELD);
2850 return -EBADMSG;
2851 }
2852
2853 sz = le64toh(o->object.size) - offsetof(Object, field.payload);
2854
2855 /* Let's see if we already returned this field name before. */
2856 found = false;
2857 ORDERED_HASHMAP_FOREACH(of, j->files, i) {
2858 if (of == f)
2859 break;
2860
2861 /* Skip this file it didn't have any fields indexed */
2862 if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
2863 continue;
2864
2865 r = journal_file_find_field_object_with_hash(of, o->field.payload, sz, le64toh(o->field.hash), NULL, NULL);
2866 if (r < 0)
2867 return r;
2868 if (r > 0) {
2869 found = true;
2870 break;
2871 }
2872 }
2873
2874 if (found)
2875 continue;
2876
2877 /* Check if this is really a valid string containing no NUL byte */
2878 if (memchr(o->field.payload, 0, sz))
2879 return -EBADMSG;
2880
2881 if (sz > j->data_threshold)
2882 sz = j->data_threshold;
2883
2884 if (!GREEDY_REALLOC(j->fields_buffer, j->fields_buffer_allocated, sz + 1))
2885 return -ENOMEM;
2886
2887 memcpy(j->fields_buffer, o->field.payload, sz);
2888 j->fields_buffer[sz] = 0;
2889
2890 if (!field_is_valid(j->fields_buffer))
2891 return -EBADMSG;
2892
2893 *field = j->fields_buffer;
2894 return 1;
2895 }
2896}
2897
2898_public_ void sd_journal_restart_fields(sd_journal *j) {
2899 if (!j)
2900 return;
2901
2902 j->fields_file = NULL;
2903 j->fields_hash_table_index = 0;
2904 j->fields_offset = 0;
2905 j->fields_file_lost = false;
2906}
2907
85210bff 2908_public_ int sd_journal_reliable_fd(sd_journal *j) {
1ae464e0
TA
2909 assert_return(j, -EINVAL);
2910 assert_return(!journal_pid_changed(j), -ECHILD);
85210bff
LP
2911
2912 return !j->on_network;
2913}
d4205751
LP
2914
2915static char *lookup_field(const char *field, void *userdata) {
2916 sd_journal *j = userdata;
2917 const void *data;
2918 size_t size, d;
2919 int r;
2920
2921 assert(field);
2922 assert(j);
2923
2924 r = sd_journal_get_data(j, field, &data, &size);
2925 if (r < 0 ||
2926 size > REPLACE_VAR_MAX)
2927 return strdup(field);
2928
2929 d = strlen(field) + 1;
2930
2931 return strndup((const char*) data + d, size - d);
2932}
2933
2934_public_ int sd_journal_get_catalog(sd_journal *j, char **ret) {
2935 const void *data;
2936 size_t size;
2937 sd_id128_t id;
2938 _cleanup_free_ char *text = NULL, *cid = NULL;
2939 char *t;
2940 int r;
2941
1ae464e0
TA
2942 assert_return(j, -EINVAL);
2943 assert_return(!journal_pid_changed(j), -ECHILD);
2944 assert_return(ret, -EINVAL);
d4205751
LP
2945
2946 r = sd_journal_get_data(j, "MESSAGE_ID", &data, &size);
2947 if (r < 0)
2948 return r;
2949
2950 cid = strndup((const char*) data + 11, size - 11);
2951 if (!cid)
2952 return -ENOMEM;
2953
2954 r = sd_id128_from_string(cid, &id);
2955 if (r < 0)
2956 return r;
2957
844ec79b 2958 r = catalog_get(CATALOG_DATABASE, id, &text);
d4205751
LP
2959 if (r < 0)
2960 return r;
2961
2962 t = replace_var(text, lookup_field, j);
2963 if (!t)
2964 return -ENOMEM;
2965
2966 *ret = t;
2967 return 0;
2968}
8f1e860f
LP
2969
2970_public_ int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **ret) {
1ae464e0 2971 assert_return(ret, -EINVAL);
8f1e860f 2972
844ec79b 2973 return catalog_get(CATALOG_DATABASE, id, ret);
8f1e860f 2974}
93b73b06
LP
2975
2976_public_ int sd_journal_set_data_threshold(sd_journal *j, size_t sz) {
1ae464e0
TA
2977 assert_return(j, -EINVAL);
2978 assert_return(!journal_pid_changed(j), -ECHILD);
93b73b06
LP
2979
2980 j->data_threshold = sz;
2981 return 0;
2982}
2983
2984_public_ int sd_journal_get_data_threshold(sd_journal *j, size_t *sz) {
1ae464e0
TA
2985 assert_return(j, -EINVAL);
2986 assert_return(!journal_pid_changed(j), -ECHILD);
2987 assert_return(sz, -EINVAL);
93b73b06
LP
2988
2989 *sz = j->data_threshold;
2990 return 0;
2991}
39fd5b08
JS
2992
2993_public_ int sd_journal_has_runtime_files(sd_journal *j) {
2994 assert_return(j, -EINVAL);
2995
2996 return j->has_runtime_files;
2997}
2998
2999_public_ int sd_journal_has_persistent_files(sd_journal *j) {
3000 assert_return(j, -EINVAL);
3001
3002 return j->has_persistent_files;
3003}