]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-device: make device_get_device_id() public
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 25 Aug 2024 19:36:16 +0000 (04:36 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 27 Aug 2024 20:26:04 +0000 (05:26 +0900)
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
man/sd_device_get_syspath.xml
src/libsystemd/libsystemd.sym
src/libsystemd/sd-device/device-private.c
src/libsystemd/sd-device/device-private.h
src/libsystemd/sd-device/sd-device.c
src/libsystemd/sd-device/test-sd-device.c
src/systemd/sd-device.h
src/udev/udev-manager.c
src/udev/udev-node.c
src/udev/udev-watch.c

index f551c25e6b79539a142bf0272ccad7c1fc8a5cad..ad617aa4d58835eeafdbea15944e7a7a235a6275 100644 (file)
@@ -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',
index f21d7a297bc1ce1888b369b34dc6dc24c6deb5cc..63ef61e1ff54f09209f2dd021f886035b7e58ccd 100644 (file)
@@ -29,6 +29,7 @@
     <refname>sd_device_get_ifindex</refname>
     <refname>sd_device_get_driver</refname>
     <refname>sd_device_get_diskseq</refname>
+    <refname>sd_device_get_device_id</refname>
 
     <refpurpose>Returns various fields of device objects</refpurpose>
   </refnamediv>
         <paramdef>uint64_t *<parameter>ret</parameter></paramdef>
       </funcprototype>
 
+      <funcprototype>
+        <funcdef>int <function>sd_device_get_device_id</function></funcdef>
+        <paramdef>sd_device *<parameter>device</parameter></paramdef>
+        <paramdef>uint64_t *<parameter>ret</parameter></paramdef>
+      </funcprototype>
+
     </funcsynopsis>
   </refsynopsisdiv>
 
     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 <literal>block</literal>.</para>
+
+    <para><function>sd_device_get_device_id()</function> 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
+    <function>sd_device_new_from_device_id()</function>, 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
+    <literal>b</literal> or <literal>c</literal>, respectively, followed by the device node major and minor
+    numbers separated with a colon. Example: <literal>b259:1</literal> or <literal>c10:121</literal>. Whan a
+    network interface device is specified, this returns <literal>n</literal> followed by the interface index,
+    which can be obtained by <function>sd_device_get_ifindex()</function>. Example: <literal>n1</literal>.
+    When a device in the <literal>driver</literal> subsystem is specified, this returns
+    <literal>+drivers:</literal> followed by its driver subsystem and sysfs name separated with a colon.
+    Example: <literal>+drivers:pci:iwlwifi</literal> for a driver device record whose driver subsystem is
+    <literal>pci</literal> and sysfs name is <literal>iwlwifi</literal>,
+    When an other type of device is specified, this function returns <literal>+</literal> followed by its
+    subsystem and sysfs name separated with a colon. Example: <literal>+acpi:ACPI0003:00</literal>,
+    <literal>+input:input16</literal>, or <literal>+pci:0000:00:1f.6</literal>.</para>
   </refsect1>
 
   <refsect1>
     <function>sd_device_get_ifindex()</function>,
     <function>sd_device_get_driver()</function>, and
     <function>sd_device_get_diskseq()</function> were added in version 251.</para>
-    <para><function>sd_device_get_driver_subsystem()</function> was added in version 257.</para>
+    <para><function>sd_device_get_driver_subsystem()</function> and
+    <function>sd_device_get_device_id()</function> were added in version 257.</para>
   </refsect1>
 
   <refsect1>
index e79c1a65d2903f4852a2b6f8911aa9c0bfbad68a..84053ee7c920f768645b335310374c0134885175 100644 (file)
@@ -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;
index cd85ec9c6297bcfa02870e029c175f36fa60886d..1c148b8573c2abe322252502abf45f5fed509ee3 100644 (file)
@@ -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;
 
index e0b1efe50683218b53b5bb7094af86ea01b91b3f..eab54203f0dff60664dca34138789fcccddc8a71 100644 (file)
@@ -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);
index 6dedfc43790ca1bef9b07e1565973aa60eb02d6b..383445edc8f8829ae558297743960196ab3c8409 100644 (file)
@@ -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;
 }
 
index ae745ab32103c6c70b18573ae14b6a475120cede..a048860dbe72c1d250b6e4ba07ccf21098df4494 100644 (file)
@@ -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,
index 86f90a1fd1d0008d15ac2fe3eacb8ee8a641ba9d..ff6641470156382c2f97b3b6db965276d3c4d26d 100644 (file)
@@ -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);
index cc8f7fdf8ff91840a76e66598e17b5070d614cc5..a031444c2e7942882158e65b64db3b141158ea32 100644 (file)
@@ -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;
 
index 253ffa7b7ca6a1f26c09cb75804cd9a2e7b600fe..a4e73025a0ed6df6636696b7f20b0384bf9cda15 100644 (file)
@@ -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");
 
index 1e7b9c04cae625f3e28afd4a89b051c5a01bbb17..c28c43b2af47ba385db228c35ff8ade7db3299ae 100644 (file)
@@ -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");