]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
inotify-util: improve reported error codes when inotify_add_watch() fails
authorLennart Poettering <lennart@poettering.net>
Tue, 9 Nov 2021 11:40:39 +0000 (12:40 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 9 Nov 2021 11:52:07 +0000 (12:52 +0100)
src/basic/inotify-util.c

index 848f8590fab2e7c44f666a8ff6b32a37004ebd9e..6da974dec03315089787f1f3ffb11c01f95b4270 100644 (file)
@@ -2,14 +2,26 @@
 
 #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;
+        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)
-                return -errno;
+        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;
 }