]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/sd-journal.c
journal: unify code for up and for down traversal
[thirdparty/systemd.git] / src / journal / sd-journal.c
CommitLineData
87d2c1ff
LP
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
87d2c1ff 22#include <errno.h>
87d2c1ff 23#include <fcntl.h>
3fbf9cbb 24#include <stddef.h>
87d2c1ff
LP
25
26#include "sd-journal.h"
27#include "journal-def.h"
cec736d2 28#include "journal-file.h"
260a2be4 29#include "hashmap.h"
cec736d2 30#include "list.h"
de7b95cd 31#include "lookup3.h"
87d2c1ff 32
cec736d2 33typedef struct Match Match;
87d2c1ff 34
cec736d2
LP
35struct Match {
36 char *data;
37 size_t size;
de7b95cd 38 uint64_t le_hash;
87d2c1ff 39
cec736d2
LP
40 LIST_FIELDS(Match, matches);
41};
87d2c1ff 42
87d2c1ff 43struct sd_journal {
260a2be4 44 Hashmap *files;
87d2c1ff 45
cec736d2 46 JournalFile *current_file;
7210bfb3 47 uint64_t current_field;
87d2c1ff 48
cec736d2 49 LIST_HEAD(Match, matches);
de7b95cd 50 unsigned n_matches;
cec736d2 51};
87d2c1ff 52
1cc101f1 53int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
cec736d2 54 Match *m;
87d2c1ff 55
cec736d2 56 assert(j);
1cc101f1
LP
57
58 if (size <= 0)
59 return -EINVAL;
60
61 assert(data);
87d2c1ff 62
cec736d2
LP
63 m = new0(Match, 1);
64 if (!m)
65 return -ENOMEM;
87d2c1ff 66
1cc101f1
LP
67 m->size = size;
68
cec736d2
LP
69 m->data = malloc(m->size);
70 if (!m->data) {
71 free(m);
72 return -ENOMEM;
87d2c1ff
LP
73 }
74
1cc101f1 75 memcpy(m->data, data, size);
de7b95cd 76 m->le_hash = hash64(m->data, size);
87d2c1ff 77
cec736d2 78 LIST_PREPEND(Match, matches, j->matches, m);
de7b95cd
LP
79 j->n_matches ++;
80
87d2c1ff
LP
81 return 0;
82}
83
cec736d2
LP
84void sd_journal_flush_matches(sd_journal *j) {
85 assert(j);
87d2c1ff 86
cec736d2
LP
87 while (j->matches) {
88 Match *m = j->matches;
87d2c1ff 89
cec736d2
LP
90 LIST_REMOVE(Match, matches, j->matches, m);
91 free(m->data);
92 free(m);
87d2c1ff 93 }
de7b95cd
LP
94
95 j->n_matches = 0;
87d2c1ff
LP
96}
97
cec736d2 98static int compare_order(JournalFile *af, Object *ao, uint64_t ap,
ae2cc8ef 99 JournalFile *bf, Object *bo, uint64_t bp) {
87d2c1ff 100
cec736d2 101 uint64_t a, b;
87d2c1ff 102
ae2cc8ef 103 /* We operate on two different files here, hence we can access
1cc101f1
LP
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;
ae2cc8ef 114
cec736d2 115 if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
87d2c1ff 116
cec736d2
LP
117 /* If this is from the same seqnum source, compare
118 * seqnums */
119 a = le64toh(ao->entry.seqnum);
120 b = le64toh(bo->entry.seqnum);
87d2c1ff 121
ae2cc8ef
LP
122 if (a < b)
123 return -1;
124 if (a > b)
125 return 1;
1cc101f1
LP
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. */
ae2cc8ef 130 }
87d2c1ff 131
ae2cc8ef 132 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
87d2c1ff 133
cec736d2
LP
134 /* If the boot id matches compare monotonic time */
135 a = le64toh(ao->entry.monotonic);
136 b = le64toh(bo->entry.monotonic);
87d2c1ff 137
ae2cc8ef
LP
138 if (a < b)
139 return -1;
140 if (a > b)
141 return 1;
87d2c1ff
LP
142 }
143
ae2cc8ef
LP
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;
87d2c1ff
LP
163}
164
e892bd17 165static int move_to_next_with_matches(sd_journal *j, JournalFile *f, direction_t direction, Object **o, uint64_t *p) {
de7b95cd
LP
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
e892bd17 185 return journal_file_next_entry(f, c, direction, o, p);
de7b95cd
LP
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 {
e892bd17 194 r = journal_file_find_first_entry(f, j->matches->data, j->matches->size, direction, &c, &cp);
de7b95cd
LP
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 */
e892bd17
LP
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 }
de7b95cd
LP
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
e892bd17 271static int real_journal_next(sd_journal *j, direction_t direction) {
cec736d2
LP
272 JournalFile *f, *new_current = NULL;
273 Iterator i;
87d2c1ff 274 int r;
cec736d2
LP
275 uint64_t new_offset = 0;
276 Object *new_entry = NULL;
87d2c1ff 277
cec736d2 278 assert(j);
87d2c1ff 279
cec736d2
LP
280 HASHMAP_FOREACH(f, j->files, i) {
281 Object *o;
282 uint64_t p;
87d2c1ff 283
e892bd17 284 r = move_to_next_with_matches(j, f, direction, &o, &p);
87d2c1ff
LP
285 if (r < 0)
286 return r;
cec736d2
LP
287 else if (r == 0)
288 continue;
87d2c1ff 289
ae2cc8ef
LP
290 if (!new_current ||
291 compare_order(new_current, new_entry, new_offset, f, o, p) > 0) {
cec736d2
LP
292 new_current = f;
293 new_entry = o;
294 new_offset = p;
87d2c1ff 295 }
87d2c1ff
LP
296 }
297
cec736d2
LP
298 if (new_current) {
299 j->current_file = new_current;
3fbf9cbb 300 j->current_file->current_offset = new_offset;
7210bfb3 301 j->current_field = 0;
ae2cc8ef
LP
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
e892bd17 312 r = move_to_next_with_matches(j, f, direction, &o, &p);
ae2cc8ef
LP
313 if (r < 0)
314 return r;
315 else if (r == 0)
316 continue;
317
7210bfb3 318 if (compare_order(new_current, new_entry, new_offset, f, o, p) == 0)
ae2cc8ef 319 f->current_offset = p;
ae2cc8ef
LP
320 }
321
cec736d2 322 return 1;
87d2c1ff
LP
323 }
324
87d2c1ff
LP
325 return 0;
326}
327
e892bd17
LP
328int sd_journal_next(sd_journal *j) {
329 return real_journal_next(j, DIRECTION_DOWN);
330}
87d2c1ff 331
e892bd17
LP
332int sd_journal_previous(sd_journal *j) {
333 return real_journal_next(j, DIRECTION_UP);
87d2c1ff
LP
334}
335
3fbf9cbb 336int sd_journal_get_cursor(sd_journal *j, char **cursor) {
cec736d2 337 Object *o;
87d2c1ff 338 int r;
3fbf9cbb 339 char bid[33], sid[33];
87d2c1ff 340
cec736d2
LP
341 assert(j);
342 assert(cursor);
87d2c1ff 343
3fbf9cbb
LP
344 if (!j->current_file || j->current_file->current_offset <= 0)
345 return -EADDRNOTAVAIL;
87d2c1ff 346
cec736d2 347 r = journal_file_move_to_object(j->current_file, j->current_file->current_offset, OBJECT_ENTRY, &o);
87d2c1ff
LP
348 if (r < 0)
349 return r;
350
3fbf9cbb
LP
351 sd_id128_to_string(j->current_file->header->seqnum_id, sid);
352 sd_id128_to_string(o->entry.boot_id, bid);
87d2c1ff 353
3fbf9cbb
LP
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;
87d2c1ff
LP
362
363 return 1;
364}
365
3fbf9cbb 366int sd_journal_set_cursor(sd_journal *j, const char *cursor) {
cec736d2 367 return -EINVAL;
87d2c1ff
LP
368}
369
3fbf9cbb
LP
370static 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
406static 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
87d2c1ff
LP
449int sd_journal_open(sd_journal **ret) {
450 sd_journal *j;
87d2c1ff 451 const char *p;
87d2c1ff
LP
452 const char search_paths[] =
453 "/run/log/journal\0"
454 "/var/log/journal\0";
3fbf9cbb 455 int r;
87d2c1ff
LP
456
457 assert(ret);
458
459 j = new0(sd_journal, 1);
460 if (!j)
461 return -ENOMEM;
462
260a2be4 463 j->files = hashmap_new(string_hash_func, string_compare_func);
3fbf9cbb
LP
464 if (!j->files) {
465 r = -ENOMEM;
260a2be4 466 goto fail;
3fbf9cbb
LP
467 }
468
469 /* We ignore most errors here, since the idea is to only open
470 * what's actually accessible, and ignore the rest. */
260a2be4 471
87d2c1ff
LP
472 NULSTR_FOREACH(p, search_paths) {
473 DIR *d;
474
475 d = opendir(p);
476 if (!d) {
3fbf9cbb
LP
477 if (errno != ENOENT)
478 log_debug("Failed to open %s: %m", p);
87d2c1ff
LP
479 continue;
480 }
481
482 for (;;) {
483 struct dirent buf, *de;
3fbf9cbb 484 sd_id128_t id;
87d2c1ff 485
3fbf9cbb
LP
486 r = readdir_r(d, &buf, &de);
487 if (r != 0 || !de)
87d2c1ff
LP
488 break;
489
3fbf9cbb
LP
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));
87d2c1ff 494
3fbf9cbb
LP
495 } else if ((de->d_type == DT_DIR || de->d_type == DT_UNKNOWN) &&
496 sd_id128_from_string(de->d_name, &id) >= 0) {
87d2c1ff 497
3fbf9cbb
LP
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));
260a2be4
LP
501 }
502 }
3fbf9cbb
LP
503
504 closedir(d);
87d2c1ff
LP
505 }
506
507 *ret = j;
508 return 0;
509
510fail:
511 sd_journal_close(j);
512
513 return r;
514};
515
516void sd_journal_close(sd_journal *j) {
517 assert(j);
518
260a2be4
LP
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 }
87d2c1ff 527
1cc101f1
LP
528 sd_journal_flush_matches(j);
529
87d2c1ff
LP
530 free(j);
531}
3fbf9cbb
LP
532
533int 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
556int 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
de7b95cd 572 r = sd_id128_get_boot(&id);
3fbf9cbb
LP
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
588int sd_journal_get_field(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++) {
de7b95cd 618 uint64_t p, l, h;
3fbf9cbb
LP
619 size_t t;
620
621 p = le64toh(o->entry.items[i].object_offset);
de7b95cd 622 h = o->entry.items[j->current_field].hash;
3fbf9cbb
LP
623 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
624 if (r < 0)
625 return r;
626
de7b95cd
LP
627 if (h != o->data.hash)
628 return -EBADMSG;
629
3fbf9cbb
LP
630 l = le64toh(o->object.size) - offsetof(Object, data.payload);
631
161e54f8
LP
632 if (l >= field_length+1 &&
633 memcmp(o->data.payload, field, field_length) == 0 &&
634 o->data.payload[field_length] == '=') {
3fbf9cbb 635
161e54f8 636 t = (size_t) l;
3fbf9cbb 637
161e54f8
LP
638 if ((uint64_t) t != l)
639 return -E2BIG;
3fbf9cbb 640
161e54f8
LP
641 *data = o->data.payload;
642 *size = t;
3fbf9cbb 643
161e54f8
LP
644 return 1;
645 }
3fbf9cbb 646
161e54f8
LP
647 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
648 if (r < 0)
649 return r;
3fbf9cbb
LP
650 }
651
652 return 0;
653}
654
655int sd_journal_iterate_fields(sd_journal *j, const void **data, size_t *size) {
656 JournalFile *f;
de7b95cd 657 uint64_t p, l, n, h;
3fbf9cbb
LP
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);
7210bfb3 678 if (j->current_field >= n)
3fbf9cbb
LP
679 return 0;
680
7210bfb3 681 p = le64toh(o->entry.items[j->current_field].object_offset);
de7b95cd 682 h = o->entry.items[j->current_field].hash;
3fbf9cbb
LP
683 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
684 if (r < 0)
685 return r;
686
de7b95cd
LP
687 if (h != o->data.hash)
688 return -EBADMSG;
689
3fbf9cbb
LP
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
7210bfb3 700 j->current_field ++;
3fbf9cbb
LP
701
702 return 1;
703}
c2373f84
LP
704
705int sd_journal_seek_head(sd_journal *j) {
706 assert(j);
707 return -EINVAL;
708}
709
710int sd_journal_seek_tail(sd_journal *j) {
711 assert(j);
712 return -EINVAL;
713}