chiark / gitweb /
journal: beef up journal output of systemctl and journalctl
[elogind.git] / src / journal / sd-journal.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2011 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <sys/inotify.h>
27
28 #include "sd-journal.h"
29 #include "journal-def.h"
30 #include "journal-file.h"
31 #include "hashmap.h"
32 #include "list.h"
33 #include "lookup3.h"
34 #include "compress.h"
35 #include "journal-internal.h"
36
37 #define JOURNAL_FILES_MAX 1024
38
39 static void detach_location(sd_journal *j) {
40         Iterator i;
41         JournalFile *f;
42
43         assert(j);
44
45         j->current_file = NULL;
46         j->current_field = 0;
47
48         HASHMAP_FOREACH(f, j->files, i)
49                 f->current_offset = 0;
50 }
51
52 static void reset_location(sd_journal *j) {
53         assert(j);
54
55         detach_location(j);
56         zero(j->current_location);
57 }
58
59 static void init_location(Location *l, JournalFile *f, Object *o) {
60         assert(l);
61         assert(f);
62         assert(o->object.type == OBJECT_ENTRY);
63
64         l->type = LOCATION_DISCRETE;
65         l->seqnum = le64toh(o->entry.seqnum);
66         l->seqnum_id = f->header->seqnum_id;
67         l->realtime = le64toh(o->entry.realtime);
68         l->monotonic = le64toh(o->entry.monotonic);
69         l->boot_id = le64toh(o->entry.boot_id);
70         l->xor_hash = le64toh(o->entry.xor_hash);
71
72         l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true;
73 }
74
75 static void set_location(sd_journal *j, JournalFile *f, Object *o, uint64_t offset) {
76         assert(j);
77         assert(f);
78         assert(o);
79
80         init_location(&j->current_location, f, o);
81
82         j->current_file = f;
83         j->current_field = 0;
84
85         f->current_offset = offset;
86 }
87
88 static int same_field(const void *_a, size_t s, const void *_b, size_t t) {
89         const uint8_t *a = _a, *b = _b;
90         size_t j;
91         bool a_good = false, b_good = false, different = false;
92
93         for (j = 0; j < s && j < t; j++) {
94
95                 if (a[j] == '=')
96                         a_good = true;
97                 if (b[j] == '=')
98                         b_good = true;
99                 if (a[j] != b[j])
100                         different = true;
101
102                 if (a_good && b_good)
103                         return different ? 0 : 1;
104         }
105
106         return -EINVAL;
107 }
108
109 _public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
110         Match *m, *after = NULL;
111         uint64_t le_hash;
112
113         if (!j)
114                 return -EINVAL;
115         if (!data)
116                 return -EINVAL;
117         if (size <= 0)
118                 return -EINVAL;
119
120         le_hash = htole64(hash64(data, size));
121
122         LIST_FOREACH(matches, m, j->matches) {
123                 int r;
124
125                 if (m->le_hash == le_hash &&
126                     m->size == size &&
127                     memcmp(m->data, data, size) == 0)
128                         return 0;
129
130                 r = same_field(data, size, m->data, m->size);
131                 if (r < 0)
132                         return r;
133                 else if (r > 0)
134                         after = m;
135         }
136
137         m = new0(Match, 1);
138         if (!m)
139                 return -ENOMEM;
140
141         m->size = size;
142
143         m->data = malloc(m->size);
144         if (!m->data) {
145                 free(m);
146                 return -ENOMEM;
147         }
148
149         memcpy(m->data, data, size);
150         m->le_hash = le_hash;
151
152         /* Matches for the same fields we order adjacent to each
153          * other */
154         LIST_INSERT_AFTER(Match, matches, j->matches, after, m);
155         j->n_matches ++;
156
157         detach_location(j);
158
159         return 0;
160 }
161
162 _public_ void sd_journal_flush_matches(sd_journal *j) {
163         if (!j)
164                 return;
165
166         while (j->matches) {
167                 Match *m = j->matches;
168
169                 LIST_REMOVE(Match, matches, j->matches, m);
170                 free(m->data);
171                 free(m);
172         }
173
174         j->n_matches = 0;
175
176         detach_location(j);
177 }
178
179 static int compare_order(JournalFile *af, Object *ao,
180                          JournalFile *bf, Object *bo) {
181
182         uint64_t a, b;
183
184         assert(af);
185         assert(ao);
186         assert(bf);
187         assert(bo);
188
189         /* We operate on two different files here, hence we can access
190          * two objects at the same time, which we normally can't.
191          *
192          * If contents and timestamps match, these entries are
193          * identical, even if the seqnum does not match */
194
195         if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id) &&
196             ao->entry.monotonic == bo->entry.monotonic &&
197             ao->entry.realtime == bo->entry.realtime &&
198             ao->entry.xor_hash == bo->entry.xor_hash)
199                 return 0;
200
201         if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
202
203                 /* If this is from the same seqnum source, compare
204                  * seqnums */
205                 a = le64toh(ao->entry.seqnum);
206                 b = le64toh(bo->entry.seqnum);
207
208                 if (a < b)
209                         return -1;
210                 if (a > b)
211                         return 1;
212
213                 /* Wow! This is weird, different data but the same
214                  * seqnums? Something is borked, but let's make the
215                  * best of it and compare by time. */
216         }
217
218         if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
219
220                 /* If the boot id matches compare monotonic time */
221                 a = le64toh(ao->entry.monotonic);
222                 b = le64toh(bo->entry.monotonic);
223
224                 if (a < b)
225                         return -1;
226                 if (a > b)
227                         return 1;
228         }
229
230         /* Otherwise compare UTC time */
231         a = le64toh(ao->entry.realtime);
232         b = le64toh(ao->entry.realtime);
233
234         if (a < b)
235                 return -1;
236         if (a > b)
237                 return 1;
238
239         /* Finally, compare by contents */
240         a = le64toh(ao->entry.xor_hash);
241         b = le64toh(ao->entry.xor_hash);
242
243         if (a < b)
244                 return -1;
245         if (a > b)
246                 return 1;
247
248         return 0;
249 }
250
251 static int compare_with_location(JournalFile *af, Object *ao, Location *l) {
252         uint64_t a;
253
254         assert(af);
255         assert(ao);
256         assert(l);
257         assert(l->type == LOCATION_DISCRETE);
258
259         if (l->monotonic_set &&
260             sd_id128_equal(ao->entry.boot_id, l->boot_id) &&
261             l->realtime_set &&
262             le64toh(ao->entry.realtime) == l->realtime &&
263             l->xor_hash_set &&
264             le64toh(ao->entry.xor_hash) == l->xor_hash)
265                 return 0;
266
267         if (l->seqnum_set &&
268             sd_id128_equal(af->header->seqnum_id, l->seqnum_id)) {
269
270                 a = le64toh(ao->entry.seqnum);
271
272                 if (a < l->seqnum)
273                         return -1;
274                 if (a > l->seqnum)
275                         return 1;
276         }
277
278         if (l->monotonic_set &&
279             sd_id128_equal(ao->entry.boot_id, l->boot_id)) {
280
281                 a = le64toh(ao->entry.monotonic);
282
283                 if (a < l->monotonic)
284                         return -1;
285                 if (a > l->monotonic)
286                         return 1;
287         }
288
289         if (l->realtime_set) {
290
291                 a = le64toh(ao->entry.realtime);
292
293                 if (a < l->realtime)
294                         return -1;
295                 if (a > l->realtime)
296                         return 1;
297         }
298
299         if (l->xor_hash_set) {
300                 a = le64toh(ao->entry.xor_hash);
301
302                 if (a < l->xor_hash)
303                         return -1;
304                 if (a > l->xor_hash)
305                         return 1;
306         }
307
308         return 0;
309 }
310
311 static int find_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
312         Object *o = NULL;
313         uint64_t p = 0;
314         int r;
315
316         assert(j);
317
318         if (!j->matches) {
319                 /* No matches is simple */
320
321                 if (j->current_location.type == LOCATION_HEAD)
322                         r = journal_file_next_entry(f, NULL, 0, DIRECTION_DOWN, &o, &p);
323                 else if (j->current_location.type == LOCATION_TAIL)
324                         r = journal_file_next_entry(f, NULL, 0, DIRECTION_UP, &o, &p);
325                 else if (j->current_location.seqnum_set &&
326                          sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
327                         r = journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, &o, &p);
328                 else if (j->current_location.monotonic_set)
329                         r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, &o, &p);
330                 else if (j->current_location.realtime_set)
331                         r = journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, &o, &p);
332                 else
333                         r = journal_file_next_entry(f, NULL, 0, direction, &o, &p);
334
335                 if (r <= 0)
336                         return r;
337
338         } else  {
339                 Match *m, *term_match = NULL;
340                 Object *to = NULL;
341                 uint64_t tp = 0;
342
343                 /* We have matches, first, let's jump to the monotonic
344                  * position if we have any, since it implies a
345                  * match. */
346
347                 if (j->current_location.type == LOCATION_DISCRETE &&
348                     j->current_location.monotonic_set) {
349
350                         r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, &o, &p);
351                         if (r <= 0)
352                                 return r == -ENOENT ? 0 : r;
353                 }
354
355                 LIST_FOREACH(matches, m, j->matches) {
356                         Object *c, *d;
357                         uint64_t cp, dp;
358
359                         r = journal_file_find_data_object_with_hash(f, m->data, m->size, m->le_hash, &d, &dp);
360                         if (r <= 0)
361                                 return r;
362
363                         if (j->current_location.type == LOCATION_HEAD)
364                                 r = journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_DOWN, &c, &cp);
365                         else if (j->current_location.type == LOCATION_TAIL)
366                                 r = journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_UP, &c, &cp);
367                         else if (j->current_location.seqnum_set &&
368                                  sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
369                                 r = journal_file_move_to_entry_by_seqnum_for_data(f, dp, j->current_location.seqnum, direction, &c, &cp);
370                         else if (j->current_location.realtime_set)
371                                 r = journal_file_move_to_entry_by_realtime_for_data(f, dp, j->current_location.realtime, direction, &c, &cp);
372                         else
373                                 r = journal_file_next_entry_for_data(f, NULL, 0, dp, direction, &c, &cp);
374
375                         if (r < 0)
376                                 return r;
377
378                         if (!term_match) {
379                                 term_match = m;
380
381                                 if (r > 0) {
382                                         to = c;
383                                         tp = cp;
384                                 }
385                         } else if (same_field(term_match->data, term_match->size, m->data, m->size)) {
386
387                                 /* Same field as previous match... */
388                                 if (r > 0) {
389
390                                         /* Find the earliest of the OR matches */
391
392                                         if (!to ||
393                                             (direction == DIRECTION_DOWN && cp < tp) ||
394                                             (direction == DIRECTION_UP && cp > tp)) {
395                                                 to = c;
396                                                 tp = cp;
397                                         }
398
399                                 }
400
401                         } else {
402
403                                 /* Previous term is finished, did anything match? */
404                                 if (!to)
405                                         return 0;
406
407                                 /* Find the last of the AND matches */
408                                 if (!o ||
409                                     (direction == DIRECTION_DOWN && tp > p) ||
410                                     (direction == DIRECTION_UP && tp < p)) {
411                                         o = to;
412                                         p = tp;
413                                 }
414
415                                 term_match = m;
416
417                                 if (r > 0) {
418                                         to = c;
419                                         tp = cp;
420                                 } else {
421                                         to = NULL;
422                                         tp = 0;
423                                 }
424                         }
425                 }
426
427                 /* Last term is finished, did anything match? */
428                 if (!to)
429                         return 0;
430
431                 if (!o ||
432                     (direction == DIRECTION_DOWN && tp > p) ||
433                     (direction == DIRECTION_UP && tp < p)) {
434                         o = to;
435                         p = tp;
436                 }
437
438                 if (!o)
439                         return 0;
440         }
441
442         if (ret)
443                 *ret = o;
444
445         if (offset)
446                 *offset = p;
447
448         return 1;
449 }
450
451 static int next_with_matches(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
452         int r;
453         uint64_t cp;
454         Object *c;
455
456         assert(j);
457         assert(f);
458         assert(ret);
459         assert(offset);
460
461         c = *ret;
462         cp = *offset;
463
464         if (!j->matches) {
465                 /* No matches is easy */
466
467                 r = journal_file_next_entry(f, c, cp, direction, &c, &cp);
468                 if (r <= 0)
469                         return r;
470
471                 if (ret)
472                         *ret = c;
473                 if (offset)
474                         *offset = cp;
475                 return 1;
476         }
477
478         /* So there are matches we have to adhere to, let's find the
479          * first entry that matches all of them */
480
481         for (;;) {
482                 uint64_t np, n;
483                 bool found, term_result = false;
484                 Match *m, *term_match = NULL;
485                 Object *npo = NULL;
486
487                 n = journal_file_entry_n_items(c);
488
489                 /* Make sure we don't match the entry we are starting
490                  * from. */
491                 found = cp != *offset;
492
493                 np = 0;
494                 LIST_FOREACH(matches, m, j->matches) {
495                         uint64_t q, k;
496                         Object *qo = NULL;
497
498                         /* Let's check if this is the beginning of a
499                          * new term, i.e. has a different field prefix
500                          * as the preceeding match. */
501                         if (!term_match) {
502                                 term_match = m;
503                                 term_result = false;
504                         } else if (!same_field(term_match->data, term_match->size, m->data, m->size)) {
505                                 if (!term_result)
506                                         found = false;
507
508                                 term_match = m;
509                                 term_result = false;
510                         }
511
512                         for (k = 0; k < n; k++)
513                                 if (c->entry.items[k].hash == m->le_hash)
514                                         break;
515
516                         if (k >= n) {
517                                 /* Hmm, didn't find any field that
518                                  * matched this rule, so ignore this
519                                  * match. Go on with next match */
520                                 continue;
521                         }
522
523                         term_result = true;
524
525                         /* Hmm, so, this field matched, let's remember
526                          * where we'd have to try next, in case the other
527                          * matches are not OK */
528
529                         r = journal_file_next_entry_for_data(f, c, cp, le64toh(c->entry.items[k].object_offset), direction, &qo, &q);
530                         if (r < 0)
531                                 return r;
532
533                         if (r > 0) {
534
535                                 if (direction == DIRECTION_DOWN) {
536                                         if (q > np) {
537                                                 np = q;
538                                                 npo = qo;
539                                         }
540                                 } else {
541                                         if (np == 0 || q < np) {
542                                                 np = q;
543                                                 npo = qo;
544                                         }
545                                 }
546                         }
547                 }
548
549                 /* Check the last term */
550                 if (term_match && !term_result)
551                         found = false;
552
553                 /* Did this entry match against all matches? */
554                 if (found) {
555                         if (ret)
556                                 *ret = c;
557                         if (offset)
558                                 *offset = cp;
559                         return 1;
560                 }
561
562                 /* Did we find a subsequent entry? */
563                 if (np == 0)
564                         return 0;
565
566                 /* Hmm, ok, this entry only matched partially, so
567                  * let's try another one */
568                 cp = np;
569                 c = npo;
570         }
571 }
572
573 static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
574         Object *c;
575         uint64_t cp;
576         int compare_value, r;
577
578         assert(j);
579         assert(f);
580
581         if (f->current_offset > 0) {
582                 cp = f->current_offset;
583
584                 r = journal_file_move_to_object(f, OBJECT_ENTRY, cp, &c);
585                 if (r < 0)
586                         return r;
587
588                 r = next_with_matches(j, f, direction, &c, &cp);
589                 if (r <= 0)
590                         return r;
591
592                 compare_value = 1;
593         } else {
594                 r = find_location(j, f, direction, &c, &cp);
595                 if (r <= 0)
596                         return r;
597
598                 compare_value = 0;
599         }
600
601         for (;;) {
602                 bool found;
603
604                 if (j->current_location.type == LOCATION_DISCRETE) {
605                         int k;
606
607                         k = compare_with_location(f, c, &j->current_location);
608                         if (direction == DIRECTION_DOWN)
609                                 found = k >= compare_value;
610                         else
611                                 found = k <= -compare_value;
612                 } else
613                         found = true;
614
615                 if (found) {
616                         if (ret)
617                                 *ret = c;
618                         if (offset)
619                                 *offset = cp;
620                         return 1;
621                 }
622
623                 r = next_with_matches(j, f, direction, &c, &cp);
624                 if (r <= 0)
625                         return r;
626         }
627 }
628
629 static int real_journal_next(sd_journal *j, direction_t direction) {
630         JournalFile *f, *new_current = NULL;
631         Iterator i;
632         int r;
633         uint64_t new_offset = 0;
634         Object *new_entry = NULL;
635
636         if (!j)
637                 return -EINVAL;
638
639         HASHMAP_FOREACH(f, j->files, i) {
640                 Object *o;
641                 uint64_t p;
642                 bool found;
643
644                 r = next_beyond_location(j, f, direction, &o, &p);
645                 if (r < 0)
646                         return r;
647                 else if (r == 0)
648                         continue;
649
650                 if (!new_current)
651                         found = true;
652                 else {
653                         int k;
654
655                         k = compare_order(f, o, new_current, new_entry);
656
657                         if (direction == DIRECTION_DOWN)
658                                 found = k < 0;
659                         else
660                                 found = k > 0;
661                 }
662
663                 if (found) {
664                         new_current = f;
665                         new_entry = o;
666                         new_offset = p;
667                 }
668         }
669
670         if (!new_current)
671                 return 0;
672
673         set_location(j, new_current, new_entry, new_offset);
674
675         return 1;
676 }
677
678 _public_ int sd_journal_next(sd_journal *j) {
679         return real_journal_next(j, DIRECTION_DOWN);
680 }
681
682 _public_ int sd_journal_previous(sd_journal *j) {
683         return real_journal_next(j, DIRECTION_UP);
684 }
685
686 static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) {
687         int c = 0, r;
688
689         if (!j)
690                 return -EINVAL;
691
692         if (skip == 0) {
693                 /* If this is not a discrete skip, then at least
694                  * resolve the current location */
695                 if (j->current_location.type != LOCATION_DISCRETE)
696                         return real_journal_next(j, direction);
697
698                 return 0;
699         }
700
701         do {
702                 r = real_journal_next(j, direction);
703                 if (r < 0)
704                         return r;
705
706                 if (r == 0)
707                         return c;
708
709                 skip--;
710                 c++;
711         } while (skip > 0);
712
713         return c;
714 }
715
716 _public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
717         return real_journal_next_skip(j, DIRECTION_DOWN, skip);
718 }
719
720 _public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
721         return real_journal_next_skip(j, DIRECTION_UP, skip);
722 }
723
724 _public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) {
725         Object *o;
726         int r;
727         char bid[33], sid[33];
728
729         if (!j)
730                 return -EINVAL;
731         if (!cursor)
732                 return -EINVAL;
733
734         if (!j->current_file || j->current_file->current_offset <= 0)
735                 return -EADDRNOTAVAIL;
736
737         r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
738         if (r < 0)
739                 return r;
740
741         sd_id128_to_string(j->current_file->header->seqnum_id, sid);
742         sd_id128_to_string(o->entry.boot_id, bid);
743
744         if (asprintf(cursor,
745                      "s=%s;i=%llx;b=%s;m=%llx;t=%llx;x=%llx;p=%s",
746                      sid, (unsigned long long) le64toh(o->entry.seqnum),
747                      bid, (unsigned long long) le64toh(o->entry.monotonic),
748                      (unsigned long long) le64toh(o->entry.realtime),
749                      (unsigned long long) le64toh(o->entry.xor_hash),
750                      file_name_from_path(j->current_file->path)) < 0)
751                 return -ENOMEM;
752
753         return 1;
754 }
755
756 _public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
757         char *w;
758         size_t l;
759         char *state;
760         unsigned long long seqnum, monotonic, realtime, xor_hash;
761         bool
762                 seqnum_id_set = false,
763                 seqnum_set = false,
764                 boot_id_set = false,
765                 monotonic_set = false,
766                 realtime_set = false,
767                 xor_hash_set = false;
768         sd_id128_t seqnum_id, boot_id;
769
770         if (!j)
771                 return -EINVAL;
772         if (!cursor)
773                 return -EINVAL;
774
775         FOREACH_WORD_SEPARATOR(w, l, cursor, ";", state) {
776                 char *item;
777                 int k = 0;
778
779                 if (l < 2 || w[1] != '=')
780                         return -EINVAL;
781
782                 item = strndup(w, l);
783                 if (!item)
784                         return -ENOMEM;
785
786                 switch (w[0]) {
787
788                 case 's':
789                         seqnum_id_set = true;
790                         k = sd_id128_from_string(w+2, &seqnum_id);
791                         break;
792
793                 case 'i':
794                         seqnum_set = true;
795                         if (sscanf(w+2, "%llx", &seqnum) != 1)
796                                 k = -EINVAL;
797                         break;
798
799                 case 'b':
800                         boot_id_set = true;
801                         k = sd_id128_from_string(w+2, &boot_id);
802                         break;
803
804                 case 'm':
805                         monotonic_set = true;
806                         if (sscanf(w+2, "%llx", &monotonic) != 1)
807                                 k = -EINVAL;
808                         break;
809
810                 case 't':
811                         realtime_set = true;
812                         if (sscanf(w+2, "%llx", &realtime) != 1)
813                                 k = -EINVAL;
814                         break;
815
816                 case 'x':
817                         xor_hash_set = true;
818                         if (sscanf(w+2, "%llx", &xor_hash) != 1)
819                                 k = -EINVAL;
820                         break;
821                 }
822
823                 free(item);
824
825                 if (k < 0)
826                         return k;
827         }
828
829         if ((!seqnum_set || !seqnum_id_set) &&
830             (!monotonic_set || !boot_id_set) &&
831             !realtime_set)
832                 return -EINVAL;
833
834         reset_location(j);
835
836         j->current_location.type = LOCATION_DISCRETE;
837
838         if (realtime_set) {
839                 j->current_location.realtime = (uint64_t) realtime;
840                 j->current_location.realtime_set = true;
841         }
842
843         if (seqnum_set && seqnum_id_set) {
844                 j->current_location.seqnum = (uint64_t) seqnum;
845                 j->current_location.seqnum_id = seqnum_id;
846                 j->current_location.seqnum_set = true;
847         }
848
849         if (monotonic_set && boot_id_set) {
850                 j->current_location.monotonic = (uint64_t) monotonic;
851                 j->current_location.boot_id = boot_id;
852                 j->current_location.monotonic_set = true;
853         }
854
855         if (xor_hash_set) {
856                 j->current_location.xor_hash = (uint64_t) xor_hash;
857                 j->current_location.xor_hash_set = true;
858         }
859
860         return 0;
861 }
862
863 _public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
864         if (!j)
865                 return -EINVAL;
866
867         reset_location(j);
868         j->current_location.type = LOCATION_DISCRETE;
869         j->current_location.boot_id = boot_id;
870         j->current_location.monotonic = usec;
871         j->current_location.monotonic_set = true;
872
873         return 0;
874 }
875
876 _public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
877         if (!j)
878                 return -EINVAL;
879
880         reset_location(j);
881         j->current_location.type = LOCATION_DISCRETE;
882         j->current_location.realtime = usec;
883         j->current_location.realtime_set = true;
884
885         return 0;
886 }
887
888 _public_ int sd_journal_seek_head(sd_journal *j) {
889         if (!j)
890                 return -EINVAL;
891
892         reset_location(j);
893         j->current_location.type = LOCATION_HEAD;
894
895         return 0;
896 }
897
898 _public_ int sd_journal_seek_tail(sd_journal *j) {
899         if (!j)
900                 return -EINVAL;
901
902         reset_location(j);
903         j->current_location.type = LOCATION_TAIL;
904
905         return 0;
906 }
907
908 static int add_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
909         char *fn;
910         int r;
911         JournalFile *f;
912
913         assert(j);
914         assert(prefix);
915         assert(filename);
916
917         if ((j->flags & SD_JOURNAL_SYSTEM_ONLY) &&
918             !startswith(filename, "system.journal"))
919                 return 0;
920
921         if (dir)
922                 fn = join(prefix, "/", dir, "/", filename, NULL);
923         else
924                 fn = join(prefix, "/", filename, NULL);
925
926         if (!fn)
927                 return -ENOMEM;
928
929         if (hashmap_get(j->files, fn)) {
930                 free(fn);
931                 return 0;
932         }
933
934         if (hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
935                 log_debug("Too many open journal files, not adding %s, ignoring.", fn);
936                 free(fn);
937                 return 0;
938         }
939
940         r = journal_file_open(fn, O_RDONLY, 0, NULL, &f);
941         free(fn);
942
943         if (r < 0) {
944                 if (errno == ENOENT)
945                         return 0;
946
947                 return r;
948         }
949
950         /* journal_file_dump(f); */
951
952         r = hashmap_put(j->files, f->path, f);
953         if (r < 0) {
954                 journal_file_close(f);
955                 return r;
956         }
957
958         log_debug("File %s got added.", f->path);
959
960         return 0;
961 }
962
963 static int remove_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
964         char *fn;
965         JournalFile *f;
966
967         assert(j);
968         assert(prefix);
969         assert(filename);
970
971         if (dir)
972                 fn = join(prefix, "/", dir, "/", filename, NULL);
973         else
974                 fn = join(prefix, "/", filename, NULL);
975
976         if (!fn)
977                 return -ENOMEM;
978
979         f = hashmap_get(j->files, fn);
980         free(fn);
981
982         if (!f)
983                 return 0;
984
985         hashmap_remove(j->files, f->path);
986         journal_file_close(f);
987
988         log_debug("File %s got removed.", f->path);
989         return 0;
990 }
991
992 static int add_directory(sd_journal *j, const char *prefix, const char *dir) {
993         char *fn;
994         int r;
995         DIR *d;
996         int wd;
997         sd_id128_t id, mid;
998
999         assert(j);
1000         assert(prefix);
1001         assert(dir);
1002
1003         if ((j->flags & SD_JOURNAL_LOCAL_ONLY) &&
1004             (sd_id128_from_string(dir, &id) < 0 ||
1005              sd_id128_get_machine(&mid) < 0 ||
1006              !sd_id128_equal(id, mid)))
1007             return 0;
1008
1009         fn = join(prefix, "/", dir, NULL);
1010         if (!fn)
1011                 return -ENOMEM;
1012
1013         d = opendir(fn);
1014
1015         if (!d) {
1016                 free(fn);
1017                 if (errno == ENOENT)
1018                         return 0;
1019
1020                 return -errno;
1021         }
1022
1023         wd = inotify_add_watch(j->inotify_fd, fn,
1024                                IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1025                                IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|
1026                                IN_DONT_FOLLOW|IN_ONLYDIR);
1027         if (wd > 0) {
1028                 if (hashmap_put(j->inotify_wd_dirs, INT_TO_PTR(wd), fn) < 0)
1029                         inotify_rm_watch(j->inotify_fd, wd);
1030                 else
1031                         fn = NULL;
1032         }
1033
1034         free(fn);
1035
1036         for (;;) {
1037                 struct dirent buf, *de;
1038
1039                 r = readdir_r(d, &buf, &de);
1040                 if (r != 0 || !de)
1041                         break;
1042
1043                 if (!dirent_is_file_with_suffix(de, ".journal"))
1044                         continue;
1045
1046                 r = add_file(j, prefix, dir, de->d_name);
1047                 if (r < 0)
1048                         log_debug("Failed to add file %s/%s/%s: %s", prefix, dir, de->d_name, strerror(-r));
1049         }
1050
1051         closedir(d);
1052
1053         log_debug("Directory %s/%s got added.", prefix, dir);
1054
1055         return 0;
1056 }
1057
1058 static void remove_directory_wd(sd_journal *j, int wd) {
1059         char *p;
1060
1061         assert(j);
1062         assert(wd > 0);
1063
1064         if (j->inotify_fd >= 0)
1065                 inotify_rm_watch(j->inotify_fd, wd);
1066
1067         p = hashmap_remove(j->inotify_wd_dirs, INT_TO_PTR(wd));
1068
1069         if (p) {
1070                 log_debug("Directory %s got removed.", p);
1071                 free(p);
1072         }
1073 }
1074
1075 static void add_root_wd(sd_journal *j, const char *p) {
1076         int wd;
1077         char *k;
1078
1079         assert(j);
1080         assert(p);
1081
1082         wd = inotify_add_watch(j->inotify_fd, p,
1083                                IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1084                                IN_DONT_FOLLOW|IN_ONLYDIR);
1085         if (wd <= 0)
1086                 return;
1087
1088         k = strdup(p);
1089         if (!k || hashmap_put(j->inotify_wd_roots, INT_TO_PTR(wd), k) < 0) {
1090                 inotify_rm_watch(j->inotify_fd, wd);
1091                 free(k);
1092         }
1093 }
1094
1095 static void remove_root_wd(sd_journal *j, int wd) {
1096         char *p;
1097
1098         assert(j);
1099         assert(wd > 0);
1100
1101         if (j->inotify_fd >= 0)
1102                 inotify_rm_watch(j->inotify_fd, wd);
1103
1104         p = hashmap_remove(j->inotify_wd_roots, INT_TO_PTR(wd));
1105
1106         if (p) {
1107                 log_debug("Root %s got removed.", p);
1108                 free(p);
1109         }
1110 }
1111
1112 _public_ int sd_journal_open(sd_journal **ret, int flags) {
1113         sd_journal *j;
1114         const char *p;
1115         const char search_paths[] =
1116                 "/run/log/journal\0"
1117                 "/var/log/journal\0";
1118         int r;
1119
1120         if (!ret)
1121                 return -EINVAL;
1122
1123         if (flags & ~(SD_JOURNAL_LOCAL_ONLY|
1124                       SD_JOURNAL_RUNTIME_ONLY|
1125                       SD_JOURNAL_SYSTEM_ONLY))
1126                 return -EINVAL;
1127
1128         j = new0(sd_journal, 1);
1129         if (!j)
1130                 return -ENOMEM;
1131
1132         j->flags = flags;
1133
1134         j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1135         if (j->inotify_fd < 0) {
1136                 r = -errno;
1137                 goto fail;
1138         }
1139
1140         j->files = hashmap_new(string_hash_func, string_compare_func);
1141         if (!j->files) {
1142                 r = -ENOMEM;
1143                 goto fail;
1144         }
1145
1146         j->inotify_wd_dirs = hashmap_new(trivial_hash_func, trivial_compare_func);
1147         j->inotify_wd_roots = hashmap_new(trivial_hash_func, trivial_compare_func);
1148
1149         if (!j->inotify_wd_dirs || !j->inotify_wd_roots) {
1150                 r = -ENOMEM;
1151                 goto fail;
1152         }
1153
1154         /* We ignore most errors here, since the idea is to only open
1155          * what's actually accessible, and ignore the rest. */
1156
1157         NULSTR_FOREACH(p, search_paths) {
1158                 DIR *d;
1159
1160                 if ((flags & SD_JOURNAL_RUNTIME_ONLY) &&
1161                     !path_startswith(p, "/run"))
1162                         continue;
1163
1164                 d = opendir(p);
1165                 if (!d) {
1166                         if (errno != ENOENT)
1167                                 log_debug("Failed to open %s: %m", p);
1168                         continue;
1169                 }
1170
1171                 add_root_wd(j, p);
1172
1173                 for (;;) {
1174                         struct dirent buf, *de;
1175                         sd_id128_t id;
1176
1177                         r = readdir_r(d, &buf, &de);
1178                         if (r != 0 || !de)
1179                                 break;
1180
1181                         if (dirent_is_file_with_suffix(de, ".journal")) {
1182                                 r = add_file(j, p, NULL, de->d_name);
1183                                 if (r < 0)
1184                                         log_debug("Failed to add file %s/%s: %s", p, de->d_name, strerror(-r));
1185
1186                         } else if ((de->d_type == DT_DIR || de->d_type == DT_UNKNOWN) &&
1187                                    sd_id128_from_string(de->d_name, &id) >= 0) {
1188
1189                                 r = add_directory(j, p, de->d_name);
1190                                 if (r < 0)
1191                                         log_debug("Failed to add directory %s/%s: %s", p, de->d_name, strerror(-r));
1192                         }
1193                 }
1194
1195                 closedir(d);
1196         }
1197
1198         *ret = j;
1199         return 0;
1200
1201 fail:
1202         sd_journal_close(j);
1203
1204         return r;
1205 };
1206
1207 _public_ void sd_journal_close(sd_journal *j) {
1208         if (!j)
1209                 return;
1210
1211         if (j->inotify_wd_dirs) {
1212                 void *k;
1213
1214                 while ((k = hashmap_first_key(j->inotify_wd_dirs)))
1215                         remove_directory_wd(j, PTR_TO_INT(k));
1216
1217                 hashmap_free(j->inotify_wd_dirs);
1218         }
1219
1220         if (j->inotify_wd_roots) {
1221                 void *k;
1222
1223                 while ((k = hashmap_first_key(j->inotify_wd_roots)))
1224                         remove_root_wd(j, PTR_TO_INT(k));
1225
1226                 hashmap_free(j->inotify_wd_roots);
1227         }
1228
1229         if (j->files) {
1230                 JournalFile *f;
1231
1232                 while ((f = hashmap_steal_first(j->files)))
1233                         journal_file_close(f);
1234
1235                 hashmap_free(j->files);
1236         }
1237
1238         sd_journal_flush_matches(j);
1239
1240         if (j->inotify_fd >= 0)
1241                 close_nointr_nofail(j->inotify_fd);
1242
1243         free(j);
1244 }
1245
1246 _public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
1247         Object *o;
1248         JournalFile *f;
1249         int r;
1250
1251         if (!j)
1252                 return -EINVAL;
1253         if (!ret)
1254                 return -EINVAL;
1255
1256         f = j->current_file;
1257         if (!f)
1258                 return -EADDRNOTAVAIL;
1259
1260         if (f->current_offset <= 0)
1261                 return -EADDRNOTAVAIL;
1262
1263         r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1264         if (r < 0)
1265                 return r;
1266
1267         *ret = le64toh(o->entry.realtime);
1268         return 0;
1269 }
1270
1271 _public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
1272         Object *o;
1273         JournalFile *f;
1274         int r;
1275         sd_id128_t id;
1276
1277         if (!j)
1278                 return -EINVAL;
1279         if (!ret)
1280                 return -EINVAL;
1281
1282         f = j->current_file;
1283         if (!f)
1284                 return -EADDRNOTAVAIL;
1285
1286         if (f->current_offset <= 0)
1287                 return -EADDRNOTAVAIL;
1288
1289         r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1290         if (r < 0)
1291                 return r;
1292
1293         if (ret_boot_id)
1294                 *ret_boot_id = o->entry.boot_id;
1295         else {
1296                 r = sd_id128_get_boot(&id);
1297                 if (r < 0)
1298                         return r;
1299
1300                 if (!sd_id128_equal(id, o->entry.boot_id))
1301                         return -ESTALE;
1302         }
1303
1304         *ret = le64toh(o->entry.monotonic);
1305         return 0;
1306 }
1307
1308 _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
1309         JournalFile *f;
1310         uint64_t i, n;
1311         size_t field_length;
1312         int r;
1313         Object *o;
1314
1315         if (!j)
1316                 return -EINVAL;
1317         if (!field)
1318                 return -EINVAL;
1319         if (!data)
1320                 return -EINVAL;
1321         if (!size)
1322                 return -EINVAL;
1323
1324         if (isempty(field) || strchr(field, '='))
1325                 return -EINVAL;
1326
1327         f = j->current_file;
1328         if (!f)
1329                 return -EADDRNOTAVAIL;
1330
1331         if (f->current_offset <= 0)
1332                 return -EADDRNOTAVAIL;
1333
1334         r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1335         if (r < 0)
1336                 return r;
1337
1338         field_length = strlen(field);
1339
1340         n = journal_file_entry_n_items(o);
1341         for (i = 0; i < n; i++) {
1342                 uint64_t p, l, le_hash;
1343                 size_t t;
1344
1345                 p = le64toh(o->entry.items[i].object_offset);
1346                 le_hash = o->entry.items[i].hash;
1347                 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
1348                 if (r < 0)
1349                         return r;
1350
1351                 if (le_hash != o->data.hash)
1352                         return -EBADMSG;
1353
1354                 l = le64toh(o->object.size) - offsetof(Object, data.payload);
1355
1356                 if (o->object.flags & OBJECT_COMPRESSED) {
1357
1358 #ifdef HAVE_XZ
1359                         if (uncompress_startswith(o->data.payload, l,
1360                                                   &f->compress_buffer, &f->compress_buffer_size,
1361                                                   field, field_length, '=')) {
1362
1363                                 uint64_t rsize;
1364
1365                                 if (!uncompress_blob(o->data.payload, l,
1366                                                      &f->compress_buffer, &f->compress_buffer_size, &rsize))
1367                                         return -EBADMSG;
1368
1369                                 *data = f->compress_buffer;
1370                                 *size = (size_t) rsize;
1371
1372                                 return 0;
1373                         }
1374 #else
1375                         return -EPROTONOSUPPORT;
1376 #endif
1377
1378                 } else if (l >= field_length+1 &&
1379                            memcmp(o->data.payload, field, field_length) == 0 &&
1380                            o->data.payload[field_length] == '=') {
1381
1382                         t = (size_t) l;
1383
1384                         if ((uint64_t) t != l)
1385                                 return -E2BIG;
1386
1387                         *data = o->data.payload;
1388                         *size = t;
1389
1390                         return 0;
1391                 }
1392
1393                 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1394                 if (r < 0)
1395                         return r;
1396         }
1397
1398         return -ENOENT;
1399 }
1400
1401 _public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
1402         JournalFile *f;
1403         uint64_t p, l, n, le_hash;
1404         int r;
1405         Object *o;
1406         size_t t;
1407
1408         if (!j)
1409                 return -EINVAL;
1410         if (!data)
1411                 return -EINVAL;
1412         if (!size)
1413                 return -EINVAL;
1414
1415         f = j->current_file;
1416         if (!f)
1417                 return -EADDRNOTAVAIL;
1418
1419         if (f->current_offset <= 0)
1420                 return -EADDRNOTAVAIL;
1421
1422         r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1423         if (r < 0)
1424                 return r;
1425
1426         n = journal_file_entry_n_items(o);
1427         if (j->current_field >= n)
1428                 return 0;
1429
1430         p = le64toh(o->entry.items[j->current_field].object_offset);
1431         le_hash = o->entry.items[j->current_field].hash;
1432         r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
1433         if (r < 0)
1434                 return r;
1435
1436         if (le_hash != o->data.hash)
1437                 return -EBADMSG;
1438
1439         l = le64toh(o->object.size) - offsetof(Object, data.payload);
1440         t = (size_t) l;
1441
1442         /* We can't read objects larger than 4G on a 32bit machine */
1443         if ((uint64_t) t != l)
1444                 return -E2BIG;
1445
1446         if (o->object.flags & OBJECT_COMPRESSED) {
1447 #ifdef HAVE_XZ
1448                 uint64_t rsize;
1449
1450                 if (!uncompress_blob(o->data.payload, l, &f->compress_buffer, &f->compress_buffer_size, &rsize))
1451                         return -EBADMSG;
1452
1453                 *data = f->compress_buffer;
1454                 *size = (size_t) rsize;
1455 #else
1456                 return -EPROTONOSUPPORT;
1457 #endif
1458         } else {
1459                 *data = o->data.payload;
1460                 *size = t;
1461         }
1462
1463         j->current_field ++;
1464
1465         return 1;
1466 }
1467
1468 _public_ void sd_journal_restart_data(sd_journal *j) {
1469         if (!j)
1470                 return;
1471
1472         j->current_field = 0;
1473 }
1474
1475 _public_ int sd_journal_get_fd(sd_journal *j) {
1476         if (!j)
1477                 return -EINVAL;
1478
1479         return j->inotify_fd;
1480 }
1481
1482 static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
1483         char *p;
1484         int r;
1485
1486         assert(j);
1487         assert(e);
1488
1489         /* Is this a subdirectory we watch? */
1490         p = hashmap_get(j->inotify_wd_dirs, INT_TO_PTR(e->wd));
1491         if (p) {
1492
1493                 if (!(e->mask & IN_ISDIR) && e->len > 0 && endswith(e->name, ".journal")) {
1494
1495                         /* Event for a journal file */
1496
1497                         if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1498                                 r = add_file(j, p, NULL, e->name);
1499                                 if (r < 0)
1500                                         log_debug("Failed to add file %s/%s: %s", p, e->name, strerror(-r));
1501                         } else if (e->mask & (IN_DELETE|IN_UNMOUNT)) {
1502
1503                                 r = remove_file(j, p, NULL, e->name);
1504                                 if (r < 0)
1505                                         log_debug("Failed to remove file %s/%s: %s", p, e->name, strerror(-r));
1506                         }
1507
1508                 } else if (e->len == 0) {
1509
1510                         /* Event for the directory itself */
1511
1512                         if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT))
1513                                 remove_directory_wd(j, e->wd);
1514                 }
1515
1516                 return;
1517         }
1518
1519         /* Must be the root directory then? */
1520         p = hashmap_get(j->inotify_wd_roots, INT_TO_PTR(e->wd));
1521         if (p) {
1522                 sd_id128_t id;
1523
1524                 if (!(e->mask & IN_ISDIR) && e->len > 0 && endswith(e->name, ".journal")) {
1525
1526                         /* Event for a journal file */
1527
1528                         if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1529                                 r = add_file(j, p, NULL, e->name);
1530                                 if (r < 0)
1531                                         log_debug("Failed to add file %s/%s: %s", p, e->name, strerror(-r));
1532                         } else if (e->mask & (IN_DELETE|IN_UNMOUNT)) {
1533
1534                                 r = remove_file(j, p, NULL, e->name);
1535                                 if (r < 0)
1536                                         log_debug("Failed to remove file %s/%s: %s", p, e->name, strerror(-r));
1537                         }
1538
1539                 } else if ((e->mask & IN_ISDIR) && e->len > 0 && sd_id128_from_string(e->name, &id) >= 0) {
1540
1541                         /* Event for subdirectory */
1542
1543                         if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1544
1545                                 r = add_directory(j, p, e->name);
1546                                 if (r < 0)
1547                                         log_debug("Failed to add directory %s/%s: %s", p, e->name, strerror(-r));
1548                         }
1549                 }
1550
1551                 return;
1552         }
1553
1554         if (e->mask & IN_IGNORED)
1555                 return;
1556
1557         log_warning("Unknown inotify event.");
1558 }
1559
1560 _public_ int sd_journal_process(sd_journal *j) {
1561         uint8_t buffer[sizeof(struct inotify_event) + FILENAME_MAX];
1562
1563         if (!j)
1564                 return -EINVAL;
1565
1566         for (;;) {
1567                 struct inotify_event *e;
1568                 ssize_t l;
1569
1570                 l = read(j->inotify_fd, buffer, sizeof(buffer));
1571                 if (l < 0) {
1572                         if (errno == EINTR || errno == EAGAIN)
1573                                 return 0;
1574
1575                         return -errno;
1576                 }
1577
1578                 e = (struct inotify_event*) buffer;
1579                 while (l > 0) {
1580                         size_t step;
1581
1582                         process_inotify_event(j, e);
1583
1584                         step = sizeof(struct inotify_event) + e->len;
1585                         assert(step <= (size_t) l);
1586
1587                         e = (struct inotify_event*) ((uint8_t*) e + step);
1588                         l -= step;
1589                 }
1590         }
1591 }
1592
1593 _public_ int sd_journal_query_unique(sd_journal *j, const char *field) {
1594         if (!j)
1595                 return -EINVAL;
1596         if (!field)
1597                 return -EINVAL;
1598
1599         return -ENOTSUP;
1600 }
1601
1602 _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l) {
1603         if (!j)
1604                 return -EINVAL;
1605         if (!data)
1606                 return -EINVAL;
1607         if (!l)
1608                 return -EINVAL;
1609
1610         return -ENOTSUP;
1611 }
1612
1613 _public_ void sd_journal_restart_unique(sd_journal *j) {
1614         if (!j)
1615                 return;
1616 }