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 #define JOURNAL_FILES_MAX 1024
37 typedef struct Match Match;
44 LIST_FIELDS(Match, matches);
47 typedef enum location_type {
53 typedef struct Location {
74 Location current_location;
75 JournalFile *current_file;
76 uint64_t current_field;
79 Hashmap *inotify_wd_dirs;
80 Hashmap *inotify_wd_roots;
82 LIST_HEAD(Match, matches);
86 static void detach_location(sd_journal *j) {
92 j->current_file = NULL;
95 HASHMAP_FOREACH(f, j->files, i)
96 f->current_offset = 0;
99 static void reset_location(sd_journal *j) {
103 zero(j->current_location);
106 static void init_location(Location *l, JournalFile *f, Object *o) {
109 assert(o->object.type == OBJECT_ENTRY);
111 l->type = LOCATION_DISCRETE;
112 l->seqnum = le64toh(o->entry.seqnum);
113 l->seqnum_id = f->header->seqnum_id;
114 l->realtime = le64toh(o->entry.realtime);
115 l->monotonic = le64toh(o->entry.monotonic);
116 l->boot_id = le64toh(o->entry.boot_id);
117 l->xor_hash = le64toh(o->entry.xor_hash);
119 l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true;
122 static void set_location(sd_journal *j, JournalFile *f, Object *o, uint64_t offset) {
127 init_location(&j->current_location, f, o);
130 j->current_field = 0;
132 f->current_offset = offset;
135 static int same_field(const void *_a, size_t s, const void *_b, size_t t) {
136 const uint8_t *a = _a, *b = _b;
138 bool a_good = false, b_good = false, different = false;
140 for (j = 0; j < s && j < t; j++) {
149 if (a_good && b_good)
150 return different ? 0 : 1;
156 int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
157 Match *m, *after = NULL;
167 le_hash = htole64(hash64(data, size));
169 LIST_FOREACH(matches, m, j->matches) {
172 if (m->le_hash == le_hash &&
174 memcmp(m->data, data, size) == 0)
177 r = same_field(data, size, m->data, m->size);
190 m->data = malloc(m->size);
196 memcpy(m->data, data, size);
197 m->le_hash = le_hash;
199 /* Matches for the same fields we order adjacent to each
201 LIST_INSERT_AFTER(Match, matches, j->matches, after, m);
209 void sd_journal_flush_matches(sd_journal *j) {
213 Match *m = j->matches;
215 LIST_REMOVE(Match, matches, j->matches, m);
225 static int compare_order(JournalFile *af, Object *ao,
226 JournalFile *bf, Object *bo) {
235 /* We operate on two different files here, hence we can access
236 * two objects at the same time, which we normally can't.
238 * If contents and timestamps match, these entries are
239 * identical, even if the seqnum does not match */
241 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id) &&
242 ao->entry.monotonic == bo->entry.monotonic &&
243 ao->entry.realtime == bo->entry.realtime &&
244 ao->entry.xor_hash == bo->entry.xor_hash)
247 if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
249 /* If this is from the same seqnum source, compare
251 a = le64toh(ao->entry.seqnum);
252 b = le64toh(bo->entry.seqnum);
259 /* Wow! This is weird, different data but the same
260 * seqnums? Something is borked, but let's make the
261 * best of it and compare by time. */
264 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
266 /* If the boot id matches compare monotonic time */
267 a = le64toh(ao->entry.monotonic);
268 b = le64toh(bo->entry.monotonic);
276 /* Otherwise compare UTC time */
277 a = le64toh(ao->entry.realtime);
278 b = le64toh(ao->entry.realtime);
285 /* Finally, compare by contents */
286 a = le64toh(ao->entry.xor_hash);
287 b = le64toh(ao->entry.xor_hash);
297 static int compare_with_location(JournalFile *af, Object *ao, Location *l) {
303 assert(l->type == LOCATION_DISCRETE);
305 if (l->monotonic_set &&
306 sd_id128_equal(ao->entry.boot_id, l->boot_id) &&
308 le64toh(ao->entry.realtime) == l->realtime &&
310 le64toh(ao->entry.xor_hash) == l->xor_hash)
314 sd_id128_equal(af->header->seqnum_id, l->seqnum_id)) {
316 a = le64toh(ao->entry.seqnum);
324 if (l->monotonic_set &&
325 sd_id128_equal(ao->entry.boot_id, l->boot_id)) {
327 a = le64toh(ao->entry.monotonic);
329 if (a < l->monotonic)
331 if (a > l->monotonic)
335 if (l->realtime_set) {
337 a = le64toh(ao->entry.realtime);
345 if (l->xor_hash_set) {
346 a = le64toh(ao->entry.xor_hash);
357 static int find_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
365 /* No matches is simple */
367 if (j->current_location.type == LOCATION_HEAD)
368 r = journal_file_next_entry(f, NULL, 0, DIRECTION_DOWN, &o, &p);
369 else if (j->current_location.type == LOCATION_TAIL)
370 r = journal_file_next_entry(f, NULL, 0, DIRECTION_UP, &o, &p);
371 else if (j->current_location.seqnum_set &&
372 sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
373 r = journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, &o, &p);
374 else if (j->current_location.monotonic_set)
375 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, &o, &p);
376 else if (j->current_location.realtime_set)
377 r = journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, &o, &p);
379 r = journal_file_next_entry(f, NULL, 0, direction, &o, &p);
385 Match *m, *term_match = NULL;
389 /* We have matches, first, let's jump to the monotonic
390 * position if we have any, since it implies a
393 if (j->current_location.type == LOCATION_DISCRETE &&
394 j->current_location.monotonic_set) {
396 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, &o, &p);
401 LIST_FOREACH(matches, m, j->matches) {
405 r = journal_file_find_data_object_with_hash(f, m->data, m->size, m->le_hash, &d, &dp);
409 if (j->current_location.type == LOCATION_HEAD)
410 r = journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_DOWN, &c, &cp);
411 else if (j->current_location.type == LOCATION_TAIL)
412 r = journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_UP, &c, &cp);
413 else if (j->current_location.seqnum_set &&
414 sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
415 r = journal_file_move_to_entry_by_seqnum_for_data(f, dp, j->current_location.seqnum, direction, &c, &cp);
416 else if (j->current_location.realtime_set)
417 r = journal_file_move_to_entry_by_realtime_for_data(f, dp, j->current_location.realtime, direction, &c, &cp);
419 r = journal_file_next_entry_for_data(f, NULL, 0, dp, direction, &c, &cp);
428 } else if (same_field(term_match->data, term_match->size, m->data, m->size)) {
430 /* Same field as previous match... */
433 /* Find the earliest of the OR matches */
436 (direction == DIRECTION_DOWN && cp < tp) ||
437 (direction == DIRECTION_UP && cp > tp)) {
446 /* Previous term is finished, did anything match? */
450 /* Find the last of the AND matches */
452 (direction == DIRECTION_DOWN && tp > p) ||
453 (direction == DIRECTION_UP && tp < p)) {
470 /* Last term is finished, did anything match? */
475 (direction == DIRECTION_DOWN && tp > p) ||
476 (direction == DIRECTION_UP && tp < p)) {
494 static int next_with_matches(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
508 /* No matches is easy */
510 r = journal_file_next_entry(f, c, cp, direction, &c, &cp);
521 /* So there are matches we have to adhere to, let's find the
522 * first entry that matches all of them */
526 bool found, term_result = false;
527 Match *m, *term_match = NULL;
530 n = journal_file_entry_n_items(c);
532 /* Make sure we don't match the entry we are starting
534 found = cp > *offset;
537 LIST_FOREACH(matches, m, j->matches) {
541 /* Let's check if this is the beginning of a
542 * new term, i.e. has a different field prefix
543 * as the preceeding match. */
547 } else if (!same_field(term_match->data, term_match->size, m->data, m->size)) {
555 for (k = 0; k < n; k++)
556 if (c->entry.items[k].hash == m->le_hash)
560 /* Hmm, didn't find any field that
561 * matched this rule, so ignore this
562 * match. Go on with next match */
568 /* Hmm, so, this field matched, let's remember
569 * where we'd have to try next, in case the other
570 * matches are not OK */
572 r = journal_file_next_entry_for_data(f, c, cp, le64toh(c->entry.items[k].object_offset), direction, &qo, &q);
575 if (direction == DIRECTION_DOWN) {
581 if (np == 0 || q < np) {
589 /* Check the last term */
590 if (term_match && !term_result)
593 /* Did this entry match against all matches? */
602 /* Did we find a subsequent entry? */
606 /* Hmm, ok, this entry only matched partially, so
607 * let's try another one */
613 static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
616 int compare_value, r;
621 if (f->current_offset > 0) {
622 cp = f->current_offset;
624 r = journal_file_move_to_object(f, OBJECT_ENTRY, cp, &c);
628 r = next_with_matches(j, f, direction, &c, &cp);
634 r = find_location(j, f, direction, &c, &cp);
644 if (j->current_location.type == LOCATION_DISCRETE) {
647 k = compare_with_location(f, c, &j->current_location);
648 if (direction == DIRECTION_DOWN)
649 found = k >= compare_value;
651 found = k <= -compare_value;
663 r = next_with_matches(j, f, direction, &c, &cp);
669 static int real_journal_next(sd_journal *j, direction_t direction) {
670 JournalFile *f, *new_current = NULL;
673 uint64_t new_offset = 0;
674 Object *new_entry = NULL;
678 HASHMAP_FOREACH(f, j->files, i) {
683 r = next_beyond_location(j, f, direction, &o, &p);
694 k = compare_order(f, o, new_current, new_entry);
696 if (direction == DIRECTION_DOWN)
712 set_location(j, new_current, new_entry, new_offset);
717 int sd_journal_next(sd_journal *j) {
718 return real_journal_next(j, DIRECTION_DOWN);
721 int sd_journal_previous(sd_journal *j) {
722 return real_journal_next(j, DIRECTION_UP);
725 int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
731 r = sd_journal_next(j);
745 int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
751 r = sd_journal_previous(j);
765 int sd_journal_get_cursor(sd_journal *j, char **cursor) {
768 char bid[33], sid[33];
773 if (!j->current_file || j->current_file->current_offset <= 0)
774 return -EADDRNOTAVAIL;
776 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
780 sd_id128_to_string(j->current_file->header->seqnum_id, sid);
781 sd_id128_to_string(o->entry.boot_id, bid);
784 "s=%s;i=%llx;b=%s;m=%llx;t=%llx;x=%llx;p=%s",
785 sid, (unsigned long long) le64toh(o->entry.seqnum),
786 bid, (unsigned long long) le64toh(o->entry.monotonic),
787 (unsigned long long) le64toh(o->entry.realtime),
788 (unsigned long long) le64toh(o->entry.xor_hash),
789 file_name_from_path(j->current_file->path)) < 0)
795 int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
799 unsigned long long seqnum, monotonic, realtime, xor_hash;
801 seqnum_id_set = false,
804 monotonic_set = false,
805 realtime_set = false,
806 xor_hash_set = false;
807 sd_id128_t seqnum_id, boot_id;
812 FOREACH_WORD_SEPARATOR(w, l, cursor, ";", state) {
816 if (l < 2 || w[1] != '=')
819 item = strndup(w, l);
826 seqnum_id_set = true;
827 k = sd_id128_from_string(w+2, &seqnum_id);
832 if (sscanf(w+2, "%llx", &seqnum) != 1)
838 k = sd_id128_from_string(w+2, &boot_id);
842 monotonic_set = true;
843 if (sscanf(w+2, "%llx", &monotonic) != 1)
849 if (sscanf(w+2, "%llx", &realtime) != 1)
855 if (sscanf(w+2, "%llx", &xor_hash) != 1)
866 if ((!seqnum_set || !seqnum_id_set) &&
867 (!monotonic_set || !boot_id_set) &&
873 j->current_location.type = LOCATION_DISCRETE;
876 j->current_location.realtime = (uint64_t) realtime;
877 j->current_location.realtime_set = true;
880 if (seqnum_set && seqnum_id_set) {
881 j->current_location.seqnum = (uint64_t) seqnum;
882 j->current_location.seqnum_id = seqnum_id;
883 j->current_location.seqnum_set = true;
886 if (monotonic_set && boot_id_set) {
887 j->current_location.monotonic = (uint64_t) monotonic;
888 j->current_location.boot_id = boot_id;
889 j->current_location.monotonic_set = true;
893 j->current_location.xor_hash = (uint64_t) xor_hash;
894 j->current_location.xor_hash_set = true;
900 int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
904 j->current_location.type = LOCATION_DISCRETE;
905 j->current_location.boot_id = boot_id;
906 j->current_location.monotonic = usec;
907 j->current_location.monotonic_set = true;
912 int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
916 j->current_location.type = LOCATION_DISCRETE;
917 j->current_location.realtime = usec;
918 j->current_location.realtime_set = true;
923 int sd_journal_seek_head(sd_journal *j) {
927 j->current_location.type = LOCATION_HEAD;
932 int sd_journal_seek_tail(sd_journal *j) {
936 j->current_location.type = LOCATION_TAIL;
941 static int add_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
951 fn = join(prefix, "/", dir, "/", filename, NULL);
953 fn = join(prefix, "/", filename, NULL);
958 if (hashmap_get(j->files, fn)) {
963 if (hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
964 log_debug("Too many open journal files, not adding %s, ignoring.", fn);
969 r = journal_file_open(fn, O_RDONLY, 0, NULL, &f);
979 journal_file_dump(f);
981 r = hashmap_put(j->files, f->path, f);
983 journal_file_close(f);
987 log_debug("File %s got added.", f->path);
992 static int remove_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
1001 fn = join(prefix, "/", dir, "/", filename, NULL);
1003 fn = join(prefix, "/", filename, NULL);
1008 f = hashmap_get(j->files, fn);
1014 hashmap_remove(j->files, f->path);
1015 journal_file_close(f);
1017 log_debug("File %s got removed.", f->path);
1021 static int add_directory(sd_journal *j, const char *prefix, const char *dir) {
1031 fn = join(prefix, "/", dir, NULL);
1039 if (errno == ENOENT)
1045 wd = inotify_add_watch(j->inotify_fd, fn,
1046 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1047 IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|
1048 IN_DONT_FOLLOW|IN_ONLYDIR);
1050 if (hashmap_put(j->inotify_wd_dirs, INT_TO_PTR(wd), fn) < 0)
1051 inotify_rm_watch(j->inotify_fd, wd);
1059 struct dirent buf, *de;
1061 r = readdir_r(d, &buf, &de);
1065 if (!dirent_is_file_with_suffix(de, ".journal"))
1068 r = add_file(j, prefix, dir, de->d_name);
1070 log_debug("Failed to add file %s/%s/%s: %s", prefix, dir, de->d_name, strerror(-r));
1075 log_debug("Directory %s/%s got added.", prefix, dir);
1080 static void remove_directory_wd(sd_journal *j, int wd) {
1086 if (j->inotify_fd >= 0)
1087 inotify_rm_watch(j->inotify_fd, wd);
1089 p = hashmap_remove(j->inotify_wd_dirs, INT_TO_PTR(wd));
1092 log_debug("Directory %s got removed.", p);
1097 static void add_root_wd(sd_journal *j, const char *p) {
1104 wd = inotify_add_watch(j->inotify_fd, p,
1105 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1106 IN_DONT_FOLLOW|IN_ONLYDIR);
1111 if (!k || hashmap_put(j->inotify_wd_roots, INT_TO_PTR(wd), k) < 0) {
1112 inotify_rm_watch(j->inotify_fd, wd);
1117 static void remove_root_wd(sd_journal *j, int wd) {
1123 if (j->inotify_fd >= 0)
1124 inotify_rm_watch(j->inotify_fd, wd);
1126 p = hashmap_remove(j->inotify_wd_roots, INT_TO_PTR(wd));
1129 log_debug("Root %s got removed.", p);
1134 int sd_journal_open(sd_journal **ret) {
1137 const char search_paths[] =
1138 "/run/log/journal\0"
1139 "/var/log/journal\0";
1144 j = new0(sd_journal, 1);
1148 j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1149 if (j->inotify_fd < 0) {
1154 j->files = hashmap_new(string_hash_func, string_compare_func);
1160 j->inotify_wd_dirs = hashmap_new(trivial_hash_func, trivial_compare_func);
1161 j->inotify_wd_roots = hashmap_new(trivial_hash_func, trivial_compare_func);
1163 if (!j->inotify_wd_dirs || !j->inotify_wd_roots) {
1168 /* We ignore most errors here, since the idea is to only open
1169 * what's actually accessible, and ignore the rest. */
1171 NULSTR_FOREACH(p, search_paths) {
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 void sd_journal_close(sd_journal *j) {
1220 if (j->inotify_wd_dirs) {
1223 while ((k = hashmap_first_key(j->inotify_wd_dirs)))
1224 remove_directory_wd(j, PTR_TO_INT(k));
1226 hashmap_free(j->inotify_wd_dirs);
1229 if (j->inotify_wd_roots) {
1232 while ((k = hashmap_first_key(j->inotify_wd_roots)))
1233 remove_root_wd(j, PTR_TO_INT(k));
1235 hashmap_free(j->inotify_wd_roots);
1241 while ((f = hashmap_steal_first(j->files)))
1242 journal_file_close(f);
1244 hashmap_free(j->files);
1247 sd_journal_flush_matches(j);
1249 if (j->inotify_fd >= 0)
1250 close_nointr_nofail(j->inotify_fd);
1255 int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
1263 f = j->current_file;
1265 return -EADDRNOTAVAIL;
1267 if (f->current_offset <= 0)
1268 return -EADDRNOTAVAIL;
1270 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1274 *ret = le64toh(o->entry.realtime);
1278 int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
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);
1299 *ret_boot_id = o->entry.boot_id;
1301 r = sd_id128_get_boot(&id);
1305 if (!sd_id128_equal(id, o->entry.boot_id))
1309 *ret = le64toh(o->entry.monotonic);
1313 int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
1316 size_t field_length;
1325 if (isempty(field) || strchr(field, '='))
1328 f = j->current_file;
1330 return -EADDRNOTAVAIL;
1332 if (f->current_offset <= 0)
1333 return -EADDRNOTAVAIL;
1335 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1339 field_length = strlen(field);
1341 n = journal_file_entry_n_items(o);
1342 for (i = 0; i < n; i++) {
1343 uint64_t p, l, le_hash;
1346 p = le64toh(o->entry.items[i].object_offset);
1347 le_hash = o->entry.items[j->current_field].hash;
1348 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
1352 if (le_hash != o->data.hash)
1355 l = le64toh(o->object.size) - offsetof(Object, data.payload);
1357 if (l >= field_length+1 &&
1358 memcmp(o->data.payload, field, field_length) == 0 &&
1359 o->data.payload[field_length] == '=') {
1363 if ((uint64_t) t != l)
1366 *data = o->data.payload;
1372 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1380 int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
1382 uint64_t p, l, n, le_hash;
1391 f = j->current_file;
1393 return -EADDRNOTAVAIL;
1395 if (f->current_offset <= 0)
1396 return -EADDRNOTAVAIL;
1398 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1402 n = journal_file_entry_n_items(o);
1403 if (j->current_field >= n)
1406 p = le64toh(o->entry.items[j->current_field].object_offset);
1407 le_hash = o->entry.items[j->current_field].hash;
1408 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
1412 if (le_hash != o->data.hash)
1415 l = le64toh(o->object.size) - offsetof(Object, data.payload);
1418 /* We can't read objects larger than 4G on a 32bit machine */
1419 if ((uint64_t) t != l)
1422 *data = o->data.payload;
1425 j->current_field ++;
1430 void sd_journal_restart_data(sd_journal *j) {
1433 j->current_field = 0;
1436 int sd_journal_get_fd(sd_journal *j) {
1439 return j->inotify_fd;
1442 static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
1449 /* Is this a subdirectory we watch? */
1450 p = hashmap_get(j->inotify_wd_dirs, INT_TO_PTR(e->wd));
1453 if (!(e->mask & IN_ISDIR) && e->len > 0 && endswith(e->name, ".journal")) {
1455 /* Event for a journal file */
1457 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1458 r = add_file(j, p, NULL, e->name);
1460 log_debug("Failed to add file %s/%s: %s", p, e->name, strerror(-r));
1461 } else if (e->mask & (IN_DELETE|IN_UNMOUNT)) {
1463 r = remove_file(j, p, NULL, e->name);
1465 log_debug("Failed to remove file %s/%s: %s", p, e->name, strerror(-r));
1468 } else if (e->len == 0) {
1470 /* Event for the directory itself */
1472 if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT))
1473 remove_directory_wd(j, e->wd);
1479 /* Must be the root directory then? */
1480 p = hashmap_get(j->inotify_wd_roots, INT_TO_PTR(e->wd));
1484 if (!(e->mask & IN_ISDIR) && e->len > 0 && endswith(e->name, ".journal")) {
1486 /* Event for a journal file */
1488 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1489 r = add_file(j, p, NULL, e->name);
1491 log_debug("Failed to add file %s/%s: %s", p, e->name, strerror(-r));
1492 } else if (e->mask & (IN_DELETE|IN_UNMOUNT)) {
1494 r = remove_file(j, p, NULL, e->name);
1496 log_debug("Failed to remove file %s/%s: %s", p, e->name, strerror(-r));
1499 } else if ((e->mask & IN_ISDIR) && e->len > 0 && sd_id128_from_string(e->name, &id) >= 0) {
1501 /* Event for subdirectory */
1503 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1505 r = add_directory(j, p, e->name);
1507 log_debug("Failed to add directory %s/%s: %s", p, e->name, strerror(-r));
1514 if (e->mask & IN_IGNORED)
1517 log_warning("Unknown inotify event.");
1520 int sd_journal_process(sd_journal *j) {
1521 uint8_t buffer[sizeof(struct inotify_event) + FILENAME_MAX];
1526 struct inotify_event *e;
1529 l = read(j->inotify_fd, buffer, sizeof(buffer));
1531 if (errno == EINTR || errno == EAGAIN)
1537 e = (struct inotify_event*) buffer;
1541 process_inotify_event(j, e);
1543 step = sizeof(struct inotify_event) + e->len;
1544 assert(step <= (size_t) l);
1546 e = (struct inotify_event*) ((uint8_t*) e + step);