Introduce several more helper functions to read sysfs attributes and use them where applicable (#41065)
Let's not use sd_device_get_sysattr_value() unless the result is sanitized, escaped, or filtered.
This commit is contained in:
@@ -43,7 +43,7 @@ static int device_is_power_sink(sd_device *device) {
|
||||
FOREACH_DEVICE(e, d) {
|
||||
const char *val;
|
||||
|
||||
r = sd_device_get_sysattr_value(d, "power_role", &val);
|
||||
r = device_get_sysattr_safe_string(d, "power_role", &val);
|
||||
if (r < 0) {
|
||||
if (r != -ENOENT)
|
||||
log_device_debug_errno(d, r, "Failed to read 'power_role' sysfs attribute, ignoring: %m");
|
||||
@@ -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;
|
||||
}
|
||||
@@ -93,7 +92,7 @@ static bool battery_is_discharging(sd_device *d) {
|
||||
}
|
||||
|
||||
/* Possible values: "Unknown", "Charging", "Discharging", "Not charging", "Full" */
|
||||
r = sd_device_get_sysattr_value(d, "status", &val);
|
||||
r = device_get_sysattr_safe_string(d, "status", &val);
|
||||
if (r < 0) {
|
||||
log_device_debug_errno(d, r, "Failed to read 'status' sysfs attribute, assuming the battery is discharging: %m");
|
||||
return true;
|
||||
@@ -130,7 +129,7 @@ int on_ac_power(void) {
|
||||
* https://docs.kernel.org/admin-guide/abi-testing.html#abi-file-testing-sysfs-class-power */
|
||||
|
||||
const char *val;
|
||||
r = sd_device_get_sysattr_value(d, "type", &val);
|
||||
r = device_get_sysattr_safe_string(d, "type", &val);
|
||||
if (r < 0) {
|
||||
log_device_debug_errno(d, r, "Failed to read 'type' sysfs attribute, ignoring device: %m");
|
||||
continue;
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "alloc-util.h"
|
||||
#include "blockdev-util.h"
|
||||
#include "data-fd-util.h"
|
||||
#include "device-private.h"
|
||||
#include "device-util.h"
|
||||
#include "devnum-util.h"
|
||||
#include "dissect-image.h"
|
||||
@@ -1027,7 +1028,7 @@ int loop_device_open(
|
||||
#endif
|
||||
nr = info.lo_number;
|
||||
|
||||
if (sd_device_get_sysattr_value(dev, "loop/backing_file", &s) >= 0) {
|
||||
if (device_get_sysattr_safe_string(dev, "loop/backing_file", &s) >= 0) {
|
||||
backing_file = strdup(s);
|
||||
if (!backing_file)
|
||||
return -ENOMEM;
|
||||
|
||||
@@ -178,7 +178,7 @@ int device_get_sysattr_bool_filtered(sd_device *device, const char *sysattr) {
|
||||
return device_get_sysattr_bool(device, sysattr);
|
||||
}
|
||||
|
||||
int device_get_sysattr_value_filtered(sd_device *device, const char *sysattr, const char **ret_value) {
|
||||
int device_get_sysattr_safe_string_filtered(sd_device *device, const char *sysattr, const char **ret_value) {
|
||||
int r;
|
||||
|
||||
r = naming_sysattr_allowed(device, sysattr);
|
||||
@@ -187,5 +187,5 @@ int device_get_sysattr_value_filtered(sd_device *device, const char *sysattr, co
|
||||
if (r == 0)
|
||||
return -ENOENT;
|
||||
|
||||
return sd_device_get_sysattr_value(device, sysattr, ret_value);
|
||||
return device_get_sysattr_safe_string(device, sysattr, ret_value);
|
||||
}
|
||||
|
||||
@@ -104,4 +104,4 @@ DECLARE_STRING_TABLE_LOOKUP(alternative_names_policy, NamePolicy);
|
||||
int device_get_sysattr_int_filtered(sd_device *device, const char *sysattr, int *ret_value);
|
||||
int device_get_sysattr_unsigned_filtered(sd_device *device, const char *sysattr, unsigned *ret_value);
|
||||
int device_get_sysattr_bool_filtered(sd_device *device, const char *sysattr);
|
||||
int device_get_sysattr_value_filtered(sd_device *device, const char *sysattr, const char **ret_value);
|
||||
int device_get_sysattr_safe_string_filtered(sd_device *device, const char *sysattr, const char **ret_value);
|
||||
|
||||
@@ -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. "
|
||||
|
||||
Reference in New Issue
Block a user