]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-device: introduce sd_device_get_driver_subsystem()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 25 Aug 2024 19:09:49 +0000 (04:09 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 27 Aug 2024 20:18:14 +0000 (05:18 +0900)
To create the sd_device object of a driver, the function
sd_device_new_from_subsystem_sysname() requires "drivers" for subsystem
and e.g. "pci:iwlwifi" for sysname. Similarly, sd_device_new_from_device_id()
also requires driver subsystem. However, we have never provided a
way to get the driver subsystem ("pci" for the previous example) from
an existing sd_device object.

Let's introduce a way to get driver subsystem.

man/rules/meson.build
man/sd_device_get_syspath.xml
src/libsystemd/libsystemd.sym
src/libsystemd/sd-device/sd-device.c
src/libsystemd/sd-device/test-sd-device.c
src/systemd/sd-device.h

index a42143ec7cf526c501beb01942193113fa806e35..f551c25e6b79539a142bf0272ccad7c1fc8a5cad 100644 (file)
@@ -530,6 +530,7 @@ manpages = [
    'sd_device_get_devtype',
    'sd_device_get_diskseq',
    'sd_device_get_driver',
+   'sd_device_get_driver_subsystem',
    'sd_device_get_ifindex',
    'sd_device_get_subsystem',
    'sd_device_get_sysname',
index 718da91e87d8e5802e3ebdda16cdabe78a96b32c..f21d7a297bc1ce1888b369b34dc6dc24c6deb5cc 100644 (file)
@@ -22,6 +22,7 @@
     <refname>sd_device_get_sysname</refname>
     <refname>sd_device_get_sysnum</refname>
     <refname>sd_device_get_subsystem</refname>
+    <refname>sd_device_get_driver_subsystem</refname>
     <refname>sd_device_get_devtype</refname>
     <refname>sd_device_get_devname</refname>
     <refname>sd_device_get_devnum</refname>
         <paramdef>const char **<parameter>ret</parameter></paramdef>
       </funcprototype>
 
+      <funcprototype>
+        <funcdef>int <function>sd_device_get_driver_subsystem</function></funcdef>
+        <paramdef>sd_device *<parameter>device</parameter></paramdef>
+        <paramdef>const char **<parameter>ret</parameter></paramdef>
+      </funcprototype>
+
       <funcprototype>
         <funcdef>int <function>sd_device_get_devtype</function></funcdef>
         <paramdef>sd_device *<parameter>device</parameter></paramdef>
     record. This is a short string fitting into a filename, and thus does not contain a slash and cannot be
     empty. Example: <literal>tty</literal>, <literal>block</literal> or <literal>net</literal>.</para>
 
+    <para><function>sd_device_get_driver_subsystem()</function> returns the connected bus type of the devices
+    loaded by the specified driver device record. For example, when <literal>iwlwifi</literal> driver device
+    is specified, which is used by the wireless network interfaces connected to PCI bus, this function returns
+    <literal>pci</literal>. This function only succeeds when <function>sd_device_get_subsystem()</function>
+    returns <literal>drivers</literal>. Example: <literal>pci</literal>, <literal>i2c</literal>, or
+    <literal>hid</literal>.</para>
+
     <para><function>sd_device_get_devtype()</function> returns the device type of the specified device
     record, if the subsystem manages multiple types of devices. Example: for devices of the
     <literal>block</literal> subsystem this can be <literal>disk</literal> or <literal>partition</literal>
     <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>
   </refsect1>
 
   <refsect1>
index 50ef8096db0640ccc86202629086a5ca38d0ac93..e79c1a65d2903f4852a2b6f8911aa9c0bfbad68a 100644 (file)
@@ -1046,6 +1046,7 @@ global:
         sd_varlink_take_fd;
         sd_varlink_unref;
         sd_varlink_wait;
+        sd_device_get_driver_subsystem;
         sd_device_monitor_is_running;
         sd_device_monitor_get_fd;
         sd_device_monitor_receive;
index 3d683fb280269e9499d189550b941219f3629b61..6dedfc43790ca1bef9b07e1565973aa60eb02d6b 100644 (file)
@@ -1198,6 +1198,20 @@ _public_ int sd_device_get_subsystem(sd_device *device, const char **ret) {
         return 0;
 }
 
+_public_ int sd_device_get_driver_subsystem(sd_device *device, const char **ret) {
+        assert_return(device, -EINVAL);
+
+        if (!device_in_subsystem(device, "drivers"))
+                return -ENOENT;
+
+        assert(device->driver_subsystem);
+
+        if (ret)
+                *ret = device->driver_subsystem;
+
+        return 0;
+}
+
 _public_ int sd_device_get_devtype(sd_device *device, const char **devtype) {
         int r;
 
index 9fde1a08143f7f5042d2512c2191e316e05bb931..ae745ab32103c6c70b18573ae14b6a475120cede 100644 (file)
@@ -79,9 +79,11 @@ static void test_sd_device_one(sd_device *d) {
                                                * sd_device_new_from_device_id() may not work as expected. */
                 const char *name, *id;
 
-                if (streq(subsystem, "drivers"))
-                        name = strjoina(d->driver_subsystem, ":", sysname);
-                else
+                if (streq(subsystem, "drivers")) {
+                        const char *driver_subsystem;
+                        ASSERT_OK(sd_device_get_driver_subsystem(d, &driver_subsystem));
+                        name = strjoina(driver_subsystem, ":", sysname);
+                } else
                         name = sysname;
                 assert_se(sd_device_new_from_subsystem_sysname(&dev, subsystem, name) >= 0);
                 assert_se(sd_device_get_syspath(dev, &val) >= 0);
index c84cc552029abd944e5622341f8745dca0f94867..86f90a1fd1d0008d15ac2fe3eacb8ee8a641ba9d 100644 (file)
@@ -74,6 +74,7 @@ int sd_device_get_parent_with_subsystem_devtype(sd_device *child, const char *su
 
 int sd_device_get_syspath(sd_device *device, const char **ret);
 int sd_device_get_subsystem(sd_device *device, const char **ret);
+int sd_device_get_driver_subsystem(sd_device *device, const char **ret);
 int sd_device_get_devtype(sd_device *device, const char **ret);
 int sd_device_get_devnum(sd_device *device, dev_t *devnum);
 int sd_device_get_ifindex(sd_device *device, int *ifindex);