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 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.
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.
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/>.
26 #include <sys/inotify.h>
28 #include "sd-journal.h"
29 #include "journal-def.h"
30 #include "journal-file.h"
35 #include "journal-internal.h"
37 #define JOURNAL_FILES_MAX 1024
39 static void detach_location(sd_journal *j) {
45 j->current_file = NULL;
48 HASHMAP_FOREACH(f, j->files, i)
49 f->current_offset = 0;
52 static void reset_location(sd_journal *j) {
56 zero(j->current_location);
59 static void init_location(Location *l, JournalFile *f, Object *o) {
62 assert(o->object.type == OBJECT_ENTRY);
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);
72 l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true;
75 static void set_location(sd_journal *j, JournalFile *f, Object *o, uint64_t offset) {
80 init_location(&j->current_location, f, o);
85 f->current_offset = offset;
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;
91 bool a_good = false, b_good = false, different = false;
93 for (j = 0; j < s && j < t; j++) {
102 if (a_good && b_good)
103 return different ? 0 : 1;
109 int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
110 Match *m, *after = NULL;
120 le_hash = htole64(hash64(data, size));
122 LIST_FOREACH(matches, m, j->matches) {
125 if (m->le_hash == le_hash &&
127 memcmp(m->data, data, size) == 0)
130 r = same_field(data, size, m->data, m->size);
143 m->data = malloc(m->size);
149 memcpy(m->data, data, size);
150 m->le_hash = le_hash;
152 /* Matches for the same fields we order adjacent to each
154 LIST_INSERT_AFTER(Match, matches, j->matches, after, m);
162 void sd_journal_flush_matches(sd_journal *j) {
166 Match *m = j->matches;
168 LIST_REMOVE(Match, matches, j->matches, m);
178 static int compare_order(JournalFile *af, Object *ao,
179 JournalFile *bf, Object *bo) {
188 /* We operate on two different files here, hence we can access
189 * two objects at the same time, which we normally can't.
191 * If contents and timestamps match, these entries are
192 * identical, even if the seqnum does not match */
194 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id) &&
195 ao->entry.monotonic == bo->entry.monotonic &&
196 ao->entry.realtime == bo->entry.realtime &&
197 ao->entry.xor_hash == bo->entry.xor_hash)
200 if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
202 /* If this is from the same seqnum source, compare
204 a = le64toh(ao->entry.seqnum);
205 b = le64toh(bo->entry.seqnum);
212 /* Wow! This is weird, different data but the same
213 * seqnums? Something is borked, but let's make the
214 * best of it and compare by time. */
217 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
219 /* If the boot id matches compare monotonic time */
220 a = le64toh(ao->entry.monotonic);
221 b = le64toh(bo->entry.monotonic);
229 /* Otherwise compare UTC time */
230 a = le64toh(ao->entry.realtime);
231 b = le64toh(ao->entry.realtime);
238 /* Finally, compare by contents */
239 a = le64toh(ao->entry.xor_hash);
240 b = le64toh(ao->entry.xor_hash);
250 static int compare_with_location(JournalFile *af, Object *ao, Location *l) {
256 assert(l->type == LOCATION_DISCRETE);
258 if (l->monotonic_set &&
259 sd_id128_equal(ao->entry.boot_id, l->boot_id) &&
261 le64toh(ao->entry.realtime) == l->realtime &&
263 le64toh(ao->entry.xor_hash) == l->xor_hash)
267 sd_id128_equal(af->header->seqnum_id, l->seqnum_id)) {
269 a = le64toh(ao->entry.seqnum);
277 if (l->monotonic_set &&
278 sd_id128_equal(ao->entry.boot_id, l->boot_id)) {
280 a = le64toh(ao->entry.monotonic);
282 if (a < l->monotonic)
284 if (a > l->monotonic)
288 if (l->realtime_set) {
290 a = le64toh(ao->entry.realtime);
298 if (l->xor_hash_set) {
299 a = le64toh(ao->entry.xor_hash);
310 static int find_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
318 /* No matches is simple */
320 if (j->current_location.type == LOCATION_HEAD)
321 r = journal_file_next_entry(f, NULL, 0, DIRECTION_DOWN, &o, &p);
322 else if (j->current_location.type == LOCATION_TAIL)
323 r = journal_file_next_entry(f, NULL, 0, DIRECTION_UP, &o, &p);
324 else if (j->current_location.seqnum_set &&
325 sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
326 r = journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, &o, &p);
327 else if (j->current_location.monotonic_set)
328 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, &o, &p);
329 else if (j->current_location.realtime_set)
330 r = journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, &o, &p);
332 r = journal_file_next_entry(f, NULL, 0, direction, &o, &p);
338 Match *m, *term_match = NULL;
342 /* We have matches, first, let's jump to the monotonic
343 * position if we have any, since it implies a
346 if (j->current_location.type == LOCATION_DISCRETE &&
347 j->current_location.monotonic_set) {
349 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, &o, &p);
354 LIST_FOREACH(matches, m, j->matches) {
358 r = journal_file_find_data_object_with_hash(f, m->data, m->size, m->le_hash, &d, &dp);
362 if (j->current_location.type == LOCATION_HEAD)
363 r = journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_DOWN, &c, &cp);
364 else if (j->current_location.type == LOCATION_TAIL)
365 r = journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_UP, &c, &cp);
366 else if (j->current_location.seqnum_set &&
367 sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
368 r = journal_file_move_to_entry_by_seqnum_for_data(f, dp, j->current_location.seqnum, direction, &c, &cp);
369 else if (j->current_location.realtime_set)
370 r = journal_file_move_to_entry_by_realtime_for_data(f, dp, j->current_location.realtime, direction, &c, &cp);
372 r = journal_file_next_entry_for_data(f, NULL, 0, dp, direction, &c, &cp);
381 } else if (same_field(term_match->data, term_match->size, m->data, m->size)) {
383 /* Same field as previous match... */
386 /* Find the earliest of the OR matches */
389 (direction == DIRECTION_DOWN && cp < tp) ||
390 (direction == DIRECTION_UP && cp > tp)) {
399 /* Previous term is finished, did anything match? */
403 /* Find the last of the AND matches */
405 (direction == DIRECTION_DOWN && tp > p) ||
406 (direction == DIRECTION_UP && tp < p)) {
423 /* Last term is finished, did anything match? */
428 (direction == DIRECTION_DOWN && tp > p) ||
429 (direction == DIRECTION_UP && tp < p)) {
447 static int next_with_matches(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
461 /* No matches is easy */
463 r = journal_file_next_entry(f, c, cp, direction, &c, &cp);
474 /* So there are matches we have to adhere to, let's find the
475 * first entry that matches all of them */
479 bool found, term_result = false;
480 Match *m, *term_match = NULL;
483 n = journal_file_entry_n_items(c);
485 /* Make sure we don't match the entry we are starting
487 found = cp > *offset;
490 LIST_FOREACH(matches, m, j->matches) {
494 /* Let's check if this is the beginning of a
495 * new term, i.e. has a different field prefix
496 * as the preceeding match. */
500 } else if (!same_field(term_match->data, term_match->size, m->data, m->size)) {
508 for (k = 0; k < n; k++)
509 if (c->entry.items[k].hash == m->le_hash)
513 /* Hmm, didn't find any field that
514 * matched this rule, so ignore this
515 * match. Go on with next match */
521 /* Hmm, so, this field matched, let's remember
522 * where we'd have to try next, in case the other
523 * matches are not OK */
525 r = journal_file_next_entry_for_data(f, c, cp, le64toh(c->entry.items[k].object_offset), direction, &qo, &q);
528 if (direction == DIRECTION_DOWN) {
534 if (np == 0 || q < np) {
542 /* Check the last term */
543 if (term_match && !term_result)
546 /* Did this entry match against all matches? */
555 /* Did we find a subsequent entry? */
559 /* Hmm, ok, this entry only matched partially, so
560 * let's try another one */
566 static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
569 int compare_value, r;
574 if (f->current_offset > 0) {
575 cp = f->current_offset;
577 r = journal_file_move_to_object(f, OBJECT_ENTRY, cp, &c);
581 r = next_with_matches(j, f, direction, &c, &cp);
587 r = find_location(j, f, direction, &c, &cp);
597 if (j->current_location.type == LOCATION_DISCRETE) {
600 k = compare_with_location(f, c, &j->current_location);
601 if (direction == DIRECTION_DOWN)
602 found = k >= compare_value;
604 found = k <= -compare_value;
616 r = next_with_matches(j, f, direction, &c, &cp);
622 static int real_journal_next(sd_journal *j, direction_t direction) {
623 JournalFile *f, *new_current = NULL;
626 uint64_t new_offset = 0;
627 Object *new_entry = NULL;
631 HASHMAP_FOREACH(f, j->files, i) {
636 r = next_beyond_location(j, f, direction, &o, &p);
647 k = compare_order(f, o, new_current, new_entry);
649 if (direction == DIRECTION_DOWN)
665 set_location(j, new_current, new_entry, new_offset);
670 int sd_journal_next(sd_journal *j) {
671 return real_journal_next(j, DIRECTION_DOWN);
674 int sd_journal_previous(sd_journal *j) {
675 return real_journal_next(j, DIRECTION_UP);
678 int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
684 r = sd_journal_next(j);
698 int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
704 r = sd_journal_previous(j);
718 int sd_journal_get_cursor(sd_journal *j, char **cursor) {
721 char bid[33], sid[33];
726 if (!j->current_file || j->current_file->current_offset <= 0)
727 return -EADDRNOTAVAIL;
729 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
733 sd_id128_to_string(j->current_file->header->seqnum_id, sid);
734 sd_id128_to_string(o->entry.boot_id, bid);
737 "s=%s;i=%llx;b=%s;m=%llx;t=%llx;x=%llx;p=%s",
738 sid, (unsigned long long) le64toh(o->entry.seqnum),
739 bid, (unsigned long long) le64toh(o->entry.monotonic),
740 (unsigned long long) le64toh(o->entry.realtime),
741 (unsigned long long) le64toh(o->entry.xor_hash),
742 file_name_from_path(j->current_file->path)) < 0)
748 int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
752 unsigned long long seqnum, monotonic, realtime, xor_hash;
754 seqnum_id_set = false,
757 monotonic_set = false,
758 realtime_set = false,
759 xor_hash_set = false;
760 sd_id128_t seqnum_id, boot_id;
765 FOREACH_WORD_SEPARATOR(w, l, cursor, ";", state) {
769 if (l < 2 || w[1] != '=')
772 item = strndup(w, l);
779 seqnum_id_set = true;
780 k = sd_id128_from_string(w+2, &seqnum_id);
785 if (sscanf(w+2, "%llx", &seqnum) != 1)
791 k = sd_id128_from_string(w+2, &boot_id);
795 monotonic_set = true;
796 if (sscanf(w+2, "%llx", &monotonic) != 1)
802 if (sscanf(w+2, "%llx", &realtime) != 1)
808 if (sscanf(w+2, "%llx", &xor_hash) != 1)
819 if ((!seqnum_set || !seqnum_id_set) &&
820 (!monotonic_set || !boot_id_set) &&
826 j->current_location.type = LOCATION_DISCRETE;
829 j->current_location.realtime = (uint64_t) realtime;
830 j->current_location.realtime_set = true;
833 if (seqnum_set && seqnum_id_set) {
834 j->current_location.seqnum = (uint64_t) seqnum;
835 j->current_location.seqnum_id = seqnum_id;
836 j->current_location.seqnum_set = true;
839 if (monotonic_set && boot_id_set) {
840 j->current_location.monotonic = (uint64_t) monotonic;
841 j->current_location.boot_id = boot_id;
842 j->current_location.monotonic_set = true;
846 j->current_location.xor_hash = (uint64_t) xor_hash;
847 j->current_location.xor_hash_set = true;
853 int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
857 j->current_location.type = LOCATION_DISCRETE;
858 j->current_location.boot_id = boot_id;
859 j->current_location.monotonic = usec;
860 j->current_location.monotonic_set = true;
865 int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
869 j->current_location.type = LOCATION_DISCRETE;
870 j->current_location.realtime = usec;
871 j->current_location.realtime_set = true;
876 int sd_journal_seek_head(sd_journal *j) {
880 j->current_location.type = LOCATION_HEAD;
885 int sd_journal_seek_tail(sd_journal *j) {
889 j->current_location.type = LOCATION_TAIL;
894 static int add_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
903 if ((j->flags & SD_JOURNAL_SYSTEM_ONLY) &&
904 !startswith(filename, "system.journal"))
908 fn = join(prefix, "/", dir, "/", filename, NULL);
910 fn = join(prefix, "/", filename, NULL);
915 if (hashmap_get(j->files, fn)) {
920 if (hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
921 log_debug("Too many open journal files, not adding %s, ignoring.", fn);
926 r = journal_file_open(fn, O_RDONLY, 0, NULL, &f);
936 /* journal_file_dump(f); */
938 r = hashmap_put(j->files, f->path, f);
940 journal_file_close(f);
944 log_debug("File %s got added.", f->path);
949 static int remove_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
958 fn = join(prefix, "/", dir, "/", filename, NULL);
960 fn = join(prefix, "/", filename, NULL);
965 f = hashmap_get(j->files, fn);
971 hashmap_remove(j->files, f->path);
972 journal_file_close(f);
974 log_debug("File %s got removed.", f->path);
978 static int add_directory(sd_journal *j, const char *prefix, const char *dir) {
989 if ((j->flags & SD_JOURNAL_LOCAL_ONLY) &&
990 (sd_id128_from_string(dir, &id) < 0 ||
991 sd_id128_get_machine(&mid) < 0 ||
992 !sd_id128_equal(id, mid)))
995 fn = join(prefix, "/", dir, NULL);
1003 if (errno == ENOENT)
1009 wd = inotify_add_watch(j->inotify_fd, fn,
1010 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1011 IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|
1012 IN_DONT_FOLLOW|IN_ONLYDIR);
1014 if (hashmap_put(j->inotify_wd_dirs, INT_TO_PTR(wd), fn) < 0)
1015 inotify_rm_watch(j->inotify_fd, wd);
1023 struct dirent buf, *de;
1025 r = readdir_r(d, &buf, &de);
1029 if (!dirent_is_file_with_suffix(de, ".journal"))
1032 r = add_file(j, prefix, dir, de->d_name);
1034 log_debug("Failed to add file %s/%s/%s: %s", prefix, dir, de->d_name, strerror(-r));
1039 log_debug("Directory %s/%s got added.", prefix, dir);
1044 static void remove_directory_wd(sd_journal *j, int wd) {
1050 if (j->inotify_fd >= 0)
1051 inotify_rm_watch(j->inotify_fd, wd);
1053 p = hashmap_remove(j->inotify_wd_dirs, INT_TO_PTR(wd));
1056 log_debug("Directory %s got removed.", p);
1061 static void add_root_wd(sd_journal *j, const char *p) {
1068 wd = inotify_add_watch(j->inotify_fd, p,
1069 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1070 IN_DONT_FOLLOW|IN_ONLYDIR);
1075 if (!k || hashmap_put(j->inotify_wd_roots, INT_TO_PTR(wd), k) < 0) {
1076 inotify_rm_watch(j->inotify_fd, wd);
1081 static void remove_root_wd(sd_journal *j, int wd) {
1087 if (j->inotify_fd >= 0)
1088 inotify_rm_watch(j->inotify_fd, wd);
1090 p = hashmap_remove(j->inotify_wd_roots, INT_TO_PTR(wd));
1093 log_debug("Root %s got removed.", p);
1098 int sd_journal_open(sd_journal **ret, int flags) {
1101 const char search_paths[] =
1102 "/run/log/journal\0"
1103 "/var/log/journal\0";
1108 j = new0(sd_journal, 1);
1114 j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1115 if (j->inotify_fd < 0) {
1120 j->files = hashmap_new(string_hash_func, string_compare_func);
1126 j->inotify_wd_dirs = hashmap_new(trivial_hash_func, trivial_compare_func);
1127 j->inotify_wd_roots = hashmap_new(trivial_hash_func, trivial_compare_func);
1129 if (!j->inotify_wd_dirs || !j->inotify_wd_roots) {
1134 /* We ignore most errors here, since the idea is to only open
1135 * what's actually accessible, and ignore the rest. */
1137 NULSTR_FOREACH(p, search_paths) {
1140 if ((flags & SD_JOURNAL_RUNTIME_ONLY) &&
1141 !path_startswith(p, "/run"))
1146 if (errno != ENOENT)
1147 log_debug("Failed to open %s: %m", p);
1154 struct dirent buf, *de;
1157 r = readdir_r(d, &buf, &de);
1161 if (dirent_is_file_with_suffix(de, ".journal")) {
1162 r = add_file(j, p, NULL, de->d_name);
1164 log_debug("Failed to add file %s/%s: %s", p, de->d_name, strerror(-r));
1166 } else if ((de->d_type == DT_DIR || de->d_type == DT_UNKNOWN) &&
1167 sd_id128_from_string(de->d_name, &id) >= 0) {
1169 r = add_directory(j, p, de->d_name);
1171 log_debug("Failed to add directory %s/%s: %s", p, de->d_name, strerror(-r));
1182 sd_journal_close(j);
1187 void sd_journal_close(sd_journal *j) {
1190 if (j->inotify_wd_dirs) {
1193 while ((k = hashmap_first_key(j->inotify_wd_dirs)))
1194 remove_directory_wd(j, PTR_TO_INT(k));
1196 hashmap_free(j->inotify_wd_dirs);
1199 if (j->inotify_wd_roots) {
1202 while ((k = hashmap_first_key(j->inotify_wd_roots)))
1203 remove_root_wd(j, PTR_TO_INT(k));
1205 hashmap_free(j->inotify_wd_roots);
1211 while ((f = hashmap_steal_first(j->files)))
1212 journal_file_close(f);
1214 hashmap_free(j->files);
1217 sd_journal_flush_matches(j);
1219 if (j->inotify_fd >= 0)
1220 close_nointr_nofail(j->inotify_fd);
1225 int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
1233 f = j->current_file;
1235 return -EADDRNOTAVAIL;
1237 if (f->current_offset <= 0)
1238 return -EADDRNOTAVAIL;
1240 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1244 *ret = le64toh(o->entry.realtime);
1248 int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
1257 f = j->current_file;
1259 return -EADDRNOTAVAIL;
1261 if (f->current_offset <= 0)
1262 return -EADDRNOTAVAIL;
1264 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1269 *ret_boot_id = o->entry.boot_id;
1271 r = sd_id128_get_boot(&id);
1275 if (!sd_id128_equal(id, o->entry.boot_id))
1279 *ret = le64toh(o->entry.monotonic);
1283 int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
1286 size_t field_length;
1295 if (isempty(field) || strchr(field, '='))
1298 f = j->current_file;
1300 return -EADDRNOTAVAIL;
1302 if (f->current_offset <= 0)
1303 return -EADDRNOTAVAIL;
1305 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1309 field_length = strlen(field);
1311 n = journal_file_entry_n_items(o);
1312 for (i = 0; i < n; i++) {
1313 uint64_t p, l, le_hash;
1316 p = le64toh(o->entry.items[i].object_offset);
1317 le_hash = o->entry.items[i].hash;
1318 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
1322 if (le_hash != o->data.hash)
1325 l = le64toh(o->object.size) - offsetof(Object, data.payload);
1327 if (o->object.flags & OBJECT_COMPRESSED) {
1330 if (uncompress_startswith(o->data.payload, l,
1331 &f->compress_buffer, &f->compress_buffer_size,
1332 field, field_length, '=')) {
1336 if (!uncompress_blob(o->data.payload, l,
1337 &f->compress_buffer, &f->compress_buffer_size, &rsize))
1340 *data = f->compress_buffer;
1341 *size = (size_t) rsize;
1346 return -EPROTONOSUPPORT;
1349 } else if (l >= field_length+1 &&
1350 memcmp(o->data.payload, field, field_length) == 0 &&
1351 o->data.payload[field_length] == '=') {
1355 if ((uint64_t) t != l)
1358 *data = o->data.payload;
1364 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1372 int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
1374 uint64_t p, l, n, le_hash;
1383 f = j->current_file;
1385 return -EADDRNOTAVAIL;
1387 if (f->current_offset <= 0)
1388 return -EADDRNOTAVAIL;
1390 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1394 n = journal_file_entry_n_items(o);
1395 if (j->current_field >= n)
1398 p = le64toh(o->entry.items[j->current_field].object_offset);
1399 le_hash = o->entry.items[j->current_field].hash;
1400 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
1404 if (le_hash != o->data.hash)
1407 l = le64toh(o->object.size) - offsetof(Object, data.payload);
1410 /* We can't read objects larger than 4G on a 32bit machine */
1411 if ((uint64_t) t != l)
1414 if (o->object.flags & OBJECT_COMPRESSED) {
1418 if (!uncompress_blob(o->data.payload, l, &f->compress_buffer, &f->compress_buffer_size, &rsize))
1421 *data = f->compress_buffer;
1422 *size = (size_t) rsize;
1424 return -EPROTONOSUPPORT;
1427 *data = o->data.payload;
1431 j->current_field ++;
1436 void sd_journal_restart_data(sd_journal *j) {
1439 j->current_field = 0;
1442 int sd_journal_get_fd(sd_journal *j) {
1445 return j->inotify_fd;
1448 static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
1455 /* Is this a subdirectory we watch? */
1456 p = hashmap_get(j->inotify_wd_dirs, INT_TO_PTR(e->wd));
1459 if (!(e->mask & IN_ISDIR) && e->len > 0 && endswith(e->name, ".journal")) {
1461 /* Event for a journal file */
1463 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1464 r = add_file(j, p, NULL, e->name);
1466 log_debug("Failed to add file %s/%s: %s", p, e->name, strerror(-r));
1467 } else if (e->mask & (IN_DELETE|IN_UNMOUNT)) {
1469 r = remove_file(j, p, NULL, e->name);
1471 log_debug("Failed to remove file %s/%s: %s", p, e->name, strerror(-r));
1474 } else if (e->len == 0) {
1476 /* Event for the directory itself */
1478 if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT))
1479 remove_directory_wd(j, e->wd);
1485 /* Must be the root directory then? */
1486 p = hashmap_get(j->inotify_wd_roots, INT_TO_PTR(e->wd));
1490 if (!(e->mask & IN_ISDIR) && e->len > 0 && endswith(e->name, ".journal")) {
1492 /* Event for a journal file */
1494 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1495 r = add_file(j, p, NULL, e->name);
1497 log_debug("Failed to add file %s/%s: %s", p, e->name, strerror(-r));
1498 } else if (e->mask & (IN_DELETE|IN_UNMOUNT)) {
1500 r = remove_file(j, p, NULL, e->name);
1502 log_debug("Failed to remove file %s/%s: %s", p, e->name, strerror(-r));
1505 } else if ((e->mask & IN_ISDIR) && e->len > 0 && sd_id128_from_string(e->name, &id) >= 0) {
1507 /* Event for subdirectory */
1509 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1511 r = add_directory(j, p, e->name);
1513 log_debug("Failed to add directory %s/%s: %s", p, e->name, strerror(-r));
1520 if (e->mask & IN_IGNORED)
1523 log_warning("Unknown inotify event.");
1526 int sd_journal_process(sd_journal *j) {
1527 uint8_t buffer[sizeof(struct inotify_event) + FILENAME_MAX];
1532 struct inotify_event *e;
1535 l = read(j->inotify_fd, buffer, sizeof(buffer));
1537 if (errno == EINTR || errno == EAGAIN)
1543 e = (struct inotify_event*) buffer;
1547 process_inotify_event(j, e);
1549 step = sizeof(struct inotify_event) + e->len;
1550 assert(step <= (size_t) l);
1552 e = (struct inotify_event*) ((uint8_t*) e + step);
1558 int sd_journal_query_unique(sd_journal *j, const char *field) {
1565 int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l) {
1571 void sd_journal_restart_unique(sd_journal *j) {