tree-wide: use device_get_sysattr_unsigned() and friends

This commit is contained in:
Yu Watanabe
2026-05-18 02:55:56 +09:00
parent 6ad682d70f
commit 6f72bb7bc9
10 changed files with 47 additions and 104 deletions
+7 -16
View 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",
+5 -8
View 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++;
}
+6 -5
View 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) {
+4 -8
View 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,
+4 -5
View 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;
}
+4 -25
View 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. "
+3 -9
View 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;
}
+5 -10
View 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;
}
+6 -9
View 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;
}
+3 -9
View 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;