]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-device: add helper to read a unsigned int attribute
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 23 Aug 2022 14:24:02 +0000 (16:24 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 23 Aug 2022 15:04:31 +0000 (17:04 +0200)
There are dozens of places where this could be used, but I don't
want to do the conversion now because it's quite a bit of work.
I think we could export this function later on, because reading
numerical attributes is so common. But for now, I'm just adding the
helper to use it one place.

src/libsystemd/sd-device/device-private.h
src/libsystemd/sd-device/sd-device.c
src/libsystemd/sd-device/test-sd-device.c

index 3d670a3afccf1769730b6f13b84359e857192b79..d993b77b49c8b3341feddb21e636f13b12b26102 100644 (file)
@@ -18,6 +18,7 @@ static inline int device_new_from_watch_handle(sd_device **ret, int wd) {
 }
 
 int device_get_property_bool(sd_device *device, const char *key);
+int device_get_sysattr_unsigned(sd_device *device, const char *sysattr, unsigned *ret_value);
 int device_get_sysattr_bool(sd_device *device, const char *sysattr);
 int device_get_device_id(sd_device *device, const char **ret);
 int device_get_devlink_priority(sd_device *device, int *ret);
index 6bc4e6a019fa131b0d373b07f1aefa40fef496d7..51bee24d511f3bc2c9eac2b10d5a7755d7955d7c 100644 (file)
@@ -2206,6 +2206,25 @@ _public_ int sd_device_get_sysattr_value(sd_device *device, const char *sysattr,
         return 0;
 }
 
+int device_get_sysattr_unsigned(sd_device *device, const char *sysattr, unsigned *ret_value) {
+        const char *value;
+        int r;
+
+        r = sd_device_get_sysattr_value(device, sysattr, &value);
+        if (r < 0)
+                return r;
+
+        unsigned v;
+        r = safe_atou(value, &v);
+        if (r < 0)
+                return log_device_debug_errno(device, r, "Failed to parse '%s' attribute: %m", sysattr);
+
+        if (ret_value)
+                *ret_value = v;
+        /* We return "true" if the value is positive. */
+        return v > 0;
+}
+
 int device_get_sysattr_bool(sd_device *device, const char *sysattr) {
         const char *value;
         int r;
index 8172c64e456d31d60dc40b583b2099048c54d9e4..3deb9beaac7d16b8629826020be013b6ebc27415 100644 (file)
@@ -180,6 +180,13 @@ static void test_sd_device_one(sd_device *d) {
 
         r = sd_device_get_sysattr_value(d, "name_assign_type", &val);
         assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r) || IN_SET(r, -ENOENT, -EINVAL));
+
+        if (r > 0) {
+                unsigned x;
+
+                assert_se(device_get_sysattr_unsigned(d, "name_assign_type", NULL) >= 0);
+                assert_se(device_get_sysattr_unsigned(d, "name_assign_type", &x) >= 0);
+        }
 }
 
 TEST(sd_device_enumerator_devices) {