/* move any old watches directory out of the way, and then restore * the watches */ 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_dev_path(udev), "/.udev/watch.old", NULL); util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/watch", NULL); if (rename(filename, oldname) == 0) { DIR *dir; struct dirent *ent; dir = opendir(oldname); if (dir == NULL) { err(udev, "unable to open old watches dir '%s', old watches will not be restored: %m", oldname); 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; int maj, min; char type; 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) goto unlink; s[len] = '\0'; if (sscanf(s, "%c%i:%i", &type, &maj, &min) != 3) goto unlink; dev = udev_device_new_from_devnum(udev, type, makedev(maj, min)); if (dev == NULL) goto unlink; info(udev, "restoring old watch on '%s'\n", udev_device_get_devnode(dev)); udev_watch_begin(udev, dev); udev_device_unref(dev); unlink: unlinkat(dirfd(dir), ent->d_name, 0); } closedir(dir); rmdir(oldname); } else if (errno != ENOENT) { err(udev, "unable to move watches dir '%s', old watches will not be restored: %m", filename); } }
/* move any old watches directory out of the way, and then restore * the watches */ void udev_watch_restore(struct udev *udev) { if (inotify_fd < 0) return; if (rename("/run/udev/watch", "/run/udev/watch.old") == 0) { DIR *dir; struct dirent *ent; dir = opendir("/run/udev/watch.old"); if (dir == NULL) { 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]; ssize_t len; struct udev_device *dev; if (ent->d_name[0] == '.') continue; len = readlinkat(dirfd(dir), ent->d_name, device, sizeof(device)); if (len <= 0 || len == (ssize_t)sizeof(device)) goto unlink; device[len] = '\0'; dev = udev_device_new_from_device_id(udev, device); if (dev == NULL) goto unlink; log_debug("restoring old watch on '%s'", udev_device_get_devnode(dev)); udev_watch_begin(udev, dev); udev_device_unref(dev); unlink: unlinkat(dirfd(dir), ent->d_name, 0); } closedir(dir); rmdir("/run/udev/watch.old"); } else if (errno != ENOENT) { log_error("unable to move watches dir /run/udev/watch; old watches will not be restored: %m"); } }
/* move any old watches directory out of the way, and then restore * the watches */ void udev_watch_restore(struct udev *udev) { char filename[UTIL_PATH_SIZE], oldname[UTIL_PATH_SIZE]; if (inotify_fd < 0) return; util_strlcpy(oldname, udev_get_dev_path(udev), sizeof(oldname)); util_strlcat(oldname, "/.udev/watch.old", sizeof(oldname)); util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename)); util_strlcat(filename, "/.udev/watch", sizeof(filename)); if (rename(filename, oldname) == 0) { DIR *dir; struct dirent *ent; dir = opendir(oldname); if (dir == NULL) { err(udev, "unable to open old watches dir '%s', old watches will not be restored: %m", oldname); return; } while ((ent = readdir(dir)) != NULL) { char path[UTIL_PATH_SIZE]; char buf[UTIL_PATH_SIZE]; ssize_t syslen; ssize_t len; struct udev_device *dev; if (ent->d_name[0] < '0' || ent->d_name[0] > '9') continue; util_strlcpy(path, oldname, sizeof(path)); util_strlcat(path, "/", sizeof(path)); util_strlcat(path, ent->d_name, sizeof(path)); syslen = util_strlcpy(buf, udev_get_sys_path(udev), sizeof(buf)); len = readlink(path, &buf[syslen], sizeof(buf)-syslen); if (len <= 0 || len >= (ssize_t)(sizeof(buf)-syslen)) { unlink(path); continue; } buf[syslen + len] = '\0'; dbg(udev, "old watch to '%s' found\n", buf); dev = udev_device_new_from_syspath(udev, buf); if (dev == NULL) { unlink(path); continue; } info(udev, "restoring old watch on '%s'\n", udev_device_get_devnode(dev)); udev_watch_begin(udev, dev); udev_device_unref(dev); unlink(path); } closedir(dir); rmdir(oldname); } else if (errno != ENOENT) { err(udev, "unable to move watches dir '%s', old watches will not be restored: %m", filename); } }