From: Yu Watanabe Date: Fri, 24 Aug 2018 07:11:13 +0000 (+0900) Subject: libudev-device: create sd_device at first in udev_device_new_from_*() X-Git-Tag: v240~720^2~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=02e7ae2fdcdc15ee129c59b859943dbefc3eb2ef;p=thirdparty%2Fsystemd.git libudev-device: create sd_device at first in udev_device_new_from_*() --- diff --git a/src/libudev/libudev-device-internal.h b/src/libudev/libudev-device-internal.h index 974601f79f4..73db709e935 100644 --- a/src/libudev/libudev-device-internal.h +++ b/src/libudev/libudev-device-internal.h @@ -36,4 +36,4 @@ struct udev_device { bool sysattrs_read; }; -struct udev_device *udev_device_new(struct udev *udev); +struct udev_device *udev_device_new(struct udev *udev, sd_device *device); diff --git a/src/libudev/libudev-device-private.c b/src/libudev/libudev-device-private.c index 22b421299a8..a7868a11cb2 100644 --- a/src/libudev/libudev-device-private.c +++ b/src/libudev/libudev-device-private.c @@ -200,79 +200,59 @@ int udev_device_rename(struct udev_device *udev_device, const char *name) { } struct udev_device *udev_device_shallow_clone(struct udev_device *old_device) { - struct udev_device *device; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; int r; assert(old_device); - device = udev_device_new(old_device->udev); - if (!device) - return NULL; - - r = device_shallow_clone(old_device->device, &device->device); + r = device_shallow_clone(old_device->device, &device); if (r < 0) { - udev_device_unref(device); errno = -r; return NULL; } - return device; + return udev_device_new(old_device->udev, device); } struct udev_device *udev_device_clone_with_db(struct udev_device *udev_device_old) { - struct udev_device *udev_device; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; int r; assert(udev_device_old); - udev_device = udev_device_new(udev_device_old->udev); - if (!udev_device) - return NULL; - - r = device_clone_with_db(udev_device_old->device, &udev_device->device); + r = device_clone_with_db(udev_device_old->device, &device); if (r < 0) { - udev_device_unref(udev_device); errno = -r; return NULL; } - return udev_device; + return udev_device_new(udev_device_old->udev, device); } struct udev_device *udev_device_new_from_nulstr(struct udev *udev, char *nulstr, ssize_t buflen) { - struct udev_device *device; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; int r; - device = udev_device_new(udev); - if (!device) - return NULL; - - r = device_new_from_nulstr(&device->device, (uint8_t*)nulstr, buflen); + r = device_new_from_nulstr(&device, (uint8_t*)nulstr, buflen); if (r < 0) { - udev_device_unref(device); errno = -r; return NULL; } - return device; + return udev_device_new(udev, device); } struct udev_device *udev_device_new_from_synthetic_event(struct udev *udev, const char *syspath, const char *action) { - struct udev_device *device; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; int r; - device = udev_device_new(udev); - if (!device) - return NULL; - - r = device_new_from_synthetic_event(&device->device, syspath, action); + r = device_new_from_synthetic_event(&device, syspath, action); if (r < 0) { - udev_device_unref(device); errno = -r; return NULL; } - return device; + return udev_device_new(udev, device); } int udev_device_copy_properties(struct udev_device *udev_device_dst, struct udev_device *udev_device_src) { diff --git a/src/libudev/libudev-device.c b/src/libudev/libudev-device.c index 18dbe03739c..95ad5ced572 100644 --- a/src/libudev/libudev-device.c +++ b/src/libudev/libudev-device.c @@ -187,9 +187,11 @@ _public_ const char *udev_device_get_property_value(struct udev_device *udev_dev return value; } -struct udev_device *udev_device_new(struct udev *udev) { +struct udev_device *udev_device_new(struct udev *udev, sd_device *device) { struct udev_device *udev_device; + assert(device); + udev_device = new(struct udev_device, 1); if (!udev_device) { errno = ENOMEM; @@ -199,6 +201,7 @@ struct udev_device *udev_device_new(struct udev *udev) { *udev_device = (struct udev_device) { .n_ref = 1, .udev = udev, + .device = sd_device_ref(device), }; udev_list_init(udev, &udev_device->properties, true); @@ -224,21 +227,16 @@ struct udev_device *udev_device_new(struct udev *udev) { * Returns: a new udev device, or #NULL, if it does not exist **/ _public_ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath) { - struct udev_device *udev_device; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; int r; - udev_device = udev_device_new(udev); - if (!udev_device) - return NULL; - - r = sd_device_new_from_syspath(&udev_device->device, syspath); + r = sd_device_new_from_syspath(&device, syspath); if (r < 0) { errno = -r; - udev_device_unref(udev_device); return NULL; } - return udev_device; + return udev_device_new(udev, device); } /** @@ -258,21 +256,16 @@ _public_ struct udev_device *udev_device_new_from_syspath(struct udev *udev, con * Returns: a new udev device, or #NULL, if it does not exist **/ _public_ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum) { - struct udev_device *udev_device; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; int r; - udev_device = udev_device_new(udev); - if (!udev_device) - return NULL; - - r = sd_device_new_from_devnum(&udev_device->device, type, devnum); + r = sd_device_new_from_devnum(&device, type, devnum); if (r < 0) { errno = -r; - udev_device_unref(udev_device); return NULL; } - return udev_device; + return udev_device_new(udev, device); } /** @@ -294,21 +287,16 @@ _public_ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char * Returns: a new udev device, or #NULL, if it does not exist **/ _public_ struct udev_device *udev_device_new_from_device_id(struct udev *udev, const char *id) { - struct udev_device *udev_device; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; int r; - udev_device = udev_device_new(udev); - if (!udev_device) - return NULL; - - r = sd_device_new_from_device_id(&udev_device->device, id); + r = sd_device_new_from_device_id(&device, id); if (r < 0) { errno = -r; - udev_device_unref(udev_device); return NULL; } - return udev_device; + return udev_device_new(udev, device); } /** @@ -327,21 +315,16 @@ _public_ struct udev_device *udev_device_new_from_device_id(struct udev *udev, c * Returns: a new udev device, or #NULL, if it does not exist **/ _public_ struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname) { - struct udev_device *udev_device; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; int r; - udev_device = udev_device_new(udev); - if (!udev_device) - return NULL; - - r = sd_device_new_from_subsystem_sysname(&udev_device->device, subsystem, sysname); + r = sd_device_new_from_subsystem_sysname(&device, subsystem, sysname); if (r < 0) { errno = -r; - udev_device_unref(udev_device); return NULL; } - return udev_device; + return udev_device_new(udev, device); } /** @@ -359,44 +342,31 @@ _public_ struct udev_device *udev_device_new_from_subsystem_sysname(struct udev * Returns: a new udev device, or #NULL, if it does not exist **/ _public_ struct udev_device *udev_device_new_from_environment(struct udev *udev) { - struct udev_device *udev_device; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; int r; - udev_device = udev_device_new(udev); - if (!udev_device) - return NULL; - - r = device_new_from_strv(&udev_device->device, environ); + r = device_new_from_strv(&device, environ); if (r < 0) { errno = -r; - udev_device_unref(udev_device); return NULL; } - return udev_device; + return udev_device_new(udev, device); } static struct udev_device *device_new_from_parent(struct udev_device *child) { - struct udev_device *parent; + sd_device *parent; int r; assert_return_errno(child, NULL, EINVAL); - parent = udev_device_new(child->udev); - if (!parent) - return NULL; - - r = sd_device_get_parent(child->device, &parent->device); + r = sd_device_get_parent(child->device, &parent); if (r < 0) { errno = -r; - udev_device_unref(parent); return NULL; } - /* the parent is unref'ed with the child, so take a ref from libudev as well */ - sd_device_ref(parent->device); - - return parent; + return udev_device_new(child->udev, parent); } /**