]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/sd-journal.c
2a3824d0e89295da8c71274bfd21fb04407ec74b
[thirdparty/systemd.git] / src / journal / sd-journal.c
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
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
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
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <inttypes.h>
23 #include <linux/magic.h>
24 #include <poll.h>
25 #include <stddef.h>
26 #include <sys/inotify.h>
27 #include <sys/vfs.h>
28 #include <unistd.h>
29
30 #include "sd-journal.h"
31
32 #include "alloc-util.h"
33 #include "catalog.h"
34 #include "compress.h"
35 #include "dirent-util.h"
36 #include "fd-util.h"
37 #include "fileio.h"
38 #include "formats-util.h"
39 #include "fs-util.h"
40 #include "hashmap.h"
41 #include "hostname-util.h"
42 #include "io-util.h"
43 #include "journal-def.h"
44 #include "journal-file.h"
45 #include "journal-internal.h"
46 #include "list.h"
47 #include "lookup3.h"
48 #include "missing.h"
49 #include "path-util.h"
50 #include "replace-var.h"
51 #include "stat-util.h"
52 #include "stdio-util.h"
53 #include "string-util.h"
54 #include "strv.h"
55
56 #define JOURNAL_FILES_MAX 7168
57
58 #define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
59
60 #define REPLACE_VAR_MAX 256
61
62 #define DEFAULT_DATA_THRESHOLD (64*1024)
63
64 static void remove_file_real(sd_journal *j, JournalFile *f);
65
66 static 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
75 static int journal_put_error(sd_journal *j, int r, const char *path) {
76 char *copy;
77 int k;
78
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
90 if (r >= 0)
91 return r;
92
93 k = hashmap_ensure_allocated(&j->errors, NULL);
94 if (k < 0)
95 return k;
96
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;
115 }
116
117 static void detach_location(sd_journal *j) {
118 Iterator i;
119 JournalFile *f;
120
121 assert(j);
122
123 j->current_file = NULL;
124 j->current_field = 0;
125
126 ORDERED_HASHMAP_FOREACH(f, j->files, i)
127 journal_file_reset_location(f);
128 }
129
130 static void reset_location(sd_journal *j) {
131 assert(j);
132
133 detach_location(j);
134 zero(j->current_location);
135 }
136
137 static void init_location(Location *l, LocationType type, JournalFile *f, Object *o) {
138 assert(l);
139 assert(type == LOCATION_DISCRETE || type == LOCATION_SEEK);
140 assert(f);
141 assert(o->object.type == OBJECT_ENTRY);
142
143 l->type = type;
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);
148 l->boot_id = o->entry.boot_id;
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
154 static void set_location(sd_journal *j, JournalFile *f, Object *o) {
155 assert(j);
156 assert(f);
157 assert(o);
158
159 init_location(&j->current_location, LOCATION_DISCRETE, f, o);
160
161 j->current_file = f;
162 j->current_field = 0;
163
164 /* Let f know its candidate entry was picked. */
165 assert(f->location_type == LOCATION_SEEK);
166 f->location_type = LOCATION_DISCRETE;
167 }
168
169 static 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
201 static bool same_field(const void *_a, size_t s, const void *_b, size_t t) {
202 const uint8_t *a = _a, *b = _b;
203 size_t j;
204
205 for (j = 0; j < s && j < t; j++) {
206
207 if (a[j] != b[j])
208 return false;
209
210 if (a[j] == '=')
211 return true;
212 }
213
214 assert_not_reached("\"=\" not found");
215 }
216
217 static 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;
228 LIST_PREPEND(matches, p->matches, m);
229 }
230
231 return m;
232 }
233
234 static void match_free(Match *m) {
235 assert(m);
236
237 while (m->matches)
238 match_free(m->matches);
239
240 if (m->parent)
241 LIST_REMOVE(matches, m->parent->matches, m);
242
243 free(m->data);
244 free(m);
245 }
246
247 static void match_free_if_empty(Match *m) {
248 if (!m || m->matches)
249 return;
250
251 match_free(m);
252 }
253
254 _public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
255 Match *l3, *l4, *add_here = NULL, *m;
256 le64_t le_hash;
257
258 assert_return(j, -EINVAL);
259 assert_return(!journal_pid_changed(j), -ECHILD);
260 assert_return(data, -EINVAL);
261
262 if (size == 0)
263 size = strlen(data);
264
265 assert_return(match_is_valid(data, size), -EINVAL);
266
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 */
272
273 if (!j->level0) {
274 j->level0 = match_new(NULL, MATCH_AND_TERM);
275 if (!j->level0)
276 return -ENOMEM;
277 }
278
279 if (!j->level1) {
280 j->level1 = match_new(j->level0, MATCH_OR_TERM);
281 if (!j->level1)
282 return -ENOMEM;
283 }
284
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);
294
295 le_hash = htole64(hash64(data, size));
296
297 LIST_FOREACH(matches, l3, j->level2->matches) {
298 assert(l3->type == MATCH_OR_TERM);
299
300 LIST_FOREACH(matches, l4, l3->matches) {
301 assert(l4->type == MATCH_DISCRETE);
302
303 /* Exactly the same match already? Then ignore
304 * this addition */
305 if (l4->le_hash == le_hash &&
306 l4->size == size &&
307 memcmp(l4->data, data, size) == 0)
308 return 0;
309
310 /* Same field? Then let's add this to this OR term */
311 if (same_field(data, size, l4->data, l4->size)) {
312 add_here = l3;
313 break;
314 }
315 }
316
317 if (add_here)
318 break;
319 }
320
321 if (!add_here) {
322 add_here = match_new(j->level2, MATCH_OR_TERM);
323 if (!add_here)
324 goto fail;
325 }
326
327 m = match_new(add_here, MATCH_DISCRETE);
328 if (!m)
329 goto fail;
330
331 m->le_hash = le_hash;
332 m->size = size;
333 m->data = memdup(data, size);
334 if (!m->data)
335 goto fail;
336
337 detach_location(j);
338
339 return 0;
340
341 fail:
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);
346
347 return -ENOMEM;
348 }
349
350 _public_ int sd_journal_add_conjunction(sd_journal *j) {
351 assert_return(j, -EINVAL);
352 assert_return(!journal_pid_changed(j), -ECHILD);
353
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
363 j->level1 = NULL;
364 j->level2 = NULL;
365
366 return 0;
367 }
368
369 _public_ int sd_journal_add_disjunction(sd_journal *j) {
370 assert_return(j, -EINVAL);
371 assert_return(!journal_pid_changed(j), -ECHILD);
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;
384
385 j->level2 = NULL;
386 return 0;
387 }
388
389 static char *match_make_string(Match *m) {
390 char *p, *r;
391 Match *i;
392 bool enclose = false;
393
394 if (!m)
395 return strdup("none");
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) {
411 k = strjoin(p, m->type == MATCH_OR_TERM ? " OR " : " AND ", t, NULL);
412 free(p);
413 free(t);
414
415 if (!k)
416 return NULL;
417
418 p = k;
419
420 enclose = true;
421 } else
422 p = t;
423 }
424
425 if (enclose) {
426 r = strjoin("(", p, ")", NULL);
427 free(p);
428 return r;
429 }
430
431 return p;
432 }
433
434 char *journal_make_match_string(sd_journal *j) {
435 assert(j);
436
437 return match_make_string(j->level0);
438 }
439
440 _public_ void sd_journal_flush_matches(sd_journal *j) {
441 if (!j)
442 return;
443
444 if (j->level0)
445 match_free(j->level0);
446
447 j->level0 = j->level1 = j->level2 = NULL;
448
449 detach_location(j);
450 }
451
452 _pure_ static int compare_with_location(JournalFile *f, Location *l) {
453 assert(f);
454 assert(l);
455 assert(f->location_type == LOCATION_SEEK);
456 assert(l->type == LOCATION_DISCRETE || l->type == LOCATION_SEEK);
457
458 if (l->monotonic_set &&
459 sd_id128_equal(f->current_boot_id, l->boot_id) &&
460 l->realtime_set &&
461 f->current_realtime == l->realtime &&
462 l->xor_hash_set &&
463 f->current_xor_hash == l->xor_hash)
464 return 0;
465
466 if (l->seqnum_set &&
467 sd_id128_equal(f->header->seqnum_id, l->seqnum_id)) {
468
469 if (f->current_seqnum < l->seqnum)
470 return -1;
471 if (f->current_seqnum > l->seqnum)
472 return 1;
473 }
474
475 if (l->monotonic_set &&
476 sd_id128_equal(f->current_boot_id, l->boot_id)) {
477
478 if (f->current_monotonic < l->monotonic)
479 return -1;
480 if (f->current_monotonic > l->monotonic)
481 return 1;
482 }
483
484 if (l->realtime_set) {
485
486 if (f->current_realtime < l->realtime)
487 return -1;
488 if (f->current_realtime > l->realtime)
489 return 1;
490 }
491
492 if (l->xor_hash_set) {
493
494 if (f->current_xor_hash < l->xor_hash)
495 return -1;
496 if (f->current_xor_hash > l->xor_hash)
497 return 1;
498 }
499
500 return 0;
501 }
502
503 static 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
512 int r;
513 uint64_t np = 0;
514 Object *n;
515
516 assert(j);
517 assert(m);
518 assert(f);
519
520 if (m->type == MATCH_DISCRETE) {
521 uint64_t dp;
522
523 r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), NULL, &dp);
524 if (r <= 0)
525 return r;
526
527 return journal_file_move_to_entry_by_offset_for_data(f, dp, after_offset, direction, ret, offset);
528
529 } else if (m->type == MATCH_OR_TERM) {
530 Match *i;
531
532 /* Find the earliest match beyond after_offset */
533
534 LIST_FOREACH(matches, i, m->matches) {
535 uint64_t cp;
536
537 r = next_for_match(j, i, f, after_offset, direction, NULL, &cp);
538 if (r < 0)
539 return r;
540 else if (r > 0) {
541 if (np == 0 || (direction == DIRECTION_DOWN ? cp < np : cp > np))
542 np = cp;
543 }
544 }
545
546 if (np == 0)
547 return 0;
548
549 } else if (m->type == MATCH_AND_TERM) {
550 Match *i, *last_moved;
551
552 /* Always jump to the next matching entry and repeat
553 * this until we find an offset that matches for all
554 * matches. */
555
556 if (!m->matches)
557 return 0;
558
559 r = next_for_match(j, m->matches, f, after_offset, direction, NULL, &np);
560 if (r <= 0)
561 return r;
562
563 assert(direction == DIRECTION_DOWN ? np >= after_offset : np <= after_offset);
564 last_moved = m->matches;
565
566 LIST_LOOP_BUT_ONE(matches, i, m->matches, last_moved) {
567 uint64_t cp;
568
569 r = next_for_match(j, i, f, np, direction, NULL, &cp);
570 if (r <= 0)
571 return r;
572
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;
577 }
578 }
579 }
580
581 assert(np > 0);
582
583 r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
584 if (r < 0)
585 return r;
586
587 if (ret)
588 *ret = n;
589 if (offset)
590 *offset = np;
591
592 return 1;
593 }
594
595 static 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
603 int r;
604
605 assert(j);
606 assert(m);
607 assert(f);
608
609 if (m->type == MATCH_DISCRETE) {
610 uint64_t dp;
611
612 r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), NULL, &dp);
613 if (r <= 0)
614 return r;
615
616 /* FIXME: missing: find by monotonic */
617
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);
631
632 return journal_file_next_entry_for_data(f, NULL, 0, dp, direction, ret, offset);
633
634 } else if (m->type == MATCH_OR_TERM) {
635 uint64_t np = 0;
636 Object *n;
637 Match *i;
638
639 /* Find the earliest match */
640
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;
650 }
651 }
652
653 if (np == 0)
654 return 0;
655
656 r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
657 if (r < 0)
658 return r;
659
660 if (ret)
661 *ret = n;
662 if (offset)
663 *offset = np;
664
665 return 1;
666
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)
684 return r;
685
686 if (np == 0 || (direction == DIRECTION_DOWN ? cp > np : cp < np))
687 np = cp;
688 }
689
690 return next_for_match(j, m, f, np, direction, ret, offset);
691 }
692 }
693
694 static 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)
712 return journal_file_next_entry(f, 0, DIRECTION_DOWN, ret, offset);
713 if (j->current_location.type == LOCATION_TAIL)
714 return journal_file_next_entry(f, 0, DIRECTION_UP, ret, offset);
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;
721 }
722 if (j->current_location.realtime_set)
723 return journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, ret, offset);
724
725 return journal_file_next_entry(f, 0, direction, ret, offset);
726 } else
727 return find_location_for_match(j, j->level0, f, direction, ret, offset);
728 }
729
730 static int next_with_matches(
731 sd_journal *j,
732 JournalFile *f,
733 direction_t direction,
734 Object **ret,
735 uint64_t *offset) {
736
737 assert(j);
738 assert(f);
739 assert(ret);
740 assert(offset);
741
742 /* No matches is easy. We simple advance the file
743 * pointer by one. */
744 if (!j->level0)
745 return journal_file_next_entry(f, f->current_offset, direction, ret, offset);
746
747 /* If we have a match then we look for the next matching entry
748 * with an offset at least one step larger */
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);
753 }
754
755 static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction) {
756 Object *c;
757 uint64_t cp, n_entries;
758 int r;
759
760 assert(j);
761 assert(f);
762
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;
770
771 f->last_n_entries = n_entries;
772
773 if (f->last_direction == direction && f->current_offset > 0) {
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. */
777 if (f->location_type != LOCATION_SEEK) {
778 r = next_with_matches(j, f, direction, &c, &cp);
779 if (r <= 0)
780 return r;
781
782 journal_file_save_location(f, c, cp);
783 }
784 } else {
785 f->last_direction = direction;
786
787 r = find_location_with_matches(j, f, direction, &c, &cp);
788 if (r <= 0)
789 return r;
790
791 journal_file_save_location(f, c, cp);
792 }
793
794 /* OK, we found the spot, now let's advance until an entry
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
800 for (;;) {
801 bool found;
802
803 if (j->current_location.type == LOCATION_DISCRETE) {
804 int k;
805
806 k = compare_with_location(f, &j->current_location);
807
808 found = direction == DIRECTION_DOWN ? k > 0 : k < 0;
809 } else
810 found = true;
811
812 if (found)
813 return 1;
814
815 r = next_with_matches(j, f, direction, &c, &cp);
816 if (r <= 0)
817 return r;
818
819 journal_file_save_location(f, c, cp);
820 }
821 }
822
823 static int real_journal_next(sd_journal *j, direction_t direction) {
824 JournalFile *f, *new_file = NULL;
825 Iterator i;
826 Object *o;
827 int r;
828
829 assert_return(j, -EINVAL);
830 assert_return(!journal_pid_changed(j), -ECHILD);
831
832 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
833 bool found;
834
835 r = next_beyond_location(j, f, direction);
836 if (r < 0) {
837 log_debug_errno(r, "Can't iterate through %s, ignoring: %m", f->path);
838 remove_file_real(j, f);
839 continue;
840 } else if (r == 0) {
841 f->location_type = LOCATION_TAIL;
842 continue;
843 }
844
845 if (!new_file)
846 found = true;
847 else {
848 int k;
849
850 k = journal_file_compare_locations(f, new_file);
851
852 found = direction == DIRECTION_DOWN ? k < 0 : k > 0;
853 }
854
855 if (found)
856 new_file = f;
857 }
858
859 if (!new_file)
860 return 0;
861
862 r = journal_file_move_to_object(new_file, OBJECT_ENTRY, new_file->current_offset, &o);
863 if (r < 0)
864 return r;
865
866 set_location(j, new_file, o);
867
868 return 1;
869 }
870
871 _public_ int sd_journal_next(sd_journal *j) {
872 return real_journal_next(j, DIRECTION_DOWN);
873 }
874
875 _public_ int sd_journal_previous(sd_journal *j) {
876 return real_journal_next(j, DIRECTION_UP);
877 }
878
879 static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) {
880 int c = 0, r;
881
882 assert_return(j, -EINVAL);
883 assert_return(!journal_pid_changed(j), -ECHILD);
884
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);
896 if (r < 0)
897 return r;
898
899 if (r == 0)
900 return c;
901
902 skip--;
903 c++;
904 } while (skip > 0);
905
906 return c;
907 }
908
909 _public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
910 return real_journal_next_skip(j, DIRECTION_DOWN, skip);
911 }
912
913 _public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
914 return real_journal_next_skip(j, DIRECTION_UP, skip);
915 }
916
917 _public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) {
918 Object *o;
919 int r;
920 char bid[33], sid[33];
921
922 assert_return(j, -EINVAL);
923 assert_return(!journal_pid_changed(j), -ECHILD);
924 assert_return(cursor, -EINVAL);
925
926 if (!j->current_file || j->current_file->current_offset <= 0)
927 return -EADDRNOTAVAIL;
928
929 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
930 if (r < 0)
931 return r;
932
933 sd_id128_to_string(j->current_file->header->seqnum_id, sid);
934 sd_id128_to_string(o->entry.boot_id, bid);
935
936 if (asprintf(cursor,
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)
942 return -ENOMEM;
943
944 return 0;
945 }
946
947 _public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
948 const char *word, *state;
949 size_t l;
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
960 assert_return(j, -EINVAL);
961 assert_return(!journal_pid_changed(j), -ECHILD);
962 assert_return(!isempty(cursor), -EINVAL);
963
964 FOREACH_WORD_SEPARATOR(word, l, cursor, ";", state) {
965 char *item;
966 int k = 0;
967
968 if (l < 2 || word[1] != '=')
969 return -EINVAL;
970
971 item = strndup(word, l);
972 if (!item)
973 return -ENOMEM;
974
975 switch (word[0]) {
976
977 case 's':
978 seqnum_id_set = true;
979 k = sd_id128_from_string(item+2, &seqnum_id);
980 break;
981
982 case 'i':
983 seqnum_set = true;
984 if (sscanf(item+2, "%llx", &seqnum) != 1)
985 k = -EINVAL;
986 break;
987
988 case 'b':
989 boot_id_set = true;
990 k = sd_id128_from_string(item+2, &boot_id);
991 break;
992
993 case 'm':
994 monotonic_set = true;
995 if (sscanf(item+2, "%llx", &monotonic) != 1)
996 k = -EINVAL;
997 break;
998
999 case 't':
1000 realtime_set = true;
1001 if (sscanf(item+2, "%llx", &realtime) != 1)
1002 k = -EINVAL;
1003 break;
1004
1005 case 'x':
1006 xor_hash_set = true;
1007 if (sscanf(item+2, "%llx", &xor_hash) != 1)
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
1025 j->current_location.type = LOCATION_SEEK;
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
1052 _public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) {
1053 int r;
1054 Object *o;
1055
1056 assert_return(j, -EINVAL);
1057 assert_return(!journal_pid_changed(j), -ECHILD);
1058 assert_return(!isempty(cursor), -EINVAL);
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
1067 for (;;) {
1068 _cleanup_free_ char *item = NULL;
1069 unsigned long long ll;
1070 sd_id128_t id;
1071 int k = 0;
1072
1073 r = extract_first_word(&cursor, &item, ";", EXTRACT_DONT_COALESCE_SEPARATORS);
1074 if (r < 0)
1075 return r;
1076
1077 if (r == 0)
1078 break;
1079
1080 if (strlen(item) < 2 || item[1] != '=')
1081 return -EINVAL;
1082
1083 switch (item[0]) {
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
1135 _public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
1136 assert_return(j, -EINVAL);
1137 assert_return(!journal_pid_changed(j), -ECHILD);
1138
1139 reset_location(j);
1140 j->current_location.type = LOCATION_SEEK;
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
1148 _public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
1149 assert_return(j, -EINVAL);
1150 assert_return(!journal_pid_changed(j), -ECHILD);
1151
1152 reset_location(j);
1153 j->current_location.type = LOCATION_SEEK;
1154 j->current_location.realtime = usec;
1155 j->current_location.realtime_set = true;
1156
1157 return 0;
1158 }
1159
1160 _public_ int sd_journal_seek_head(sd_journal *j) {
1161 assert_return(j, -EINVAL);
1162 assert_return(!journal_pid_changed(j), -ECHILD);
1163
1164 reset_location(j);
1165 j->current_location.type = LOCATION_HEAD;
1166
1167 return 0;
1168 }
1169
1170 _public_ int sd_journal_seek_tail(sd_journal *j) {
1171 assert_return(j, -EINVAL);
1172 assert_return(!journal_pid_changed(j), -ECHILD);
1173
1174 reset_location(j);
1175 j->current_location.type = LOCATION_TAIL;
1176
1177 return 0;
1178 }
1179
1180 static 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 =
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);
1197 }
1198
1199 static bool file_has_type_prefix(const char *prefix, const char *filename) {
1200 const char *full, *tilded, *atted;
1201
1202 full = strjoina(prefix, ".journal");
1203 tilded = strjoina(full, "~");
1204 atted = strjoina(prefix, "@");
1205
1206 return streq(filename, full) ||
1207 streq(filename, tilded) ||
1208 startswith(filename, atted);
1209 }
1210
1211 static bool file_type_wanted(int flags, const char *filename) {
1212 assert(filename);
1213
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
1227 xsprintf(prefix, "user-"UID_FMT, getuid());
1228
1229 if (file_has_type_prefix(prefix, filename))
1230 return true;
1231 }
1232
1233 return false;
1234 }
1235
1236 static 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
1247 static 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
1258 static int add_any_file(sd_journal *j, int fd, const char *path) {
1259 JournalFile *f = NULL;
1260 bool close_fd = false;
1261 int r, k;
1262
1263 assert(j);
1264 assert(fd >= 0 || path);
1265
1266 if (path && ordered_hashmap_get(j->files, path))
1267 return 0;
1268
1269 if (ordered_hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
1270 log_debug("Too many open journal files, not adding %s.", path);
1271 r = -ETOOMANYREFS;
1272 goto fail;
1273 }
1274
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);
1290 if (r < 0) {
1291 if (close_fd)
1292 safe_close(fd);
1293 log_debug_errno(r, "Failed to open journal file %s: %m", path);
1294 goto fail;
1295 }
1296
1297 /* journal_file_dump(f); */
1298
1299 r = ordered_hashmap_put(j->files, f->path, f);
1300 if (r < 0) {
1301 f->close_fd = close_fd;
1302 (void) journal_file_close(f);
1303 goto fail;
1304 }
1305
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
1311 log_debug("File %s added.", f->path);
1312
1313 check_network(j, f->fd);
1314
1315 j->current_invalidate_counter++;
1316
1317 return 0;
1318
1319 fail:
1320 k = journal_put_error(j, r, path);
1321 if (k < 0)
1322 return k;
1323
1324 return r;
1325 }
1326
1327 static int add_file(sd_journal *j, const char *prefix, const char *filename) {
1328 const char *path;
1329
1330 assert(j);
1331 assert(prefix);
1332 assert(filename);
1333
1334 if (j->no_new_files)
1335 return 0;
1336
1337 if (!file_type_wanted(j->flags, filename))
1338 return 0;
1339
1340 path = strjoina(prefix, "/", filename);
1341 return add_any_file(j, -1, path);
1342 }
1343
1344 static void remove_file(sd_journal *j, const char *prefix, const char *filename) {
1345 const char *path;
1346 JournalFile *f;
1347
1348 assert(j);
1349 assert(prefix);
1350 assert(filename);
1351
1352 path = strjoina(prefix, "/", filename);
1353 f = ordered_hashmap_get(j->files, path);
1354 if (!f)
1355 return;
1356
1357 remove_file_real(j, f);
1358 }
1359
1360 static void remove_file_real(sd_journal *j, JournalFile *f) {
1361 assert(j);
1362 assert(f);
1363
1364 ordered_hashmap_remove(j->files, f->path);
1365
1366 log_debug("File %s removed.", f->path);
1367
1368 if (j->current_file == f) {
1369 j->current_file = NULL;
1370 j->current_field = 0;
1371 }
1372
1373 if (j->unique_file == f) {
1374 /* Jump to the next unique_file or NULL if that one was last */
1375 j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
1376 j->unique_offset = 0;
1377 if (!j->unique_file)
1378 j->unique_file_lost = true;
1379 }
1380
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
1388 (void) journal_file_close(f);
1389
1390 j->current_invalidate_counter++;
1391 }
1392
1393 static 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
1408 static int add_directory(sd_journal *j, const char *prefix, const char *dirname) {
1409 _cleanup_free_ char *path = NULL;
1410 _cleanup_closedir_ DIR *d = NULL;
1411 struct dirent *de = NULL;
1412 Directory *m;
1413 int r, k;
1414
1415 assert(j);
1416 assert(prefix);
1417
1418 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1419 * and reenumerates directory contents */
1420
1421 if (dirname)
1422 path = strjoin(prefix, "/", dirname, NULL);
1423 else
1424 path = strdup(prefix);
1425 if (!path) {
1426 r = -ENOMEM;
1427 goto fail;
1428 }
1429
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
1441 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1442 * relative, by dropping the initial slash */
1443 d = xopendirat(j->toplevel_fd, skip_slash(path), 0);
1444 if (!d) {
1445 r = log_debug_errno(errno, "Failed to open directory %s: %m", path);
1446 goto fail;
1447 }
1448
1449 m = hashmap_get(j->directories_by_path, path);
1450 if (!m) {
1451 m = new0(Directory, 1);
1452 if (!m) {
1453 r = -ENOMEM;
1454 goto fail;
1455 }
1456
1457 m->is_root = false;
1458 m->path = path;
1459
1460 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
1461 free(m);
1462 r = -ENOMEM;
1463 goto fail;
1464 }
1465
1466 path = NULL; /* avoid freeing in cleanup */
1467 j->current_invalidate_counter++;
1468
1469 log_debug("Directory %s added.", m->path);
1470
1471 } else if (m->is_root)
1472 return 0;
1473
1474 if (m->wd <= 0 && j->inotify_fd >= 0) {
1475 /* Watch this directory, if it not being watched yet. */
1476
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);
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
1486 FOREACH_DIRENT_ALL(de, d, r = log_debug_errno(errno, "Failed to read directory %s: %m", m->path); goto fail) {
1487
1488 if (dirent_is_file_with_suffix(de, ".journal") ||
1489 dirent_is_file_with_suffix(de, ".journal~"))
1490 (void) add_file(j, m->path, de->d_name);
1491 }
1492
1493 check_network(j, dirfd(d));
1494
1495 return 0;
1496
1497 fail:
1498 k = journal_put_error(j, r, path ?: prefix);
1499 if (k < 0)
1500 return k;
1501
1502 return r;
1503 }
1504
1505 static int add_root_directory(sd_journal *j, const char *p, bool missing_ok) {
1506
1507 _cleanup_closedir_ DIR *d = NULL;
1508 struct dirent *de;
1509 Directory *m;
1510 int r, k;
1511
1512 assert(j);
1513
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. */
1517
1518 if (p) {
1519 /* If there's a path specified, use it. */
1520
1521 if ((j->flags & SD_JOURNAL_RUNTIME_ONLY) &&
1522 !path_has_prefix(j, p, "/run"))
1523 return -EINVAL;
1524
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);
1562 }
1563
1564 m = hashmap_get(j->directories_by_path, p);
1565 if (!m) {
1566 m = new0(Directory, 1);
1567 if (!m) {
1568 r = -ENOMEM;
1569 goto fail;
1570 }
1571
1572 m->is_root = true;
1573
1574 m->path = strdup(p);
1575 if (!m->path) {
1576 free(m);
1577 r = -ENOMEM;
1578 goto fail;
1579 }
1580
1581 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
1582 free(m->path);
1583 free(m);
1584 r = -ENOMEM;
1585 goto fail;
1586 }
1587
1588 j->current_invalidate_counter++;
1589
1590 log_debug("Root directory %s added.", m->path);
1591
1592 } else if (!m->is_root)
1593 return 0;
1594
1595 if (m->wd <= 0 && j->inotify_fd >= 0) {
1596
1597 m->wd = inotify_add_watch_fd(j->inotify_fd, dirfd(d),
1598 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1599 IN_ONLYDIR);
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 }
1604
1605 if (j->no_new_files)
1606 return 0;
1607
1608 FOREACH_DIRENT_ALL(de, d, r = log_debug_errno(errno, "Failed to read directory %s: %m", m->path); goto fail) {
1609 sd_id128_t id;
1610
1611 if (dirent_is_file_with_suffix(de, ".journal") ||
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);
1617 }
1618
1619 check_network(j, dirfd(d));
1620
1621 return 0;
1622
1623 fail:
1624 k = journal_put_error(j, r, p);
1625 if (k < 0)
1626 return k;
1627
1628 return r;
1629 }
1630
1631 static void remove_directory(sd_journal *j, Directory *d) {
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)
1644 log_debug("Root directory %s removed.", d->path);
1645 else
1646 log_debug("Directory %s removed.", d->path);
1647
1648 free(d->path);
1649 free(d);
1650 }
1651
1652 static int add_search_paths(sd_journal *j) {
1653
1654 static const char search_paths[] =
1655 "/run/log/journal\0"
1656 "/var/log/journal\0";
1657 const char *p;
1658
1659 assert(j);
1660
1661 /* We ignore most errors here, since the idea is to only open
1662 * what's actually accessible, and ignore the rest. */
1663
1664 NULSTR_FOREACH(p, search_paths)
1665 (void) add_root_directory(j, p, true);
1666
1667 return 0;
1668 }
1669
1670 static int add_current_paths(sd_journal *j) {
1671 Iterator i;
1672 JournalFile *f;
1673
1674 assert(j);
1675 assert(j->no_new_files);
1676
1677 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
1678 * treat them as fatal. */
1679
1680 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
1681 _cleanup_free_ char *dir;
1682 int r;
1683
1684 dir = dirname_malloc(f->path);
1685 if (!dir)
1686 return -ENOMEM;
1687
1688 r = add_directory(j, dir, NULL);
1689 if (r < 0)
1690 return r;
1691 }
1692
1693 return 0;
1694 }
1695
1696 static int allocate_inotify(sd_journal *j) {
1697 assert(j);
1698
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 }
1704
1705 return hashmap_ensure_allocated(&j->directories_by_wd, NULL);
1706 }
1707
1708 static sd_journal *journal_new(int flags, const char *path) {
1709 sd_journal *j;
1710
1711 j = new0(sd_journal, 1);
1712 if (!j)
1713 return NULL;
1714
1715 j->original_pid = getpid();
1716 j->toplevel_fd = -1;
1717 j->inotify_fd = -1;
1718 j->flags = flags;
1719 j->data_threshold = DEFAULT_DATA_THRESHOLD;
1720
1721 if (path) {
1722 j->path = strdup(path);
1723 if (!j->path)
1724 goto fail;
1725 }
1726
1727 j->files = ordered_hashmap_new(&string_hash_ops);
1728 j->directories_by_path = hashmap_new(&string_hash_ops);
1729 j->mmap = mmap_cache_new();
1730 if (!j->files || !j->directories_by_path || !j->mmap)
1731 goto fail;
1732
1733 return j;
1734
1735 fail:
1736 sd_journal_close(j);
1737 return NULL;
1738 }
1739
1740 _public_ int sd_journal_open(sd_journal **ret, int flags) {
1741 sd_journal *j;
1742 int r;
1743
1744 assert_return(ret, -EINVAL);
1745 assert_return((flags & ~(SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_RUNTIME_ONLY|SD_JOURNAL_SYSTEM|SD_JOURNAL_CURRENT_USER)) == 0, -EINVAL);
1746
1747 j = journal_new(flags, NULL);
1748 if (!j)
1749 return -ENOMEM;
1750
1751 r = add_search_paths(j);
1752 if (r < 0)
1753 goto fail;
1754
1755 *ret = j;
1756 return 0;
1757
1758 fail:
1759 sd_journal_close(j);
1760
1761 return r;
1762 }
1763
1764 _public_ int sd_journal_open_container(sd_journal **ret, const char *machine, int flags) {
1765 _cleanup_free_ char *root = NULL, *class = NULL;
1766 sd_journal *j;
1767 char *p;
1768 int r;
1769
1770 /* This is pretty much deprecated, people should use machined's OpenMachineRootDirectory() call instead in
1771 * combination with sd_journal_open_directory_fd(). */
1772
1773 assert_return(machine, -EINVAL);
1774 assert_return(ret, -EINVAL);
1775 assert_return((flags & ~(SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM)) == 0, -EINVAL);
1776 assert_return(machine_name_is_valid(machine), -EINVAL);
1777
1778 p = strjoina("/run/systemd/machines/", machine);
1779 r = parse_env_file(p, NEWLINE, "ROOT", &root, "CLASS", &class, NULL);
1780 if (r == -ENOENT)
1781 return -EHOSTDOWN;
1782 if (r < 0)
1783 return r;
1784 if (!root)
1785 return -ENODATA;
1786
1787 if (!streq_ptr(class, "container"))
1788 return -EIO;
1789
1790 j = journal_new(flags, NULL);
1791 if (!j)
1792 return -ENOMEM;
1793
1794 j->prefix = root;
1795 root = NULL;
1796
1797 r = add_search_paths(j);
1798 if (r < 0)
1799 goto fail;
1800
1801 *ret = j;
1802 return 0;
1803
1804 fail:
1805 sd_journal_close(j);
1806 return r;
1807 }
1808
1809 _public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) {
1810 sd_journal *j;
1811 int r;
1812
1813 assert_return(ret, -EINVAL);
1814 assert_return(path, -EINVAL);
1815 assert_return((flags & ~SD_JOURNAL_OS_ROOT) == 0, -EINVAL);
1816
1817 j = journal_new(flags, path);
1818 if (!j)
1819 return -ENOMEM;
1820
1821 if (flags & SD_JOURNAL_OS_ROOT)
1822 r = add_search_paths(j);
1823 else
1824 r = add_root_directory(j, path, false);
1825 if (r < 0)
1826 goto fail;
1827
1828 *ret = j;
1829 return 0;
1830
1831 fail:
1832 sd_journal_close(j);
1833 return r;
1834 }
1835
1836 _public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int flags) {
1837 sd_journal *j;
1838 const char **path;
1839 int r;
1840
1841 assert_return(ret, -EINVAL);
1842 assert_return(flags == 0, -EINVAL);
1843
1844 j = journal_new(flags, NULL);
1845 if (!j)
1846 return -ENOMEM;
1847
1848 STRV_FOREACH(path, paths) {
1849 r = add_any_file(j, -1, *path);
1850 if (r < 0)
1851 goto fail;
1852 }
1853
1854 j->no_new_files = true;
1855
1856 *ret = j;
1857 return 0;
1858
1859 fail:
1860 sd_journal_close(j);
1861 return r;
1862 }
1863
1864 _public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) {
1865 sd_journal *j;
1866 struct stat st;
1867 int r;
1868
1869 assert_return(ret, -EINVAL);
1870 assert_return(fd >= 0, -EBADF);
1871 assert_return((flags & ~SD_JOURNAL_OS_ROOT) == 0, -EINVAL);
1872
1873 if (fstat(fd, &st) < 0)
1874 return -errno;
1875
1876 if (!S_ISDIR(st.st_mode))
1877 return -EBADFD;
1878
1879 j = journal_new(flags, NULL);
1880 if (!j)
1881 return -ENOMEM;
1882
1883 j->toplevel_fd = fd;
1884
1885 if (flags & SD_JOURNAL_OS_ROOT)
1886 r = add_search_paths(j);
1887 else
1888 r = add_root_directory(j, NULL, false);
1889 if (r < 0)
1890 goto fail;
1891
1892 *ret = j;
1893 return 0;
1894
1895 fail:
1896 sd_journal_close(j);
1897 return r;
1898 }
1899
1900 _public_ int sd_journal_open_files_fd(sd_journal **ret, int fds[], unsigned n_fds, int flags) {
1901 Iterator iterator;
1902 JournalFile *f;
1903 sd_journal *j;
1904 unsigned i;
1905 int r;
1906
1907 assert_return(ret, -EINVAL);
1908 assert_return(n_fds > 0, -EBADF);
1909 assert_return(flags == 0, -EINVAL);
1910
1911 j = journal_new(flags, NULL);
1912 if (!j)
1913 return -ENOMEM;
1914
1915 for (i = 0; i < n_fds; i++) {
1916 struct stat st;
1917
1918 if (fds[i] < 0) {
1919 r = -EBADF;
1920 goto fail;
1921 }
1922
1923 if (fstat(fds[i], &st) < 0) {
1924 r = -errno;
1925 goto fail;
1926 }
1927
1928 if (!S_ISREG(st.st_mode)) {
1929 r = -EBADFD;
1930 goto fail;
1931 }
1932
1933 r = add_any_file(j, fds[i], NULL);
1934 if (r < 0)
1935 goto fail;
1936 }
1937
1938 j->no_new_files = true;
1939 j->no_inotify = true;
1940
1941 *ret = j;
1942 return 0;
1943
1944 fail:
1945 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
1946 * remain open */
1947 ORDERED_HASHMAP_FOREACH(f, j->files, iterator)
1948 f->close_fd = false;
1949
1950 sd_journal_close(j);
1951 return r;
1952 }
1953
1954 _public_ void sd_journal_close(sd_journal *j) {
1955 Directory *d;
1956 JournalFile *f;
1957 char *p;
1958
1959 if (!j)
1960 return;
1961
1962 sd_journal_flush_matches(j);
1963
1964 while ((f = ordered_hashmap_steal_first(j->files)))
1965 (void) journal_file_close(f);
1966
1967 ordered_hashmap_free(j->files);
1968
1969 while ((d = hashmap_first(j->directories_by_path)))
1970 remove_directory(j, d);
1971
1972 while ((d = hashmap_first(j->directories_by_wd)))
1973 remove_directory(j, d);
1974
1975 hashmap_free(j->directories_by_path);
1976 hashmap_free(j->directories_by_wd);
1977
1978 safe_close(j->inotify_fd);
1979
1980 if (j->mmap) {
1981 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j->mmap), mmap_cache_get_missed(j->mmap));
1982 mmap_cache_unref(j->mmap);
1983 }
1984
1985 while ((p = hashmap_steal_first(j->errors)))
1986 free(p);
1987 hashmap_free(j->errors);
1988
1989 free(j->path);
1990 free(j->prefix);
1991 free(j->unique_field);
1992 free(j->fields_buffer);
1993 free(j);
1994 }
1995
1996 _public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
1997 Object *o;
1998 JournalFile *f;
1999 int r;
2000
2001 assert_return(j, -EINVAL);
2002 assert_return(!journal_pid_changed(j), -ECHILD);
2003 assert_return(ret, -EINVAL);
2004
2005 f = j->current_file;
2006 if (!f)
2007 return -EADDRNOTAVAIL;
2008
2009 if (f->current_offset <= 0)
2010 return -EADDRNOTAVAIL;
2011
2012 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2013 if (r < 0)
2014 return r;
2015
2016 *ret = le64toh(o->entry.realtime);
2017 return 0;
2018 }
2019
2020 _public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
2021 Object *o;
2022 JournalFile *f;
2023 int r;
2024 sd_id128_t id;
2025
2026 assert_return(j, -EINVAL);
2027 assert_return(!journal_pid_changed(j), -ECHILD);
2028
2029 f = j->current_file;
2030 if (!f)
2031 return -EADDRNOTAVAIL;
2032
2033 if (f->current_offset <= 0)
2034 return -EADDRNOTAVAIL;
2035
2036 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2037 if (r < 0)
2038 return r;
2039
2040 if (ret_boot_id)
2041 *ret_boot_id = o->entry.boot_id;
2042 else {
2043 r = sd_id128_get_boot(&id);
2044 if (r < 0)
2045 return r;
2046
2047 if (!sd_id128_equal(id, o->entry.boot_id))
2048 return -ESTALE;
2049 }
2050
2051 if (ret)
2052 *ret = le64toh(o->entry.monotonic);
2053
2054 return 0;
2055 }
2056
2057 static bool field_is_valid(const char *field) {
2058 const char *p;
2059
2060 assert(field);
2061
2062 if (isempty(field))
2063 return false;
2064
2065 if (startswith(field, "__"))
2066 return false;
2067
2068 for (p = field; *p; p++) {
2069
2070 if (*p == '_')
2071 continue;
2072
2073 if (*p >= 'A' && *p <= 'Z')
2074 continue;
2075
2076 if (*p >= '0' && *p <= '9')
2077 continue;
2078
2079 return false;
2080 }
2081
2082 return true;
2083 }
2084
2085 _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
2086 JournalFile *f;
2087 uint64_t i, n;
2088 size_t field_length;
2089 int r;
2090 Object *o;
2091
2092 assert_return(j, -EINVAL);
2093 assert_return(!journal_pid_changed(j), -ECHILD);
2094 assert_return(field, -EINVAL);
2095 assert_return(data, -EINVAL);
2096 assert_return(size, -EINVAL);
2097 assert_return(field_is_valid(field), -EINVAL);
2098
2099 f = j->current_file;
2100 if (!f)
2101 return -EADDRNOTAVAIL;
2102
2103 if (f->current_offset <= 0)
2104 return -EADDRNOTAVAIL;
2105
2106 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2107 if (r < 0)
2108 return r;
2109
2110 field_length = strlen(field);
2111
2112 n = journal_file_entry_n_items(o);
2113 for (i = 0; i < n; i++) {
2114 uint64_t p, l;
2115 le64_t le_hash;
2116 size_t t;
2117 int compression;
2118
2119 p = le64toh(o->entry.items[i].object_offset);
2120 le_hash = o->entry.items[i].hash;
2121 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
2122 if (r < 0)
2123 return r;
2124
2125 if (le_hash != o->data.hash)
2126 return -EBADMSG;
2127
2128 l = le64toh(o->object.size) - offsetof(Object, data.payload);
2129
2130 compression = o->object.flags & OBJECT_COMPRESSION_MASK;
2131 if (compression) {
2132 #if defined(HAVE_XZ) || defined(HAVE_LZ4)
2133 r = decompress_startswith(compression,
2134 o->data.payload, l,
2135 &f->compress_buffer, &f->compress_buffer_size,
2136 field, field_length, '=');
2137 if (r < 0)
2138 log_debug_errno(r, "Cannot decompress %s object of length %"PRIu64" at offset "OFSfmt": %m",
2139 object_compressed_to_string(compression), l, p);
2140 else if (r > 0) {
2141
2142 size_t rsize;
2143
2144 r = decompress_blob(compression,
2145 o->data.payload, l,
2146 &f->compress_buffer, &f->compress_buffer_size, &rsize,
2147 j->data_threshold);
2148 if (r < 0)
2149 return r;
2150
2151 *data = f->compress_buffer;
2152 *size = (size_t) rsize;
2153
2154 return 0;
2155 }
2156 #else
2157 return -EPROTONOSUPPORT;
2158 #endif
2159 } else if (l >= field_length+1 &&
2160 memcmp(o->data.payload, field, field_length) == 0 &&
2161 o->data.payload[field_length] == '=') {
2162
2163 t = (size_t) l;
2164
2165 if ((uint64_t) t != l)
2166 return -E2BIG;
2167
2168 *data = o->data.payload;
2169 *size = t;
2170
2171 return 0;
2172 }
2173
2174 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2175 if (r < 0)
2176 return r;
2177 }
2178
2179 return -ENOENT;
2180 }
2181
2182 static int return_data(sd_journal *j, JournalFile *f, Object *o, const void **data, size_t *size) {
2183 size_t t;
2184 uint64_t l;
2185 int compression;
2186
2187 l = le64toh(o->object.size) - offsetof(Object, data.payload);
2188 t = (size_t) l;
2189
2190 /* We can't read objects larger than 4G on a 32bit machine */
2191 if ((uint64_t) t != l)
2192 return -E2BIG;
2193
2194 compression = o->object.flags & OBJECT_COMPRESSION_MASK;
2195 if (compression) {
2196 #if defined(HAVE_XZ) || defined(HAVE_LZ4)
2197 size_t rsize;
2198 int r;
2199
2200 r = decompress_blob(compression,
2201 o->data.payload, l, &f->compress_buffer,
2202 &f->compress_buffer_size, &rsize, j->data_threshold);
2203 if (r < 0)
2204 return r;
2205
2206 *data = f->compress_buffer;
2207 *size = (size_t) rsize;
2208 #else
2209 return -EPROTONOSUPPORT;
2210 #endif
2211 } else {
2212 *data = o->data.payload;
2213 *size = t;
2214 }
2215
2216 return 0;
2217 }
2218
2219 _public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
2220 JournalFile *f;
2221 uint64_t p, n;
2222 le64_t le_hash;
2223 int r;
2224 Object *o;
2225
2226 assert_return(j, -EINVAL);
2227 assert_return(!journal_pid_changed(j), -ECHILD);
2228 assert_return(data, -EINVAL);
2229 assert_return(size, -EINVAL);
2230
2231 f = j->current_file;
2232 if (!f)
2233 return -EADDRNOTAVAIL;
2234
2235 if (f->current_offset <= 0)
2236 return -EADDRNOTAVAIL;
2237
2238 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2239 if (r < 0)
2240 return r;
2241
2242 n = journal_file_entry_n_items(o);
2243 if (j->current_field >= n)
2244 return 0;
2245
2246 p = le64toh(o->entry.items[j->current_field].object_offset);
2247 le_hash = o->entry.items[j->current_field].hash;
2248 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
2249 if (r < 0)
2250 return r;
2251
2252 if (le_hash != o->data.hash)
2253 return -EBADMSG;
2254
2255 r = return_data(j, f, o, data, size);
2256 if (r < 0)
2257 return r;
2258
2259 j->current_field++;
2260
2261 return 1;
2262 }
2263
2264 _public_ void sd_journal_restart_data(sd_journal *j) {
2265 if (!j)
2266 return;
2267
2268 j->current_field = 0;
2269 }
2270
2271 _public_ int sd_journal_get_fd(sd_journal *j) {
2272 int r;
2273
2274 assert_return(j, -EINVAL);
2275 assert_return(!journal_pid_changed(j), -ECHILD);
2276
2277 if (j->no_inotify)
2278 return -EMEDIUMTYPE;
2279
2280 if (j->inotify_fd >= 0)
2281 return j->inotify_fd;
2282
2283 r = allocate_inotify(j);
2284 if (r < 0)
2285 return r;
2286
2287 log_debug("Reiterating files to get inotify watches established");
2288
2289 /* Iterate through all dirs again, to add them to the
2290 * inotify */
2291 if (j->no_new_files)
2292 r = add_current_paths(j);
2293 else if (j->flags & SD_JOURNAL_OS_ROOT)
2294 r = add_search_paths(j);
2295 else if (j->toplevel_fd >= 0)
2296 r = add_root_directory(j, NULL, false);
2297 else if (j->path)
2298 r = add_root_directory(j, j->path, true);
2299 else
2300 r = add_search_paths(j);
2301 if (r < 0)
2302 return r;
2303
2304 return j->inotify_fd;
2305 }
2306
2307 _public_ int sd_journal_get_events(sd_journal *j) {
2308 int fd;
2309
2310 assert_return(j, -EINVAL);
2311 assert_return(!journal_pid_changed(j), -ECHILD);
2312
2313 fd = sd_journal_get_fd(j);
2314 if (fd < 0)
2315 return fd;
2316
2317 return POLLIN;
2318 }
2319
2320 _public_ int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec) {
2321 int fd;
2322
2323 assert_return(j, -EINVAL);
2324 assert_return(!journal_pid_changed(j), -ECHILD);
2325 assert_return(timeout_usec, -EINVAL);
2326
2327 fd = sd_journal_get_fd(j);
2328 if (fd < 0)
2329 return fd;
2330
2331 if (!j->on_network) {
2332 *timeout_usec = (uint64_t) -1;
2333 return 0;
2334 }
2335
2336 /* If we are on the network we need to regularly check for
2337 * changes manually */
2338
2339 *timeout_usec = j->last_process_usec + JOURNAL_FILES_RECHECK_USEC;
2340 return 1;
2341 }
2342
2343 static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
2344 Directory *d;
2345
2346 assert(j);
2347 assert(e);
2348
2349 /* Is this a subdirectory we watch? */
2350 d = hashmap_get(j->directories_by_wd, INT_TO_PTR(e->wd));
2351 if (d) {
2352 sd_id128_t id;
2353
2354 if (!(e->mask & IN_ISDIR) && e->len > 0 &&
2355 (endswith(e->name, ".journal") ||
2356 endswith(e->name, ".journal~"))) {
2357
2358 /* Event for a journal file */
2359
2360 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
2361 (void) add_file(j, d->path, e->name);
2362 else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT))
2363 remove_file(j, d->path, e->name);
2364
2365 } else if (!d->is_root && e->len == 0) {
2366
2367 /* Event for a subdirectory */
2368
2369 if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT))
2370 remove_directory(j, d);
2371
2372 } else if (d->is_root && (e->mask & IN_ISDIR) && e->len > 0 && sd_id128_from_string(e->name, &id) >= 0) {
2373
2374 /* Event for root directory */
2375
2376 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
2377 (void) add_directory(j, d->path, e->name);
2378 }
2379
2380 return;
2381 }
2382
2383 if (e->mask & IN_IGNORED)
2384 return;
2385
2386 log_debug("Unknown inotify event.");
2387 }
2388
2389 static int determine_change(sd_journal *j) {
2390 bool b;
2391
2392 assert(j);
2393
2394 b = j->current_invalidate_counter != j->last_invalidate_counter;
2395 j->last_invalidate_counter = j->current_invalidate_counter;
2396
2397 return b ? SD_JOURNAL_INVALIDATE : SD_JOURNAL_APPEND;
2398 }
2399
2400 _public_ int sd_journal_process(sd_journal *j) {
2401 bool got_something = false;
2402
2403 assert_return(j, -EINVAL);
2404 assert_return(!journal_pid_changed(j), -ECHILD);
2405
2406 j->last_process_usec = now(CLOCK_MONOTONIC);
2407
2408 for (;;) {
2409 union inotify_event_buffer buffer;
2410 struct inotify_event *e;
2411 ssize_t l;
2412
2413 l = read(j->inotify_fd, &buffer, sizeof(buffer));
2414 if (l < 0) {
2415 if (errno == EAGAIN || errno == EINTR)
2416 return got_something ? determine_change(j) : SD_JOURNAL_NOP;
2417
2418 return -errno;
2419 }
2420
2421 got_something = true;
2422
2423 FOREACH_INOTIFY_EVENT(e, buffer, l)
2424 process_inotify_event(j, e);
2425 }
2426 }
2427
2428 _public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
2429 int r;
2430 uint64_t t;
2431
2432 assert_return(j, -EINVAL);
2433 assert_return(!journal_pid_changed(j), -ECHILD);
2434
2435 if (j->inotify_fd < 0) {
2436
2437 /* This is the first invocation, hence create the
2438 * inotify watch */
2439 r = sd_journal_get_fd(j);
2440 if (r < 0)
2441 return r;
2442
2443 /* The journal might have changed since the context
2444 * object was created and we weren't watching before,
2445 * hence don't wait for anything, and return
2446 * immediately. */
2447 return determine_change(j);
2448 }
2449
2450 r = sd_journal_get_timeout(j, &t);
2451 if (r < 0)
2452 return r;
2453
2454 if (t != (uint64_t) -1) {
2455 usec_t n;
2456
2457 n = now(CLOCK_MONOTONIC);
2458 t = t > n ? t - n : 0;
2459
2460 if (timeout_usec == (uint64_t) -1 || timeout_usec > t)
2461 timeout_usec = t;
2462 }
2463
2464 do {
2465 r = fd_wait_for_event(j->inotify_fd, POLLIN, timeout_usec);
2466 } while (r == -EINTR);
2467
2468 if (r < 0)
2469 return r;
2470
2471 return sd_journal_process(j);
2472 }
2473
2474 _public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to) {
2475 Iterator i;
2476 JournalFile *f;
2477 bool first = true;
2478 uint64_t fmin = 0, tmax = 0;
2479 int r;
2480
2481 assert_return(j, -EINVAL);
2482 assert_return(!journal_pid_changed(j), -ECHILD);
2483 assert_return(from || to, -EINVAL);
2484 assert_return(from != to, -EINVAL);
2485
2486 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
2487 usec_t fr, t;
2488
2489 r = journal_file_get_cutoff_realtime_usec(f, &fr, &t);
2490 if (r == -ENOENT)
2491 continue;
2492 if (r < 0)
2493 return r;
2494 if (r == 0)
2495 continue;
2496
2497 if (first) {
2498 fmin = fr;
2499 tmax = t;
2500 first = false;
2501 } else {
2502 fmin = MIN(fr, fmin);
2503 tmax = MAX(t, tmax);
2504 }
2505 }
2506
2507 if (from)
2508 *from = fmin;
2509 if (to)
2510 *to = tmax;
2511
2512 return first ? 0 : 1;
2513 }
2514
2515 _public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t *from, uint64_t *to) {
2516 Iterator i;
2517 JournalFile *f;
2518 bool found = false;
2519 int r;
2520
2521 assert_return(j, -EINVAL);
2522 assert_return(!journal_pid_changed(j), -ECHILD);
2523 assert_return(from || to, -EINVAL);
2524 assert_return(from != to, -EINVAL);
2525
2526 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
2527 usec_t fr, t;
2528
2529 r = journal_file_get_cutoff_monotonic_usec(f, boot_id, &fr, &t);
2530 if (r == -ENOENT)
2531 continue;
2532 if (r < 0)
2533 return r;
2534 if (r == 0)
2535 continue;
2536
2537 if (found) {
2538 if (from)
2539 *from = MIN(fr, *from);
2540 if (to)
2541 *to = MAX(t, *to);
2542 } else {
2543 if (from)
2544 *from = fr;
2545 if (to)
2546 *to = t;
2547 found = true;
2548 }
2549 }
2550
2551 return found;
2552 }
2553
2554 void journal_print_header(sd_journal *j) {
2555 Iterator i;
2556 JournalFile *f;
2557 bool newline = false;
2558
2559 assert(j);
2560
2561 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
2562 if (newline)
2563 putchar('\n');
2564 else
2565 newline = true;
2566
2567 journal_file_print_header(f);
2568 }
2569 }
2570
2571 _public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) {
2572 Iterator i;
2573 JournalFile *f;
2574 uint64_t sum = 0;
2575
2576 assert_return(j, -EINVAL);
2577 assert_return(!journal_pid_changed(j), -ECHILD);
2578 assert_return(bytes, -EINVAL);
2579
2580 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
2581 struct stat st;
2582
2583 if (fstat(f->fd, &st) < 0)
2584 return -errno;
2585
2586 sum += (uint64_t) st.st_blocks * 512ULL;
2587 }
2588
2589 *bytes = sum;
2590 return 0;
2591 }
2592
2593 _public_ int sd_journal_query_unique(sd_journal *j, const char *field) {
2594 char *f;
2595
2596 assert_return(j, -EINVAL);
2597 assert_return(!journal_pid_changed(j), -ECHILD);
2598 assert_return(!isempty(field), -EINVAL);
2599 assert_return(field_is_valid(field), -EINVAL);
2600
2601 f = strdup(field);
2602 if (!f)
2603 return -ENOMEM;
2604
2605 free(j->unique_field);
2606 j->unique_field = f;
2607 j->unique_file = NULL;
2608 j->unique_offset = 0;
2609 j->unique_file_lost = false;
2610
2611 return 0;
2612 }
2613
2614 _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l) {
2615 size_t k;
2616
2617 assert_return(j, -EINVAL);
2618 assert_return(!journal_pid_changed(j), -ECHILD);
2619 assert_return(data, -EINVAL);
2620 assert_return(l, -EINVAL);
2621 assert_return(j->unique_field, -EINVAL);
2622
2623 k = strlen(j->unique_field);
2624
2625 if (!j->unique_file) {
2626 if (j->unique_file_lost)
2627 return 0;
2628
2629 j->unique_file = ordered_hashmap_first(j->files);
2630 if (!j->unique_file)
2631 return 0;
2632
2633 j->unique_offset = 0;
2634 }
2635
2636 for (;;) {
2637 JournalFile *of;
2638 Iterator i;
2639 Object *o;
2640 const void *odata;
2641 size_t ol;
2642 bool found;
2643 int r;
2644
2645 /* Proceed to next data object in the field's linked list */
2646 if (j->unique_offset == 0) {
2647 r = journal_file_find_field_object(j->unique_file, j->unique_field, k, &o, NULL);
2648 if (r < 0)
2649 return r;
2650
2651 j->unique_offset = r > 0 ? le64toh(o->field.head_data_offset) : 0;
2652 } else {
2653 r = journal_file_move_to_object(j->unique_file, OBJECT_DATA, j->unique_offset, &o);
2654 if (r < 0)
2655 return r;
2656
2657 j->unique_offset = le64toh(o->data.next_field_offset);
2658 }
2659
2660 /* We reached the end of the list? Then start again, with the next file */
2661 if (j->unique_offset == 0) {
2662 j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
2663 if (!j->unique_file)
2664 return 0;
2665
2666 continue;
2667 }
2668
2669 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2670 * instead, so that we can look at this data object at the same
2671 * time as one on another file */
2672 r = journal_file_move_to_object(j->unique_file, OBJECT_UNUSED, j->unique_offset, &o);
2673 if (r < 0)
2674 return r;
2675
2676 /* Let's do the type check by hand, since we used 0 context above. */
2677 if (o->object.type != OBJECT_DATA) {
2678 log_debug("%s:offset " OFSfmt ": object has type %d, expected %d",
2679 j->unique_file->path, j->unique_offset,
2680 o->object.type, OBJECT_DATA);
2681 return -EBADMSG;
2682 }
2683
2684 r = return_data(j, j->unique_file, o, &odata, &ol);
2685 if (r < 0)
2686 return r;
2687
2688 /* Check if we have at least the field name and "=". */
2689 if (ol <= k) {
2690 log_debug("%s:offset " OFSfmt ": object has size %zu, expected at least %zu",
2691 j->unique_file->path, j->unique_offset,
2692 ol, k + 1);
2693 return -EBADMSG;
2694 }
2695
2696 if (memcmp(odata, j->unique_field, k) || ((const char*) odata)[k] != '=') {
2697 log_debug("%s:offset " OFSfmt ": object does not start with \"%s=\"",
2698 j->unique_file->path, j->unique_offset,
2699 j->unique_field);
2700 return -EBADMSG;
2701 }
2702
2703 /* OK, now let's see if we already returned this data
2704 * object by checking if it exists in the earlier
2705 * traversed files. */
2706 found = false;
2707 ORDERED_HASHMAP_FOREACH(of, j->files, i) {
2708 if (of == j->unique_file)
2709 break;
2710
2711 /* Skip this file it didn't have any fields indexed */
2712 if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
2713 continue;
2714
2715 r = journal_file_find_data_object_with_hash(of, odata, ol, le64toh(o->data.hash), NULL, NULL);
2716 if (r < 0)
2717 return r;
2718 if (r > 0) {
2719 found = true;
2720 break;
2721 }
2722 }
2723
2724 if (found)
2725 continue;
2726
2727 r = return_data(j, j->unique_file, o, data, l);
2728 if (r < 0)
2729 return r;
2730
2731 return 1;
2732 }
2733 }
2734
2735 _public_ void sd_journal_restart_unique(sd_journal *j) {
2736 if (!j)
2737 return;
2738
2739 j->unique_file = NULL;
2740 j->unique_offset = 0;
2741 j->unique_file_lost = false;
2742 }
2743
2744 _public_ int sd_journal_enumerate_fields(sd_journal *j, const char **field) {
2745 int r;
2746
2747 assert_return(j, -EINVAL);
2748 assert_return(!journal_pid_changed(j), -ECHILD);
2749 assert_return(field, -EINVAL);
2750
2751 if (!j->fields_file) {
2752 if (j->fields_file_lost)
2753 return 0;
2754
2755 j->fields_file = ordered_hashmap_first(j->files);
2756 if (!j->fields_file)
2757 return 0;
2758
2759 j->fields_hash_table_index = 0;
2760 j->fields_offset = 0;
2761 }
2762
2763 for (;;) {
2764 JournalFile *f, *of;
2765 Iterator i;
2766 uint64_t m;
2767 Object *o;
2768 size_t sz;
2769 bool found;
2770
2771 f = j->fields_file;
2772
2773 if (j->fields_offset == 0) {
2774 bool eof = false;
2775
2776 /* We are not yet positioned at any field. Let's pick the first one */
2777 r = journal_file_map_field_hash_table(f);
2778 if (r < 0)
2779 return r;
2780
2781 m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem);
2782 for (;;) {
2783 if (j->fields_hash_table_index >= m) {
2784 /* Reached the end of the hash table, go to the next file. */
2785 eof = true;
2786 break;
2787 }
2788
2789 j->fields_offset = le64toh(f->field_hash_table[j->fields_hash_table_index].head_hash_offset);
2790
2791 if (j->fields_offset != 0)
2792 break;
2793
2794 /* Empty hash table bucket, go to next one */
2795 j->fields_hash_table_index++;
2796 }
2797
2798 if (eof) {
2799 /* Proceed with next file */
2800 j->fields_file = ordered_hashmap_next(j->files, f->path);
2801 if (!j->fields_file) {
2802 *field = NULL;
2803 return 0;
2804 }
2805
2806 j->fields_offset = 0;
2807 j->fields_hash_table_index = 0;
2808 continue;
2809 }
2810
2811 } else {
2812 /* We are already positioned at a field. If so, let's figure out the next field from it */
2813
2814 r = journal_file_move_to_object(f, OBJECT_FIELD, j->fields_offset, &o);
2815 if (r < 0)
2816 return r;
2817
2818 j->fields_offset = le64toh(o->field.next_hash_offset);
2819 if (j->fields_offset == 0) {
2820 /* Reached the end of the hash table chain */
2821 j->fields_hash_table_index++;
2822 continue;
2823 }
2824 }
2825
2826 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
2827 r = journal_file_move_to_object(f, OBJECT_UNUSED, j->fields_offset, &o);
2828 if (r < 0)
2829 return r;
2830
2831 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
2832 if (o->object.type != OBJECT_FIELD) {
2833 log_debug("%s:offset " OFSfmt ": object has type %i, expected %i", f->path, j->fields_offset, o->object.type, OBJECT_FIELD);
2834 return -EBADMSG;
2835 }
2836
2837 sz = le64toh(o->object.size) - offsetof(Object, field.payload);
2838
2839 /* Let's see if we already returned this field name before. */
2840 found = false;
2841 ORDERED_HASHMAP_FOREACH(of, j->files, i) {
2842 if (of == f)
2843 break;
2844
2845 /* Skip this file it didn't have any fields indexed */
2846 if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
2847 continue;
2848
2849 r = journal_file_find_field_object_with_hash(of, o->field.payload, sz, le64toh(o->field.hash), NULL, NULL);
2850 if (r < 0)
2851 return r;
2852 if (r > 0) {
2853 found = true;
2854 break;
2855 }
2856 }
2857
2858 if (found)
2859 continue;
2860
2861 /* Check if this is really a valid string containing no NUL byte */
2862 if (memchr(o->field.payload, 0, sz))
2863 return -EBADMSG;
2864
2865 if (sz > j->data_threshold)
2866 sz = j->data_threshold;
2867
2868 if (!GREEDY_REALLOC(j->fields_buffer, j->fields_buffer_allocated, sz + 1))
2869 return -ENOMEM;
2870
2871 memcpy(j->fields_buffer, o->field.payload, sz);
2872 j->fields_buffer[sz] = 0;
2873
2874 if (!field_is_valid(j->fields_buffer))
2875 return -EBADMSG;
2876
2877 *field = j->fields_buffer;
2878 return 1;
2879 }
2880 }
2881
2882 _public_ void sd_journal_restart_fields(sd_journal *j) {
2883 if (!j)
2884 return;
2885
2886 j->fields_file = NULL;
2887 j->fields_hash_table_index = 0;
2888 j->fields_offset = 0;
2889 j->fields_file_lost = false;
2890 }
2891
2892 _public_ int sd_journal_reliable_fd(sd_journal *j) {
2893 assert_return(j, -EINVAL);
2894 assert_return(!journal_pid_changed(j), -ECHILD);
2895
2896 return !j->on_network;
2897 }
2898
2899 static char *lookup_field(const char *field, void *userdata) {
2900 sd_journal *j = userdata;
2901 const void *data;
2902 size_t size, d;
2903 int r;
2904
2905 assert(field);
2906 assert(j);
2907
2908 r = sd_journal_get_data(j, field, &data, &size);
2909 if (r < 0 ||
2910 size > REPLACE_VAR_MAX)
2911 return strdup(field);
2912
2913 d = strlen(field) + 1;
2914
2915 return strndup((const char*) data + d, size - d);
2916 }
2917
2918 _public_ int sd_journal_get_catalog(sd_journal *j, char **ret) {
2919 const void *data;
2920 size_t size;
2921 sd_id128_t id;
2922 _cleanup_free_ char *text = NULL, *cid = NULL;
2923 char *t;
2924 int r;
2925
2926 assert_return(j, -EINVAL);
2927 assert_return(!journal_pid_changed(j), -ECHILD);
2928 assert_return(ret, -EINVAL);
2929
2930 r = sd_journal_get_data(j, "MESSAGE_ID", &data, &size);
2931 if (r < 0)
2932 return r;
2933
2934 cid = strndup((const char*) data + 11, size - 11);
2935 if (!cid)
2936 return -ENOMEM;
2937
2938 r = sd_id128_from_string(cid, &id);
2939 if (r < 0)
2940 return r;
2941
2942 r = catalog_get(CATALOG_DATABASE, id, &text);
2943 if (r < 0)
2944 return r;
2945
2946 t = replace_var(text, lookup_field, j);
2947 if (!t)
2948 return -ENOMEM;
2949
2950 *ret = t;
2951 return 0;
2952 }
2953
2954 _public_ int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **ret) {
2955 assert_return(ret, -EINVAL);
2956
2957 return catalog_get(CATALOG_DATABASE, id, ret);
2958 }
2959
2960 _public_ int sd_journal_set_data_threshold(sd_journal *j, size_t sz) {
2961 assert_return(j, -EINVAL);
2962 assert_return(!journal_pid_changed(j), -ECHILD);
2963
2964 j->data_threshold = sz;
2965 return 0;
2966 }
2967
2968 _public_ int sd_journal_get_data_threshold(sd_journal *j, size_t *sz) {
2969 assert_return(j, -EINVAL);
2970 assert_return(!journal_pid_changed(j), -ECHILD);
2971 assert_return(sz, -EINVAL);
2972
2973 *sz = j->data_threshold;
2974 return 0;
2975 }
2976
2977 _public_ int sd_journal_has_runtime_files(sd_journal *j) {
2978 assert_return(j, -EINVAL);
2979
2980 return j->has_runtime_files;
2981 }
2982
2983 _public_ int sd_journal_has_persistent_files(sd_journal *j) {
2984 assert_return(j, -EINVAL);
2985
2986 return j->has_persistent_files;
2987 }