]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/udev/udev-watch.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / udev / udev-watch.c
index 1ae89334bbd11f52f4e635b177c32b7410e4a6ef..68b51d04a3598ece06ab7a6c5063426b306450ee 100644 (file)
  * Copyright © 2009 Scott James Remnant <scott@netsplit.com>
  */
 
-#include <errno.h>
-#include <stddef.h>
-#include <stdio.h>
 #include <sys/inotify.h>
 #include <unistd.h>
 
+#include "alloc-util.h"
+#include "device-private.h"
+#include "device-util.h"
 #include "dirent-util.h"
+#include "fs-util.h"
+#include "mkdir.h"
 #include "stdio-util.h"
-#include "udev.h"
+#include "udev-watch.h"
 
 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
- */
-int udev_watch_init(struct udev *udev) {
+ * watches for us. */
+int udev_watch_init(void) {
         inotify_fd = inotify_init1(IN_CLOEXEC);
         if (inotify_fd < 0)
-                log_error_errno(errno, "inotify_init failed: %m");
+                return -errno;
+
         return inotify_fd;
 }
 
-/* move any old watches directory out of the way, and then restore
- * the watches
- */
-void udev_watch_restore(struct udev *udev) {
+/* Move any old watches directory out of the way, and then restore the watches. */
+int udev_watch_restore(void) {
+        struct dirent *ent;
+        DIR *dir;
+        int r;
+
         if (inotify_fd < 0)
-                return;
+                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+                                       "Invalid inotify descriptor.");
 
-        if (rename("/run/udev/watch", "/run/udev/watch.old") == 0) {
-                DIR *dir;
-                struct dirent *ent;
+        if (rename("/run/udev/watch", "/run/udev/watch.old") < 0) {
+                if (errno != ENOENT)
+                        return log_warning_errno(errno, "Failed to move watches directory /run/udev/watch. Old watches will not be restored: %m");
 
-                dir = opendir("/run/udev/watch.old");
-                if (dir == NULL) {
-                        log_error_errno(errno, "unable to open old watches dir /run/udev/watch.old; old watches will not be restored: %m");
-                        return;
-                }
+                return 0;
+        }
 
-                FOREACH_DIRENT_ALL(ent, dir, break) {
-                        char device[UTIL_PATH_SIZE];
-                        ssize_t len;
-                        struct udev_device *dev;
+        dir = opendir("/run/udev/watch.old");
+        if (!dir)
+                return log_warning_errno(errno, "Failed to open old watches directory /run/udev/watch.old. Old watches will not be restored: %m");
 
-                        if (ent->d_name[0] == '.')
-                                continue;
+        FOREACH_DIRENT_ALL(ent, dir, break) {
+                _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
+                _cleanup_free_ char *device = NULL;
 
-                        len = readlinkat(dirfd(dir), ent->d_name, device, sizeof(device));
-                        if (len <= 0 || len == (ssize_t)sizeof(device))
-                                goto unlink;
-                        device[len] = '\0';
+                if (ent->d_name[0] == '.')
+                        continue;
 
-                        dev = udev_device_new_from_device_id(udev, device);
-                        if (dev == NULL)
-                                goto unlink;
+                r = readlinkat_malloc(dirfd(dir), ent->d_name, &device);
+                if (r < 0) {
+                        log_debug_errno(r, "Failed to read link '/run/udev/watch.old/%s', ignoring: %m", ent->d_name);
+                        goto unlink;
+                }
 
-                        log_debug("restoring old watch on '%s'", udev_device_get_devnode(dev));
-                        udev_watch_begin(udev, dev);
-                        udev_device_unref(dev);
-unlink:
-                        (void) unlinkat(dirfd(dir), ent->d_name, 0);
+                r = sd_device_new_from_device_id(&dev, device);
+                if (r < 0) {
+                        log_debug_errno(r, "Failed to create sd_device object for '%s', ignoring: %m", device);
+                        goto unlink;
                 }
 
-                closedir(dir);
-                rmdir("/run/udev/watch.old");
+                log_device_debug(dev, "Restoring old watch");
+                (void) udev_watch_begin(dev);
+unlink:
+                (void) unlinkat(dirfd(dir), ent->d_name, 0);
+        }
 
-        } else if (errno != ENOENT)
-                log_error_errno(errno, "unable to move watches dir /run/udev/watch; old watches will not be restored: %m");
+        (void) closedir(dir);
+        (void) rmdir("/run/udev/watch.old");
+
+        return 0;
 }
 
-void udev_watch_begin(struct udev *udev, struct udev_device *dev) {
-        char filename[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
-        int wd;
-        int r;
+int udev_watch_begin(sd_device *dev) {
+        char filename[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
+        const char *devnode, *id_filename;
+        int wd, r;
 
         if (inotify_fd < 0)
-                return;
-
-        log_debug("adding watch on '%s'", udev_device_get_devnode(dev));
-        wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
-        if (wd < 0) {
-                log_error_errno(errno, "inotify_add_watch(%d, %s, %o) failed: %m",
-                                inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
-                return;
-        }
+                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+                                       "Invalid inotify descriptor.");
+
+        r = sd_device_get_devname(dev, &devnode);
+        if (r < 0)
+                return log_device_error_errno(dev, r, "Failed to get device name: %m");
+
+        log_device_debug(dev, "Adding watch on '%s'", devnode);
+        wd = inotify_add_watch(inotify_fd, devnode, IN_CLOSE_WRITE);
+        if (wd < 0)
+                return log_device_full(dev,
+                                       errno == ENOENT ? LOG_DEBUG : LOG_ERR,
+                                       errno,
+                                       "Failed to add device '%s' to watch: %m", devnode);
+
+        device_set_watch_handle(dev, wd);
 
         xsprintf(filename, "/run/udev/watch/%d", wd);
-        mkdir_parents(filename, 0755);
-        unlink(filename);
-        r = symlink(udev_device_get_id_filename(dev), filename);
+        r = mkdir_parents(filename, 0755);
+        if (r < 0)
+                return log_device_error_errno(dev, r, "Failed to create parent directory of '%s': %m", filename);
+        (void) unlink(filename);
+
+        r = device_get_id_filename(dev, &id_filename);
         if (r < 0)
-                log_error_errno(errno, "Failed to create symlink %s: %m", filename);
+                return log_device_error_errno(dev, r, "Failed to get device id-filename: %m");
 
-        udev_device_set_watch_handle(dev, wd);
+        if (symlink(id_filename, filename) < 0)
+                return log_device_error_errno(dev, errno, "Failed to create symlink %s: %m", filename);
+
+        return 0;
 }
 
-void udev_watch_end(struct udev *udev, struct udev_device *dev) {
-        int wd;
-        char filename[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
+int udev_watch_end(sd_device *dev) {
+        char filename[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
+        int wd, r;
 
         if (inotify_fd < 0)
-                return;
+                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+                                       "Invalid inotify descriptor.");
 
-        wd = udev_device_get_watch_handle(dev);
-        if (wd < 0)
-                return;
+        r = device_get_watch_handle(dev, &wd);
+        if (r == -ENOENT)
+                return 0;
+        if (r < 0)
+                return log_device_debug_errno(dev, r, "Failed to get watch handle, ignoring: %m");
 
-        log_debug("removing watch on '%s'", udev_device_get_devnode(dev));
-        inotify_rm_watch(inotify_fd, wd);
+        log_device_debug(dev, "Removing watch");
+        (void) inotify_rm_watch(inotify_fd, wd);
 
         xsprintf(filename, "/run/udev/watch/%d", wd);
-        unlink(filename);
+        (void) unlink(filename);
+
+        device_set_watch_handle(dev, -1);
 
-        udev_device_set_watch_handle(dev, -1);
+        return 0;
 }
 
-struct udev_device *udev_watch_lookup(struct udev *udev, int wd) {
-        char filename[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
-        char device[UTIL_NAME_SIZE];
-        ssize_t len;
+int udev_watch_lookup(int wd, sd_device **ret) {
+        char filename[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
+        _cleanup_free_ char *device = NULL;
+        int r;
+
+        assert(ret);
+
+        if (inotify_fd < 0)
+                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+                                       "Invalid inotify descriptor.");
 
-        if (inotify_fd < 0 || wd < 0)
-                return NULL;
+        if (wd < 0)
+                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+                                       "Invalid watch handle.");
 
         xsprintf(filename, "/run/udev/watch/%d", wd);
-        len = readlink(filename, device, sizeof(device));
-        if (len <= 0 || (size_t)len == sizeof(device))
-                return NULL;
-        device[len] = '\0';
+        r = readlink_malloc(filename, &device);
+        if (r == -ENOENT)
+                return 0;
+        if (r < 0)
+                return log_debug_errno(r, "Failed to read link '%s': %m", filename);
+
+        r = sd_device_new_from_device_id(ret, device);
+        if (r == -ENODEV)
+                return 0;
+        if (r < 0)
+                return log_debug_errno(r, "Failed to create sd_device object for '%s': %m", device);
 
-        return udev_device_new_from_device_id(udev, device);
+        return 1;
 }