]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-device: use path_extract_filename() at one more place
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 17 Apr 2022 05:25:27 +0000 (14:25 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 17 Apr 2022 19:34:14 +0000 (04:34 +0900)
This also does several cleanups.

src/libsystemd/sd-device/sd-device.c

index 6c8e7d646fb18e13b0ffae83adc1e9be55f6f63b..6c2d1243ac398bd08c538aacfc4bc94ef5119dbe 100644 (file)
@@ -1437,37 +1437,30 @@ int device_get_device_id(sd_device *device, const char **ret) {
                         return r;
 
                 if (sd_device_get_devnum(device, &devnum) >= 0) {
-                        assert(subsystem);
-
                         /* use dev_t — b259:131072, c254:0 */
-                        r = asprintf(&id, "%c%u:%u",
+                        if (asprintf(&id, "%c%u:%u",
                                      streq(subsystem, "block") ? 'b' : 'c',
-                                     major(devnum), minor(devnum));
-                        if (r < 0)
+                                     major(devnum), minor(devnum)) < 0)
                                 return -ENOMEM;
                 } else if (sd_device_get_ifindex(device, &ifindex) >= 0) {
                         /* use netdev ifindex — n3 */
-                        r = asprintf(&id, "n%u", (unsigned) ifindex);
-                        if (r < 0)
+                        if (asprintf(&id, "n%u", (unsigned) ifindex) < 0)
                                 return -ENOMEM;
                 } else {
-                        /* use $subsys:$sysname — pci:0000:00:1f.2
-                         * sysname() has '!' translated, get it from devpath
-                         */
-                        const char *sysname;
-
-                        sysname = basename(device->devpath);
-                        if (!sysname)
-                                return -EINVAL;
+                        _cleanup_free_ char *sysname = NULL;
 
-                        if (!subsystem)
-                                return -EINVAL;
+                        /* use $subsys:$sysname — pci:0000:00:1f.2
+                         * sd_device_get_sysname() has '!' translated, get it from devpath */
+                        r = path_extract_filename(device->devpath, &sysname);
+                        if (r < 0)
+                                return r;
 
-                        if (streq(subsystem, "drivers"))
-                                /* the 'drivers' pseudo-subsystem is special, and needs the real subsystem
-                                 * encoded as well */
+                        if (streq(subsystem, "drivers")) {
+                                /* the 'drivers' pseudo-subsystem is special, and needs the real
+                                 * subsystem encoded as well */
+                                assert(device->driver_subsystem);
                                 id = strjoin("+drivers:", device->driver_subsystem, ":", sysname);
-                        else
+                        else
                                 id = strjoin("+", subsystem, ":", sysname);
                         if (!id)
                                 return -ENOMEM;