]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: use device_get_sysattr_unsigned() and friends
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 11 Mar 2026 22:07:12 +0000 (07:07 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 17 May 2026 17:55:56 +0000 (02:55 +0900)
src/backlight/backlight.c
src/login/logind-core.c
src/mount/mount-tool.c
src/network/networkd-link.c
src/shared/battery-util.c
src/shared/netif-sriov.c
src/sleep/battery-capacity.c
src/udev/udev-builtin-hwdb.c
src/udev/udev-builtin-input_id.c
src/udev/udev-builtin-path_id.c

index 29ce29fab91785755ba97fc7ee1ea0d6f39e3ca6..a84640d45dd8706a0529a1c55782794d10a1901d 100644 (file)
@@ -66,13 +66,9 @@ static int has_multiple_graphics_cards(void) {
                 return r;
 
         FOREACH_DEVICE(e, dev) {
-                const char *s;
-                unsigned long c;
+                uint32_t c;
 
-                if (sd_device_get_sysattr_value(dev, "class", &s) < 0)
-                        continue;
-
-                if (safe_atolu(s, &c) < 0)
+                if (device_get_sysattr_u32(dev, "class", &c) < 0)
                         continue;
 
                 if (c != PCI_CLASS_GRAPHICS_CARD)
@@ -180,7 +176,7 @@ static int same_device(sd_device *a, sd_device *b) {
 
 static int validate_device(sd_device *device) {
         _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *enumerate = NULL;
-        const char *v, *sysname;
+        const char *sysname;
         sd_device *parent;
         int r;
 
@@ -205,10 +201,10 @@ static int validate_device(sd_device *device) {
         if (r > 0)
                 return true; /* We assume LED device is always valid. */
 
-        r = sd_device_get_sysattr_value(device, "type", &v);
+        r = device_get_sysattr_streq(device, "type", "raw");
         if (r < 0)
                 return log_device_debug_errno(device, r, "Failed to read 'type' sysattr: %m");
-        if (!streq(v, "raw"))
+        if (r == 0)
                 return true;
 
         r = find_pci_or_platform_parent(device, &parent);
@@ -316,7 +312,7 @@ static int read_max_brightness(sd_device *device, unsigned *ret) {
 
         r = device_get_sysattr_unsigned(device, "max_brightness", &max_brightness);
         if (r < 0)
-                return log_device_warning_errno(device, r, "Failed to read/parse 'max_brightness' attribute: %m");
+                return log_device_warning_errno(device, r, "Failed to read 'max_brightness' attribute: %m");
 
         /* If max_brightness is 0, then there is no actual backlight device. This happens on desktops
          * with Asus mainboards that load the eeepc-wmi module. */
@@ -418,21 +414,16 @@ static int shall_clamp(sd_device *device, unsigned *ret) {
 }
 
 static int read_brightness(sd_device *device, unsigned max_brightness, unsigned *ret_brightness) {
-        const char *value;
         unsigned brightness;
         int r;
 
         assert(device);
         assert(ret_brightness);
 
-        r = sd_device_get_sysattr_value(device, "brightness", &value);
+        r = device_get_sysattr_unsigned(device, "brightness", &brightness);
         if (r < 0)
                 return log_device_debug_errno(device, r, "Failed to read 'brightness' attribute: %m");
 
-        r = safe_atou(value, &brightness);
-        if (r < 0)
-                return log_device_debug_errno(device, r, "Failed to parse 'brightness' attribute: %s", value);
-
         if (brightness > max_brightness)
                 return log_device_debug_errno(device, SYNTHETIC_ERRNO(EINVAL),
                                               "brightness=%u is larger than max_brightness=%u",
index 33f895f0041e9a89531b6fa65da21ecf429c094f..c8846d592b750b512051e22e32480ca37b29896c 100644 (file)
@@ -13,6 +13,7 @@
 #include "bus-locator.h"
 #include "cgroup-util.h"
 #include "conf-parser.h"
+#include "device-private.h"
 #include "device-util.h"
 #include "efi-loader.h"
 #include "errno-util.h"
@@ -676,21 +677,17 @@ static int manager_count_external_displays(Manager *m) {
                         continue;
 
                 /* Ignore ports that are not enabled */
-                const char *enabled;
-                r = sd_device_get_sysattr_value(d, "enabled", &enabled);
-                if (r == -ENOENT)
+                r = device_get_sysattr_streq(d, "enabled", "enabled");
+                if (IN_SET(r, 0, -ENOENT))
                         continue;
                 if (r < 0)
                         return r;
-                if (!streq(enabled, "enabled"))
-                        continue;
 
                 /* We count any connector which is not explicitly "disconnected" as connected. */
-                const char *status = NULL;
-                r = sd_device_get_sysattr_value(d, "status", &status);
+                r = device_get_sysattr_streq(d, "status", "disconnected");
                 if (r < 0 && r != -ENOENT)
                         return r;
-                if (!streq_ptr(status, "disconnected"))
+                if (r <= 0)
                         n++;
         }
 
index 2e391ee4d2450190b2bf4ff7417712e79607fcfd..0869fe27e15e62216ad2fc993389b72c8ad7a17c 100644 (file)
@@ -1228,7 +1228,6 @@ static int acquire_description(sd_device *d) {
 }
 
 static int acquire_removable(sd_device *d) {
-        const char *v;
         int r;
 
         assert(d);
@@ -1238,8 +1237,13 @@ static int acquire_removable(sd_device *d) {
                 return 0;
 
         for (;;) {
-                if (sd_device_get_sysattr_value(d, "removable", &v) >= 0)
+                r = device_get_sysattr_bool(d, "removable");
+                if (r == 0)
+                        return 0; /* not a removable device */
+                if (r > 0)
                         break;
+                if (r != -ENOENT)
+                        return log_device_debug_errno(d, r, "Failed to read 'removable' sysattr: %m");
 
                 r = sd_device_get_parent(d, &d);
                 if (r == -ENODEV)
@@ -1252,9 +1256,6 @@ static int acquire_removable(sd_device *d) {
                         return r;
         }
 
-        if (parse_boolean(v) <= 0)
-                return 0;
-
         log_debug("Discovered removable device.");
 
         if (arg_action == ACTION_DEFAULT) {
index 33275538364db0e2880e12293004c3295dfdeb9d..a69a5e8979c3c8ab66027b638691c579bacc1d38 100644 (file)
@@ -23,6 +23,7 @@
 #include "alloc-util.h"
 #include "arphrd-util.h"
 #include "bitfield.h"
+#include "device-private.h"
 #include "device-util.h"
 #include "dns-domain.h"
 #include "errno-util.h"
@@ -66,7 +67,6 @@
 #include "networkd-wifi.h"
 #include "networkd-wwan-bus.h"
 #include "ordered-set.h"
-#include "parse-util.h"
 #include "set.h"
 #include "socket-util.h"
 #include "string-table.h"
@@ -1334,13 +1334,9 @@ static int link_get_network(Link *link, Network **ret) {
                         continue;
 
                 if (network->match.ifname && link->dev) {
-                        uint8_t name_assign_type = NET_NAME_UNKNOWN;
-                        const char *attr;
-
-                        if (sd_device_get_sysattr_value(link->dev, "name_assign_type", &attr) >= 0)
-                                (void) safe_atou8(attr, &name_assign_type);
-
-                        warn = name_assign_type == NET_NAME_ENUM;
+                        uint8_t name_assign_type;
+                        if (device_get_sysattr_u8(link->dev, "name_assign_type", &name_assign_type) >= 0)
+                                warn = name_assign_type == NET_NAME_ENUM;
                 }
 
                 log_link_full(link, warn ? LOG_WARNING : LOG_DEBUG,
index 7c3336f4654925e3f99ca2cfc6e1c40f1fbecdd3..7e18c2ee87ab839793a74882f6ad05fe85c280cd 100644 (file)
@@ -75,11 +75,10 @@ static bool battery_is_discharging(sd_device *d) {
 
         assert(d);
 
-        r = sd_device_get_sysattr_value(d, "scope", &val);
-        if (r < 0) {
-                if (r != -ENOENT)
-                        log_device_debug_errno(d, r, "Failed to read 'scope' sysfs attribute, ignoring: %m");
-        } else if (streq(val, "Device")) {
+        r = device_get_sysattr_streq(d, "scope", "Device");
+        if (r < 0 && r != -ENOENT)
+                log_device_debug_errno(d, r, "Failed to read 'scope' sysfs attribute, ignoring: %m");
+        if (r > 0) {
                 log_device_debug(d, "The power supply is a device battery, ignoring device.");
                 return false;
         }
index f621a9dde7e4f108d3bb43a1fb3da66a71b3e0f4..f5df7fb392ac6e59190fd61ee47fcdb534752500 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "alloc-util.h"
 #include "conf-parser.h"
+#include "device-private.h"
 #include "device-util.h"
 #include "ether-addr-util.h"
 #include "netif-sriov.h"
@@ -267,28 +268,11 @@ int sr_iov_set_netlink_message(SRIOV *sr_iov, SRIOVAttribute attr, sd_netlink_me
 }
 
 int sr_iov_get_num_vfs(sd_device *device, uint32_t *ret) {
-        const char *str;
-        uint32_t n;
-        int r;
-
-        assert(device);
-        assert(ret);
-
-        r = sd_device_get_sysattr_value(device, "device/sriov_numvfs", &str);
-        if (r < 0)
-                return r;
-
-        r = safe_atou32(str, &n);
-        if (r < 0)
-                return r;
-
-        *ret = n;
-        return 0;
+        return device_get_sysattr_u32(device, "device/sriov_numvfs", ret);
 }
 
 int sr_iov_set_num_vfs(sd_device *device, uint32_t num_vfs, OrderedHashmap *sr_iov_by_section) {
         char val[DECIMAL_STR_MAX(uint32_t)];
-        const char *str;
         int r;
 
         assert(device);
@@ -327,14 +311,9 @@ int sr_iov_set_num_vfs(sd_device *device, uint32_t num_vfs, OrderedHashmap *sr_i
          * maximum allowed number of VFs from the sriov_totalvfs sysattr. Note that the sysattr
          * currently exists only for PCI drivers. Hence, ignore -ENOENT.
          * TODO: netdevsim provides the information in debugfs. */
-        r = sd_device_get_sysattr_value(device, "device/sriov_totalvfs", &str);
+        uint32_t max_num_vfs;
+        r = device_get_sysattr_u32(device, "device/sriov_totalvfs", &max_num_vfs);
         if (r >= 0) {
-                uint32_t max_num_vfs;
-
-                r = safe_atou32(str, &max_num_vfs);
-                if (r < 0)
-                        return log_device_debug_errno(device, r, "Failed to parse device/sriov_totalvfs sysfs attribute '%s': %m", str);
-
                 if (num_vfs > max_num_vfs)
                         return log_device_debug_errno(device, SYNTHETIC_ERRNO(ERANGE),
                                                       "Specified number of virtual functions is out of range. "
index f6a1ed25ac3f9a90564e58e255536355cc7fae40..168fff91c067d1ca6d30bfd899886631df7bc2dc 100644 (file)
@@ -5,6 +5,7 @@
 #include "alloc-util.h"
 #include "battery-capacity.h"
 #include "battery-util.h"
+#include "device-private.h"
 #include "device-util.h"
 #include "extract-word.h"
 #include "fd-util.h"
@@ -368,20 +369,13 @@ int battery_trip_point_alarm_exists(void) {
                 return log_debug_errno(r, "Failed to initialize battery enumerator: %m");
 
         FOREACH_DEVICE(e, dev) {
-                const char *alarm_attr;
-                int has_alarm;
-
                 has_battery = true;
 
-                r = sd_device_get_sysattr_value(dev, "alarm", &alarm_attr);
+                int has_alarm;
+                r = device_get_sysattr_int(dev, "alarm", &has_alarm);
                 if (r < 0)
                         return log_device_debug_errno(dev, r, "Failed to read battery alarm attribute: %m");
 
-                r = safe_atoi(alarm_attr, &has_alarm);
-                if (r < 0)
-                        return log_device_debug_errno(dev, r,
-                                                      "Failed to parse battery alarm attribute '%s': %m",
-                                                      alarm_attr);
                 if (has_alarm <= 0)
                         return false;
         }
index 5cda0f9efd4d954e47b2874f64945fc329328b8e..351c861037e3ff924f1cbb4580bb4a25f09c2766 100644 (file)
@@ -10,7 +10,6 @@
 #include "device-util.h"
 #include "hwdb-util.h"
 #include "options.h"
-#include "parse-util.h"
 #include "string-util.h"
 #include "udev-builtin.h"
 
@@ -49,20 +48,16 @@ int udev_builtin_hwdb_lookup(
 }
 
 static const char* modalias_usb(sd_device *dev, char *s, size_t size) {
-        const char *v, *p, *n = NULL;
-        uint16_t vn, pn;
+        const char *n = NULL;
+        uint16_t v, p;
 
-        if (sd_device_get_sysattr_value(dev, "idVendor", &v) < 0)
+        if (device_get_sysattr_u16_full(dev, "idVendor", 16, &v) < 0)
                 return NULL;
-        if (sd_device_get_sysattr_value(dev, "idProduct", &p) < 0)
-                return NULL;
-        if (safe_atoux16(v, &vn) < 0)
-                return NULL;
-        if (safe_atoux16(p, &pn) < 0)
+        if (device_get_sysattr_u16_full(dev, "idProduct", 16, &p) < 0)
                 return NULL;
         (void) device_get_sysattr_safe_string(dev, "product", &n);
 
-        (void) snprintf(s, size, "usb:v%04Xp%04X:%s", vn, pn, strempty(n));
+        (void) snprintf(s, size, "usb:v%04Xp%04X:%s", v, p, strempty(n));
         return s;
 }
 
index da4a9b33a45f29c293b6c312a2816eb9b5083743..8a3176088971dbeb7ad5bcd67772f7334293cecb 100644 (file)
@@ -131,17 +131,14 @@ static void get_cap_mask(
 }
 
 static struct input_id get_input_id(sd_device *dev) {
-        const char *v;
         struct input_id id = {};
 
-        if (sd_device_get_sysattr_value(dev, "id/bustype", &v) >= 0)
-                (void) safe_atoux16(v, &id.bustype);
-        if (sd_device_get_sysattr_value(dev, "id/vendor", &v) >= 0)
-                (void) safe_atoux16(v, &id.vendor);
-        if (sd_device_get_sysattr_value(dev, "id/product", &v) >= 0)
-                (void) safe_atoux16(v, &id.product);
-        if (sd_device_get_sysattr_value(dev, "id/version", &v) >= 0)
-                (void) safe_atoux16(v, &id.version);
+        assert(dev);
+
+        (void) device_get_sysattr_u16_full(dev, "id/bustype", 16, &id.bustype);
+        (void) device_get_sysattr_u16_full(dev, "id/vendor",  16, &id.vendor);
+        (void) device_get_sysattr_u16_full(dev, "id/product", 16, &id.product);
+        (void) device_get_sysattr_u16_full(dev, "id/version", 16, &id.version);
 
         return id;
 }
index af4e2b2d40d85f05e0207261d664954607fa8f2d..f5d33cd24666349d9ce6d67ec6f61bea6b9e443a 100644 (file)
@@ -472,18 +472,13 @@ static void handle_scsi_tape(sd_device *dev, char **path) {
 
 static int get_usb_revision(sd_device *dev) {
         uint8_t protocol;
-        const char *s;
         int r;
 
         assert(dev);
 
         /* Returns usb revision 1, 2, or 3. */
 
-        r = sd_device_get_sysattr_value(dev, "bDeviceProtocol", &s);
-        if (r < 0)
-                return r;
-
-        r = safe_atou8_full(s, 16, &protocol);
+        r = device_get_sysattr_u8_full(dev, "bDeviceProtocol", 16, &protocol);
         if (r < 0)
                 return r;
 
@@ -491,11 +486,10 @@ static int get_usb_revision(sd_device *dev) {
         case USB_HUB_PR_HS_NO_TT: /* Full speed hub (USB1) or Hi-speed hub without TT (USB2) */
 
                 /* See speed_show() in drivers/usb/core/sysfs.c of the kernel. */
-                r = sd_device_get_sysattr_value(dev, "speed", &s);
+                r = device_get_sysattr_streq(dev, "speed", "480");
                 if (r < 0)
                         return r;
-
-                if (streq(s, "480"))
+                if (r > 0)
                         return 2;
 
                 return 1;