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