From 3cc7a9fd2584b68f6fdfc3936e4fbf5ed42e1ae3 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 10 Nov 2023 16:10:22 +0100 Subject: [PATCH] udev-util: add generic device_get_{vendor,model}_string() helpers We chck the same props in various places, add a single implementation of a call to inquire this. --- src/core/device.c | 4 +--- src/mount/mount-tool.c | 23 +++++------------------ src/network/networkctl.c | 9 +++------ src/network/networkd-json.c | 10 ++++------ src/shared/udev-util.c | 28 ++++++++++++++++++++++++++++ src/shared/udev-util.h | 3 +++ src/udev/iocost/iocost.c | 13 +++++-------- 7 files changed, 49 insertions(+), 41 deletions(-) diff --git a/src/core/device.c b/src/core/device.c index 659e52bf795..d07adb2d2ae 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -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 */ diff --git a/src/mount/mount-tool.c b/src/mount/mount-tool.c index 90feb2f1979..55d3149ea32 100644 --- a/src/mount/mount-tool.c +++ b/src/mount/mount-tool.c @@ -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: diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 48b0f2eca0e..ec31e8eb444 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -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); diff --git a/src/network/networkd-json.c b/src/network/networkd-json.c index a72eb4ca3a9..eed8d9f7879 100644 --- a/src/network/networkd-json.c +++ b/src/network/networkd-json.c @@ -14,12 +14,13 @@ #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, diff --git a/src/shared/udev-util.c b/src/shared/udev-util.c index ca286ab24f5..8d55fb58b37 100644 --- a/src/shared/udev-util.c +++ b/src/shared/udev-util.c @@ -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; +} diff --git a/src/shared/udev-util.h b/src/shared/udev-util.h index 3f9d7740e92..129b11fae31 100644 --- a/src/shared/udev-util.h +++ b/src/shared/udev-util.h @@ -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); diff --git a/src/udev/iocost/iocost.c b/src/udev/iocost/iocost.c index fc8223e2b6e..2b2633e3c2b 100644 --- a/src/udev/iocost/iocost.c +++ b/src/udev/iocost/iocost.c @@ -11,11 +11,12 @@ #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); -- 2.47.3