1 /* SPDX-License-Identifier: LGPL-2.1-or-later */ 2 #pragma once 3 4 #include <inttypes.h> 5 #include <limits.h> 6 #include <stddef.h> 7 #include <sys/inotify.h> 8 9 #include "log.h" 10 11 #define INOTIFY_EVENT_MAX (offsetof(struct inotify_event, name) + NAME_MAX + 1) 12 13 #define _FOREACH_INOTIFY_EVENT(e, buffer, sz, log_level, start, end) \ 14 for (struct inotify_event \ 15 *start = &((buffer).ev), \ 16 *end = (struct inotify_event*) ((uint8_t*) start + (sz)), \ 17 *e = start; \ 18 (size_t) ((uint8_t*) end - (uint8_t*) e) >= sizeof(struct inotify_event) && \ 19 ((size_t) ((uint8_t*) end - (uint8_t*) e) >= sizeof(struct inotify_event) + e->len || \ 20 (log_full(log_level, "Received invalid inotify event, ignoring."), false)); \ 21 e = (struct inotify_event*) ((uint8_t*) e + sizeof(struct inotify_event) + e->len)) 22 23 #define _FOREACH_INOTIFY_EVENT_FULL(e, buffer, sz, log_level) \ 24 _FOREACH_INOTIFY_EVENT(e, buffer, sz, log_level, UNIQ_T(start, UNIQ), UNIQ_T(end, UNIQ)) 25 26 #define FOREACH_INOTIFY_EVENT(e, buffer, sz) \ 27 _FOREACH_INOTIFY_EVENT_FULL(e, buffer, sz, LOG_DEBUG) 28 29 #define FOREACH_INOTIFY_EVENT_WARN(e, buffer, sz) \ 30 _FOREACH_INOTIFY_EVENT_FULL(e, buffer, sz, LOG_WARNING) 31 32 union inotify_event_buffer { 33 struct inotify_event ev; 34 uint8_t raw[INOTIFY_EVENT_MAX]; 35 }; 36 37 int inotify_add_watch_fd(int fd, int what, uint32_t mask); 38 int inotify_add_watch_and_warn(int fd, const char *pathname, uint32_t mask); 39