* 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;
}
#include "cgroup-util.h"
#include "cpu-set-util.h"
#include "dev-setup.h"
+#include "device-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-util.h"
#include "fs-util.h"
#include "hashmap.h"
#include "io-util.h"
+#include "libudev-device-internal.h"
#include "list.h"
#include "netlink-util.h"
#include "parse-util.h"
#include "string-util.h"
#include "terminal-util.h"
#include "udev-util.h"
+#include "udev-watch.h"
#include "udev.h"
#include "user-util.h"
/* apply/restore inotify watch */
if (udev_event->inotify_watch) {
- udev_watch_begin(dev);
+ udev_watch_begin(dev->device);
udev_device_update_db(dev);
}
return 1;
}
-static int synthesize_change(struct udev_device *dev) {
- char filename[UTIL_PATH_SIZE];
+static int synthesize_change(sd_device *dev) {
+ const char *subsystem, *sysname, *devname, *syspath, *devtype;
+ char filename[PATH_MAX];
int r;
- if (streq_ptr("block", udev_device_get_subsystem(dev)) &&
- streq_ptr("disk", udev_device_get_devtype(dev)) &&
- !startswith(udev_device_get_sysname(dev), "dm-")) {
- bool part_table_read = false;
- bool has_partitions = false;
+ r = sd_device_get_subsystem(dev, &subsystem);
+ if (r < 0)
+ return r;
+
+ r = sd_device_get_sysname(dev, &sysname);
+ if (r < 0)
+ return r;
+
+ r = sd_device_get_devname(dev, &devname);
+ if (r < 0)
+ return r;
+
+ r = sd_device_get_syspath(dev, &syspath);
+ if (r < 0)
+ return r;
+
+ r = sd_device_get_devtype(dev, &devtype);
+ if (r < 0)
+ return r;
+
+ if (streq_ptr("block", subsystem) &&
+ streq_ptr("disk", devtype) &&
+ !startswith(sysname, "dm-")) {
+ _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
+ bool part_table_read = false, has_partitions = false;
+ sd_device *d;
int fd;
- _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
- struct udev_list_entry *item;
/*
* Try to re-read the partition table. This only succeeds if
* partition table is found, and we will not get an event for
* the disk.
*/
- fd = open(udev_device_get_devnode(dev), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
+ fd = open(devname, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
if (fd >= 0) {
r = flock(fd, LOCK_EX|LOCK_NB);
if (r >= 0)
}
/* search for partitions */
- e = udev_enumerate_new(NULL);
- if (!e)
- return -ENOMEM;
-
- r = udev_enumerate_add_match_parent(e, dev);
+ r = sd_device_enumerator_new(&e);
if (r < 0)
return r;
- r = udev_enumerate_add_match_subsystem(e, "block");
+ r = sd_device_enumerator_allow_uninitialized(e);
if (r < 0)
return r;
- r = udev_enumerate_scan_devices(e);
+ r = sd_device_enumerator_add_match_parent(e, dev);
if (r < 0)
return r;
- udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
- _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
+ r = sd_device_enumerator_add_match_subsystem(e, "block", true);
+ if (r < 0)
+ return r;
- d = udev_device_new_from_syspath(NULL, udev_list_entry_get_name(item));
- if (!d)
- continue;
+ FOREACH_DEVICE(e, d) {
+ const char *t;
- if (!streq_ptr("partition", udev_device_get_devtype(d)))
+ if (sd_device_get_devtype(d, &t) < 0 ||
+ !streq("partition", t))
continue;
has_partitions = true;
* We have partitions but re-reading the partition table did not
* work, synthesize "change" for the disk and all partitions.
*/
- log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
- strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
+ log_debug("Device '%s' is closed, synthesising 'change'", devname);
+ strscpyl(filename, sizeof(filename), syspath, "/uevent", NULL);
write_string_file(filename, "change", WRITE_STRING_FILE_CREATE);
- udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
- _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
+ FOREACH_DEVICE(e, d) {
+ const char *t, *n, *s;
- d = udev_device_new_from_syspath(NULL, udev_list_entry_get_name(item));
- if (!d)
+ if (sd_device_get_devtype(d, &t) < 0 ||
+ !streq("partition", t))
continue;
- if (!streq_ptr("partition", udev_device_get_devtype(d)))
+ if (sd_device_get_devname(d, &n) < 0 ||
+ sd_device_get_syspath(d, &s) < 0)
continue;
- log_debug("device %s closed, synthesising partition '%s' 'change'",
- udev_device_get_devnode(dev), udev_device_get_devnode(d));
- strscpyl(filename, sizeof(filename), udev_device_get_syspath(d), "/uevent", NULL);
+ log_debug("Device '%s' is closed, synthesising partition '%s' 'change'", devname, n);
+ strscpyl(filename, sizeof(filename), s, "/uevent", NULL);
write_string_file(filename, "change", WRITE_STRING_FILE_CREATE);
}
return 0;
}
- log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
- strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
+ log_debug("Device %s is closed, synthesising 'change'", devname);
+ strscpyl(filename, sizeof(filename), syspath, "/uevent", NULL);
write_string_file(filename, "change", WRITE_STRING_FILE_CREATE);
return 0;
}
FOREACH_INOTIFY_EVENT(e, buffer, l) {
- _cleanup_(udev_device_unrefp) struct udev_device *dev = NULL;
+ _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
+ const char *devnode;
+
+ if (udev_watch_lookup(e->wd, &dev) < 0)
+ continue;
- dev = udev_watch_lookup(e->wd);
- if (!dev)
+ if (sd_device_get_devname(dev, &devnode) < 0)
continue;
- log_debug("inotify event: %x for %s", e->mask, udev_device_get_devnode(dev));
+ log_debug("inotify event: %x for %s", e->mask, devnode);
if (e->mask & IN_CLOSE_WRITE) {
synthesize_change(dev);