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.
This commit is contained in:
Yu Watanabe
2022-03-24 23:12:34 +00:00
committed by Luca Boccassi
parent f72f802118
commit 00adc340bb
8 changed files with 27 additions and 15 deletions
+23 -4
View File
@@ -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;
-1
View File
@@ -432,7 +432,6 @@ int acquire_terminal(
for (;;) {
union inotify_event_buffer buffer;
struct inotify_event *e;
ssize_t l;
if (timeout != USEC_INFINITY) {