summaryrefslogtreecommitdiffstats
path: root/src/basic/inotify-util.c
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.c
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.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/basic/inotify-util.c b/src/basic/inotify-util.c
new file mode 100644
index 0000000..6da974d
--- /dev/null
+++ b/src/basic/inotify-util.c
@@ -0,0 +1,41 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "fd-util.h"
+#include "inotify-util.h"
+#include "stat-util.h"
+
+int inotify_add_watch_fd(int fd, int what, uint32_t mask) {
+ int wd, r;
+
+ /* This is like inotify_add_watch(), except that the file to watch is not referenced by a path, but by an fd */
+ wd = inotify_add_watch(fd, FORMAT_PROC_FD_PATH(what), mask);
+ if (wd < 0) {
+ if (errno != ENOENT)
+ return -errno;
+
+ /* Didn't work with ENOENT? If so, then either /proc/ isn't mounted, or the fd is bad */
+ r = proc_mounted();
+ if (r == 0)
+ return -ENOSYS;
+ if (r > 0)
+ return -EBADF;
+
+ return -ENOENT; /* OK, no clue, let's propagate the original error */
+ }
+
+ return wd;
+}
+
+int inotify_add_watch_and_warn(int fd, const char *pathname, uint32_t mask) {
+ int wd;
+
+ wd = inotify_add_watch(fd, pathname, mask);
+ if (wd < 0) {
+ if (errno == ENOSPC)
+ return log_error_errno(errno, "Failed to add a watch for %s: inotify watch limit reached", pathname);
+
+ return log_error_errno(errno, "Failed to add a watch for %s: %m", pathname);
+ }
+
+ return wd;
+}