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;
529 n = journal_file_entry_n_items(c);
531 /* Make sure we don't match the entry we are starting
533 found = cp > *offset;
536 LIST_FOREACH(matches, m, j->matches) {
539 /* Let's check if this is the beginning of a
540 * new term, i.e. has a different field prefix
541 * as the preceeding match. */
545 } else if (!same_field(term_match->data, term_match->size, m->data, m->size)) {
553 for (k = 0; k < n; k++)
554 if (c->entry.items[k].hash == m->le_hash)
558 /* Hmm, didn't find any field that
559 * matched this rule, so ignore this
560 * match. Go on with next match */
566 /* Hmm, so, this field matched, let's remember
567 * where we'd have to try next, in case the other
568 * matches are not OK */
570 r = journal_file_next_entry_for_data(f, c, cp, le64toh(c->entry.items[k].object_offset), direction, NULL, &q);
573 if (direction == DIRECTION_DOWN) {
577 if (np == 0 || q < np)
583 /* Check the last term */
584 if (term_match && term_result)
587 /* Did this entry match against all matches? */
596 /* Did we find a subsequent entry? */
600 /* Hmm, ok, this entry only matched partially, so
601 * let's try another one */
606 static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
609 int compare_value, r;
614 if (f->current_offset > 0) {
615 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &c);
619 cp = f->current_offset;
621 r = next_with_matches(j, f, direction, &c, &cp);
627 r = find_location(j, f, direction, &c, &cp);
637 if (j->current_location.type == LOCATION_DISCRETE) {
640 k = compare_with_location(f, c, &j->current_location);
641 if (direction == DIRECTION_DOWN)
642 found = k >= compare_value;
644 found = k <= -compare_value;
656 r = next_with_matches(j, f, direction, &c, &cp);
662 static int real_journal_next(sd_journal *j, direction_t direction) {
663 JournalFile *f, *new_current = NULL;
666 uint64_t new_offset = 0;
667 Object *new_entry = NULL;
671 HASHMAP_FOREACH(f, j->files, i) {
676 r = next_beyond_location(j, f, direction, &o, &p);
687 k = compare_order(f, o, new_current, new_entry);
689 if (direction == DIRECTION_DOWN)
705 set_location(j, new_current, new_entry, new_offset);
710 int sd_journal_next(sd_journal *j) {
711 return real_journal_next(j, DIRECTION_DOWN);
714 int sd_journal_previous(sd_journal *j) {
715 return real_journal_next(j, DIRECTION_UP);
718 int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
724 r = sd_journal_next(j);
738 int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
744 r = sd_journal_previous(j);
758 int sd_journal_get_cursor(sd_journal *j, char **cursor) {
761 char bid[33], sid[33];
766 if (!j->current_file || j->current_file->current_offset <= 0)
767 return -EADDRNOTAVAIL;
769 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
773 sd_id128_to_string(j->current_file->header->seqnum_id, sid);
774 sd_id128_to_string(o->entry.boot_id, bid);
777 "s=%s;i=%llx;b=%s;m=%llx;t=%llx;x=%llx;p=%s",
778 sid, (unsigned long long) le64toh(o->entry.seqnum),
779 bid, (unsigned long long) le64toh(o->entry.monotonic),
780 (unsigned long long) le64toh(o->entry.realtime),
781 (unsigned long long) le64toh(o->entry.xor_hash),
782 file_name_from_path(j->current_file->path)) < 0)
788 int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
792 unsigned long long seqnum, monotonic, realtime, xor_hash;
794 seqnum_id_set = false,
797 monotonic_set = false,
798 realtime_set = false,
799 xor_hash_set = false;
800 sd_id128_t seqnum_id, boot_id;
805 FOREACH_WORD_SEPARATOR(w, l, cursor, ";", state) {
809 if (l < 2 || w[1] != '=')
812 item = strndup(w, l);
819 seqnum_id_set = true;
820 k = sd_id128_from_string(w+2, &seqnum_id);
825 if (sscanf(w+2, "%llx", &seqnum) != 1)
831 k = sd_id128_from_string(w+2, &boot_id);
835 monotonic_set = true;
836 if (sscanf(w+2, "%llx", &monotonic) != 1)
842 if (sscanf(w+2, "%llx", &realtime) != 1)
848 if (sscanf(w+2, "%llx", &xor_hash) != 1)
859 if ((!seqnum_set || !seqnum_id_set) &&
860 (!monotonic_set || !boot_id_set) &&
866 j->current_location.type = LOCATION_DISCRETE;
869 j->current_location.realtime = (uint64_t) realtime;
870 j->current_location.realtime_set = true;
873 if (seqnum_set && seqnum_id_set) {
874 j->current_location.seqnum = (uint64_t) seqnum;
875 j->current_location.seqnum_id = seqnum_id;
876 j->current_location.seqnum_set = true;
879 if (monotonic_set && boot_id_set) {
880 j->current_location.monotonic = (uint64_t) monotonic;
881 j->current_location.boot_id = boot_id;
882 j->current_location.monotonic_set = true;
886 j->current_location.xor_hash = (uint64_t) xor_hash;
887 j->current_location.xor_hash_set = true;
893 int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
897 j->current_location.type = LOCATION_DISCRETE;
898 j->current_location.boot_id = boot_id;
899 j->current_location.monotonic = usec;
900 j->current_location.monotonic_set = true;
905 int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
909 j->current_location.type = LOCATION_DISCRETE;
910 j->current_location.realtime = usec;
911 j->current_location.realtime_set = true;
916 int sd_journal_seek_head(sd_journal *j) {
920 j->current_location.type = LOCATION_HEAD;
925 int sd_journal_seek_tail(sd_journal *j) {
929 j->current_location.type = LOCATION_TAIL;
934 static int add_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
944 fn = join(prefix, "/", dir, "/", filename, NULL);
946 fn = join(prefix, "/", filename, NULL);
951 if (hashmap_get(j->files, fn)) {
956 if (hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
957 log_debug("Too many open journal files, not adding %s, ignoring.", fn);
962 r = journal_file_open(fn, O_RDONLY, 0, NULL, &f);
972 journal_file_dump(f);
974 r = hashmap_put(j->files, f->path, f);
976 journal_file_close(f);
980 log_debug("File %s got added.", f->path);
985 static int remove_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
994 fn = join(prefix, "/", dir, "/", filename, NULL);
996 fn = join(prefix, "/", filename, NULL);
1001 f = hashmap_get(j->files, fn);
1007 hashmap_remove(j->files, f->path);
1008 journal_file_close(f);
1010 log_debug("File %s got removed.", f->path);
1014 static int add_directory(sd_journal *j, const char *prefix, const char *dir) {
1024 fn = join(prefix, "/", dir, NULL);
1032 if (errno == ENOENT)
1038 wd = inotify_add_watch(j->inotify_fd, fn,
1039 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1040 IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|
1041 IN_DONT_FOLLOW|IN_ONLYDIR);
1043 if (hashmap_put(j->inotify_wd_dirs, INT_TO_PTR(wd), fn) < 0)
1044 inotify_rm_watch(j->inotify_fd, wd);
1052 struct dirent buf, *de;
1054 r = readdir_r(d, &buf, &de);
1058 if (!dirent_is_file_with_suffix(de, ".journal"))
1061 r = add_file(j, prefix, dir, de->d_name);
1063 log_debug("Failed to add file %s/%s/%s: %s", prefix, dir, de->d_name, strerror(-r));
1068 log_debug("Directory %s/%s got added.", prefix, dir);
1073 static void remove_directory_wd(sd_journal *j, int wd) {
1079 if (j->inotify_fd >= 0)
1080 inotify_rm_watch(j->inotify_fd, wd);
1082 p = hashmap_remove(j->inotify_wd_dirs, INT_TO_PTR(wd));
1085 log_debug("Directory %s got removed.", p);
1090 static void add_root_wd(sd_journal *j, const char *p) {
1097 wd = inotify_add_watch(j->inotify_fd, p,
1098 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1099 IN_DONT_FOLLOW|IN_ONLYDIR);
1104 if (!k || hashmap_put(j->inotify_wd_roots, INT_TO_PTR(wd), k) < 0) {
1105 inotify_rm_watch(j->inotify_fd, wd);
1110 static void remove_root_wd(sd_journal *j, int wd) {
1116 if (j->inotify_fd >= 0)
1117 inotify_rm_watch(j->inotify_fd, wd);
1119 p = hashmap_remove(j->inotify_wd_roots, INT_TO_PTR(wd));
1122 log_debug("Root %s got removed.", p);
1127 int sd_journal_open(sd_journal **ret) {
1130 const char search_paths[] =
1131 "/run/log/journal\0"
1132 "/var/log/journal\0";
1137 j = new0(sd_journal, 1);
1141 j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1142 if (j->inotify_fd < 0) {
1147 j->files = hashmap_new(string_hash_func, string_compare_func);
1153 j->inotify_wd_dirs = hashmap_new(trivial_hash_func, trivial_compare_func);
1154 j->inotify_wd_roots = hashmap_new(trivial_hash_func, trivial_compare_func);
1156 if (!j->inotify_wd_dirs || !j->inotify_wd_roots) {
1161 /* We ignore most errors here, since the idea is to only open
1162 * what's actually accessible, and ignore the rest. */
1164 NULSTR_FOREACH(p, search_paths) {
1169 if (errno != ENOENT)
1170 log_debug("Failed to open %s: %m", p);
1177 struct dirent buf, *de;
1180 r = readdir_r(d, &buf, &de);
1184 if (dirent_is_file_with_suffix(de, ".journal")) {
1185 r = add_file(j, p, NULL, de->d_name);
1187 log_debug("Failed to add file %s/%s: %s", p, de->d_name, strerror(-r));
1189 } else if ((de->d_type == DT_DIR || de->d_type == DT_UNKNOWN) &&
1190 sd_id128_from_string(de->d_name, &id) >= 0) {
1192 r = add_directory(j, p, de->d_name);
1194 log_debug("Failed to add directory %s/%s: %s", p, de->d_name, strerror(-r));
1205 sd_journal_close(j);
1210 void sd_journal_close(sd_journal *j) {
1213 if (j->inotify_wd_dirs) {
1216 while ((k = hashmap_first_key(j->inotify_wd_dirs)))
1217 remove_directory_wd(j, PTR_TO_INT(k));
1219 hashmap_free(j->inotify_wd_dirs);
1222 if (j->inotify_wd_roots) {
1225 while ((k = hashmap_first_key(j->inotify_wd_roots)))
1226 remove_root_wd(j, PTR_TO_INT(k));
1228 hashmap_free(j->inotify_wd_roots);
1234 while ((f = hashmap_steal_first(j->files)))
1235 journal_file_close(f);
1237 hashmap_free(j->files);
1240 sd_journal_flush_matches(j);
1242 if (j->inotify_fd >= 0)
1243 close_nointr_nofail(j->inotify_fd);
1248 int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
1256 f = j->current_file;
1258 return -EADDRNOTAVAIL;
1260 if (f->current_offset <= 0)
1261 return -EADDRNOTAVAIL;
1263 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1267 *ret = le64toh(o->entry.realtime);
1271 int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
1280 f = j->current_file;
1282 return -EADDRNOTAVAIL;
1284 if (f->current_offset <= 0)
1285 return -EADDRNOTAVAIL;
1287 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1292 *ret_boot_id = o->entry.boot_id;
1294 r = sd_id128_get_boot(&id);
1298 if (!sd_id128_equal(id, o->entry.boot_id))
1302 *ret = le64toh(o->entry.monotonic);
1306 int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
1309 size_t field_length;
1318 if (isempty(field) || strchr(field, '='))
1321 f = j->current_file;
1323 return -EADDRNOTAVAIL;
1325 if (f->current_offset <= 0)
1326 return -EADDRNOTAVAIL;
1328 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1332 field_length = strlen(field);
1334 n = journal_file_entry_n_items(o);
1335 for (i = 0; i < n; i++) {
1336 uint64_t p, l, le_hash;
1339 p = le64toh(o->entry.items[i].object_offset);
1340 le_hash = o->entry.items[j->current_field].hash;
1341 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
1345 if (le_hash != o->data.hash)
1348 l = le64toh(o->object.size) - offsetof(Object, data.payload);
1350 if (l >= field_length+1 &&
1351 memcmp(o->data.payload, field, field_length) == 0 &&
1352 o->data.payload[field_length] == '=') {
1356 if ((uint64_t) t != l)
1359 *data = o->data.payload;
1365 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1373 int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
1375 uint64_t p, l, n, le_hash;
1384 f = j->current_file;
1386 return -EADDRNOTAVAIL;
1388 if (f->current_offset <= 0)
1389 return -EADDRNOTAVAIL;
1391 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1395 n = journal_file_entry_n_items(o);
1396 if (j->current_field >= n)
1399 p = le64toh(o->entry.items[j->current_field].object_offset);
1400 le_hash = o->entry.items[j->current_field].hash;
1401 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
1405 if (le_hash != o->data.hash)
1408 l = le64toh(o->object.size) - offsetof(Object, data.payload);
1411 /* We can't read objects larger than 4G on a 32bit machine */
1412 if ((uint64_t) t != l)
1415 *data = o->data.payload;
1418 j->current_field ++;
1423 void sd_journal_restart_data(sd_journal *j) {
1426 j->current_field = 0;
1429 int sd_journal_get_fd(sd_journal *j) {
1432 return j->inotify_fd;
1435 static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
1442 /* Is this a subdirectory we watch? */
1443 p = hashmap_get(j->inotify_wd_dirs, INT_TO_PTR(e->wd));
1446 if (!(e->mask & IN_ISDIR) && e->len > 0 && endswith(e->name, ".journal")) {
1448 /* Event for a journal file */
1450 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1451 r = add_file(j, p, NULL, e->name);
1453 log_debug("Failed to add file %s/%s: %s", p, e->name, strerror(-r));
1454 } else if (e->mask & (IN_DELETE|IN_UNMOUNT)) {
1456 r = remove_file(j, p, NULL, e->name);
1458 log_debug("Failed to remove file %s/%s: %s", p, e->name, strerror(-r));
1461 } else if (e->len == 0) {
1463 /* Event for the directory itself */
1465 if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT))
1466 remove_directory_wd(j, e->wd);
1472 /* Must be the root directory then? */
1473 p = hashmap_get(j->inotify_wd_roots, INT_TO_PTR(e->wd));
1477 if (!(e->mask & IN_ISDIR) && e->len > 0 && endswith(e->name, ".journal")) {
1479 /* Event for a journal file */
1481 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1482 r = add_file(j, p, NULL, e->name);
1484 log_debug("Failed to add file %s/%s: %s", p, e->name, strerror(-r));
1485 } else if (e->mask & (IN_DELETE|IN_UNMOUNT)) {
1487 r = remove_file(j, p, NULL, e->name);
1489 log_debug("Failed to remove file %s/%s: %s", p, e->name, strerror(-r));
1492 } else if ((e->mask & IN_ISDIR) && e->len > 0 && sd_id128_from_string(e->name, &id) >= 0) {
1494 /* Event for subdirectory */
1496 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
1498 r = add_directory(j, p, e->name);
1500 log_debug("Failed to add directory %s/%s: %s", p, e->name, strerror(-r));
1507 if (e->mask & IN_IGNORED)
1510 log_warning("Unknown inotify event.");
1513 int sd_journal_process(sd_journal *j) {
1514 uint8_t buffer[sizeof(struct inotify_event) + FILENAME_MAX];
1519 struct inotify_event *e;
1522 l = read(j->inotify_fd, buffer, sizeof(buffer));
1524 if (errno == EINTR || errno == EAGAIN)
1530 e = (struct inotify_event*) buffer;
1534 process_inotify_event(j, e);
1536 step = sizeof(struct inotify_event) + e->len;
1537 assert(step <= (size_t) l);
1539 e = (struct inotify_event*) ((uint8_t*) e + step);