diff --git a/src/shared/rm-rf.h b/src/shared/rm-rf.h index 6e52bbb41d..2e107da0e1 100644 --- a/src/shared/rm-rf.h +++ b/src/shared/rm-rf.h @@ -34,8 +34,20 @@ static inline int rm_rf(const char *path, RemoveFlags flags) { return rm_rf_at(AT_FDCWD, path, flags); } -/* Useful for usage with _cleanup_(), destroys a directory and frees the pointer */ -static inline char *rm_rf_physical_and_free(char *p) { +/* Useful for using with _cleanup_(), destroys a directory on a temporary file system. */ +static inline const char* rm_rf_safe(const char *p) { + PROTECT_ERRNO; + + if (!p) + return NULL; + + (void) rm_rf(p, REMOVE_ROOT|REMOVE_MISSING_OK|REMOVE_CHMOD); + return NULL; +} +DEFINE_TRIVIAL_CLEANUP_FUNC(const char*, rm_rf_safe); + +/* Similar as above, but allow to destroy a directory on a physical file system, and also frees the pointer. */ +static inline char* rm_rf_physical_and_free(char *p) { PROTECT_ERRNO; if (!p) @@ -46,8 +58,8 @@ static inline char *rm_rf_physical_and_free(char *p) { } DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rm_rf_physical_and_free); -/* Similar as above, but also has magic btrfs subvolume powers */ -static inline char *rm_rf_subvolume_and_free(char *p) { +/* Similar as above, but also has magic btrfs subvolume powers. */ +static inline char* rm_rf_subvolume_and_free(char *p) { PROTECT_ERRNO; if (!p) diff --git a/src/udev/udev-watch.c b/src/udev/udev-watch.c index 82db39fecd..0254cf6ed9 100644 --- a/src/udev/udev-watch.c +++ b/src/udev/udev-watch.c @@ -44,50 +44,39 @@ int device_new_from_watch_handle_at(sd_device **ret, int dirfd, int wd) { } int udev_watch_restore(int inotify_fd) { - _cleanup_closedir_ DIR *dir = NULL; + _cleanup_(rm_rf_safep) const char *old = "/run/udev/watch.old/"; int r; /* Move any old watches directory out of the way, and then restore the watches. */ assert(inotify_fd >= 0); - (void) rm_rf("/run/udev/watch.old", REMOVE_ROOT); - - if (rename("/run/udev/watch", "/run/udev/watch.old") < 0) { + rm_rf_safe(old); + if (rename("/run/udev/watch/", old) < 0) { if (errno == ENOENT) return 0; - r = log_warning_errno(errno, - "Failed to move watches directory '/run/udev/watch/'. " - "Old watches will not be restored: %m"); - goto finalize; + return log_warning_errno(errno, "Failed to move watches directory '/run/udev/watch/': %m"); } - dir = opendir("/run/udev/watch.old"); - if (!dir) { - r = log_warning_errno(errno, - "Failed to open old watches directory '/run/udev/watch.old/'. " - "Old watches will not be restored: %m"); - goto finalize; - } + _cleanup_closedir_ DIR *dir = opendir(old); + if (!dir) + return log_warning_errno(errno, "Failed to open old watches directory '%s': %m", old); - FOREACH_DIRENT_ALL(de, dir, break) { - _cleanup_(sd_device_unrefp) sd_device *dev = NULL; - int wd; + FOREACH_DIRENT(de, dir, break) { /* For backward compatibility, read symlink from watch handle to device ID. This is necessary * when udevd is restarted after upgrading from v248 or older. The new format (ID -> wd) was * introduced by e7f781e473f5119bf9246208a6de9f6b76a39c5d (v249). */ - if (dot_or_dot_dot(de->d_name)) - continue; - + int wd; if (safe_atoi(de->d_name, &wd) < 0) - continue; + continue; /* This should be ID -> wd symlink. Skipping. */ + _cleanup_(sd_device_unrefp) sd_device *dev = NULL; r = device_new_from_watch_handle_at(&dev, dirfd(dir), wd); if (r < 0) { - log_full_errno(r == -ENODEV ? LOG_DEBUG : LOG_WARNING, r, + log_full_errno(ERRNO_IS_NEG_DEVICE_ABSENT(r) ? LOG_DEBUG : LOG_WARNING, r, "Failed to create sd_device object from saved watch handle '%i', ignoring: %m", wd); continue; @@ -96,11 +85,7 @@ int udev_watch_restore(int inotify_fd) { (void) udev_watch_begin(inotify_fd, dev); } - r = 0; - -finalize: - (void) rm_rf("/run/udev/watch.old", REMOVE_ROOT); - return r; + return 0; } static int udev_watch_clear(sd_device *dev, int dirfd, int *ret_wd) {