chiark / gitweb /
journal: implement seek to head/tail
[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
26 #include "sd-journal.h"
27 #include "journal-def.h"
28 #include "journal-file.h"
29 #include "hashmap.h"
30 #include "list.h"
31 #include "lookup3.h"
32
33 typedef struct Match Match;
34
35 struct Match {
36         char *data;
37         size_t size;
38         uint64_t le_hash;
39
40         LIST_FIELDS(Match, matches);
41 };
42
43 struct sd_journal {
44         Hashmap *files;
45
46         JournalFile *current_file;
47         uint64_t current_field;
48
49         LIST_HEAD(Match, matches);
50         unsigned n_matches;
51 };
52
53 int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
54         Match *m;
55
56         assert(j);
57
58         if (size <= 0)
59                 return -EINVAL;
60
61         assert(data);
62
63         m = new0(Match, 1);
64         if (!m)
65                 return -ENOMEM;
66
67         m->size = size;
68
69         m->data = malloc(m->size);
70         if (!m->data) {
71                 free(m);
72                 return -ENOMEM;
73         }
74
75         memcpy(m->data, data, size);
76         m->le_hash = hash64(m->data, size);
77
78         LIST_PREPEND(Match, matches, j->matches, m);
79         j->n_matches ++;
80
81         return 0;
82 }
83
84 void sd_journal_flush_matches(sd_journal *j) {
85         assert(j);
86
87         while (j->matches) {
88                 Match *m = j->matches;
89
90                 LIST_REMOVE(Match, matches, j->matches, m);
91                 free(m->data);
92                 free(m);
93         }
94
95         j->n_matches = 0;
96 }
97
98 static int compare_order(JournalFile *af, Object *ao, uint64_t ap,
99                          JournalFile *bf, Object *bo, uint64_t bp) {
100
101         uint64_t a, b;
102
103         /* We operate on two different files here, hence we can access
104          * two objects at the same time, which we normally can't.
105          *
106          * If contents and timestamps match, these entries are
107          * identical, even if the seqnum does not match */
108
109         if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id) &&
110             ao->entry.monotonic == bo->entry.monotonic &&
111             ao->entry.realtime == bo->entry.realtime &&
112             ao->entry.xor_hash == bo->entry.xor_hash)
113                 return 0;
114
115         if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
116
117                 /* If this is from the same seqnum source, compare
118                  * seqnums */
119                 a = le64toh(ao->entry.seqnum);
120                 b = le64toh(bo->entry.seqnum);
121
122                 if (a < b)
123                         return -1;
124                 if (a > b)
125                         return 1;
126
127                 /* Wow! This is weird, different data but the same
128                  * seqnums? Something is borked, but let's make the
129                  * best of it and compare by time. */
130         }
131
132         if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
133
134                 /* If the boot id matches compare monotonic time */
135                 a = le64toh(ao->entry.monotonic);
136                 b = le64toh(bo->entry.monotonic);
137
138                 if (a < b)
139                         return -1;
140                 if (a > b)
141                         return 1;
142         }
143
144         /* Otherwise compare UTC time */
145         a = le64toh(ao->entry.realtime);
146         b = le64toh(ao->entry.realtime);
147
148         if (a < b)
149                 return -1;
150         if (a > b)
151                 return 1;
152
153         /* Finally, compare by contents */
154         a = le64toh(ao->entry.xor_hash);
155         b = le64toh(ao->entry.xor_hash);
156
157         if (a < b)
158                 return -1;
159         if (a > b)
160                 return 1;
161
162         return 0;
163 }
164
165 static int move_to_next_with_matches(sd_journal *j, JournalFile *f, direction_t direction, Object **o, uint64_t *p) {
166         int r;
167         uint64_t cp;
168         Object *c;
169
170         assert(j);
171         assert(f);
172         assert(o);
173         assert(p);
174
175         if (!j->matches) {
176                 /* No matches is easy, just go on to the next entry */
177
178                 if (f->current_offset > 0) {
179                         r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &c);
180                         if (r < 0)
181                                 return r;
182                 } else
183                         c = NULL;
184
185                 return journal_file_next_entry(f, c, direction, o, p);
186         }
187
188         /* So there are matches we have to adhere to, let's find the
189          * first entry that matches all of them */
190
191         if (f->current_offset > 0)
192                 cp = f->current_offset;
193         else {
194                 r = journal_file_find_first_entry(f, j->matches->data, j->matches->size, direction, &c, &cp);
195                 if (r <= 0)
196                         return r;
197
198                 /* We can shortcut this if there's only one match */
199                 if (j->n_matches == 1) {
200                         *o = c;
201                         *p = cp;
202                         return r;
203                 }
204         }
205
206         for (;;) {
207                 uint64_t np, n;
208                 bool found;
209                 Match *m;
210
211                 r = journal_file_move_to_object(f, cp, OBJECT_ENTRY, &c);
212                 if (r < 0)
213                         return r;
214
215                 n = journal_file_entry_n_items(c);
216
217                 /* Make sure we don't match the entry we are starting
218                  * from. */
219                 found = f->current_offset != cp;
220
221                 np = 0;
222                 LIST_FOREACH(matches, m, j->matches) {
223                         uint64_t q, k;
224
225                         for (k = 0; k < n; k++)
226                                 if (c->entry.items[k].hash == m->le_hash)
227                                         break;
228
229                         if (k >= n) {
230                                 /* Hmm, didn't find any field that matched, so ignore
231                                  * this match. Go on with next match */
232
233                                 found = false;
234                                 continue;
235                         }
236
237                         /* Hmm, so, this field matched, let's remember
238                          * where we'd have to try next, in case the other
239                          * matches are not OK */
240
241                         if (direction == DIRECTION_DOWN) {
242                                 q = le64toh(c->entry.items[k].next_entry_offset);
243
244                                 if (q > np)
245                                         np = q;
246                         } else {
247                                 q = le64toh(c->entry.items[k].prev_entry_offset);
248
249                                 if (q != 0 && (np == 0 || q < np))
250                                         np = q;
251                         }
252                 }
253
254                 /* Did this entry match against all matches? */
255                 if (found) {
256                         *o = c;
257                         *p = cp;
258                         return 1;
259                 }
260
261                 /* Did we find a subsequent entry? */
262                 if (np == 0)
263                         return 0;
264
265                 /* Hmm, ok, this entry only matched partially, so
266                  * let's try another one */
267                 cp = np;
268         }
269 }
270
271 static int real_journal_next(sd_journal *j, direction_t direction) {
272         JournalFile *f, *new_current = NULL;
273         Iterator i;
274         int r;
275         uint64_t new_offset = 0;
276         Object *new_entry = NULL;
277
278         assert(j);
279
280         HASHMAP_FOREACH(f, j->files, i) {
281                 Object *o;
282                 uint64_t p;
283
284                 r = move_to_next_with_matches(j, f, direction, &o, &p);
285                 if (r < 0)
286                         return r;
287                 else if (r == 0)
288                         continue;
289
290                 if (!new_current ||
291                     compare_order(new_current, new_entry, new_offset, f, o, p) > 0) {
292                         new_current = f;
293                         new_entry = o;
294                         new_offset = p;
295                 }
296         }
297
298         if (new_current) {
299                 j->current_file = new_current;
300                 j->current_file->current_offset = new_offset;
301                 j->current_field = 0;
302
303                 /* Skip over any identical entries in the other files too */
304
305                 HASHMAP_FOREACH(f, j->files, i) {
306                         Object *o;
307                         uint64_t p;
308
309                         if (j->current_file == f)
310                                 continue;
311
312                         r = move_to_next_with_matches(j, f, direction, &o, &p);
313                         if (r < 0)
314                                 return r;
315                         else if (r == 0)
316                                 continue;
317
318                         if (compare_order(new_current, new_entry, new_offset, f, o, p) == 0)
319                                 f->current_offset = p;
320                 }
321
322                 return 1;
323         }
324
325         return 0;
326 }
327
328 int sd_journal_next(sd_journal *j) {
329         return real_journal_next(j, DIRECTION_DOWN);
330 }
331
332 int sd_journal_previous(sd_journal *j) {
333         return real_journal_next(j, DIRECTION_UP);
334 }
335
336 int sd_journal_get_cursor(sd_journal *j, char **cursor) {
337         Object *o;
338         int r;
339         char bid[33], sid[33];
340
341         assert(j);
342         assert(cursor);
343
344         if (!j->current_file || j->current_file->current_offset <= 0)
345                 return -EADDRNOTAVAIL;
346
347         r = journal_file_move_to_object(j->current_file, j->current_file->current_offset, OBJECT_ENTRY, &o);
348         if (r < 0)
349                 return r;
350
351         sd_id128_to_string(j->current_file->header->seqnum_id, sid);
352         sd_id128_to_string(o->entry.boot_id, bid);
353
354         if (asprintf(cursor,
355                      "s=%s;i=%llx;b=%s;m=%llx;t=%llx;x=%llx;p=%s",
356                      sid, (unsigned long long) le64toh(o->entry.seqnum),
357                      bid, (unsigned long long) le64toh(o->entry.monotonic),
358                      (unsigned long long) le64toh(o->entry.realtime),
359                      (unsigned long long) le64toh(o->entry.xor_hash),
360                      file_name_from_path(j->current_file->path)) < 0)
361                 return -ENOMEM;
362
363         return 1;
364 }
365
366 int sd_journal_set_cursor(sd_journal *j, const char *cursor) {
367         return -EINVAL;
368 }
369
370 static int add_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
371         char *fn;
372         int r;
373         JournalFile *f;
374
375         assert(j);
376         assert(prefix);
377         assert(filename);
378
379         if (dir)
380                 fn = join(prefix, "/", dir, "/", filename, NULL);
381         else
382                 fn = join(prefix, "/", filename, NULL);
383
384         if (!fn)
385                 return -ENOMEM;
386
387         r = journal_file_open(fn, O_RDONLY, 0, NULL, &f);
388         free(fn);
389
390         if (r < 0) {
391                 if (errno == ENOENT)
392                         return 0;
393
394                 return r;
395         }
396
397         r = hashmap_put(j->files, f->path, f);
398         if (r < 0) {
399                 journal_file_close(f);
400                 return r;
401         }
402
403         return 0;
404 }
405
406 static int add_directory(sd_journal *j, const char *prefix, const char *dir) {
407         char *fn;
408         int r;
409         DIR *d;
410
411         assert(j);
412         assert(prefix);
413         assert(dir);
414
415         fn = join(prefix, "/", dir, NULL);
416         if (!fn)
417                 return -ENOMEM;
418
419         d = opendir(fn);
420         free(fn);
421
422         if (!d) {
423                 if (errno == ENOENT)
424                         return 0;
425
426                 return -errno;
427         }
428
429         for (;;) {
430                 struct dirent buf, *de;
431
432                 r = readdir_r(d, &buf, &de);
433                 if (r != 0 || !de)
434                         break;
435
436                 if (!dirent_is_file_with_suffix(de, ".journal"))
437                         continue;
438
439                 r = add_file(j, prefix, dir, de->d_name);
440                 if (r < 0)
441                         log_debug("Failed to add file %s/%s/%s: %s", prefix, dir, de->d_name, strerror(-r));
442         }
443
444         closedir(d);
445
446         return 0;
447 }
448
449 int sd_journal_open(sd_journal **ret) {
450         sd_journal *j;
451         const char *p;
452         const char search_paths[] =
453                 "/run/log/journal\0"
454                 "/var/log/journal\0";
455         int r;
456
457         assert(ret);
458
459         j = new0(sd_journal, 1);
460         if (!j)
461                 return -ENOMEM;
462
463         j->files = hashmap_new(string_hash_func, string_compare_func);
464         if (!j->files) {
465                 r = -ENOMEM;
466                 goto fail;
467         }
468
469         /* We ignore most errors here, since the idea is to only open
470          * what's actually accessible, and ignore the rest. */
471
472         NULSTR_FOREACH(p, search_paths) {
473                 DIR *d;
474
475                 d = opendir(p);
476                 if (!d) {
477                         if (errno != ENOENT)
478                                 log_debug("Failed to open %s: %m", p);
479                         continue;
480                 }
481
482                 for (;;) {
483                         struct dirent buf, *de;
484                         sd_id128_t id;
485
486                         r = readdir_r(d, &buf, &de);
487                         if (r != 0 || !de)
488                                 break;
489
490                         if (dirent_is_file_with_suffix(de, ".journal")) {
491                                 r = add_file(j, p, NULL, de->d_name);
492                                 if (r < 0)
493                                         log_debug("Failed to add file %s/%s: %s", p, de->d_name, strerror(-r));
494
495                         } else if ((de->d_type == DT_DIR || de->d_type == DT_UNKNOWN) &&
496                                    sd_id128_from_string(de->d_name, &id) >= 0) {
497
498                                 r = add_directory(j, p, de->d_name);
499                                 if (r < 0)
500                                         log_debug("Failed to add directory %s/%s: %s", p, de->d_name, strerror(-r));
501                         }
502                 }
503
504                 closedir(d);
505         }
506
507         *ret = j;
508         return 0;
509
510 fail:
511         sd_journal_close(j);
512
513         return r;
514 };
515
516 void sd_journal_close(sd_journal *j) {
517         assert(j);
518
519         if (j->files) {
520                 JournalFile *f;
521
522                 while ((f = hashmap_steal_first(j->files)))
523                         journal_file_close(f);
524
525                 hashmap_free(j->files);
526         }
527
528         sd_journal_flush_matches(j);
529
530         free(j);
531 }
532
533 int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
534         Object *o;
535         JournalFile *f;
536         int r;
537
538         assert(j);
539         assert(ret);
540
541         f = j->current_file;
542         if (!f)
543                 return 0;
544
545         if (f->current_offset <= 0)
546                 return 0;
547
548         r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
549         if (r < 0)
550                 return r;
551
552         *ret = le64toh(o->entry.realtime);
553         return 1;
554 }
555
556 int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret) {
557         Object *o;
558         JournalFile *f;
559         int r;
560         sd_id128_t id;
561
562         assert(j);
563         assert(ret);
564
565         f = j->current_file;
566         if (!f)
567                 return 0;
568
569         if (f->current_offset <= 0)
570                 return 0;
571
572         r = sd_id128_get_boot(&id);
573         if (r < 0)
574                 return r;
575
576         r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
577         if (r < 0)
578                 return r;
579
580         if (!sd_id128_equal(id, o->entry.boot_id))
581                 return 0;
582
583         *ret = le64toh(o->entry.monotonic);
584         return 1;
585
586 }
587
588 int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
589         JournalFile *f;
590         uint64_t i, n;
591         size_t field_length;
592         int r;
593         Object *o;
594
595         assert(j);
596         assert(field);
597         assert(data);
598         assert(size);
599
600         if (isempty(field) || strchr(field, '='))
601                 return -EINVAL;
602
603         f = j->current_file;
604         if (!f)
605                 return 0;
606
607         if (f->current_offset <= 0)
608                 return 0;
609
610         r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
611         if (r < 0)
612                 return r;
613
614         field_length = strlen(field);
615
616         n = journal_file_entry_n_items(o);
617         for (i = 0; i < n; i++) {
618                 uint64_t p, l, h;
619                 size_t t;
620
621                 p = le64toh(o->entry.items[i].object_offset);
622                 h = o->entry.items[j->current_field].hash;
623                 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
624                 if (r < 0)
625                         return r;
626
627                 if (h != o->data.hash)
628                         return -EBADMSG;
629
630                 l = le64toh(o->object.size) - offsetof(Object, data.payload);
631
632                 if (l >= field_length+1 &&
633                     memcmp(o->data.payload, field, field_length) == 0 &&
634                     o->data.payload[field_length] == '=') {
635
636                         t = (size_t) l;
637
638                         if ((uint64_t) t != l)
639                                 return -E2BIG;
640
641                         *data = o->data.payload;
642                         *size = t;
643
644                         return 1;
645                 }
646
647                 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
648                 if (r < 0)
649                         return r;
650         }
651
652         return 0;
653 }
654
655 int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
656         JournalFile *f;
657         uint64_t p, l, n, h;
658         size_t t;
659         int r;
660         Object *o;
661
662         assert(j);
663         assert(data);
664         assert(size);
665
666         f = j->current_file;
667         if (!f)
668                 return 0;
669
670         if (f->current_offset <= 0)
671                 return 0;
672
673         r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
674         if (r < 0)
675                 return r;
676
677         n = journal_file_entry_n_items(o);
678         if (j->current_field >= n)
679                 return 0;
680
681         p = le64toh(o->entry.items[j->current_field].object_offset);
682         h = o->entry.items[j->current_field].hash;
683         r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
684         if (r < 0)
685                 return r;
686
687         if (h != o->data.hash)
688                 return -EBADMSG;
689
690         l = le64toh(o->object.size) - offsetof(Object, data.payload);
691         t = (size_t) l;
692
693         /* We can't read objects larger than 4G on a 32bit machine */
694         if ((uint64_t) t != l)
695                 return -E2BIG;
696
697         *data = o->data.payload;
698         *size = t;
699
700         j->current_field ++;
701
702         return 1;
703 }
704
705 void sd_journal_start_data(sd_journal *j) {
706         assert(j);
707
708         j->current_field = 0;
709 }
710
711 static int real_journal_seek_head(sd_journal *j, direction_t direction) {
712         Iterator i;
713         JournalFile *f;
714
715         assert(j);
716
717         j->current_file = NULL;
718         j->current_field = 0;
719
720         HASHMAP_FOREACH(f, j->files, i)
721                 f->current_offset = 0;
722
723         return real_journal_next(j, direction);
724 }
725
726 int sd_journal_seek_head(sd_journal *j) {
727         return real_journal_seek_head(j, DIRECTION_DOWN);
728 }
729
730 int sd_journal_seek_tail(sd_journal *j) {
731         return real_journal_seek_head(j, DIRECTION_UP);
732 }