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 = 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 _public_ 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 _public_ void sd_journal_flush_matches(sd_journal *j) {
167 Match *m = j->matches;
169 LIST_REMOVE(Match, matches, j->matches, m);
179 static int compare_order(JournalFile *af, Object *ao,
180 JournalFile *bf, Object *bo) {
189 /* We operate on two different files here, hence we can access
190 * two objects at the same time, which we normally can't.
192 * If contents and timestamps match, these entries are
193 * identical, even if the seqnum does not match */
195 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id) &&
196 ao->entry.monotonic == bo->entry.monotonic &&
197 ao->entry.realtime == bo->entry.realtime &&
198 ao->entry.xor_hash == bo->entry.xor_hash)
201 if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
203 /* If this is from the same seqnum source, compare
205 a = le64toh(ao->entry.seqnum);
206 b = le64toh(bo->entry.seqnum);
213 /* Wow! This is weird, different data but the same
214 * seqnums? Something is borked, but let's make the
215 * best of it and compare by time. */
218 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
220 /* If the boot id matches compare monotonic time */
221 a = le64toh(ao->entry.monotonic);
222 b = le64toh(bo->entry.monotonic);
230 /* Otherwise compare UTC time */
231 a = le64toh(ao->entry.realtime);
232 b = le64toh(ao->entry.realtime);
239 /* Finally, compare by contents */
240 a = le64toh(ao->entry.xor_hash);
241 b = le64toh(ao->entry.xor_hash);
251 static int compare_with_location(JournalFile *af, Object *ao, Location *l) {
257 assert(l->type == LOCATION_DISCRETE);
259 if (l->monotonic_set &&
260 sd_id128_equal(ao->entry.boot_id, l->boot_id) &&
262 le64toh(ao->entry.realtime) == l->realtime &&
264 le64toh(ao->entry.xor_hash) == l->xor_hash)
268 sd_id128_equal(af->header->seqnum_id, l->seqnum_id)) {
270 a = le64toh(ao->entry.seqnum);
278 if (l->monotonic_set &&
279 sd_id128_equal(ao->entry.boot_id, l->boot_id)) {
281 a = le64toh(ao->entry.monotonic);
283 if (a < l->monotonic)
285 if (a > l->monotonic)
289 if (l->realtime_set) {
291 a = le64toh(ao->entry.realtime);
299 if (l->xor_hash_set) {
300 a = le64toh(ao->entry.xor_hash);
311 static int find_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
319 /* No matches is simple */
321 if (j->current_location.type == LOCATION_HEAD)
322 r = journal_file_next_entry(f, NULL, 0, DIRECTION_DOWN, &o, &p);
323 else if (j->current_location.type == LOCATION_TAIL)
324 r = journal_file_next_entry(f, NULL, 0, DIRECTION_UP, &o, &p);
325 else if (j->current_location.seqnum_set &&
326 sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
327 r = journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, &o, &p);
328 else if (j->current_location.monotonic_set)
329 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, &o, &p);
330 else if (j->current_location.realtime_set)
331 r = journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, &o, &p);
333 r = journal_file_next_entry(f, NULL, 0, direction, &o, &p);
339 Match *m, *term_match = NULL;
343 /* We have matches, first, let's jump to the monotonic
344 * position if we have any, since it implies a
347 if (j->current_location.type == LOCATION_DISCRETE &&
348 j->current_location.monotonic_set) {
350 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, &o, &p);
352 return r == -ENOENT ? 0 : r;
355 LIST_FOREACH(matches, m, j->matches) {
359 r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), &d, &dp);
363 if (j->current_location.type == LOCATION_HEAD)
364 r = journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_DOWN, &c, &cp);
365 else if (j->current_location.type == LOCATION_TAIL)
366 r = journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_UP, &c, &cp);
367 else if (j->current_location.seqnum_set &&
368 sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
369 r = journal_file_move_to_entry_by_seqnum_for_data(f, dp, j->current_location.seqnum, direction, &c, &cp);
370 else if (j->current_location.realtime_set)
371 r = journal_file_move_to_entry_by_realtime_for_data(f, dp, j->current_location.realtime, direction, &c, &cp);
373 r = journal_file_next_entry_for_data(f, NULL, 0, dp, direction, &c, &cp);
385 } else if (same_field(term_match->data, term_match->size, m->data, m->size)) {
387 /* Same field as previous match... */
390 /* Find the earliest of the OR matches */
393 (direction == DIRECTION_DOWN && cp < tp) ||
394 (direction == DIRECTION_UP && cp > tp)) {
403 /* Previous term is finished, did anything match? */
407 /* Find the last of the AND matches */
409 (direction == DIRECTION_DOWN && tp > p) ||
410 (direction == DIRECTION_UP && tp < p)) {
427 /* Last term is finished, did anything match? */
432 (direction == DIRECTION_DOWN && tp > p) ||
433 (direction == DIRECTION_UP && tp < p)) {
451 static int next_with_matches(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
465 /* No matches is easy */
467 r = journal_file_next_entry(f, c, cp, direction, &c, &cp);
478 /* So there are matches we have to adhere to, let's find the
479 * first entry that matches all of them */
483 bool found, term_result = false;
484 Match *m, *term_match = NULL;
487 n = journal_file_entry_n_items(c);
489 /* Make sure we don't match the entry we are starting
491 found = cp != *offset;
494 LIST_FOREACH(matches, m, j->matches) {
498 /* Let's check if this is the beginning of a
499 * new term, i.e. has a different field prefix
500 * as the preceeding match. */
504 } else if (!same_field(term_match->data, term_match->size, m->data, m->size)) {
512 for (k = 0; k < n; k++)
513 if (c->entry.items[k].hash == m->le_hash)
517 /* Hmm, didn't find any field that
518 * matched this rule, so ignore this
519 * match. Go on with next match */
525 /* Hmm, so, this field matched, let's remember
526 * where we'd have to try next, in case the other
527 * matches are not OK */
529 r = journal_file_next_entry_for_data(f, c, cp, le64toh(c->entry.items[k].object_offset), direction, &qo, &q);
530 /* This pointer is invalidated if the window was
531 * remapped. May need to re-fetch it later */
538 if (direction == DIRECTION_DOWN) {
544 if (np == 0 || q < np) {
552 /* Check the last term */
553 if (term_match && !term_result)
556 /* Did this entry match against all matches? */
560 /* Re-fetch the entry */
561 r = journal_file_move_to_object(f, OBJECT_ENTRY, cp, &c);
572 /* Did we find a subsequent entry? */
576 /* Hmm, ok, this entry only matched partially, so
577 * let's try another one */
583 static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
586 int compare_value, r;
591 if (f->current_offset > 0) {
592 cp = f->current_offset;
594 r = journal_file_move_to_object(f, OBJECT_ENTRY, cp, &c);
598 r = next_with_matches(j, f, direction, &c, &cp);
604 r = find_location(j, f, direction, &c, &cp);
614 if (j->current_location.type == LOCATION_DISCRETE) {
617 k = compare_with_location(f, c, &j->current_location);
618 if (direction == DIRECTION_DOWN)
619 found = k >= compare_value;
621 found = k <= -compare_value;
633 r = next_with_matches(j, f, direction, &c, &cp);
639 static int real_journal_next(sd_journal *j, direction_t direction) {
640 JournalFile *f, *new_current = NULL;
643 uint64_t new_offset = 0;
644 Object *new_entry = NULL;
649 HASHMAP_FOREACH(f, j->files, i) {
654 r = next_beyond_location(j, f, direction, &o, &p);
665 k = compare_order(f, o, new_current, new_entry);
667 if (direction == DIRECTION_DOWN)
683 set_location(j, new_current, new_entry, new_offset);
688 _public_ int sd_journal_next(sd_journal *j) {
689 return real_journal_next(j, DIRECTION_DOWN);
692 _public_ int sd_journal_previous(sd_journal *j) {
693 return real_journal_next(j, DIRECTION_UP);
696 static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) {
703 /* If this is not a discrete skip, then at least
704 * resolve the current location */
705 if (j->current_location.type != LOCATION_DISCRETE)
706 return real_journal_next(j, direction);
712 r = real_journal_next(j, direction);
726 _public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
727 return real_journal_next_skip(j, DIRECTION_DOWN, skip);
730 _public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
731 return real_journal_next_skip(j, DIRECTION_UP, skip);
734 _public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) {
737 char bid[33], sid[33];
744 if (!j->current_file || j->current_file->current_offset <= 0)
745 return -EADDRNOTAVAIL;
747 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
751 sd_id128_to_string(j->current_file->header->seqnum_id, sid);
752 sd_id128_to_string(o->entry.boot_id, bid);
755 "s=%s;i=%llx;b=%s;m=%llx;t=%llx;x=%llx;p=%s",
756 sid, (unsigned long long) le64toh(o->entry.seqnum),
757 bid, (unsigned long long) le64toh(o->entry.monotonic),
758 (unsigned long long) le64toh(o->entry.realtime),
759 (unsigned long long) le64toh(o->entry.xor_hash),
760 file_name_from_path(j->current_file->path)) < 0)
766 _public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
770 unsigned long long seqnum, monotonic, realtime, xor_hash;
772 seqnum_id_set = false,
775 monotonic_set = false,
776 realtime_set = false,
777 xor_hash_set = false;
778 sd_id128_t seqnum_id, boot_id;
785 FOREACH_WORD_SEPARATOR(w, l, cursor, ";", state) {
789 if (l < 2 || w[1] != '=')
792 item = strndup(w, l);
799 seqnum_id_set = true;
800 k = sd_id128_from_string(w+2, &seqnum_id);
805 if (sscanf(w+2, "%llx", &seqnum) != 1)
811 k = sd_id128_from_string(w+2, &boot_id);
815 monotonic_set = true;
816 if (sscanf(w+2, "%llx", &monotonic) != 1)
822 if (sscanf(w+2, "%llx", &realtime) != 1)
828 if (sscanf(w+2, "%llx", &xor_hash) != 1)
839 if ((!seqnum_set || !seqnum_id_set) &&
840 (!monotonic_set || !boot_id_set) &&
846 j->current_location.type = LOCATION_DISCRETE;
849 j->current_location.realtime = (uint64_t) realtime;
850 j->current_location.realtime_set = true;
853 if (seqnum_set && seqnum_id_set) {
854 j->current_location.seqnum = (uint64_t) seqnum;
855 j->current_location.seqnum_id = seqnum_id;
856 j->current_location.seqnum_set = true;
859 if (monotonic_set && boot_id_set) {
860 j->current_location.monotonic = (uint64_t) monotonic;
861 j->current_location.boot_id = boot_id;
862 j->current_location.monotonic_set = true;
866 j->current_location.xor_hash = (uint64_t) xor_hash;
867 j->current_location.xor_hash_set = true;
873 _public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
878 j->current_location.type = LOCATION_DISCRETE;
879 j->current_location.boot_id = boot_id;
880 j->current_location.monotonic = usec;
881 j->current_location.monotonic_set = true;
886 _public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
891 j->current_location.type = LOCATION_DISCRETE;
892 j->current_location.realtime = usec;
893 j->current_location.realtime_set = true;
898 _public_ int sd_journal_seek_head(sd_journal *j) {
903 j->current_location.type = LOCATION_HEAD;
908 _public_ int sd_journal_seek_tail(sd_journal *j) {
913 j->current_location.type = LOCATION_TAIL;
918 static int add_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
927 if ((j->flags & SD_JOURNAL_SYSTEM_ONLY) &&
928 !startswith(filename, "system.journal"))
932 fn = join(prefix, "/", dir, "/", filename, NULL);
934 fn = join(prefix, "/", filename, NULL);
939 if (hashmap_get(j->files, fn)) {
944 if (hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
945 log_debug("Too many open journal files, not adding %s, ignoring.", fn);
950 r = journal_file_open(fn, O_RDONLY, 0, NULL, &f);
960 /* journal_file_dump(f); */
962 r = hashmap_put(j->files, f->path, f);
964 journal_file_close(f);
968 log_debug("File %s got added.", f->path);
973 static int remove_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
982 fn = join(prefix, "/", dir, "/", filename, NULL);
984 fn = join(prefix, "/", filename, NULL);
989 f = hashmap_get(j->files, fn);
995 hashmap_remove(j->files, f->path);
996 journal_file_close(f);
998 log_debug("File %s got removed.", f->path);
1002 static int add_directory(sd_journal *j, const char *prefix, const char *dir) {
1013 if ((j->flags & SD_JOURNAL_LOCAL_ONLY) &&
1014 (sd_id128_from_string(dir, &id) < 0 ||
1015 sd_id128_get_machine(&mid) < 0 ||
1016 !sd_id128_equal(id, mid)))
1019 fn = join(prefix, "/", dir, NULL);
1027 if (errno == ENOENT)
1033 wd = inotify_add_watch(j->inotify_fd, fn,
1034 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1035 IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|
1036 IN_DONT_FOLLOW|IN_ONLYDIR);
1038 if (hashmap_put(j->inotify_wd_dirs, INT_TO_PTR(wd), fn) < 0)
1039 inotify_rm_watch(j->inotify_fd, wd);
1047 struct dirent buf, *de;
1049 r = readdir_r(d, &buf, &de);
1053 if (!dirent_is_file_with_suffix(de, ".journal"))
1056 r = add_file(j, prefix, dir, de->d_name);
1058 log_debug("Failed to add file %s/%s/%s: %s", prefix, dir, de->d_name, strerror(-r));
1063 log_debug("Directory %s/%s got added.", prefix, dir);
1068 static void remove_directory_wd(sd_journal *j, int wd) {
1074 if (j->inotify_fd >= 0)
1075 inotify_rm_watch(j->inotify_fd, wd);
1077 p = hashmap_remove(j->inotify_wd_dirs, INT_TO_PTR(wd));
1080 log_debug("Directory %s got removed.", p);
1085 static void add_root_wd(sd_journal *j, const char *p) {
1092 wd = inotify_add_watch(j->inotify_fd, p,
1093 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1094 IN_DONT_FOLLOW|IN_ONLYDIR);
1099 if (!k || hashmap_put(j->inotify_wd_roots, INT_TO_PTR(wd), k) < 0) {
1100 inotify_rm_watch(j->inotify_fd, wd);
1105 static void remove_root_wd(sd_journal *j, int wd) {
1111 if (j->inotify_fd >= 0)
1112 inotify_rm_watch(j->inotify_fd, wd);
1114 p = hashmap_remove(j->inotify_wd_roots, INT_TO_PTR(wd));
1117 log_debug("Root %s got removed.", p);
1122 _public_ int sd_journal_open(sd_journal **ret, int flags) {
1125 const char search_paths[] =
1126 "/run/log/journal\0"
1127 "/var/log/journal\0";
1133 if (flags & ~(SD_JOURNAL_LOCAL_ONLY|
1134 SD_JOURNAL_RUNTIME_ONLY|
1135 SD_JOURNAL_SYSTEM_ONLY))
1138 j = new0(sd_journal, 1);
1144 j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1145 if (j->inotify_fd < 0) {
1150 j->files = hashmap_new(string_hash_func, string_compare_func);
1156 j->inotify_wd_dirs = hashmap_new(trivial_hash_func, trivial_compare_func);
1157 j->inotify_wd_roots = hashmap_new(trivial_hash_func, trivial_compare_func);
1159 if (!j->inotify_wd_dirs || !j->inotify_wd_roots) {
1164 /* We ignore most errors here, since the idea is to only open
1165 * what's actually accessible, and ignore the rest. */
1167 NULSTR_FOREACH(p, search_paths) {
1170 if ((flags & SD_JOURNAL_RUNTIME_ONLY) &&
1171 !path_startswith(p, "/run"))
1176 if (errno != ENOENT)
1177 log_debug("Failed to open %s: %m", p);
1184 struct dirent buf, *de;
1187 r = readdir_r(d, &buf, &de);
1191 if (dirent_is_file_with_suffix(de, ".journal")) {
1192 r = add_file(j, p, NULL, de->d_name);
1194 log_debug("Failed to add file %s/%s: %s", p, de->d_name, strerror(-r));
1196 } else if ((de->d_type == DT_DIR || de->d_type == DT_UNKNOWN) &&
1197 sd_id128_from_string(de->d_name, &id) >= 0) {
1199 r = add_directory(j, p, de->d_name);
1201 log_debug("Failed to add directory %s/%s: %s", p, de->d_name, strerror(-r));
1212 sd_journal_close(j);
1217 _public_ 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 _public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
1266 f = j->current_file;
1268 return -EADDRNOTAVAIL;
1270 if (f->current_offset <= 0)
1271 return -EADDRNOTAVAIL;
1273 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1277 *ret = le64toh(o->entry.realtime);
1281 _public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
1292 f = j->current_file;
1294 return -EADDRNOTAVAIL;
1296 if (f->current_offset <= 0)
1297 return -EADDRNOTAVAIL;
1299 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1304 *ret_boot_id = o->entry.boot_id;
1306 r = sd_id128_get_boot(&id);
1310 if (!sd_id128_equal(id, o->entry.boot_id))
1314 *ret = le64toh(o->entry.monotonic);
1318 _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
1321 size_t field_length;
1334 if (isempty(field) || strchr(field, '='))
1337 f = j->current_file;
1339 return -EADDRNOTAVAIL;
1341 if (f->current_offset <= 0)
1342 return -EADDRNOTAVAIL;
1344 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1348 field_length = strlen(field);
1350 n = journal_file_entry_n_items(o);
1351 for (i = 0; i < n; i++) {
1356 p = le64toh(o->entry.items[i].object_offset);
1357 le_hash = o->entry.items[i].hash;
1358 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
1362 if (le_hash != o->data.hash)
1365 l = le64toh(o->object.size) - offsetof(Object, data.payload);
1367 if (o->object.flags & OBJECT_COMPRESSED) {
1370 if (uncompress_startswith(o->data.payload, l,
1371 &f->compress_buffer, &f->compress_buffer_size,
1372 field, field_length, '=')) {
1376 if (!uncompress_blob(o->data.payload, l,
1377 &f->compress_buffer, &f->compress_buffer_size, &rsize))
1380 *data = f->compress_buffer;
1381 *size = (size_t) rsize;
1386 return -EPROTONOSUPPORT;
1389 } else if (l >= field_length+1 &&
1390 memcmp(o->data.payload, field, field_length) == 0 &&
1391 o->data.payload[field_length] == '=') {
1395 if ((uint64_t) t != l)
1398 *data = o->data.payload;
1404 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1412 _public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
1427 f = j->current_file;
1429 return -EADDRNOTAVAIL;
1431 if (f->current_offset <= 0)
1432 return -EADDRNOTAVAIL;
1434 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1438 n = journal_file_entry_n_items(o);
1439 if (j->current_field >= n)
1442 p = le64toh(o->entry.items[j->current_field].object_offset);
1443 le_hash = o->entry.items[j->current_field].hash;
1444 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
1448 if (le_hash != o->data.hash)
1451 l = le64toh(o->object.size) - offsetof(Object, data.payload);
1454 /* We can't read objects larger than 4G on a 32bit machine */
1455 if ((uint64_t) t != l)
1458 if (o->object.flags & OBJECT_COMPRESSED) {
1462 if (!uncompress_blob(o->data.payload, l, &f->compress_buffer, &f->compress_buffer_size, &rsize))
1465 *data = f->compress_buffer;
1466 *size = (size_t) rsize;
1468 return -EPROTONOSUPPORT;
1471 *data = o->data.payload;
1475 j->current_field ++;
1480 _public_ void sd_journal_restart_data(sd_journal *j) {
1484 j->current_field = 0;
1487 _public_ int sd_journal_get_fd(sd_journal *j) {
1491 return j->inotify_fd;
1494 static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
1501 /* Is this a subdirectory we watch? */
1502 p = hashmap_get(j->inotify_wd_dirs, INT_TO_PTR(e->wd));
1505 if (!(e->mask & IN_ISDIR) && e->len > 0 && endswith(e->name, ".journal")) {
1507 /* Event for a journal file */
1509 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1510 r = add_file(j, p, NULL, e->name);
1512 log_debug("Failed to add file %s/%s: %s", p, e->name, strerror(-r));
1513 } else if (e->mask & (IN_DELETE|IN_UNMOUNT)) {
1515 r = remove_file(j, p, NULL, e->name);
1517 log_debug("Failed to remove file %s/%s: %s", p, e->name, strerror(-r));
1520 } else if (e->len == 0) {
1522 /* Event for the directory itself */
1524 if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT))
1525 remove_directory_wd(j, e->wd);
1531 /* Must be the root directory then? */
1532 p = hashmap_get(j->inotify_wd_roots, INT_TO_PTR(e->wd));
1536 if (!(e->mask & IN_ISDIR) && e->len > 0 && endswith(e->name, ".journal")) {
1538 /* Event for a journal file */
1540 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1541 r = add_file(j, p, NULL, e->name);
1543 log_debug("Failed to add file %s/%s: %s", p, e->name, strerror(-r));
1544 } else if (e->mask & (IN_DELETE|IN_UNMOUNT)) {
1546 r = remove_file(j, p, NULL, e->name);
1548 log_debug("Failed to remove file %s/%s: %s", p, e->name, strerror(-r));
1551 } else if ((e->mask & IN_ISDIR) && e->len > 0 && sd_id128_from_string(e->name, &id) >= 0) {
1553 /* Event for subdirectory */
1555 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1557 r = add_directory(j, p, e->name);
1559 log_debug("Failed to add directory %s/%s: %s", p, e->name, strerror(-r));
1566 if (e->mask & IN_IGNORED)
1569 log_warning("Unknown inotify event.");
1572 _public_ int sd_journal_process(sd_journal *j) {
1573 uint8_t buffer[sizeof(struct inotify_event) + FILENAME_MAX];
1579 struct inotify_event *e;
1582 l = read(j->inotify_fd, buffer, sizeof(buffer));
1584 if (errno == EINTR || errno == EAGAIN)
1590 e = (struct inotify_event*) buffer;
1594 process_inotify_event(j, e);
1596 step = sizeof(struct inotify_event) + e->len;
1597 assert(step <= (size_t) l);
1599 e = (struct inotify_event*) ((uint8_t*) e + step);
1605 /* _public_ int sd_journal_query_unique(sd_journal *j, const char *field) { */
1607 /* return -EINVAL; */
1609 /* return -EINVAL; */
1611 /* return -ENOTSUP; */
1614 /* _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l) { */
1616 /* return -EINVAL; */
1618 /* return -EINVAL; */
1620 /* return -EINVAL; */
1622 /* return -ENOTSUP; */
1625 /* _public_ void sd_journal_restart_unique(sd_journal *j) { */