From 1ff0164be5978b824d2213bc546dac66619e1a48 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 26 Aug 2024 04:36:16 +0900 Subject: [PATCH] sd-device: make device_get_device_id() public We have already exposed sd_device_new_from_device_id(), but we have never provide the way to get device ID from an existing sd_device object. --- man/rules/meson.build | 3 ++- man/sd_device_get_syspath.xml | 26 ++++++++++++++++++++++- src/libsystemd/libsystemd.sym | 1 + src/libsystemd/sd-device/device-private.c | 4 ++-- src/libsystemd/sd-device/device-private.h | 1 - src/libsystemd/sd-device/sd-device.c | 8 +++---- src/libsystemd/sd-device/test-sd-device.c | 2 +- src/systemd/sd-device.h | 1 + src/udev/udev-manager.c | 2 +- src/udev/udev-node.c | 8 +++---- src/udev/udev-watch.c | 4 ++-- 11 files changed, 43 insertions(+), 17 deletions(-) diff --git a/man/rules/meson.build b/man/rules/meson.build index f551c25e6b7..ad617aa4d58 100644 --- a/man/rules/meson.build +++ b/man/rules/meson.build @@ -524,7 +524,8 @@ manpages = [ ['sd_bus_wait', '3', [], ''], ['sd_device_get_syspath', '3', - ['sd_device_get_devname', + ['sd_device_get_device_id', + 'sd_device_get_devname', 'sd_device_get_devnum', 'sd_device_get_devpath', 'sd_device_get_devtype', diff --git a/man/sd_device_get_syspath.xml b/man/sd_device_get_syspath.xml index f21d7a297bc..63ef61e1ff5 100644 --- a/man/sd_device_get_syspath.xml +++ b/man/sd_device_get_syspath.xml @@ -29,6 +29,7 @@ sd_device_get_ifindex sd_device_get_driver sd_device_get_diskseq + sd_device_get_device_id Returns various fields of device objects @@ -109,6 +110,12 @@ uint64_t *ret + + int sd_device_get_device_id + sd_device *device + uint64_t *ret + + @@ -171,6 +178,22 @@ the device name changing, and is relevant for block devices encapsulating devices with changing media (e.g. floppy or CD-ROM), or loopback block devices. Only defined for block devices, i.e. those of subsystem block. + + sd_device_get_device_id() returns the short string that identifies the device + record. When the device ID obtained by the function for a specified device record is passed to + sd_device_new_from_device_id(), a new instance of the same device record will be + gained. When a block or character device is specified, which has corresponding device node, this returns + b or c, respectively, followed by the device node major and minor + numbers separated with a colon. Example: b259:1 or c10:121. Whan a + network interface device is specified, this returns n followed by the interface index, + which can be obtained by sd_device_get_ifindex(). Example: n1. + When a device in the driver subsystem is specified, this returns + +drivers: followed by its driver subsystem and sysfs name separated with a colon. + Example: +drivers:pci:iwlwifi for a driver device record whose driver subsystem is + pci and sysfs name is iwlwifi, + When an other type of device is specified, this function returns + followed by its + subsystem and sysfs name separated with a colon. Example: +acpi:ACPI0003:00, + +input:input16, or +pci:0000:00:1f.6. @@ -220,7 +243,8 @@ sd_device_get_ifindex(), sd_device_get_driver(), and sd_device_get_diskseq() were added in version 251. - sd_device_get_driver_subsystem() was added in version 257. + sd_device_get_driver_subsystem() and + sd_device_get_device_id() were added in version 257. diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym index e79c1a65d29..84053ee7c92 100644 --- a/src/libsystemd/libsystemd.sym +++ b/src/libsystemd/libsystemd.sym @@ -1046,6 +1046,7 @@ global: sd_varlink_take_fd; sd_varlink_unref; sd_varlink_wait; + sd_device_get_device_id; sd_device_get_driver_subsystem; sd_device_monitor_is_running; sd_device_monitor_get_fd; diff --git a/src/libsystemd/sd-device/device-private.c b/src/libsystemd/sd-device/device-private.c index cd85ec9c629..1c148b8573c 100644 --- a/src/libsystemd/sd-device/device-private.c +++ b/src/libsystemd/sd-device/device-private.c @@ -711,7 +711,7 @@ static int device_tag(sd_device *device, const char *tag, bool add) { assert(device); assert(tag); - r = device_get_device_id(device, &id); + r = sd_device_get_device_id(device, &id); if (r < 0) return r; @@ -797,7 +797,7 @@ static int device_get_db_path(sd_device *device, char **ret) { assert(device); assert(ret); - r = device_get_device_id(device, &id); + r = sd_device_get_device_id(device, &id); if (r < 0) return r; diff --git a/src/libsystemd/sd-device/device-private.h b/src/libsystemd/sd-device/device-private.h index e0b1efe5068..eab54203f0d 100644 --- a/src/libsystemd/sd-device/device-private.h +++ b/src/libsystemd/sd-device/device-private.h @@ -26,7 +26,6 @@ static inline int device_get_sysattr_unsigned(sd_device *device, const char *sys } int device_get_sysattr_u32(sd_device *device, const char *sysattr, uint32_t *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_devnode_mode(sd_device *device, mode_t *ret); int device_get_devnode_uid(sd_device *device, uid_t *ret); diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index 6dedfc43790..383445edc8f 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -1637,9 +1637,8 @@ static int handle_db_line(sd_device *device, char key, const char *value) { } } -int device_get_device_id(sd_device *device, const char **ret) { - assert(device); - assert(ret); +_public_ int sd_device_get_device_id(sd_device *device, const char **ret) { + assert_return(device, -EINVAL); if (!device->device_id) { _cleanup_free_ char *id = NULL; @@ -1689,7 +1688,8 @@ int device_get_device_id(sd_device *device, const char **ret) { device->device_id = TAKE_PTR(id); } - *ret = device->device_id; + if (ret) + *ret = device->device_id; return 0; } diff --git a/src/libsystemd/sd-device/test-sd-device.c b/src/libsystemd/sd-device/test-sd-device.c index ae745ab3210..a048860dbe7 100644 --- a/src/libsystemd/sd-device/test-sd-device.c +++ b/src/libsystemd/sd-device/test-sd-device.c @@ -91,7 +91,7 @@ static void test_sd_device_one(sd_device *d) { dev = sd_device_unref(dev); /* The device ID depends on subsystem. */ - assert_se(device_get_device_id(d, &id) >= 0); + assert_se(sd_device_get_device_id(d, &id) >= 0); r = sd_device_new_from_device_id(&dev, id); if (r == -ENODEV && ifindex > 0) log_device_warning_errno(d, r, diff --git a/src/systemd/sd-device.h b/src/systemd/sd-device.h index 86f90a1fd1d..ff664147015 100644 --- a/src/systemd/sd-device.h +++ b/src/systemd/sd-device.h @@ -86,6 +86,7 @@ int sd_device_get_sysnum(sd_device *device, const char **ret); int sd_device_get_action(sd_device *device, sd_device_action_t *ret); int sd_device_get_seqnum(sd_device *device, uint64_t *ret); int sd_device_get_diskseq(sd_device *device, uint64_t *ret); +int sd_device_get_device_id(sd_device *device, const char **ret); int sd_device_get_is_initialized(sd_device *device); int sd_device_get_usec_initialized(sd_device *device, uint64_t *ret); diff --git a/src/udev/udev-manager.c b/src/udev/udev-manager.c index cc8f7fdf8ff..a031444c2e7 100644 --- a/src/udev/udev-manager.c +++ b/src/udev/udev-manager.c @@ -729,7 +729,7 @@ static int event_queue_insert(Manager *manager, sd_device *dev) { if (r < 0 && r != -ENOENT) return r; - r = device_get_device_id(dev, &id); + r = sd_device_get_device_id(dev, &id); if (r < 0 && r != -ENOENT) return r; diff --git a/src/udev/udev-node.c b/src/udev/udev-node.c index 253ffa7b7ca..a4e73025a0e 100644 --- a/src/udev/udev-node.c +++ b/src/udev/udev-node.c @@ -220,7 +220,7 @@ static int stack_directory_find_prioritized_devnode(sd_device *dev, int dirfd, b if (!dir) return -errno; - r = device_get_device_id(dev, &id); + r = sd_device_get_device_id(dev, &id); if (r < 0) return r; @@ -246,7 +246,7 @@ static int stack_directory_update(sd_device *dev, int fd, bool add) { assert(dev); assert(fd >= 0); - r = device_get_device_id(dev, &id); + r = sd_device_get_device_id(dev, &id); if (r < 0) return r; @@ -405,7 +405,7 @@ static int node_get_current(const char *slink, int dirfd, char **ret_id, int *re if (r < 0) return r; - r = device_get_device_id(dev, &id); + r = sd_device_get_device_id(dev, &id); if (r < 0) return r; @@ -446,7 +446,7 @@ static int link_update(sd_device *dev, const char *slink, bool add) { if (current_id) { const char *id; - r = device_get_device_id(dev, &id); + r = sd_device_get_device_id(dev, &id); if (r < 0) return log_device_debug_errno(dev, r, "Failed to get device id: %m"); diff --git a/src/udev/udev-watch.c b/src/udev/udev-watch.c index 1e7b9c04cae..c28c43b2af4 100644 --- a/src/udev/udev-watch.c +++ b/src/udev/udev-watch.c @@ -111,7 +111,7 @@ static int udev_watch_clear(sd_device *dev, int dirfd, int *ret_wd) { assert(dev); assert(dirfd >= 0); - r = device_get_device_id(dev, &id); + r = sd_device_get_device_id(dev, &id); if (r < 0) return log_device_debug_errno(dev, r, "Failed to get device ID: %m"); @@ -188,7 +188,7 @@ int udev_watch_begin(int inotify_fd, sd_device *dev) { if (r < 0) return log_device_debug_errno(dev, r, "Failed to get device node: %m"); - r = device_get_device_id(dev, &id); + r = sd_device_get_device_id(dev, &id); if (r < 0) return log_device_debug_errno(dev, r, "Failed to get device ID: %m");