]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev-util: add generic device_get_{vendor,model}_string() helpers 30003/head
authorLennart Poettering <lennart@poettering.net>
Fri, 10 Nov 2023 15:10:22 +0000 (16:10 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 13 Nov 2023 15:24:17 +0000 (16:24 +0100)
We chck the same props in various places, add a single implementation of
a call to inquire this.

src/core/device.c
src/mount/mount-tool.c
src/network/networkctl.c
src/network/networkd-json.c
src/shared/udev-util.c
src/shared/udev-util.h
src/udev/iocost/iocost.c

index 659e52bf795af51f044d67262cc06b6211f0a50f..d07adb2d2aebdae862128e094fb67f80becec301 100644 (file)
@@ -516,9 +516,7 @@ static int device_update_description(Unit *u, sd_device *dev, const char *path)
 
         desc = path;
 
-        if (dev &&
-            (sd_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE", &model) >= 0 ||
-             sd_device_get_property_value(dev, "ID_MODEL", &model) >= 0)) {
+        if (dev && device_get_model_string(dev, &model) >= 0) {
                 desc = model;
 
                 /* Try to concatenate the device model string with a label, if there is one */
index 90feb2f1979a2319a6c0206070fc075eba4d2209..55d3149ea32c3c7b872cde6cc3af3b42922c27f9 100644 (file)
@@ -35,6 +35,7 @@
 #include "stat-util.h"
 #include "strv.h"
 #include "terminal-util.h"
+#include "udev-util.h"
 #include "umask-util.h"
 #include "unit-def.h"
 #include "unit-name.h"
@@ -1133,20 +1134,6 @@ static int acquire_mount_options(sd_device *d) {
         return 1;
 }
 
-static const char *get_model(sd_device *d) {
-        const char *model;
-
-        assert(d);
-
-        if (sd_device_get_property_value(d, "ID_MODEL_FROM_DATABASE", &model) >= 0)
-                return model;
-
-        if (sd_device_get_property_value(d, "ID_MODEL", &model) >= 0)
-                return model;
-
-        return NULL;
-}
-
 static const char* get_label(sd_device *d) {
         const char *label;
 
@@ -1174,7 +1161,7 @@ static int acquire_mount_where(sd_device *d) {
 
                 name = get_label(d);
                 if (!name)
-                        name = get_model(d);
+                        (void) device_get_model_string(d, &name);
                 if (!name) {
                         const char *dn;
 
@@ -1240,12 +1227,12 @@ static int acquire_mount_where_for_loop_dev(sd_device *dev) {
 }
 
 static int acquire_description(sd_device *d) {
-        const char *model, *label;
+        const char *model = NULL, *label;
 
         if (arg_description)
                 return 0;
 
-        model = get_model(d);
+        (void) device_get_model_string(d, &model);
 
         label = get_label(d);
         if (!label)
@@ -1466,7 +1453,7 @@ static int list_devices(void) {
                                 break;
 
                         case COLUMN_MODEL:
-                                x = get_model(d);
+                                (void) device_get_model_string(d, &x);
                                 break;
 
                         case COLUMN_WWN:
index 48b0f2eca0eed310b64c32c2725c30cd9c444ca9..ec31e8eb444eac6f074bdf71d9ddb81d6217133e 100644 (file)
@@ -69,6 +69,7 @@
 #include "strv.h"
 #include "strxcpyx.h"
 #include "terminal-util.h"
+#include "udev-util.h"
 #include "unit-def.h"
 #include "verbs.h"
 #include "virt.h"
@@ -1722,12 +1723,8 @@ static int link_status_one(
 
                 (void) sd_device_get_property_value(info->sd_device, "ID_NET_DRIVER", &driver);
                 (void) sd_device_get_property_value(info->sd_device, "ID_PATH", &path);
-
-                if (sd_device_get_property_value(info->sd_device, "ID_VENDOR_FROM_DATABASE", &vendor) < 0)
-                        (void) sd_device_get_property_value(info->sd_device, "ID_VENDOR", &vendor);
-
-                if (sd_device_get_property_value(info->sd_device, "ID_MODEL_FROM_DATABASE", &model) < 0)
-                        (void) sd_device_get_property_value(info->sd_device, "ID_MODEL", &model);
+                (void) device_get_vendor_string(info->sd_device, &vendor);
+                (void) device_get_model_string(info->sd_device, &model);
         }
 
         r = net_get_type_string(info->sd_device, info->iftype, &t);
index a72eb4ca3a948180d71b6096b3e01a19eefbbdf2..eed8d9f7879aa481403b5eaf720f9ebb18077b77 100644 (file)
 #include "networkd-link.h"
 #include "networkd-manager.h"
 #include "networkd-neighbor.h"
-#include "networkd-nexthop.h"
 #include "networkd-network.h"
+#include "networkd-nexthop.h"
 #include "networkd-route-util.h"
 #include "networkd-route.h"
 #include "networkd-routing-policy-rule.h"
 #include "sort-util.h"
+#include "udev-util.h"
 #include "user-util.h"
 #include "wifi-util.h"
 
@@ -407,11 +408,8 @@ static int device_append_json(sd_device *device, JsonVariant **v) {
 
         (void) sd_device_get_property_value(device, "ID_PATH", &path);
 
-        if (sd_device_get_property_value(device, "ID_VENDOR_FROM_DATABASE", &vendor) < 0)
-                (void) sd_device_get_property_value(device, "ID_VENDOR", &vendor);
-
-        if (sd_device_get_property_value(device, "ID_MODEL_FROM_DATABASE", &model) < 0)
-                (void) sd_device_get_property_value(device, "ID_MODEL", &model);
+        (void) device_get_vendor_string(device, &vendor);
+        (void) device_get_model_string(device, &model);
 
         return json_variant_merge_objectb(
                         v,
index ca286ab24f523b53b5aa1ade5907216e31ada9e2..8d55fb58b37eccaaaa4f2608adb2410a8ab881c7 100644 (file)
@@ -382,3 +382,31 @@ bool udev_available(void) {
 
         return (cache = (path_is_read_only_fs("/sys/") <= 0));
 }
+
+int device_get_vendor_string(sd_device *device, const char **ret) {
+        int r;
+
+        assert(device);
+
+        FOREACH_STRING(field, "ID_VENDOR_FROM_DATABASE", "ID_VENDOR") {
+                r = sd_device_get_property_value(device, field, ret);
+                if (r != -ENOENT)
+                        return r;
+        }
+
+        return -ENOENT;
+}
+
+int device_get_model_string(sd_device *device, const char **ret) {
+        int r;
+
+        assert(device);
+
+        FOREACH_STRING(field, "ID_MODEL_FROM_DATABASE", "ID_MODEL") {
+                r = sd_device_get_property_value(device, field, ret);
+                if (r != -ENOENT)
+                        return r;
+        }
+
+        return -ENOENT;
+}
index 3f9d7740e92fd0b416001a6acd78fa26066cbd4b..129b11fae3194bdf1a07f09984f835aaa34a58c2 100644 (file)
@@ -22,3 +22,6 @@ size_t udev_replace_chars(char *str, const char *allow);
 int udev_queue_is_empty(void);
 
 bool udev_available(void);
+
+int device_get_vendor_string(sd_device *device, const char **ret);
+int device_get_model_string(sd_device *device, const char **ret);
index fc8223e2b6e284c0012d1d49439524fb74d5ddc2..2b2633e3c2bb071a69a43782f978bbcb1a66f784 100644 (file)
 #include "build.h"
 #include "cgroup-util.h"
 #include "conf-parser.h"
-#include "devnum-util.h"
 #include "device-util.h"
+#include "devnum-util.h"
 #include "main-func.h"
 #include "path-util.h"
 #include "pretty-print.h"
+#include "udev-util.h"
 #include "verbs.h"
 
 static char *arg_target_solution = NULL;
@@ -248,14 +249,10 @@ static int query_solutions_for_path(const char *path) {
         if (r < 0)
                 return log_error_errno(r, "Error looking up device: %m");
 
-        r = sd_device_get_property_value(device, "ID_MODEL_FROM_DATABASE", &model_name);
+        r = device_get_model_string(device, &model_name);
         if (r == -ENOENT) {
-                log_device_debug(device, "Missing ID_MODEL_FROM_DATABASE property, trying ID_MODEL");
-                r = sd_device_get_property_value(device, "ID_MODEL", &model_name);
-                if (r == -ENOENT) {
-                        log_device_info(device, "Device model not found");
-                        return 0;
-                }
+                log_device_info(device, "Device model not found");
+                return 0;
         }
         if (r < 0)
                 return log_device_error_errno(device, r, "Model name for device %s is unknown", path);