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"
36 #define JOURNAL_FILES_MAX 1024
38 typedef struct Match Match;
45 LIST_FIELDS(Match, matches);
48 typedef enum location_type {
54 typedef struct Location {
75 Location current_location;
76 JournalFile *current_file;
77 uint64_t current_field;
80 Hashmap *inotify_wd_dirs;
81 Hashmap *inotify_wd_roots;
83 LIST_HEAD(Match, matches);
87 static void detach_location(sd_journal *j) {
93 j->current_file = NULL;
96 HASHMAP_FOREACH(f, j->files, i)
97 f->current_offset = 0;
100 static void reset_location(sd_journal *j) {
104 zero(j->current_location);
107 static void init_location(Location *l, JournalFile *f, Object *o) {
110 assert(o->object.type == OBJECT_ENTRY);
112 l->type = LOCATION_DISCRETE;
113 l->seqnum = le64toh(o->entry.seqnum);
114 l->seqnum_id = f->header->seqnum_id;
115 l->realtime = le64toh(o->entry.realtime);
116 l->monotonic = le64toh(o->entry.monotonic);
117 l->boot_id = le64toh(o->entry.boot_id);
118 l->xor_hash = le64toh(o->entry.xor_hash);
120 l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true;
123 static void set_location(sd_journal *j, JournalFile *f, Object *o, uint64_t offset) {
128 init_location(&j->current_location, f, o);
131 j->current_field = 0;
133 f->current_offset = offset;
136 static int same_field(const void *_a, size_t s, const void *_b, size_t t) {
137 const uint8_t *a = _a, *b = _b;
139 bool a_good = false, b_good = false, different = false;
141 for (j = 0; j < s && j < t; j++) {
150 if (a_good && b_good)
151 return different ? 0 : 1;
157 int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
158 Match *m, *after = NULL;
168 le_hash = htole64(hash64(data, size));
170 LIST_FOREACH(matches, m, j->matches) {
173 if (m->le_hash == le_hash &&
175 memcmp(m->data, data, size) == 0)
178 r = same_field(data, size, m->data, m->size);
191 m->data = malloc(m->size);
197 memcpy(m->data, data, size);
198 m->le_hash = le_hash;
200 /* Matches for the same fields we order adjacent to each
202 LIST_INSERT_AFTER(Match, matches, j->matches, after, m);
210 void sd_journal_flush_matches(sd_journal *j) {
214 Match *m = j->matches;
216 LIST_REMOVE(Match, matches, j->matches, m);
226 static int compare_order(JournalFile *af, Object *ao,
227 JournalFile *bf, Object *bo) {
236 /* We operate on two different files here, hence we can access
237 * two objects at the same time, which we normally can't.
239 * If contents and timestamps match, these entries are
240 * identical, even if the seqnum does not match */
242 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id) &&
243 ao->entry.monotonic == bo->entry.monotonic &&
244 ao->entry.realtime == bo->entry.realtime &&
245 ao->entry.xor_hash == bo->entry.xor_hash)
248 if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
250 /* If this is from the same seqnum source, compare
252 a = le64toh(ao->entry.seqnum);
253 b = le64toh(bo->entry.seqnum);
260 /* Wow! This is weird, different data but the same
261 * seqnums? Something is borked, but let's make the
262 * best of it and compare by time. */
265 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
267 /* If the boot id matches compare monotonic time */
268 a = le64toh(ao->entry.monotonic);
269 b = le64toh(bo->entry.monotonic);
277 /* Otherwise compare UTC time */
278 a = le64toh(ao->entry.realtime);
279 b = le64toh(ao->entry.realtime);
286 /* Finally, compare by contents */
287 a = le64toh(ao->entry.xor_hash);
288 b = le64toh(ao->entry.xor_hash);
298 static int compare_with_location(JournalFile *af, Object *ao, Location *l) {
304 assert(l->type == LOCATION_DISCRETE);
306 if (l->monotonic_set &&
307 sd_id128_equal(ao->entry.boot_id, l->boot_id) &&
309 le64toh(ao->entry.realtime) == l->realtime &&
311 le64toh(ao->entry.xor_hash) == l->xor_hash)
315 sd_id128_equal(af->header->seqnum_id, l->seqnum_id)) {
317 a = le64toh(ao->entry.seqnum);
325 if (l->monotonic_set &&
326 sd_id128_equal(ao->entry.boot_id, l->boot_id)) {
328 a = le64toh(ao->entry.monotonic);
330 if (a < l->monotonic)
332 if (a > l->monotonic)
336 if (l->realtime_set) {
338 a = le64toh(ao->entry.realtime);
346 if (l->xor_hash_set) {
347 a = le64toh(ao->entry.xor_hash);
358 static int find_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
366 /* No matches is simple */
368 if (j->current_location.type == LOCATION_HEAD)
369 r = journal_file_next_entry(f, NULL, 0, DIRECTION_DOWN, &o, &p);
370 else if (j->current_location.type == LOCATION_TAIL)
371 r = journal_file_next_entry(f, NULL, 0, DIRECTION_UP, &o, &p);
372 else if (j->current_location.seqnum_set &&
373 sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
374 r = journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, &o, &p);
375 else if (j->current_location.monotonic_set)
376 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, &o, &p);
377 else if (j->current_location.realtime_set)
378 r = journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, &o, &p);
380 r = journal_file_next_entry(f, NULL, 0, direction, &o, &p);
386 Match *m, *term_match = NULL;
390 /* We have matches, first, let's jump to the monotonic
391 * position if we have any, since it implies a
394 if (j->current_location.type == LOCATION_DISCRETE &&
395 j->current_location.monotonic_set) {
397 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, &o, &p);
402 LIST_FOREACH(matches, m, j->matches) {
406 r = journal_file_find_data_object_with_hash(f, m->data, m->size, m->le_hash, &d, &dp);
410 if (j->current_location.type == LOCATION_HEAD)
411 r = journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_DOWN, &c, &cp);
412 else if (j->current_location.type == LOCATION_TAIL)
413 r = journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_UP, &c, &cp);
414 else if (j->current_location.seqnum_set &&
415 sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
416 r = journal_file_move_to_entry_by_seqnum_for_data(f, dp, j->current_location.seqnum, direction, &c, &cp);
417 else if (j->current_location.realtime_set)
418 r = journal_file_move_to_entry_by_realtime_for_data(f, dp, j->current_location.realtime, direction, &c, &cp);
420 r = journal_file_next_entry_for_data(f, NULL, 0, dp, direction, &c, &cp);
429 } else if (same_field(term_match->data, term_match->size, m->data, m->size)) {
431 /* Same field as previous match... */
434 /* Find the earliest of the OR matches */
437 (direction == DIRECTION_DOWN && cp < tp) ||
438 (direction == DIRECTION_UP && cp > tp)) {
447 /* Previous term is finished, did anything match? */
451 /* Find the last of the AND matches */
453 (direction == DIRECTION_DOWN && tp > p) ||
454 (direction == DIRECTION_UP && tp < p)) {
471 /* Last term is finished, did anything match? */
476 (direction == DIRECTION_DOWN && tp > p) ||
477 (direction == DIRECTION_UP && tp < p)) {
495 static int next_with_matches(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
509 /* No matches is easy */
511 r = journal_file_next_entry(f, c, cp, direction, &c, &cp);
522 /* So there are matches we have to adhere to, let's find the
523 * first entry that matches all of them */
527 bool found, term_result = false;
528 Match *m, *term_match = NULL;
531 n = journal_file_entry_n_items(c);
533 /* Make sure we don't match the entry we are starting
535 found = cp > *offset;
538 LIST_FOREACH(matches, m, j->matches) {
542 /* Let's check if this is the beginning of a
543 * new term, i.e. has a different field prefix
544 * as the preceeding match. */
548 } else if (!same_field(term_match->data, term_match->size, m->data, m->size)) {
556 for (k = 0; k < n; k++)
557 if (c->entry.items[k].hash == m->le_hash)
561 /* Hmm, didn't find any field that
562 * matched this rule, so ignore this
563 * match. Go on with next match */
569 /* Hmm, so, this field matched, let's remember
570 * where we'd have to try next, in case the other
571 * matches are not OK */
573 r = journal_file_next_entry_for_data(f, c, cp, le64toh(c->entry.items[k].object_offset), direction, &qo, &q);
576 if (direction == DIRECTION_DOWN) {
582 if (np == 0 || q < np) {
590 /* Check the last term */
591 if (term_match && !term_result)
594 /* Did this entry match against all matches? */
603 /* Did we find a subsequent entry? */
607 /* Hmm, ok, this entry only matched partially, so
608 * let's try another one */
614 static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
617 int compare_value, r;
622 if (f->current_offset > 0) {
623 cp = f->current_offset;
625 r = journal_file_move_to_object(f, OBJECT_ENTRY, cp, &c);
629 r = next_with_matches(j, f, direction, &c, &cp);
635 r = find_location(j, f, direction, &c, &cp);
645 if (j->current_location.type == LOCATION_DISCRETE) {
648 k = compare_with_location(f, c, &j->current_location);
649 if (direction == DIRECTION_DOWN)
650 found = k >= compare_value;
652 found = k <= -compare_value;
664 r = next_with_matches(j, f, direction, &c, &cp);
670 static int real_journal_next(sd_journal *j, direction_t direction) {
671 JournalFile *f, *new_current = NULL;
674 uint64_t new_offset = 0;
675 Object *new_entry = NULL;
679 HASHMAP_FOREACH(f, j->files, i) {
684 r = next_beyond_location(j, f, direction, &o, &p);
695 k = compare_order(f, o, new_current, new_entry);
697 if (direction == DIRECTION_DOWN)
713 set_location(j, new_current, new_entry, new_offset);
718 int sd_journal_next(sd_journal *j) {
719 return real_journal_next(j, DIRECTION_DOWN);
722 int sd_journal_previous(sd_journal *j) {
723 return real_journal_next(j, DIRECTION_UP);
726 int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
732 r = sd_journal_next(j);
746 int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
752 r = sd_journal_previous(j);
766 int sd_journal_get_cursor(sd_journal *j, char **cursor) {
769 char bid[33], sid[33];
774 if (!j->current_file || j->current_file->current_offset <= 0)
775 return -EADDRNOTAVAIL;
777 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
781 sd_id128_to_string(j->current_file->header->seqnum_id, sid);
782 sd_id128_to_string(o->entry.boot_id, bid);
785 "s=%s;i=%llx;b=%s;m=%llx;t=%llx;x=%llx;p=%s",
786 sid, (unsigned long long) le64toh(o->entry.seqnum),
787 bid, (unsigned long long) le64toh(o->entry.monotonic),
788 (unsigned long long) le64toh(o->entry.realtime),
789 (unsigned long long) le64toh(o->entry.xor_hash),
790 file_name_from_path(j->current_file->path)) < 0)
796 int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
800 unsigned long long seqnum, monotonic, realtime, xor_hash;
802 seqnum_id_set = false,
805 monotonic_set = false,
806 realtime_set = false,
807 xor_hash_set = false;
808 sd_id128_t seqnum_id, boot_id;
813 FOREACH_WORD_SEPARATOR(w, l, cursor, ";", state) {
817 if (l < 2 || w[1] != '=')
820 item = strndup(w, l);
827 seqnum_id_set = true;
828 k = sd_id128_from_string(w+2, &seqnum_id);
833 if (sscanf(w+2, "%llx", &seqnum) != 1)
839 k = sd_id128_from_string(w+2, &boot_id);
843 monotonic_set = true;
844 if (sscanf(w+2, "%llx", &monotonic) != 1)
850 if (sscanf(w+2, "%llx", &realtime) != 1)
856 if (sscanf(w+2, "%llx", &xor_hash) != 1)
867 if ((!seqnum_set || !seqnum_id_set) &&
868 (!monotonic_set || !boot_id_set) &&
874 j->current_location.type = LOCATION_DISCRETE;
877 j->current_location.realtime = (uint64_t) realtime;
878 j->current_location.realtime_set = true;
881 if (seqnum_set && seqnum_id_set) {
882 j->current_location.seqnum = (uint64_t) seqnum;
883 j->current_location.seqnum_id = seqnum_id;
884 j->current_location.seqnum_set = true;
887 if (monotonic_set && boot_id_set) {
888 j->current_location.monotonic = (uint64_t) monotonic;
889 j->current_location.boot_id = boot_id;
890 j->current_location.monotonic_set = true;
894 j->current_location.xor_hash = (uint64_t) xor_hash;
895 j->current_location.xor_hash_set = true;
901 int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
905 j->current_location.type = LOCATION_DISCRETE;
906 j->current_location.boot_id = boot_id;
907 j->current_location.monotonic = usec;
908 j->current_location.monotonic_set = true;
913 int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
917 j->current_location.type = LOCATION_DISCRETE;
918 j->current_location.realtime = usec;
919 j->current_location.realtime_set = true;
924 int sd_journal_seek_head(sd_journal *j) {
928 j->current_location.type = LOCATION_HEAD;
933 int sd_journal_seek_tail(sd_journal *j) {
937 j->current_location.type = LOCATION_TAIL;
942 static int add_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
952 fn = join(prefix, "/", dir, "/", filename, NULL);
954 fn = join(prefix, "/", filename, NULL);
959 if (hashmap_get(j->files, fn)) {
964 if (hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
965 log_debug("Too many open journal files, not adding %s, ignoring.", fn);
970 r = journal_file_open(fn, O_RDONLY, 0, NULL, &f);
980 /* journal_file_dump(f); */
982 r = hashmap_put(j->files, f->path, f);
984 journal_file_close(f);
988 log_debug("File %s got added.", f->path);
993 static int remove_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
1002 fn = join(prefix, "/", dir, "/", filename, NULL);
1004 fn = join(prefix, "/", filename, NULL);
1009 f = hashmap_get(j->files, fn);
1015 hashmap_remove(j->files, f->path);
1016 journal_file_close(f);
1018 log_debug("File %s got removed.", f->path);
1022 static int add_directory(sd_journal *j, const char *prefix, const char *dir) {
1032 fn = join(prefix, "/", dir, NULL);
1040 if (errno == ENOENT)
1046 wd = inotify_add_watch(j->inotify_fd, fn,
1047 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1048 IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|
1049 IN_DONT_FOLLOW|IN_ONLYDIR);
1051 if (hashmap_put(j->inotify_wd_dirs, INT_TO_PTR(wd), fn) < 0)
1052 inotify_rm_watch(j->inotify_fd, wd);
1060 struct dirent buf, *de;
1062 r = readdir_r(d, &buf, &de);
1066 if (!dirent_is_file_with_suffix(de, ".journal"))
1069 r = add_file(j, prefix, dir, de->d_name);
1071 log_debug("Failed to add file %s/%s/%s: %s", prefix, dir, de->d_name, strerror(-r));
1076 log_debug("Directory %s/%s got added.", prefix, dir);
1081 static void remove_directory_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_dirs, INT_TO_PTR(wd));
1093 log_debug("Directory %s got removed.", p);
1098 static void add_root_wd(sd_journal *j, const char *p) {
1105 wd = inotify_add_watch(j->inotify_fd, p,
1106 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1107 IN_DONT_FOLLOW|IN_ONLYDIR);
1112 if (!k || hashmap_put(j->inotify_wd_roots, INT_TO_PTR(wd), k) < 0) {
1113 inotify_rm_watch(j->inotify_fd, wd);
1118 static void remove_root_wd(sd_journal *j, int wd) {
1124 if (j->inotify_fd >= 0)
1125 inotify_rm_watch(j->inotify_fd, wd);
1127 p = hashmap_remove(j->inotify_wd_roots, INT_TO_PTR(wd));
1130 log_debug("Root %s got removed.", p);
1135 int sd_journal_open(sd_journal **ret) {
1138 const char search_paths[] =
1139 "/run/log/journal\0"
1140 "/var/log/journal\0";
1145 j = new0(sd_journal, 1);
1149 j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1150 if (j->inotify_fd < 0) {
1155 j->files = hashmap_new(string_hash_func, string_compare_func);
1161 j->inotify_wd_dirs = hashmap_new(trivial_hash_func, trivial_compare_func);
1162 j->inotify_wd_roots = hashmap_new(trivial_hash_func, trivial_compare_func);
1164 if (!j->inotify_wd_dirs || !j->inotify_wd_roots) {
1169 /* We ignore most errors here, since the idea is to only open
1170 * what's actually accessible, and ignore the rest. */
1172 NULSTR_FOREACH(p, search_paths) {
1177 if (errno != ENOENT)
1178 log_debug("Failed to open %s: %m", p);
1185 struct dirent buf, *de;
1188 r = readdir_r(d, &buf, &de);
1192 if (dirent_is_file_with_suffix(de, ".journal")) {
1193 r = add_file(j, p, NULL, de->d_name);
1195 log_debug("Failed to add file %s/%s: %s", p, de->d_name, strerror(-r));
1197 } else if ((de->d_type == DT_DIR || de->d_type == DT_UNKNOWN) &&
1198 sd_id128_from_string(de->d_name, &id) >= 0) {
1200 r = add_directory(j, p, de->d_name);
1202 log_debug("Failed to add directory %s/%s: %s", p, de->d_name, strerror(-r));
1213 sd_journal_close(j);
1218 void sd_journal_close(sd_journal *j) {
1221 if (j->inotify_wd_dirs) {
1224 while ((k = hashmap_first_key(j->inotify_wd_dirs)))
1225 remove_directory_wd(j, PTR_TO_INT(k));
1227 hashmap_free(j->inotify_wd_dirs);
1230 if (j->inotify_wd_roots) {
1233 while ((k = hashmap_first_key(j->inotify_wd_roots)))
1234 remove_root_wd(j, PTR_TO_INT(k));
1236 hashmap_free(j->inotify_wd_roots);
1242 while ((f = hashmap_steal_first(j->files)))
1243 journal_file_close(f);
1245 hashmap_free(j->files);
1248 sd_journal_flush_matches(j);
1250 if (j->inotify_fd >= 0)
1251 close_nointr_nofail(j->inotify_fd);
1256 int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
1264 f = j->current_file;
1266 return -EADDRNOTAVAIL;
1268 if (f->current_offset <= 0)
1269 return -EADDRNOTAVAIL;
1271 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1275 *ret = le64toh(o->entry.realtime);
1279 int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
1288 f = j->current_file;
1290 return -EADDRNOTAVAIL;
1292 if (f->current_offset <= 0)
1293 return -EADDRNOTAVAIL;
1295 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1300 *ret_boot_id = o->entry.boot_id;
1302 r = sd_id128_get_boot(&id);
1306 if (!sd_id128_equal(id, o->entry.boot_id))
1310 *ret = le64toh(o->entry.monotonic);
1314 int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
1317 size_t field_length;
1326 if (isempty(field) || strchr(field, '='))
1329 f = j->current_file;
1331 return -EADDRNOTAVAIL;
1333 if (f->current_offset <= 0)
1334 return -EADDRNOTAVAIL;
1336 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1340 field_length = strlen(field);
1342 n = journal_file_entry_n_items(o);
1343 for (i = 0; i < n; i++) {
1344 uint64_t p, l, le_hash;
1347 p = le64toh(o->entry.items[i].object_offset);
1348 le_hash = o->entry.items[i].hash;
1349 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
1353 if (le_hash != o->data.hash)
1356 l = le64toh(o->object.size) - offsetof(Object, data.payload);
1358 if (o->object.flags & OBJECT_COMPRESSED) {
1361 if (uncompress_startswith(o->data.payload, l,
1362 &f->compress_buffer, &f->compress_buffer_size,
1363 field, field_length, '=')) {
1367 if (!uncompress_blob(o->data.payload, l,
1368 &f->compress_buffer, &f->compress_buffer_size, &rsize))
1371 *data = f->compress_buffer;
1372 *size = (size_t) rsize;
1377 return -EPROTONOSUPPORT;
1380 } else if (l >= field_length+1 &&
1381 memcmp(o->data.payload, field, field_length) == 0 &&
1382 o->data.payload[field_length] == '=') {
1386 if ((uint64_t) t != l)
1389 *data = o->data.payload;
1395 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1403 int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
1405 uint64_t p, l, n, le_hash;
1414 f = j->current_file;
1416 return -EADDRNOTAVAIL;
1418 if (f->current_offset <= 0)
1419 return -EADDRNOTAVAIL;
1421 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1425 n = journal_file_entry_n_items(o);
1426 if (j->current_field >= n)
1429 p = le64toh(o->entry.items[j->current_field].object_offset);
1430 le_hash = o->entry.items[j->current_field].hash;
1431 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
1435 if (le_hash != o->data.hash)
1438 l = le64toh(o->object.size) - offsetof(Object, data.payload);
1441 /* We can't read objects larger than 4G on a 32bit machine */
1442 if ((uint64_t) t != l)
1445 if (o->object.flags & OBJECT_COMPRESSED) {
1449 if (!uncompress_blob(o->data.payload, l, &f->compress_buffer, &f->compress_buffer_size, &rsize))
1452 *data = f->compress_buffer;
1453 *size = (size_t) rsize;
1455 return -EPROTONOSUPPORT;
1458 *data = o->data.payload;
1462 j->current_field ++;
1467 void sd_journal_restart_data(sd_journal *j) {
1470 j->current_field = 0;
1473 int sd_journal_get_fd(sd_journal *j) {
1476 return j->inotify_fd;
1479 static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
1486 /* Is this a subdirectory we watch? */
1487 p = hashmap_get(j->inotify_wd_dirs, 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->len == 0) {
1507 /* Event for the directory itself */
1509 if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT))
1510 remove_directory_wd(j, e->wd);
1516 /* Must be the root directory then? */
1517 p = hashmap_get(j->inotify_wd_roots, INT_TO_PTR(e->wd));
1521 if (!(e->mask & IN_ISDIR) && e->len > 0 && endswith(e->name, ".journal")) {
1523 /* Event for a journal file */
1525 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1526 r = add_file(j, p, NULL, e->name);
1528 log_debug("Failed to add file %s/%s: %s", p, e->name, strerror(-r));
1529 } else if (e->mask & (IN_DELETE|IN_UNMOUNT)) {
1531 r = remove_file(j, p, NULL, e->name);
1533 log_debug("Failed to remove file %s/%s: %s", p, e->name, strerror(-r));
1536 } else if ((e->mask & IN_ISDIR) && e->len > 0 && sd_id128_from_string(e->name, &id) >= 0) {
1538 /* Event for subdirectory */
1540 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1542 r = add_directory(j, p, e->name);
1544 log_debug("Failed to add directory %s/%s: %s", p, e->name, strerror(-r));
1551 if (e->mask & IN_IGNORED)
1554 log_warning("Unknown inotify event.");
1557 int sd_journal_process(sd_journal *j) {
1558 uint8_t buffer[sizeof(struct inotify_event) + FILENAME_MAX];
1563 struct inotify_event *e;
1566 l = read(j->inotify_fd, buffer, sizeof(buffer));
1568 if (errno == EINTR || errno == EAGAIN)
1574 e = (struct inotify_event*) buffer;
1578 process_inotify_event(j, e);
1580 step = sizeof(struct inotify_event) + e->len;
1581 assert(step <= (size_t) l);
1583 e = (struct inotify_event*) ((uint8_t*) e + step);
1589 int sd_journal_query_unique(sd_journal *j, const char *field) {
1596 int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l) {
1602 void sd_journal_restart_unique(sd_journal *j) {