1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/inotify.h>
29 #include "sd-journal.h"
30 #include "journal-def.h"
31 #include "journal-file.h"
34 #include "path-util.h"
37 #include "journal-internal.h"
39 #define JOURNAL_FILES_MAX 1024
41 static void detach_location(sd_journal *j) {
47 j->current_file = NULL;
50 HASHMAP_FOREACH(f, j->files, i)
51 f->current_offset = 0;
54 static void reset_location(sd_journal *j) {
58 zero(j->current_location);
61 static void init_location(Location *l, JournalFile *f, Object *o) {
64 assert(o->object.type == OBJECT_ENTRY);
66 l->type = LOCATION_DISCRETE;
67 l->seqnum = le64toh(o->entry.seqnum);
68 l->seqnum_id = f->header->seqnum_id;
69 l->realtime = le64toh(o->entry.realtime);
70 l->monotonic = le64toh(o->entry.monotonic);
71 l->boot_id = o->entry.boot_id;
72 l->xor_hash = le64toh(o->entry.xor_hash);
74 l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true;
77 static void set_location(sd_journal *j, JournalFile *f, Object *o, uint64_t offset) {
82 init_location(&j->current_location, f, o);
87 f->current_offset = offset;
90 static int same_field(const void *_a, size_t s, const void *_b, size_t t) {
91 const uint8_t *a = _a, *b = _b;
93 bool a_good = false, b_good = false, different = false;
95 for (j = 0; j < s && j < t; j++) {
104 if (a_good && b_good)
105 return different ? 0 : 1;
111 _public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
112 Match *m, *after = NULL;
121 if (!memchr(data, '=', size))
123 if (*(char*) data == '=')
126 /* FIXME: iterating with multiple matches is currently
131 le_hash = htole64(hash64(data, size));
133 LIST_FOREACH(matches, m, j->matches) {
136 if (m->le_hash == le_hash &&
138 memcmp(m->data, data, size) == 0)
141 r = same_field(data, size, m->data, m->size);
154 m->data = malloc(m->size);
160 memcpy(m->data, data, size);
161 m->le_hash = le_hash;
163 /* Matches for the same fields we order adjacent to each
165 LIST_INSERT_AFTER(Match, matches, j->matches, after, m);
173 _public_ void sd_journal_flush_matches(sd_journal *j) {
178 Match *m = j->matches;
180 LIST_REMOVE(Match, matches, j->matches, m);
190 static int compare_order(JournalFile *af, Object *ao,
191 JournalFile *bf, Object *bo) {
200 /* We operate on two different files here, hence we can access
201 * two objects at the same time, which we normally can't.
203 * If contents and timestamps match, these entries are
204 * identical, even if the seqnum does not match */
206 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id) &&
207 ao->entry.monotonic == bo->entry.monotonic &&
208 ao->entry.realtime == bo->entry.realtime &&
209 ao->entry.xor_hash == bo->entry.xor_hash)
212 if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
214 /* If this is from the same seqnum source, compare
216 a = le64toh(ao->entry.seqnum);
217 b = le64toh(bo->entry.seqnum);
224 /* Wow! This is weird, different data but the same
225 * seqnums? Something is borked, but let's make the
226 * best of it and compare by time. */
229 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
231 /* If the boot id matches compare monotonic time */
232 a = le64toh(ao->entry.monotonic);
233 b = le64toh(bo->entry.monotonic);
241 /* Otherwise compare UTC time */
242 a = le64toh(ao->entry.realtime);
243 b = le64toh(ao->entry.realtime);
250 /* Finally, compare by contents */
251 a = le64toh(ao->entry.xor_hash);
252 b = le64toh(ao->entry.xor_hash);
262 static int compare_with_location(JournalFile *af, Object *ao, Location *l) {
268 assert(l->type == LOCATION_DISCRETE);
270 if (l->monotonic_set &&
271 sd_id128_equal(ao->entry.boot_id, l->boot_id) &&
273 le64toh(ao->entry.realtime) == l->realtime &&
275 le64toh(ao->entry.xor_hash) == l->xor_hash)
279 sd_id128_equal(af->header->seqnum_id, l->seqnum_id)) {
281 a = le64toh(ao->entry.seqnum);
289 if (l->monotonic_set &&
290 sd_id128_equal(ao->entry.boot_id, l->boot_id)) {
292 a = le64toh(ao->entry.monotonic);
294 if (a < l->monotonic)
296 if (a > l->monotonic)
300 if (l->realtime_set) {
302 a = le64toh(ao->entry.realtime);
310 if (l->xor_hash_set) {
311 a = le64toh(ao->entry.xor_hash);
322 static int find_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
330 /* No matches is simple */
332 if (j->current_location.type == LOCATION_HEAD)
333 r = journal_file_next_entry(f, NULL, 0, DIRECTION_DOWN, &o, &p);
334 else if (j->current_location.type == LOCATION_TAIL)
335 r = journal_file_next_entry(f, NULL, 0, DIRECTION_UP, &o, &p);
336 else if (j->current_location.seqnum_set &&
337 sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
338 r = journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, &o, &p);
339 else if (j->current_location.monotonic_set) {
340 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, &o, &p);
343 /* boot id unknown in this file */
344 if (j->current_location.realtime_set)
345 r = journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, &o, &p);
347 r = journal_file_next_entry(f, NULL, 0, direction, &o, &p);
349 } else if (j->current_location.realtime_set)
350 r = journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, &o, &p);
352 r = journal_file_next_entry(f, NULL, 0, direction, &o, &p);
358 Match *m, *term_match = NULL;
362 /* We have matches, first, let's jump to the monotonic
363 * position if we have any, since it implies a
366 if (j->current_location.type == LOCATION_DISCRETE &&
367 j->current_location.monotonic_set) {
369 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, &o, &p);
371 return r == -ENOENT ? 0 : r;
374 LIST_FOREACH(matches, m, j->matches) {
378 r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), &d, &dp);
382 if (j->current_location.type == LOCATION_HEAD)
383 r = journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_DOWN, &c, &cp);
384 else if (j->current_location.type == LOCATION_TAIL)
385 r = journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_UP, &c, &cp);
386 else if (j->current_location.seqnum_set &&
387 sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
388 r = journal_file_move_to_entry_by_seqnum_for_data(f, dp, j->current_location.seqnum, direction, &c, &cp);
389 else if (j->current_location.realtime_set)
390 r = journal_file_move_to_entry_by_realtime_for_data(f, dp, j->current_location.realtime, direction, &c, &cp);
392 r = journal_file_next_entry_for_data(f, NULL, 0, dp, direction, &c, &cp);
404 } else if (same_field(term_match->data, term_match->size, m->data, m->size)) {
406 /* Same field as previous match... */
409 /* Find the earliest of the OR matches */
412 (direction == DIRECTION_DOWN && cp < tp) ||
413 (direction == DIRECTION_UP && cp > tp)) {
422 /* Previous term is finished, did anything match? */
426 /* Find the last of the AND matches */
428 (direction == DIRECTION_DOWN && tp > p) ||
429 (direction == DIRECTION_UP && tp < p)) {
446 /* Last term is finished, did anything match? */
451 (direction == DIRECTION_DOWN && tp > p) ||
452 (direction == DIRECTION_UP && tp < p)) {
470 static int next_with_matches(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
484 /* No matches is easy */
486 r = journal_file_next_entry(f, c, cp, direction, &c, &cp);
497 /* So there are matches we have to adhere to, let's find the
498 * first entry that matches all of them */
502 bool found, term_result = false;
503 Match *m, *term_match = NULL;
506 n = journal_file_entry_n_items(c);
508 /* Make sure we don't match the entry we are starting
510 found = cp != *offset;
513 LIST_FOREACH(matches, m, j->matches) {
517 /* Let's check if this is the beginning of a
518 * new term, i.e. has a different field prefix
519 * as the preceeding match. */
523 } else if (!same_field(term_match->data, term_match->size, m->data, m->size)) {
531 for (k = 0; k < n; k++)
532 if (c->entry.items[k].hash == m->le_hash)
536 /* Hmm, didn't find any field that
537 * matched this rule, so ignore this
538 * match. Go on with next match */
544 /* Hmm, so, this field matched, let's remember
545 * where we'd have to try next, in case the other
546 * matches are not OK */
548 r = journal_file_next_entry_for_data(f, c, cp, le64toh(c->entry.items[k].object_offset), direction, &qo, &q);
549 /* This pointer is invalidated if the window was
550 * remapped. May need to re-fetch it later */
557 if (direction == DIRECTION_DOWN) {
563 if (np == 0 || q < np) {
571 /* Check the last term */
572 if (term_match && !term_result)
575 /* Did this entry match against all matches? */
579 /* Re-fetch the entry */
580 r = journal_file_move_to_object(f, OBJECT_ENTRY, cp, &c);
591 /* Did we find a subsequent entry? */
595 /* Hmm, ok, this entry only matched partially, so
596 * let's try another one */
602 static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
605 int compare_value, r;
610 if (f->current_offset > 0) {
611 cp = f->current_offset;
613 r = journal_file_move_to_object(f, OBJECT_ENTRY, cp, &c);
617 r = next_with_matches(j, f, direction, &c, &cp);
623 r = find_location(j, f, direction, &c, &cp);
633 if (j->current_location.type == LOCATION_DISCRETE) {
636 k = compare_with_location(f, c, &j->current_location);
637 if (direction == DIRECTION_DOWN)
638 found = k >= compare_value;
640 found = k <= -compare_value;
652 r = next_with_matches(j, f, direction, &c, &cp);
658 static int real_journal_next(sd_journal *j, direction_t direction) {
659 JournalFile *f, *new_current = NULL;
662 uint64_t new_offset = 0;
663 Object *new_entry = NULL;
668 HASHMAP_FOREACH(f, j->files, i) {
673 r = next_beyond_location(j, f, direction, &o, &p);
675 log_debug("Can't iterate through %s, ignoring: %s", f->path, strerror(-r));
685 k = compare_order(f, o, new_current, new_entry);
687 if (direction == DIRECTION_DOWN)
703 set_location(j, new_current, new_entry, new_offset);
708 _public_ int sd_journal_next(sd_journal *j) {
709 return real_journal_next(j, DIRECTION_DOWN);
712 _public_ int sd_journal_previous(sd_journal *j) {
713 return real_journal_next(j, DIRECTION_UP);
716 static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) {
723 /* If this is not a discrete skip, then at least
724 * resolve the current location */
725 if (j->current_location.type != LOCATION_DISCRETE)
726 return real_journal_next(j, direction);
732 r = real_journal_next(j, direction);
746 _public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
747 return real_journal_next_skip(j, DIRECTION_DOWN, skip);
750 _public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
751 return real_journal_next_skip(j, DIRECTION_UP, skip);
754 _public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) {
757 char bid[33], sid[33];
764 if (!j->current_file || j->current_file->current_offset <= 0)
765 return -EADDRNOTAVAIL;
767 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
771 sd_id128_to_string(j->current_file->header->seqnum_id, sid);
772 sd_id128_to_string(o->entry.boot_id, bid);
775 "s=%s;i=%llx;b=%s;m=%llx;t=%llx;x=%llx;p=%s",
776 sid, (unsigned long long) le64toh(o->entry.seqnum),
777 bid, (unsigned long long) le64toh(o->entry.monotonic),
778 (unsigned long long) le64toh(o->entry.realtime),
779 (unsigned long long) le64toh(o->entry.xor_hash),
780 path_get_file_name(j->current_file->path)) < 0)
786 _public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
790 unsigned long long seqnum, monotonic, realtime, xor_hash;
792 seqnum_id_set = false,
795 monotonic_set = false,
796 realtime_set = false,
797 xor_hash_set = false;
798 sd_id128_t seqnum_id, boot_id;
805 FOREACH_WORD_SEPARATOR(w, l, cursor, ";", state) {
809 if (l < 2 || w[1] != '=')
812 item = strndup(w, l);
819 seqnum_id_set = true;
820 k = sd_id128_from_string(w+2, &seqnum_id);
825 if (sscanf(w+2, "%llx", &seqnum) != 1)
831 k = sd_id128_from_string(w+2, &boot_id);
835 monotonic_set = true;
836 if (sscanf(w+2, "%llx", &monotonic) != 1)
842 if (sscanf(w+2, "%llx", &realtime) != 1)
848 if (sscanf(w+2, "%llx", &xor_hash) != 1)
859 if ((!seqnum_set || !seqnum_id_set) &&
860 (!monotonic_set || !boot_id_set) &&
866 j->current_location.type = LOCATION_DISCRETE;
869 j->current_location.realtime = (uint64_t) realtime;
870 j->current_location.realtime_set = true;
873 if (seqnum_set && seqnum_id_set) {
874 j->current_location.seqnum = (uint64_t) seqnum;
875 j->current_location.seqnum_id = seqnum_id;
876 j->current_location.seqnum_set = true;
879 if (monotonic_set && boot_id_set) {
880 j->current_location.monotonic = (uint64_t) monotonic;
881 j->current_location.boot_id = boot_id;
882 j->current_location.monotonic_set = true;
886 j->current_location.xor_hash = (uint64_t) xor_hash;
887 j->current_location.xor_hash_set = true;
893 _public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
898 j->current_location.type = LOCATION_DISCRETE;
899 j->current_location.boot_id = boot_id;
900 j->current_location.monotonic = usec;
901 j->current_location.monotonic_set = true;
906 _public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
911 j->current_location.type = LOCATION_DISCRETE;
912 j->current_location.realtime = usec;
913 j->current_location.realtime_set = true;
918 _public_ int sd_journal_seek_head(sd_journal *j) {
923 j->current_location.type = LOCATION_HEAD;
928 _public_ int sd_journal_seek_tail(sd_journal *j) {
933 j->current_location.type = LOCATION_TAIL;
938 static int add_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
947 if ((j->flags & SD_JOURNAL_SYSTEM_ONLY) &&
948 !(streq(filename, "system.journal") ||
949 (startswith(filename, "system@") && endswith(filename, ".journal"))))
953 fn = join(prefix, "/", dir, "/", filename, NULL);
955 fn = join(prefix, "/", filename, NULL);
960 if (hashmap_get(j->files, fn)) {
965 if (hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
966 log_debug("Too many open journal files, not adding %s, ignoring.", fn);
971 r = journal_file_open(fn, O_RDONLY, 0, NULL, &f);
981 /* journal_file_dump(f); */
983 r = hashmap_put(j->files, f->path, f);
985 journal_file_close(f);
989 log_debug("File %s got added.", f->path);
994 static int remove_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
1003 fn = join(prefix, "/", dir, "/", filename, NULL);
1005 fn = join(prefix, "/", filename, NULL);
1010 f = hashmap_get(j->files, fn);
1016 hashmap_remove(j->files, f->path);
1017 journal_file_close(f);
1019 log_debug("File %s got removed.", f->path);
1023 static int add_directory(sd_journal *j, const char *prefix, const char *dir) {
1034 if ((j->flags & SD_JOURNAL_LOCAL_ONLY) &&
1035 (sd_id128_from_string(dir, &id) < 0 ||
1036 sd_id128_get_machine(&mid) < 0 ||
1037 !sd_id128_equal(id, mid)))
1040 fn = join(prefix, "/", dir, NULL);
1048 if (errno == ENOENT)
1054 wd = inotify_add_watch(j->inotify_fd, fn,
1055 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1056 IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|
1057 IN_DONT_FOLLOW|IN_ONLYDIR);
1059 if (hashmap_put(j->inotify_wd_dirs, INT_TO_PTR(wd), fn) < 0)
1060 inotify_rm_watch(j->inotify_fd, wd);
1068 struct dirent buf, *de;
1070 r = readdir_r(d, &buf, &de);
1074 if (!dirent_is_file_with_suffix(de, ".journal"))
1077 r = add_file(j, prefix, dir, de->d_name);
1079 log_debug("Failed to add file %s/%s/%s: %s", prefix, dir, de->d_name, strerror(-r));
1084 log_debug("Directory %s/%s got added.", prefix, dir);
1089 static void remove_directory_wd(sd_journal *j, int wd) {
1095 if (j->inotify_fd >= 0)
1096 inotify_rm_watch(j->inotify_fd, wd);
1098 p = hashmap_remove(j->inotify_wd_dirs, INT_TO_PTR(wd));
1101 log_debug("Directory %s got removed.", p);
1106 static void add_root_wd(sd_journal *j, const char *p) {
1113 wd = inotify_add_watch(j->inotify_fd, p,
1114 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1115 IN_DONT_FOLLOW|IN_ONLYDIR);
1120 if (!k || hashmap_put(j->inotify_wd_roots, INT_TO_PTR(wd), k) < 0) {
1121 inotify_rm_watch(j->inotify_fd, wd);
1126 static void remove_root_wd(sd_journal *j, int wd) {
1132 if (j->inotify_fd >= 0)
1133 inotify_rm_watch(j->inotify_fd, wd);
1135 p = hashmap_remove(j->inotify_wd_roots, INT_TO_PTR(wd));
1138 log_debug("Root %s got removed.", p);
1143 _public_ int sd_journal_open(sd_journal **ret, int flags) {
1146 const char search_paths[] =
1147 "/run/log/journal\0"
1148 "/var/log/journal\0";
1154 if (flags & ~(SD_JOURNAL_LOCAL_ONLY|
1155 SD_JOURNAL_RUNTIME_ONLY|
1156 SD_JOURNAL_SYSTEM_ONLY))
1159 j = new0(sd_journal, 1);
1165 j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1166 if (j->inotify_fd < 0) {
1171 j->files = hashmap_new(string_hash_func, string_compare_func);
1177 j->inotify_wd_dirs = hashmap_new(trivial_hash_func, trivial_compare_func);
1178 j->inotify_wd_roots = hashmap_new(trivial_hash_func, trivial_compare_func);
1180 if (!j->inotify_wd_dirs || !j->inotify_wd_roots) {
1185 /* We ignore most errors here, since the idea is to only open
1186 * what's actually accessible, and ignore the rest. */
1188 NULSTR_FOREACH(p, search_paths) {
1191 if ((flags & SD_JOURNAL_RUNTIME_ONLY) &&
1192 !path_startswith(p, "/run"))
1197 if (errno != ENOENT)
1198 log_debug("Failed to open %s: %m", p);
1205 struct dirent buf, *de;
1208 r = readdir_r(d, &buf, &de);
1212 if (dirent_is_file_with_suffix(de, ".journal")) {
1213 r = add_file(j, p, NULL, de->d_name);
1215 log_debug("Failed to add file %s/%s: %s", p, de->d_name, strerror(-r));
1217 } else if ((de->d_type == DT_DIR || de->d_type == DT_UNKNOWN) &&
1218 sd_id128_from_string(de->d_name, &id) >= 0) {
1220 r = add_directory(j, p, de->d_name);
1222 log_debug("Failed to add directory %s/%s: %s", p, de->d_name, strerror(-r));
1233 sd_journal_close(j);
1238 _public_ void sd_journal_close(sd_journal *j) {
1242 if (j->inotify_wd_dirs) {
1245 while ((k = hashmap_first_key(j->inotify_wd_dirs)))
1246 remove_directory_wd(j, PTR_TO_INT(k));
1248 hashmap_free(j->inotify_wd_dirs);
1251 if (j->inotify_wd_roots) {
1254 while ((k = hashmap_first_key(j->inotify_wd_roots)))
1255 remove_root_wd(j, PTR_TO_INT(k));
1257 hashmap_free(j->inotify_wd_roots);
1263 while ((f = hashmap_steal_first(j->files)))
1264 journal_file_close(f);
1266 hashmap_free(j->files);
1269 sd_journal_flush_matches(j);
1271 if (j->inotify_fd >= 0)
1272 close_nointr_nofail(j->inotify_fd);
1277 _public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
1287 f = j->current_file;
1289 return -EADDRNOTAVAIL;
1291 if (f->current_offset <= 0)
1292 return -EADDRNOTAVAIL;
1294 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1298 *ret = le64toh(o->entry.realtime);
1302 _public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
1313 f = j->current_file;
1315 return -EADDRNOTAVAIL;
1317 if (f->current_offset <= 0)
1318 return -EADDRNOTAVAIL;
1320 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1325 *ret_boot_id = o->entry.boot_id;
1327 r = sd_id128_get_boot(&id);
1331 if (!sd_id128_equal(id, o->entry.boot_id))
1335 *ret = le64toh(o->entry.monotonic);
1339 _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
1342 size_t field_length;
1355 if (isempty(field) || strchr(field, '='))
1358 f = j->current_file;
1360 return -EADDRNOTAVAIL;
1362 if (f->current_offset <= 0)
1363 return -EADDRNOTAVAIL;
1365 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1369 field_length = strlen(field);
1371 n = journal_file_entry_n_items(o);
1372 for (i = 0; i < n; i++) {
1377 p = le64toh(o->entry.items[i].object_offset);
1378 le_hash = o->entry.items[i].hash;
1379 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
1383 if (le_hash != o->data.hash)
1386 l = le64toh(o->object.size) - offsetof(Object, data.payload);
1388 if (o->object.flags & OBJECT_COMPRESSED) {
1391 if (uncompress_startswith(o->data.payload, l,
1392 &f->compress_buffer, &f->compress_buffer_size,
1393 field, field_length, '=')) {
1397 if (!uncompress_blob(o->data.payload, l,
1398 &f->compress_buffer, &f->compress_buffer_size, &rsize))
1401 *data = f->compress_buffer;
1402 *size = (size_t) rsize;
1407 return -EPROTONOSUPPORT;
1410 } else if (l >= field_length+1 &&
1411 memcmp(o->data.payload, field, field_length) == 0 &&
1412 o->data.payload[field_length] == '=') {
1416 if ((uint64_t) t != l)
1419 *data = o->data.payload;
1425 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1433 _public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
1448 f = j->current_file;
1450 return -EADDRNOTAVAIL;
1452 if (f->current_offset <= 0)
1453 return -EADDRNOTAVAIL;
1455 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1459 n = journal_file_entry_n_items(o);
1460 if (j->current_field >= n)
1463 p = le64toh(o->entry.items[j->current_field].object_offset);
1464 le_hash = o->entry.items[j->current_field].hash;
1465 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
1469 if (le_hash != o->data.hash)
1472 l = le64toh(o->object.size) - offsetof(Object, data.payload);
1475 /* We can't read objects larger than 4G on a 32bit machine */
1476 if ((uint64_t) t != l)
1479 if (o->object.flags & OBJECT_COMPRESSED) {
1483 if (!uncompress_blob(o->data.payload, l, &f->compress_buffer, &f->compress_buffer_size, &rsize))
1486 *data = f->compress_buffer;
1487 *size = (size_t) rsize;
1489 return -EPROTONOSUPPORT;
1492 *data = o->data.payload;
1496 j->current_field ++;
1501 _public_ void sd_journal_restart_data(sd_journal *j) {
1505 j->current_field = 0;
1508 _public_ int sd_journal_get_fd(sd_journal *j) {
1512 return j->inotify_fd;
1515 static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
1522 /* Is this a subdirectory we watch? */
1523 p = hashmap_get(j->inotify_wd_dirs, INT_TO_PTR(e->wd));
1526 if (!(e->mask & IN_ISDIR) && e->len > 0 && endswith(e->name, ".journal")) {
1528 /* Event for a journal file */
1530 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1531 r = add_file(j, p, NULL, e->name);
1533 log_debug("Failed to add file %s/%s: %s", p, e->name, strerror(-r));
1534 } else if (e->mask & (IN_DELETE|IN_UNMOUNT)) {
1536 r = remove_file(j, p, NULL, e->name);
1538 log_debug("Failed to remove file %s/%s: %s", p, e->name, strerror(-r));
1541 } else if (e->len == 0) {
1543 /* Event for the directory itself */
1545 if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT))
1546 remove_directory_wd(j, e->wd);
1552 /* Must be the root directory then? */
1553 p = hashmap_get(j->inotify_wd_roots, INT_TO_PTR(e->wd));
1557 if (!(e->mask & IN_ISDIR) && e->len > 0 && endswith(e->name, ".journal")) {
1559 /* Event for a journal file */
1561 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1562 r = add_file(j, p, NULL, e->name);
1564 log_debug("Failed to add file %s/%s: %s", p, e->name, strerror(-r));
1565 } else if (e->mask & (IN_DELETE|IN_UNMOUNT)) {
1567 r = remove_file(j, p, NULL, e->name);
1569 log_debug("Failed to remove file %s/%s: %s", p, e->name, strerror(-r));
1572 } else if ((e->mask & IN_ISDIR) && e->len > 0 && sd_id128_from_string(e->name, &id) >= 0) {
1574 /* Event for subdirectory */
1576 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1578 r = add_directory(j, p, e->name);
1580 log_debug("Failed to add directory %s/%s: %s", p, e->name, strerror(-r));
1587 if (e->mask & IN_IGNORED)
1590 log_warning("Unknown inotify event.");
1593 _public_ int sd_journal_process(sd_journal *j) {
1594 uint8_t buffer[sizeof(struct inotify_event) + FILENAME_MAX];
1600 struct inotify_event *e;
1603 l = read(j->inotify_fd, buffer, sizeof(buffer));
1605 if (errno == EINTR || errno == EAGAIN)
1611 e = (struct inotify_event*) buffer;
1615 process_inotify_event(j, e);
1617 step = sizeof(struct inotify_event) + e->len;
1618 assert(step <= (size_t) l);
1620 e = (struct inotify_event*) ((uint8_t*) e + step);
1626 _public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
1631 r = fd_wait_for_event(j->inotify_fd, POLLIN, timeout_usec);
1632 k = sd_journal_process(j);
1640 _public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to) {
1651 HASHMAP_FOREACH(f, j->files, i) {
1654 r = journal_file_get_cutoff_realtime_usec(f, &fr, &t);
1668 *from = MIN(fr, *from);
1674 return first ? 0 : 1;
1677 _public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t *from, uint64_t *to) {
1688 HASHMAP_FOREACH(f, j->files, i) {
1691 r = journal_file_get_cutoff_monotonic_usec(f, boot_id, &fr, &t);
1705 *from = MIN(fr, *from);
1711 return first ? 0 : 1;
1715 /* _public_ int sd_journal_query_unique(sd_journal *j, const char *field) { */
1717 /* return -EINVAL; */
1719 /* return -EINVAL; */
1721 /* return -ENOTSUP; */
1724 /* _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l) { */
1726 /* return -EINVAL; */
1728 /* return -EINVAL; */
1730 /* return -EINVAL; */
1732 /* return -ENOTSUP; */
1735 /* _public_ void sd_journal_restart_unique(sd_journal *j) { */