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)
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;
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);
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. */
}
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",
#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"
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++;
}
}
static int acquire_removable(sd_device *d) {
- const char *v;
int r;
assert(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)
return r;
}
- if (parse_boolean(v) <= 0)
- return 0;
-
log_debug("Discovered removable device.");
if (arg_action == ACTION_DEFAULT) {
#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"
#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"
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,
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;
}
#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"
}
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);
* 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. "
#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"
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;
}
#include "device-util.h"
#include "hwdb-util.h"
#include "options.h"
-#include "parse-util.h"
#include "string-util.h"
#include "udev-builtin.h"
}
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;
}
}
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;
}
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;
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;