diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2018-09-14 21:13:29 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2018-10-10 21:21:14 +0200 |
commit | 70068602713e8f441c5ddc2618f007f24488e422 (patch) | |
tree | e8b6ae871f6b8d7511e2d8bc5d58c028178a9d7b /src/udev/udev-watch.c | |
parent | udev: drop unused function udev_build_argv() (diff) | |
download | systemd-70068602713e8f441c5ddc2618f007f24488e422.tar.xz systemd-70068602713e8f441c5ddc2618f007f24488e422.zip |
udev-watch: replace udev_device by sd_device and modernize code a bit
Diffstat (limited to 'src/udev/udev-watch.c')
-rw-r--r-- | src/udev/udev-watch.c | 203 |
1 files changed, 123 insertions, 80 deletions
diff --git a/src/udev/udev-watch.c b/src/udev/udev-watch.c index 439117181a..d3e4391ee9 100644 --- a/src/udev/udev-watch.c +++ b/src/udev/udev-watch.c @@ -4,136 +4,179 @@ * 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 "device-private.h" #include "dirent-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 - */ + * 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 log_error_errno(errno, "Failed to create inotify descriptor: %m"); + return inotify_fd; } -/* move any old watches directory out of the way, and then restore - * the watches - */ -void udev_watch_restore(void) { - if (inotify_fd < 0) - return; +/* 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 (rename("/run/udev/watch", "/run/udev/watch.old") == 0) { - DIR *dir; - struct dirent *ent; + if (inotify_fd < 0) + return log_error_errno(EINVAL, "Invalid inotify descriptor."); - 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; - } + if (rename("/run/udev/watch", "/run/udev/watch.old") < 0) { + if (errno != ENOENT) + return log_error_errno(errno, "Failed to move watches directory /run/udev/watch. Old watches will not be restored: %m"); - FOREACH_DIRENT_ALL(ent, dir, break) { - char device[UTIL_PATH_SIZE]; - ssize_t len; - struct udev_device *dev; + return 0; + } - if (ent->d_name[0] == '.') - continue; + dir = opendir("/run/udev/watch.old"); + if (!dir) + return log_error_errno(errno, "Failed to open old watches directory /run/udev/watch.old. Old watches will not be restored: %m"); + + FOREACH_DIRENT_ALL(ent, dir, break) { + _cleanup_(sd_device_unrefp) sd_device *dev = NULL; + char device[PATH_MAX]; + ssize_t len; + + if (ent->d_name[0] == '.') + continue; + + len = readlinkat(dirfd(dir), ent->d_name, device, sizeof(device)); + if (len <= 0) { + log_error_errno(errno, "Failed to read link '/run/udev/watch.old/%s', ignoring: %m", ent->d_name); + goto unlink; + } else if (len >= (ssize_t) sizeof(device)) { + log_error("Path specified by link '/run/udev/watch.old/%s' is truncated, ignoring.", ent->d_name); + goto unlink; + } + device[len] = '\0'; - len = readlinkat(dirfd(dir), ent->d_name, device, sizeof(device)); - if (len <= 0 || len == (ssize_t)sizeof(device)) - goto unlink; - device[len] = '\0'; + r = sd_device_new_from_device_id(&dev, device); + if (r < 0) { + log_error_errno(r, "Failed to create sd_device object for '%s', ignoring: %m", device); + goto unlink; + } - dev = udev_device_new_from_device_id(NULL, device); - if (dev == NULL) - goto unlink; + if (DEBUG_LOGGING) { + const char *devnode = NULL; - log_debug("restoring old watch on '%s'", udev_device_get_devnode(dev)); - udev_watch_begin(dev); - udev_device_unref(dev); -unlink: - (void) unlinkat(dirfd(dir), ent->d_name, 0); + (void) sd_device_get_devname(dev, &devnode); + log_debug("Restoring old watch on '%s'", strnull(devnode)); } + (void) udev_watch_begin(dev); +unlink: + (void) unlinkat(dirfd(dir), ent->d_name, 0); + } - closedir(dir); - rmdir("/run/udev/watch.old"); + (void) closedir(dir); + (void) rmdir("/run/udev/watch.old"); - } else if (errno != ENOENT) - log_error_errno(errno, "unable to move watches dir /run/udev/watch; old watches will not be restored: %m"); + return 0; } -void udev_watch_begin(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_error_errno(EINVAL, "Invalid inotify descriptor."); + + r = sd_device_get_devname(dev, &devnode); + if (r < 0) + return log_error_errno(r, "Failed to get device name: %m"); + + log_debug("Adding watch on '%s'", devnode); + wd = inotify_add_watch(inotify_fd, devnode, IN_CLOSE_WRITE); + if (wd < 0) + return log_error_errno(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) - log_error_errno(errno, "Failed to create symlink %s: %m", filename); + return log_error_errno(r, "Failed to create parent directory of '%s': %m", filename); + (void) unlink(filename); + + r = device_get_id_filename(dev, &id_filename); + if (r < 0) + return log_error_errno(r, "Failed to get device id-filename: %m"); + + if (symlink(id_filename, filename) < 0) + return log_error_errno(errno, "Failed to create symlink %s: %m", filename); - udev_device_set_watch_handle(dev, wd); + return 0; } -void udev_watch_end(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)]; + const char *devnode; + int wd, r; if (inotify_fd < 0) - return; + return log_error_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_error_errno(r, "Failed to get watch handle for device '%s', ignoring: %m", devnode); + + r = sd_device_get_devname(dev, &devnode); + if (r < 0) + return log_error_errno(r, "Failed to get device name: %m"); - log_debug("removing watch on '%s'", udev_device_get_devnode(dev)); - inotify_rm_watch(inotify_fd, wd); + log_debug("Removing watch on '%s'", devnode); + (void) inotify_rm_watch(inotify_fd, wd); xsprintf(filename, "/run/udev/watch/%d", wd); - unlink(filename); + (void) unlink(filename); - udev_device_set_watch_handle(dev, -1); + device_set_watch_handle(dev, -1); + + return 0; } -struct udev_device *udev_watch_lookup(int wd) { - char filename[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)]; - char device[UTIL_NAME_SIZE]; +int udev_watch_lookup(int wd, sd_device **ret) { + char filename[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)], device[PATH_MAX]; ssize_t len; + int r; + + assert(ret); + + if (inotify_fd < 0) + return log_error_errno(EINVAL, "Invalid inotify descriptor."); - if (inotify_fd < 0 || wd < 0) - return NULL; + if (wd < 0) + return log_error_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; + if (len <= 0) { + if (errno != ENOENT) + return log_error_errno(errno, "Failed to read link '%s': %m", filename); + return 0; + } else if (len >= (ssize_t) sizeof(device)) + return log_error_errno(ENAMETOOLONG, "Path specified by link '%s' is truncated.", filename); device[len] = '\0'; - return udev_device_new_from_device_id(NULL, device); + r = sd_device_new_from_device_id(ret, device); + if (r < 0) + return log_error_errno(r, "Failed to create sd_device object for '%s': %m", device); + + return 0; } |