libudev: introduce return_with_errno() and use it where applicable

This commit is contained in:
Yu Watanabe 2018-11-21 14:06:41 +09:00
parent 6f9fe58189
commit fd05c424c0
7 changed files with 94 additions and 160 deletions

View file

@ -334,6 +334,12 @@ static inline int __coverity_check__(int condition) {
} \ } \
} while (false) } 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 PTR_TO_INT(p) ((int) ((intptr_t) (p)))
#define INT_TO_PTR(u) ((void *) ((intptr_t) (u))) #define INT_TO_PTR(u) ((void *) ((intptr_t) (u)))
#define PTR_TO_UINT(p) ((unsigned) ((uintptr_t) (p))) #define PTR_TO_UINT(p) ((unsigned) ((uintptr_t) (p)))

View 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); r = sd_device_get_property_value(udev_device->device, "SEQNUM", &seqnum);
if (r == -ENOENT) if (r == -ENOENT)
return 0; return 0;
else if (r < 0) { else if (r < 0)
errno = -r; return_with_errno(0, r);
return 0;
}
r = safe_atollu(seqnum, &ret); r = safe_atollu(seqnum, &ret);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(0, r);
return 0;
}
return ret; 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); assert_return_errno(udev_device, makedev(0, 0), EINVAL);
r = sd_device_get_devnum(udev_device->device, &devnum); r = sd_device_get_devnum(udev_device->device, &devnum);
if (r < 0) { if (r == -ENOENT)
if (r != -ENOENT)
errno = -r;
return makedev(0, 0); return makedev(0, 0);
} if (r < 0)
return_with_errno(makedev(0, 0), r);
return devnum; 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); assert_return_errno(udev_device, NULL, EINVAL);
r = sd_device_get_driver(udev_device->device, &driver); r = sd_device_get_driver(udev_device->device, &driver);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
return driver; 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); assert_return_errno(udev_device, NULL, EINVAL);
r = sd_device_get_devtype(udev_device->device, &devtype); r = sd_device_get_devtype(udev_device->device, &devtype);
if (r < 0) { if (r == -ENOENT)
if (r != -ENOENT)
errno = -r;
return NULL; return NULL;
} if (r < 0)
return_with_errno(NULL, r);
return devtype; 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); assert_return_errno(udev_device, NULL, EINVAL);
r = sd_device_get_subsystem(udev_device->device, &subsystem); r = sd_device_get_subsystem(udev_device->device, &subsystem);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL; if (!subsystem)
} else if (!subsystem) return_with_errno(NULL, ENODATA);
errno = ENODATA;
return subsystem; 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. * 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) { _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; int r;
assert_return_errno(udev_device && key, NULL, EINVAL); assert_return_errno(udev_device && key, NULL, EINVAL);
r = sd_device_get_property_value(udev_device->device, key, &value); r = sd_device_get_property_value(udev_device->device, key, &value);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
return value; return value;
} }
@ -195,10 +184,8 @@ struct udev_device *udev_device_new(struct udev *udev, sd_device *device) {
assert(device); assert(device);
udev_device = new(struct udev_device, 1); udev_device = new(struct udev_device, 1);
if (!udev_device) { if (!udev_device)
errno = ENOMEM; return_with_errno(NULL, ENOMEM);
return NULL;
}
*udev_device = (struct udev_device) { *udev_device = (struct udev_device) {
.n_ref = 1, .n_ref = 1,
@ -233,10 +220,8 @@ _public_ struct udev_device *udev_device_new_from_syspath(struct udev *udev, con
int r; int r;
r = sd_device_new_from_syspath(&device, syspath); r = sd_device_new_from_syspath(&device, syspath);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
return udev_device_new(udev, device); 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; int r;
r = sd_device_new_from_devnum(&device, type, devnum); r = sd_device_new_from_devnum(&device, type, devnum);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
return udev_device_new(udev, device); 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; int r;
r = sd_device_new_from_device_id(&device, id); r = sd_device_new_from_device_id(&device, id);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
return udev_device_new(udev, device); return udev_device_new(udev, device);
} }
@ -321,10 +302,8 @@ _public_ struct udev_device *udev_device_new_from_subsystem_sysname(struct udev
int r; int r;
r = sd_device_new_from_subsystem_sysname(&device, subsystem, sysname); r = sd_device_new_from_subsystem_sysname(&device, subsystem, sysname);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
return udev_device_new(udev, device); return udev_device_new(udev, device);
} }
@ -348,10 +327,8 @@ _public_ struct udev_device *udev_device_new_from_environment(struct udev *udev)
int r; int r;
r = device_new_from_strv(&device, environ); r = device_new_from_strv(&device, environ);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
return udev_device_new(udev, device); 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); assert_return_errno(child, NULL, EINVAL);
r = sd_device_get_parent(child->device, &parent); r = sd_device_get_parent(child->device, &parent);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
return udev_device_new(child->udev, parent); 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 */ /* first find the correct sd_device */
r = sd_device_get_parent_with_subsystem_devtype(udev_device->device, subsystem, devtype, &parent); r = sd_device_get_parent_with_subsystem_devtype(udev_device->device, subsystem, devtype, &parent);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
/* then walk the chain of udev_device parents until the corresponding /* then walk the chain of udev_device parents until the corresponding
one is found */ 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) if (udev_device->device == parent)
return udev_device; return udev_device;
}
errno = ENOENT; return_with_errno(NULL, ENOENT);
return NULL;
} }
/** /**
@ -513,10 +484,8 @@ _public_ const char *udev_device_get_devpath(struct udev_device *udev_device) {
assert_return_errno(udev_device, NULL, EINVAL); assert_return_errno(udev_device, NULL, EINVAL);
r = sd_device_get_devpath(udev_device->device, &devpath); r = sd_device_get_devpath(udev_device->device, &devpath);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
return devpath; 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); assert_return_errno(udev_device, NULL, EINVAL);
r = sd_device_get_syspath(udev_device->device, &syspath); r = sd_device_get_syspath(udev_device->device, &syspath);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
return syspath; 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); assert_return_errno(udev_device, NULL, EINVAL);
r = sd_device_get_sysname(udev_device->device, &sysname); r = sd_device_get_sysname(udev_device->device, &sysname);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
return sysname; 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); assert_return_errno(udev_device, NULL, EINVAL);
r = sd_device_get_sysnum(udev_device->device, &sysnum); r = sd_device_get_sysnum(udev_device->device, &sysnum);
if (r < 0) { if (r == -ENOENT)
if (r != -ENOENT)
errno = -r;
return NULL; return NULL;
} if (r < 0)
return_with_errno(NULL, r);
return sysnum; 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); assert_return_errno(udev_device, NULL, EINVAL);
r = sd_device_get_devname(udev_device->device, &devnode); r = sd_device_get_devname(udev_device->device, &devnode);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
return devnode; 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. * 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) { _public_ const char *udev_device_get_action(struct udev_device *udev_device) {
const char *action = NULL; const char *action;
int r; int r;
assert_return_errno(udev_device, NULL, EINVAL); assert_return_errno(udev_device, NULL, EINVAL);
r = sd_device_get_property_value(udev_device->device, "ACTION", &action); r = sd_device_get_property_value(udev_device->device, "ACTION", &action);
if (r < 0 && r != -ENOENT) { if (r == -ENOENT)
errno = -r;
return NULL; return NULL;
} if (r < 0)
return_with_errno(NULL, r);
return action; return action;
} }
@ -723,10 +685,8 @@ _public_ unsigned long long int udev_device_get_usec_since_initialized(struct ud
assert_return(udev_device, -EINVAL); assert_return(udev_device, -EINVAL);
r = sd_device_get_usec_since_initialized(udev_device->device, &ts); r = sd_device_get_usec_since_initialized(udev_device->device, &ts);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(0, r);
return 0;
}
return ts; 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); assert_return_errno(udev_device, NULL, EINVAL);
r = sd_device_get_sysattr_value(udev_device->device, sysattr, &value); r = sd_device_get_sysattr_value(udev_device->device, sysattr, &value);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
return value; return value;
} }
@ -824,10 +782,8 @@ _public_ int udev_device_get_is_initialized(struct udev_device *udev_device) {
assert_return(udev_device, -EINVAL); assert_return(udev_device, -EINVAL);
r = sd_device_get_is_initialized(udev_device->device); r = sd_device_get_is_initialized(udev_device->device);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(0, r);
return 0;
}
return r; return r;
} }

View file

@ -54,22 +54,16 @@ _public_ struct udev_enumerate *udev_enumerate_new(struct udev *udev) {
int r; int r;
r = sd_device_enumerator_new(&e); r = sd_device_enumerator_new(&e);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
r = sd_device_enumerator_allow_uninitialized(e); r = sd_device_enumerator_allow_uninitialized(e);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
udev_enumerate = new(struct udev_enumerate, 1); udev_enumerate = new(struct udev_enumerate, 1);
if (!udev_enumerate) { if (!udev_enumerate)
errno = ENOMEM; return_with_errno(NULL, ENOMEM);
return NULL;
}
*udev_enumerate = (struct udev_enumerate) { *udev_enumerate = (struct udev_enumerate) {
.udev = udev, .udev = udev,
@ -147,10 +141,8 @@ _public_ struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enume
int r; int r;
r = sd_device_get_syspath(device, &syspath); r = sd_device_get_syspath(device, &syspath);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
udev_list_entry_add(&udev_enumerate->devices_list, syspath, NULL); 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); e = udev_list_get_entry(&udev_enumerate->devices_list);
if (!e) if (!e)
errno = ENODATA; return_with_errno(NULL, ENODATA);
return e; return e;
} }

View file

@ -40,16 +40,12 @@ _public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
int r; int r;
r = sd_hwdb_new(&hwdb_internal); r = sd_hwdb_new(&hwdb_internal);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
hwdb = new(struct udev_hwdb, 1); hwdb = new(struct udev_hwdb, 1);
if (!hwdb) { if (!hwdb)
errno = ENOMEM; return_with_errno(NULL, ENOMEM);
return NULL;
}
*hwdb = (struct udev_hwdb) { *hwdb = (struct udev_hwdb) {
.n_ref = 1, .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); udev_list_cleanup(&hwdb->properties_list);
SD_HWDB_FOREACH_PROPERTY(hwdb->hwdb, modalias, key, value) { SD_HWDB_FOREACH_PROPERTY(hwdb->hwdb, modalias, key, value)
if (!udev_list_entry_add(&hwdb->properties_list, key, value)) { if (!udev_list_entry_add(&hwdb->properties_list, key, value))
errno = ENOMEM; return_with_errno(NULL, ENOMEM);
return NULL;
}
}
e = udev_list_get_entry(&hwdb->properties_list); e = udev_list_get_entry(&hwdb->properties_list);
if (!e) if (!e)
errno = ENODATA; return_with_errno(NULL, ENODATA);
return e; return e;
} }

View file

@ -69,22 +69,16 @@ _public_ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, c
int r; int r;
g = monitor_netlink_group_from_string(name); g = monitor_netlink_group_from_string(name);
if (g < 0) { if (g < 0)
errno = EINVAL; return_with_errno(NULL, EINVAL);
return NULL;
}
r = device_monitor_new_full(&m, g, -1); r = device_monitor_new_full(&m, g, -1);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
udev_monitor = new(struct udev_monitor, 1); udev_monitor = new(struct udev_monitor, 1);
if (!udev_monitor) { if (!udev_monitor)
errno = ENOMEM; return_with_errno(NULL, ENOMEM);
return NULL;
}
*udev_monitor = (struct udev_monitor) { *udev_monitor = (struct udev_monitor) {
.udev = udev, .udev = udev,
@ -257,10 +251,8 @@ _public_ struct udev_device *udev_monitor_receive_device(struct udev_monitor *ud
assert_return(udev_monitor, NULL); assert_return(udev_monitor, NULL);
r = udev_monitor_receive_sd_device(udev_monitor, &device); r = udev_monitor_receive_sd_device(udev_monitor, &device);
if (r < 0) { if (r < 0)
errno = -r; return_with_errno(NULL, r);
return NULL;
}
return udev_device_new(udev_monitor->udev, device); return udev_device_new(udev_monitor->udev, device);
} }

View file

@ -46,10 +46,8 @@ _public_ struct udev_queue *udev_queue_new(struct udev *udev) {
struct udev_queue *udev_queue; struct udev_queue *udev_queue;
udev_queue = new(struct udev_queue, 1); udev_queue = new(struct udev_queue, 1);
if (!udev_queue) { if (!udev_queue)
errno = ENOMEM; return_with_errno(NULL, ENOMEM);
return NULL;
}
*udev_queue = (struct udev_queue) { *udev_queue = (struct udev_queue) {
.udev = udev, .udev = udev,
@ -188,8 +186,7 @@ _public_ int udev_queue_get_seqnum_is_finished(struct udev_queue *udev_queue, un
* Returns: NULL. * Returns: NULL.
**/ **/
_public_ struct udev_list_entry *udev_queue_get_queued_list_entry(struct udev_queue *udev_queue) { _public_ struct udev_list_entry *udev_queue_get_queued_list_entry(struct udev_queue *udev_queue) {
errno = ENODATA; return_with_errno(NULL, ENODATA);
return NULL;
} }
/** /**

View file

@ -72,10 +72,8 @@ _public_ struct udev *udev_new(void) {
struct udev *udev; struct udev *udev;
udev = new(struct udev, 1); udev = new(struct udev, 1);
if (!udev) { if (!udev)
errno = ENOMEM; return_with_errno(NULL, ENOMEM);
return NULL;
}
*udev = (struct udev) { *udev = (struct udev) {
.n_ref = 1, .n_ref = 1,