X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fudev%2Fudev-watch.c;h=1091ec8d69765cbf4f5c6606d3d5e7bbbc232ac7;hp=228d18fedf4935707abaa20f11c558004f2dfca7;hb=667e392408d6b56db981d8e76c31990501d0faf3;hpb=6df831f25ebc9f55cd939f04392dad9237706e45 diff --git a/src/udev/udev-watch.c b/src/udev/udev-watch.c index 228d18fed..1091ec8d6 100644 --- a/src/udev/udev-watch.c +++ b/src/udev/udev-watch.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004-2010 Kay Sievers + * Copyright (C) 2004-2012 Kay Sievers * Copyright (C) 2009 Canonical Ltd. * Copyright (C) 2009 Scott James Remnant * @@ -40,7 +40,7 @@ int udev_watch_init(struct udev *udev) { inotify_fd = inotify_init1(IN_CLOEXEC); if (inotify_fd < 0) - err(udev, "inotify_init failed: %m\n"); + log_error("inotify_init failed: %m\n"); return inotify_fd; } @@ -49,45 +49,37 @@ int udev_watch_init(struct udev *udev) */ void udev_watch_restore(struct udev *udev) { - char filename[UTIL_PATH_SIZE], oldname[UTIL_PATH_SIZE]; - if (inotify_fd < 0) return; - util_strscpyl(oldname, sizeof(oldname), udev_get_run_path(udev), "/watch.old", NULL); - util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/watch", NULL); - if (rename(filename, oldname) == 0) { + if (rename("/run/udev/watch", "/run/udev/watch.old") == 0) { DIR *dir; struct dirent *ent; - dir = opendir(oldname); + dir = opendir("/run/udev/watch.old"); if (dir == NULL) { - err(udev, "unable to open old watches dir '%s', old watches will not be restored: %m", oldname); + log_error("unable to open old watches dir /run/udev/watch.old; old watches will not be restored: %m"); return; } for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) { char device[UTIL_PATH_SIZE]; - char *s; - size_t l; ssize_t len; struct udev_device *dev; if (ent->d_name[0] == '.') continue; - 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) + len = readlinkat(dirfd(dir), ent->d_name, device, sizeof(device)); + if (len <= 0 || len == (ssize_t)sizeof(device)) goto unlink; - s[len] = '\0'; + device[len] = '\0'; - dev = udev_device_new_from_id_filename(udev, s); + dev = udev_device_new_from_id_filename(udev, device); if (dev == NULL) goto unlink; - info(udev, "restoring old watch on '%s'\n", udev_device_get_devnode(dev)); + log_debug("restoring old watch on '%s'\n", udev_device_get_devnode(dev)); udev_watch_begin(udev, dev); udev_device_unref(dev); unlink: @@ -95,10 +87,10 @@ unlink: } closedir(dir); - rmdir(oldname); + rmdir("/run/udev/watch.old"); } else if (errno != ENOENT) { - err(udev, "unable to move watches dir '%s', old watches will not be restored: %m", filename); + log_error("unable to move watches dir /run/udev/watch; old watches will not be restored: %m"); } } @@ -110,16 +102,16 @@ void udev_watch_begin(struct udev *udev, struct udev_device *dev) if (inotify_fd < 0) return; - info(udev, "adding watch on '%s'\n", udev_device_get_devnode(dev)); + log_debug("adding watch on '%s'\n", udev_device_get_devnode(dev)); wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE); if (wd < 0) { - err(udev, "inotify_add_watch(%d, %s, %o) failed: %m\n", + log_error("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/watch/%d", udev_get_run_path(udev), wd); - util_create_path(udev, filename); + snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd); + mkdir_parents(filename, 0755); unlink(filename); symlink(udev_device_get_id_filename(dev), filename); @@ -138,10 +130,10 @@ void udev_watch_end(struct udev *udev, struct udev_device *dev) if (wd < 0) return; - info(udev, "removing watch on '%s'\n", udev_device_get_devnode(dev)); + log_debug("removing watch on '%s'\n", udev_device_get_devnode(dev)); inotify_rm_watch(inotify_fd, wd); - snprintf(filename, sizeof(filename), "%s/watch/%d", udev_get_run_path(udev), wd); + snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd); unlink(filename); udev_device_set_watch_handle(dev, -1); @@ -150,21 +142,17 @@ void udev_watch_end(struct udev *udev, struct udev_device *dev) struct udev_device *udev_watch_lookup(struct udev *udev, int wd) { char filename[UTIL_PATH_SIZE]; - char majmin[UTIL_PATH_SIZE]; - char *s; - size_t l; + char device[UTIL_NAME_SIZE]; ssize_t len; if (inotify_fd < 0 || wd < 0) return NULL; - snprintf(filename, sizeof(filename), "%s/watch/%d", udev_get_run_path(udev), wd); - s = majmin; - l = util_strpcpy(&s, sizeof(majmin), udev_get_sys_path(udev)); - len = readlink(filename, s, l); - if (len <= 0 || (size_t)len == l) + snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd); + len = readlink(filename, device, sizeof(device)); + if (len <= 0 || (size_t)len == sizeof(device)) return NULL; - s[len] = '\0'; + device[len] = '\0'; - return udev_device_new_from_id_filename(udev, s); + return udev_device_new_from_id_filename(udev, device); }