chiark / gitweb /
use CLOEXEC flags instead of fcntl()
[elogind.git] / udev / udev-watch.c
index 8bc8775..d67083b 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#ifdef HAVE_INOTIFY
 #include <sys/inotify.h>
-#endif
 
 #include "udev.h"
 
-int inotify_fd = -1;
+static int inotify_fd = -1;
 
 /* inotify descriptor, will be shared with rules directory;
  * set to cloexec since we need our children to be able to add
  * watches for us
  */
-void udev_watch_init(struct udev *udev)
+int udev_watch_init(struct udev *udev)
 {
-       inotify_fd = inotify_init();
-       if (inotify_fd >= 0)
-               util_set_fd_cloexec(inotify_fd);
-       else if (errno == ENOSYS)
-               info(udev, "unable to use inotify, udevd will not monitor rule files changes\n");
-       else
+       inotify_fd = inotify_init1(IN_CLOEXEC);
+       if (inotify_fd < 0)
                err(udev, "inotify_init failed: %m\n");
+       return inotify_fd;
 }
 
 /* move any old watches directory out of the way, and then restore
@@ -72,8 +67,7 @@ void udev_watch_restore(struct udev *udev)
                }
 
                for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) {
-                       char path[UTIL_PATH_SIZE];
-                       char buf[UTIL_PATH_SIZE];
+                       char device[UTIL_PATH_SIZE];
                        char *s;
                        size_t l;
                        ssize_t len;
@@ -82,19 +76,18 @@ void udev_watch_restore(struct udev *udev)
                        if (ent->d_name[0] < '0' || ent->d_name[0] > '9')
                                continue;
 
-                       util_strscpyl(path, sizeof(path), oldname, "/", ent->d_name, NULL);
-                       s = buf;
-                       l = util_strpcpy(&s, sizeof(buf), udev_get_sys_path(udev));
-                       len = readlink(path, s, l);
+                       s = device;
+                       l = util_strpcpy(&s, sizeof(device), udev_get_sys_path(udev));
+                       len = readlinkat(dirfd(dir), ent->d_name, s, l);
                        if (len <= 0 || len >= (ssize_t)l) {
-                               unlink(path);
+                               unlinkat(dirfd(dir), ent->d_name, 0);
                                continue;
                        }
                        s[len] = '\0';
-                       dbg(udev, "old watch to '%s' found\n", buf);
-                       dev = udev_device_new_from_syspath(udev, buf);
+                       dbg(udev, "old watch to '%s' found\n", device);
+                       dev = udev_device_new_from_syspath(udev, device);
                        if (dev == NULL) {
-                               unlink(path);
+                               unlinkat(dirfd(dir), ent->d_name, 0);
                                continue;
                        }
 
@@ -102,7 +95,7 @@ void udev_watch_restore(struct udev *udev)
                        udev_watch_begin(udev, dev);
 
                        udev_device_unref(dev);
-                       unlink(path);
+                       unlinkat(dirfd(dir), ent->d_name, 0);
                }
 
                closedir(dir);
@@ -126,6 +119,7 @@ void udev_watch_begin(struct udev *udev, struct udev_device *dev)
        if (wd < 0) {
                err(udev, "inotify_add_watch(%d, %s, %o) failed: %m\n",
                    inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
+               return;
        }
 
        snprintf(filename, sizeof(filename), "%s/.udev/watch/%d", udev_get_dev_path(udev), wd);