summaryrefslogtreecommitdiffstats
path: root/src/basic/inotify-util.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 15:35:18 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 15:35:18 +0000
commitb750101eb236130cf056c675997decbac904cc49 (patch)
treea5df1a06754bdd014cb975c051c83b01c9a97532 /src/basic/inotify-util.h
parentInitial commit. (diff)
downloadsystemd-b750101eb236130cf056c675997decbac904cc49.tar.xz
systemd-b750101eb236130cf056c675997decbac904cc49.zip
Adding upstream version 252.22.upstream/252.22upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--src/basic/inotify-util.h38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/basic/inotify-util.h b/src/basic/inotify-util.h
new file mode 100644
index 0000000..61951ff
--- /dev/null
+++ b/src/basic/inotify-util.h
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#include <inttypes.h>
+#include <limits.h>
+#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, log_level, start, end) \
+ for (struct inotify_event \
+ *start = &((buffer).ev), \
+ *end = (struct inotify_event*) ((uint8_t*) start + (sz)), \
+ *e = start; \
+ (size_t) ((uint8_t*) end - (uint8_t*) e) >= sizeof(struct inotify_event) && \
+ ((size_t) ((uint8_t*) end - (uint8_t*) e) >= sizeof(struct inotify_event) + e->len || \
+ (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;
+ uint8_t raw[INOTIFY_EVENT_MAX];
+};
+
+int inotify_add_watch_fd(int fd, int what, uint32_t mask);
+int inotify_add_watch_and_warn(int fd, const char *pathname, uint32_t mask);