]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
libudev: introduce return_with_errno() and use it where applicable
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 21 Nov 2018 05:06:41 +0000 (14:06 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 22 Nov 2018 14:49:55 +0000 (23:49 +0900)
src/basic/macro.h
src/libudev/libudev-device.c
src/libudev/libudev-enumerate.c
src/libudev/libudev-hwdb.c
src/libudev/libudev-monitor.c
src/libudev/libudev-queue.c
src/libudev/libudev.c

index b529fb15ff76464cd5d5cf43dbbed43134373ca5..0a258cc614a146a99b23ba7baa64a65d518f0328 100644 (file)
@@ -334,6 +334,12 @@ static inline int __coverity_check__(int condition) {
                 }                                                       \
         } while (false)
 
+#define return_with_errno(r, err)                     \
+        do {                                          \
+                errno = abs(err);                     \
+                return r;                             \
+        } while (false)
+
 #define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
 #define INT_TO_PTR(u) ((void *) ((intptr_t) (u)))
 #define PTR_TO_UINT(p) ((unsigned) ((uintptr_t) (p)))
index 5a2abf55e1515407e24adb6bab6a7e175215c380..4077b607b37a08c101e602e040fe9a81f6a926b7 100644 (file)
@@ -55,16 +55,12 @@ _public_ unsigned long long int udev_device_get_seqnum(struct udev_device *udev_
         r = sd_device_get_property_value(udev_device->device, "SEQNUM", &seqnum);
         if (r == -ENOENT)
                 return 0;
-        else if (r < 0) {
-                errno = -r;
-                return 0;
-        }
+        else if (r < 0)
+                return_with_errno(0, r);
 
         r = safe_atollu(seqnum, &ret);
-        if (r < 0) {
-                errno = -r;
-                return 0;
-        }
+        if (r < 0)
+                return_with_errno(0, r);
 
         return ret;
 }
@@ -84,11 +80,10 @@ _public_ dev_t udev_device_get_devnum(struct udev_device *udev_device) {
         assert_return_errno(udev_device, makedev(0, 0), EINVAL);
 
         r = sd_device_get_devnum(udev_device->device, &devnum);
-        if (r < 0) {
-                if (r != -ENOENT)
-                        errno = -r;
+        if (r == -ENOENT)
                 return makedev(0, 0);
-        }
+        if (r < 0)
+                return_with_errno(makedev(0, 0), r);
 
         return devnum;
 }
@@ -108,10 +103,8 @@ _public_ const char *udev_device_get_driver(struct udev_device *udev_device) {
         assert_return_errno(udev_device, NULL, EINVAL);
 
         r = sd_device_get_driver(udev_device->device, &driver);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         return driver;
 }
@@ -131,11 +124,10 @@ _public_ const char *udev_device_get_devtype(struct udev_device *udev_device) {
         assert_return_errno(udev_device, NULL, EINVAL);
 
         r = sd_device_get_devtype(udev_device->device, &devtype);
-        if (r < 0) {
-                if (r != -ENOENT)
-                        errno = -r;
+        if (r == -ENOENT)
                 return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         return devtype;
 }
@@ -156,11 +148,10 @@ _public_ const char *udev_device_get_subsystem(struct udev_device *udev_device)
         assert_return_errno(udev_device, NULL, EINVAL);
 
         r = sd_device_get_subsystem(udev_device->device, &subsystem);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        } else if (!subsystem)
-                errno = ENODATA;
+        if (r < 0)
+                return_with_errno(NULL, r);
+        if (!subsystem)
+                return_with_errno(NULL, ENODATA);
 
         return subsystem;
 }
@@ -175,16 +166,14 @@ _public_ const char *udev_device_get_subsystem(struct udev_device *udev_device)
  * Returns: the property string, or #NULL if there is no such property.
  **/
 _public_ const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key) {
-        const char *value = NULL;
+        const char *value;
         int r;
 
         assert_return_errno(udev_device && key, NULL, EINVAL);
 
         r = sd_device_get_property_value(udev_device->device, key, &value);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         return value;
 }
@@ -195,10 +184,8 @@ struct udev_device *udev_device_new(struct udev *udev, sd_device *device) {
         assert(device);
 
         udev_device = new(struct udev_device, 1);
-        if (!udev_device) {
-                errno = ENOMEM;
-                return NULL;
-        }
+        if (!udev_device)
+                return_with_errno(NULL, ENOMEM);
 
         *udev_device = (struct udev_device) {
                 .n_ref = 1,
@@ -233,10 +220,8 @@ _public_ struct udev_device *udev_device_new_from_syspath(struct udev *udev, con
         int r;
 
         r = sd_device_new_from_syspath(&device, syspath);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         return udev_device_new(udev, device);
 }
@@ -262,10 +247,8 @@ _public_ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char
         int r;
 
         r = sd_device_new_from_devnum(&device, type, devnum);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         return udev_device_new(udev, device);
 }
@@ -293,10 +276,8 @@ _public_ struct udev_device *udev_device_new_from_device_id(struct udev *udev, c
         int r;
 
         r = sd_device_new_from_device_id(&device, id);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         return udev_device_new(udev, device);
 }
@@ -321,10 +302,8 @@ _public_ struct udev_device *udev_device_new_from_subsystem_sysname(struct udev
         int r;
 
         r = sd_device_new_from_subsystem_sysname(&device, subsystem, sysname);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         return udev_device_new(udev, device);
 }
@@ -348,10 +327,8 @@ _public_ struct udev_device *udev_device_new_from_environment(struct udev *udev)
         int r;
 
         r = device_new_from_strv(&device, environ);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         return udev_device_new(udev, device);
 }
@@ -363,10 +340,8 @@ static struct udev_device *device_new_from_parent(struct udev_device *child) {
         assert_return_errno(child, NULL, EINVAL);
 
         r = sd_device_get_parent(child->device, &parent);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         return udev_device_new(child->udev, parent);
 }
@@ -433,20 +408,16 @@ _public_ struct udev_device *udev_device_get_parent_with_subsystem_devtype(struc
 
         /* first find the correct sd_device */
         r = sd_device_get_parent_with_subsystem_devtype(udev_device->device, subsystem, devtype, &parent);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         /* then walk the chain of udev_device parents until the corresponding
            one is found */
-        while ((udev_device = udev_device_get_parent(udev_device))) {
+        while ((udev_device = udev_device_get_parent(udev_device)))
                 if (udev_device->device == parent)
                         return udev_device;
-        }
 
-        errno = ENOENT;
-        return NULL;
+        return_with_errno(NULL, ENOENT);
 }
 
 /**
@@ -513,10 +484,8 @@ _public_ const char *udev_device_get_devpath(struct udev_device *udev_device) {
         assert_return_errno(udev_device, NULL, EINVAL);
 
         r = sd_device_get_devpath(udev_device->device, &devpath);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         return devpath;
 }
@@ -537,10 +506,8 @@ _public_ const char *udev_device_get_syspath(struct udev_device *udev_device) {
         assert_return_errno(udev_device, NULL, EINVAL);
 
         r = sd_device_get_syspath(udev_device->device, &syspath);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         return syspath;
 }
@@ -560,10 +527,8 @@ _public_ const char *udev_device_get_sysname(struct udev_device *udev_device) {
         assert_return_errno(udev_device, NULL, EINVAL);
 
         r = sd_device_get_sysname(udev_device->device, &sysname);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         return sysname;
 }
@@ -583,11 +548,10 @@ _public_ const char *udev_device_get_sysnum(struct udev_device *udev_device) {
         assert_return_errno(udev_device, NULL, EINVAL);
 
         r = sd_device_get_sysnum(udev_device->device, &sysnum);
-        if (r < 0) {
-                if (r != -ENOENT)
-                        errno = -r;
+        if (r == -ENOENT)
                 return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         return sysnum;
 }
@@ -608,10 +572,8 @@ _public_ const char *udev_device_get_devnode(struct udev_device *udev_device) {
         assert_return_errno(udev_device, NULL, EINVAL);
 
         r = sd_device_get_devname(udev_device->device, &devnode);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         return devnode;
 }
@@ -690,16 +652,16 @@ _public_ struct udev_list_entry *udev_device_get_properties_list_entry(struct ud
  * Returns: the kernel action value, or #NULL if there is no action value available.
  **/
 _public_ const char *udev_device_get_action(struct udev_device *udev_device) {
-        const char *action = NULL;
+        const char *action;
         int r;
 
         assert_return_errno(udev_device, NULL, EINVAL);
 
         r = sd_device_get_property_value(udev_device->device, "ACTION", &action);
-        if (r < 0 && r != -ENOENT) {
-                errno = -r;
+        if (r == -ENOENT)
                 return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         return action;
 }
@@ -723,10 +685,8 @@ _public_ unsigned long long int udev_device_get_usec_since_initialized(struct ud
         assert_return(udev_device, -EINVAL);
 
         r = sd_device_get_usec_since_initialized(udev_device->device, &ts);
-        if (r < 0) {
-                errno = -r;
-                return 0;
-        }
+        if (r < 0)
+                return_with_errno(0, r);
 
         return ts;
 }
@@ -748,10 +708,8 @@ _public_ const char *udev_device_get_sysattr_value(struct udev_device *udev_devi
         assert_return_errno(udev_device, NULL, EINVAL);
 
         r = sd_device_get_sysattr_value(udev_device->device, sysattr, &value);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         return value;
 }
@@ -824,10 +782,8 @@ _public_ int udev_device_get_is_initialized(struct udev_device *udev_device) {
         assert_return(udev_device, -EINVAL);
 
         r = sd_device_get_is_initialized(udev_device->device);
-        if (r < 0) {
-                errno = -r;
-                return 0;
-        }
+        if (r < 0)
+                return_with_errno(0, r);
 
         return r;
 }
index 5e663bf192feb6708d77022a95bbef65f9b97ccd..34ecdbec174a8b6dd8dd89cb90e585d472bc8fd8 100644 (file)
@@ -54,22 +54,16 @@ _public_ struct udev_enumerate *udev_enumerate_new(struct udev *udev) {
         int r;
 
         r = sd_device_enumerator_new(&e);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         r = sd_device_enumerator_allow_uninitialized(e);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         udev_enumerate = new(struct udev_enumerate, 1);
-        if (!udev_enumerate) {
-                errno = ENOMEM;
-                return NULL;
-        }
+        if (!udev_enumerate)
+                return_with_errno(NULL, ENOMEM);
 
         *udev_enumerate = (struct udev_enumerate) {
                 .udev = udev,
@@ -147,10 +141,8 @@ _public_ struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enume
                         int r;
 
                         r = sd_device_get_syspath(device, &syspath);
-                        if (r < 0) {
-                                errno = -r;
-                                return NULL;
-                        }
+                        if (r < 0)
+                                return_with_errno(NULL, r);
 
                         udev_list_entry_add(&udev_enumerate->devices_list, syspath, NULL);
                 }
@@ -160,7 +152,7 @@ _public_ struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enume
 
         e = udev_list_get_entry(&udev_enumerate->devices_list);
         if (!e)
-                errno = ENODATA;
+                return_with_errno(NULL, ENODATA);
 
         return e;
 }
index 65b98c04d094a133f58a6248773d51ca087865d0..ed755e5d3cc0dcb0b72cd886f3f93a65822b8d64 100644 (file)
@@ -40,16 +40,12 @@ _public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
         int r;
 
         r = sd_hwdb_new(&hwdb_internal);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         hwdb = new(struct udev_hwdb, 1);
-        if (!hwdb) {
-                errno = ENOMEM;
-                return NULL;
-        }
+        if (!hwdb)
+                return_with_errno(NULL, ENOMEM);
 
         *hwdb = (struct udev_hwdb) {
                 .n_ref = 1,
@@ -111,16 +107,13 @@ _public_ struct udev_list_entry *udev_hwdb_get_properties_list_entry(struct udev
 
         udev_list_cleanup(&hwdb->properties_list);
 
-        SD_HWDB_FOREACH_PROPERTY(hwdb->hwdb, modalias, key, value) {
-                if (!udev_list_entry_add(&hwdb->properties_list, key, value)) {
-                        errno = ENOMEM;
-                        return NULL;
-                }
-        }
+        SD_HWDB_FOREACH_PROPERTY(hwdb->hwdb, modalias, key, value)
+                if (!udev_list_entry_add(&hwdb->properties_list, key, value))
+                        return_with_errno(NULL, ENOMEM);
 
         e = udev_list_get_entry(&hwdb->properties_list);
         if (!e)
-                errno = ENODATA;
+                return_with_errno(NULL, ENODATA);
 
         return e;
 }
index 364ec096177bc30e4bde47d43e40e73f6cf3710c..70036f5136a688b5d5aa3de0f9ca1b9245030c8b 100644 (file)
@@ -69,22 +69,16 @@ _public_ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, c
         int r;
 
         g = monitor_netlink_group_from_string(name);
-        if (g < 0) {
-                errno = EINVAL;
-                return NULL;
-        }
+        if (g < 0)
+                return_with_errno(NULL, EINVAL);
 
         r = device_monitor_new_full(&m, g, -1);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         udev_monitor = new(struct udev_monitor, 1);
-        if (!udev_monitor) {
-                errno = ENOMEM;
-                return NULL;
-        }
+        if (!udev_monitor)
+                return_with_errno(NULL, ENOMEM);
 
         *udev_monitor = (struct udev_monitor) {
                 .udev = udev,
@@ -257,10 +251,8 @@ _public_ struct udev_device *udev_monitor_receive_device(struct udev_monitor *ud
         assert_return(udev_monitor, NULL);
 
         r = udev_monitor_receive_sd_device(udev_monitor, &device);
-        if (r < 0) {
-                errno = -r;
-                return NULL;
-        }
+        if (r < 0)
+                return_with_errno(NULL, r);
 
         return udev_device_new(udev_monitor->udev, device);
 }
index 60a84cb03bedec8332c8d5185032da1ac48629c1..4e055bbc37a0e9e47c17ab3e20ceee332f97f579 100644 (file)
@@ -46,10 +46,8 @@ _public_ struct udev_queue *udev_queue_new(struct udev *udev) {
         struct udev_queue *udev_queue;
 
         udev_queue = new(struct udev_queue, 1);
-        if (!udev_queue) {
-                errno = ENOMEM;
-                return NULL;
-        }
+        if (!udev_queue)
+                return_with_errno(NULL, ENOMEM);
 
         *udev_queue = (struct udev_queue) {
                 .udev = udev,
@@ -188,8 +186,7 @@ _public_ int udev_queue_get_seqnum_is_finished(struct udev_queue *udev_queue, un
  * Returns: NULL.
  **/
 _public_ struct udev_list_entry *udev_queue_get_queued_list_entry(struct udev_queue *udev_queue) {
-        errno = ENODATA;
-        return NULL;
+        return_with_errno(NULL, ENODATA);
 }
 
 /**
index 4c26231f866d46ac957554bd64341fc995d73a99..d11e7a9279d3fc414a5ad4fa093f36cf8fc1e3ee 100644 (file)
@@ -72,10 +72,8 @@ _public_ struct udev *udev_new(void) {
         struct udev *udev;
 
         udev = new(struct udev, 1);
-        if (!udev) {
-                errno = ENOMEM;
-                return NULL;
-        }
+        if (!udev)
+                return_with_errno(NULL, ENOMEM);
 
         *udev = (struct udev) {
                 .n_ref = 1,