From: Yu Watanabe Date: Thu, 14 Apr 2022 21:38:33 +0000 (+0900) Subject: sd-device: move device_new_from_watch_handle_at() to udev-watch.c X-Git-Tag: v252-rc1~206^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3fb94c70621d73f656e02c016400075175b8b0bf;p=thirdparty%2Fsystemd.git sd-device: move device_new_from_watch_handle_at() to udev-watch.c And drop unused watch handle related functions. --- diff --git a/src/libsystemd/sd-device/device-internal.h b/src/libsystemd/sd-device/device-internal.h index 09325aae045..9a4533a8f61 100644 --- a/src/libsystemd/sd-device/device-internal.h +++ b/src/libsystemd/sd-device/device-internal.h @@ -21,8 +21,6 @@ struct sd_device { */ unsigned database_version; - int watch_handle; - sd_device *parent; OrderedHashmap *properties; diff --git a/src/libsystemd/sd-device/device-private.c b/src/libsystemd/sd-device/device-private.c index a4538999107..9e120f734ef 100644 --- a/src/libsystemd/sd-device/device-private.c +++ b/src/libsystemd/sd-device/device-private.c @@ -619,144 +619,6 @@ int device_get_devlink_priority(sd_device *device, int *ret) { return 0; } -int device_get_watch_handle(sd_device *device) { - char path_wd[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)]; - _cleanup_free_ char *buf = NULL; - const char *id, *path_id; - int wd, r; - - assert(device); - - if (device->watch_handle >= 0) - return device->watch_handle; - - r = device_get_device_id(device, &id); - if (r < 0) - return r; - - path_id = strjoina("/run/udev/watch/", id); - r = readlink_malloc(path_id, &buf); - if (r < 0) - return r; - - r = safe_atoi(buf, &wd); - if (r < 0) - return r; - - if (wd < 0) - return -EBADF; - - buf = mfree(buf); - xsprintf(path_wd, "/run/udev/watch/%d", wd); - r = readlink_malloc(path_wd, &buf); - if (r < 0) - return r; - - if (!streq(buf, id)) - return -EBADF; - - return device->watch_handle = wd; -} - -static void device_remove_watch_handle(sd_device *device) { - const char *id; - int wd; - - assert(device); - - /* First, remove the symlink from handle to device id. */ - wd = device_get_watch_handle(device); - if (wd >= 0) { - char path_wd[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)]; - - xsprintf(path_wd, "/run/udev/watch/%d", wd); - if (unlink(path_wd) < 0 && errno != ENOENT) - log_device_debug_errno(device, errno, - "sd-device: failed to remove %s, ignoring: %m", - path_wd); - } - - /* Next, remove the symlink from device id to handle. */ - if (device_get_device_id(device, &id) >= 0) { - const char *path_id; - - path_id = strjoina("/run/udev/watch/", id); - if (unlink(path_id) < 0 && errno != ENOENT) - log_device_debug_errno(device, errno, - "sd-device: failed to remove %s, ignoring: %m", - path_id); - } - - device->watch_handle = -1; -} - -int device_set_watch_handle(sd_device *device, int wd) { - char path_wd[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)]; - const char *id, *path_id; - int r; - - assert(device); - - if (wd >= 0 && wd == device_get_watch_handle(device)) - return 0; - - device_remove_watch_handle(device); - - if (wd < 0) - /* negative wd means that the caller requests to clear saved watch handle. */ - return 0; - - r = device_get_device_id(device, &id); - if (r < 0) - return r; - - path_id = strjoina("/run/udev/watch/", id); - xsprintf(path_wd, "/run/udev/watch/%d", wd); - - r = mkdir_parents(path_wd, 0755); - if (r < 0) - return r; - - if (symlink(id, path_wd) < 0) - return -errno; - - if (symlink(path_wd + STRLEN("/run/udev/watch/"), path_id) < 0) { - r = -errno; - if (unlink(path_wd) < 0 && errno != ENOENT) - log_device_debug_errno(device, errno, - "sd-device: failed to remove %s, ignoring: %m", - path_wd); - return r; - } - - device->watch_handle = wd; - - return 0; -} - -int device_new_from_watch_handle_at(sd_device **ret, int dirfd, int wd) { - char path_wd[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)]; - _cleanup_free_ char *id = NULL; - int r; - - assert(ret); - - if (wd < 0) - return -EBADF; - - if (dirfd >= 0) { - xsprintf(path_wd, "%d", wd); - r = readlinkat_malloc(dirfd, path_wd, &id); - } else { - xsprintf(path_wd, "/run/udev/watch/%d", wd); - r = readlink_malloc(path_wd, &id); - } - if (r < 0) - return r; - - return sd_device_new_from_device_id(ret, id); -} - int device_rename(sd_device *device, const char *name) { _cleanup_free_ char *new_syspath = NULL; const char *interface; diff --git a/src/libsystemd/sd-device/device-private.h b/src/libsystemd/sd-device/device-private.h index d33cddcf80f..724061a28cd 100644 --- a/src/libsystemd/sd-device/device-private.h +++ b/src/libsystemd/sd-device/device-private.h @@ -13,17 +13,12 @@ int device_new_from_mode_and_devnum(sd_device **ret, mode_t mode, dev_t devnum); int device_new_from_nulstr(sd_device **ret, char *nulstr, size_t len); int device_new_from_strv(sd_device **ret, char **strv); -int device_new_from_watch_handle_at(sd_device **ret, int dirfd, int wd); -static inline int device_new_from_watch_handle(sd_device **ret, int wd) { - return device_new_from_watch_handle_at(ret, -1, wd); -} int device_get_property_bool(sd_device *device, const char *key); int device_get_sysattr_unsigned(sd_device *device, const char *sysattr, unsigned *ret_value); int device_get_sysattr_bool(sd_device *device, const char *sysattr); int device_get_device_id(sd_device *device, const char **ret); int device_get_devlink_priority(sd_device *device, int *ret); -int device_get_watch_handle(sd_device *device); int device_get_devnode_mode(sd_device *device, mode_t *ret); int device_get_devnode_uid(sd_device *device, uid_t *ret); int device_get_devnode_gid(sd_device *device, gid_t *ret); @@ -34,7 +29,6 @@ int device_get_cached_sysattr_value(sd_device *device, const char *key, const ch void device_seal(sd_device *device); void device_set_is_initialized(sd_device *device); -int device_set_watch_handle(sd_device *device, int wd); void device_set_db_persist(sd_device *device); void device_set_devlink_priority(sd_device *device, int priority); int device_ensure_usec_initialized(sd_device *device, sd_device *device_old); diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index a7ba7dd6e98..91af4a27ed8 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -46,7 +46,6 @@ int device_new_aux(sd_device **ret) { *device = (sd_device) { .n_ref = 1, - .watch_handle = -1, .devmode = MODE_INVALID, .devuid = UID_INVALID, .devgid = GID_INVALID, diff --git a/src/udev/udev-watch.c b/src/udev/udev-watch.c index 0cc83ddb9c6..2a9ff2909b0 100644 --- a/src/udev/udev-watch.c +++ b/src/udev/udev-watch.c @@ -19,6 +19,29 @@ #include "string-util.h" #include "udev-watch.h" +int device_new_from_watch_handle_at(sd_device **ret, int dirfd, int wd) { + char path_wd[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)]; + _cleanup_free_ char *id = NULL; + int r; + + assert(ret); + + if (wd < 0) + return -EBADF; + + if (dirfd >= 0) { + xsprintf(path_wd, "%d", wd); + r = readlinkat_malloc(dirfd, path_wd, &id); + } else { + xsprintf(path_wd, "/run/udev/watch/%d", wd); + r = readlink_malloc(path_wd, &id); + } + if (r < 0) + return r; + + return sd_device_new_from_device_id(ret, id); +} + int udev_watch_restore(int inotify_fd) { _cleanup_closedir_ DIR *dir = NULL; int r; diff --git a/src/udev/udev-watch.h b/src/udev/udev-watch.h index d211d99f49d..c454deead3c 100644 --- a/src/udev/udev-watch.h +++ b/src/udev/udev-watch.h @@ -3,6 +3,11 @@ #include "sd-device.h" +int device_new_from_watch_handle_at(sd_device **ret, int dirfd, int wd); +static inline int device_new_from_watch_handle(sd_device **ret, int wd) { + return device_new_from_watch_handle_at(ret, -1, wd); +} + int udev_watch_restore(int inotify_fd); int udev_watch_begin(int inotify_fd, sd_device *dev); int udev_watch_end(int inotify_fd, sd_device *dev);