summaryrefslogtreecommitdiff
path: root/src/basic/inotify-util.h
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2022-03-24 17:58:07 +0900
committerLuca Boccassi <luca.boccassi@gmail.com>2022-03-24 23:12:34 +0000
commit00adc340bb15bc9d634db6caa48f1c964b99f79a (patch)
tree26c4b95de937589754f9db91cdba6283b16db150 /src/basic/inotify-util.h
parentf72f8021182f930bb86ff4e3a05b8f09fa7d3179 (diff)
downloadsystemd-00adc340bb15bc9d634db6caa48f1c964b99f79a.tar.gz
inotify-util: declare iterator in FOREACH_INOTIFY_EVENT()
This also makes the macro check if the event is actually in the buffer, and if it is not, then log about that and finish the loop.
Diffstat (limited to 'src/basic/inotify-util.h')
-rw-r--r--src/basic/inotify-util.h27
1 files changed, 23 insertions, 4 deletions
diff --git a/src/basic/inotify-util.h b/src/basic/inotify-util.h
index 88af08688f..eaf1922bab 100644
--- a/src/basic/inotify-util.h
+++ b/src/basic/inotify-util.h
@@ -6,12 +6,31 @@
#include <stddef.h>
#include <sys/inotify.h>
+#include "log.h"
+
#define INOTIFY_EVENT_MAX (offsetof(struct inotify_event, name) + NAME_MAX + 1)
-#define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
- for ((e) = &buffer.ev; \
- (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \
- (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len))
+#define _FOREACH_INOTIFY_EVENT(e, buffer, sz, log_level, start, end) \
+ for (struct inotify_event \
+ *start = &((buffer).ev), \
+ *end = (struct inotify_event*) ((uint8_t*) start + (sz)), \
+ *e = start; \
+ (uint8_t*) e + sizeof(struct inotify_event) <= (uint8_t*) end && \
+ (uint8_t*) e + sizeof(struct inotify_event) + e->len <= (uint8_t*) end ? true : \
+ ({ \
+ log_full(log_level, "Received invalid inotify event, ignoring."); \
+ false; \
+ }); \
+ e = (struct inotify_event*) ((uint8_t*) e + sizeof(struct inotify_event) + e->len))
+
+#define _FOREACH_INOTIFY_EVENT_FULL(e, buffer, sz, log_level) \
+ _FOREACH_INOTIFY_EVENT(e, buffer, sz, log_level, UNIQ_T(start, UNIQ), UNIQ_T(end, UNIQ))
+
+#define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
+ _FOREACH_INOTIFY_EVENT_FULL(e, buffer, sz, LOG_DEBUG)
+
+#define FOREACH_INOTIFY_EVENT_WARN(e, buffer, sz) \
+ _FOREACH_INOTIFY_EVENT_FULL(e, buffer, sz, LOG_WARNING)
union inotify_event_buffer {
struct inotify_event ev;