Merge pull request #12777 from yuwata/libudev-enumerate-issue-12776
libudev: rescan devices when filter is updated
This commit is contained in:
commit
9c5e1c24ad
|
@ -4,36 +4,7 @@
|
|||
#include "libudev.h"
|
||||
#include "sd-device.h"
|
||||
|
||||
#include "libudev-list-internal.h"
|
||||
|
||||
/**
|
||||
* udev_device:
|
||||
*
|
||||
* Opaque object representing one kernel sys device.
|
||||
*/
|
||||
struct udev_device {
|
||||
struct udev *udev;
|
||||
|
||||
/* real device object */
|
||||
sd_device *device;
|
||||
|
||||
/* legacy */
|
||||
unsigned n_ref;
|
||||
|
||||
struct udev_device *parent;
|
||||
bool parent_set;
|
||||
|
||||
struct udev_list properties;
|
||||
uint64_t properties_generation;
|
||||
struct udev_list tags;
|
||||
uint64_t tags_generation;
|
||||
struct udev_list devlinks;
|
||||
uint64_t devlinks_generation;
|
||||
bool properties_read:1;
|
||||
bool tags_read:1;
|
||||
bool devlinks_read:1;
|
||||
struct udev_list sysattrs;
|
||||
bool sysattrs_read;
|
||||
};
|
||||
struct udev_device;
|
||||
|
||||
struct udev_device *udev_device_new(struct udev *udev, sd_device *device);
|
||||
sd_device *udev_device_get_sd_device(struct udev_device *udev_device);
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include "device-private.h"
|
||||
#include "device-util.h"
|
||||
#include "libudev-device-internal.h"
|
||||
#include "libudev-list-internal.h"
|
||||
#include "parse-util.h"
|
||||
#include "time-util.h"
|
||||
|
||||
|
@ -36,6 +37,36 @@
|
|||
* a unique name inside that subsystem.
|
||||
*/
|
||||
|
||||
/**
|
||||
* udev_device:
|
||||
*
|
||||
* Opaque object representing one kernel sys device.
|
||||
*/
|
||||
struct udev_device {
|
||||
struct udev *udev;
|
||||
|
||||
/* real device object */
|
||||
sd_device *device;
|
||||
|
||||
/* legacy */
|
||||
unsigned n_ref;
|
||||
|
||||
struct udev_device *parent;
|
||||
bool parent_set;
|
||||
|
||||
struct udev_list *properties;
|
||||
uint64_t properties_generation;
|
||||
struct udev_list *tags;
|
||||
uint64_t tags_generation;
|
||||
struct udev_list *devlinks;
|
||||
uint64_t devlinks_generation;
|
||||
bool properties_read:1;
|
||||
bool tags_read:1;
|
||||
bool devlinks_read:1;
|
||||
struct udev_list *sysattrs;
|
||||
bool sysattrs_read;
|
||||
};
|
||||
|
||||
/**
|
||||
* udev_device_get_seqnum:
|
||||
* @udev_device: udev device
|
||||
|
@ -168,10 +199,24 @@ _public_ const char *udev_device_get_property_value(struct udev_device *udev_dev
|
|||
}
|
||||
|
||||
struct udev_device *udev_device_new(struct udev *udev, sd_device *device) {
|
||||
_cleanup_(udev_list_freep) struct udev_list *properties = NULL, *tags = NULL, *sysattrs = NULL, *devlinks = NULL;
|
||||
struct udev_device *udev_device;
|
||||
|
||||
assert(device);
|
||||
|
||||
properties = udev_list_new(true);
|
||||
if (!properties)
|
||||
return_with_errno(NULL, ENOMEM);
|
||||
tags = udev_list_new(true);
|
||||
if (!tags)
|
||||
return_with_errno(NULL, ENOMEM);
|
||||
sysattrs = udev_list_new(true);
|
||||
if (!sysattrs)
|
||||
return_with_errno(NULL, ENOMEM);
|
||||
devlinks = udev_list_new(true);
|
||||
if (!devlinks)
|
||||
return_with_errno(NULL, ENOMEM);
|
||||
|
||||
udev_device = new(struct udev_device, 1);
|
||||
if (!udev_device)
|
||||
return_with_errno(NULL, ENOMEM);
|
||||
|
@ -180,13 +225,12 @@ struct udev_device *udev_device_new(struct udev *udev, sd_device *device) {
|
|||
.n_ref = 1,
|
||||
.udev = udev,
|
||||
.device = sd_device_ref(device),
|
||||
.properties = TAKE_PTR(properties),
|
||||
.tags = TAKE_PTR(tags),
|
||||
.sysattrs = TAKE_PTR(sysattrs),
|
||||
.devlinks = TAKE_PTR(devlinks),
|
||||
};
|
||||
|
||||
udev_list_init(&udev_device->properties, true);
|
||||
udev_list_init(&udev_device->tags, true);
|
||||
udev_list_init(&udev_device->sysattrs, true);
|
||||
udev_list_init(&udev_device->devlinks, true);
|
||||
|
||||
return udev_device;
|
||||
}
|
||||
|
||||
|
@ -429,10 +473,10 @@ static struct udev_device *udev_device_free(struct udev_device *udev_device) {
|
|||
sd_device_unref(udev_device->device);
|
||||
udev_device_unref(udev_device->parent);
|
||||
|
||||
udev_list_cleanup(&udev_device->properties);
|
||||
udev_list_cleanup(&udev_device->sysattrs);
|
||||
udev_list_cleanup(&udev_device->tags);
|
||||
udev_list_cleanup(&udev_device->devlinks);
|
||||
udev_list_free(udev_device->properties);
|
||||
udev_list_free(udev_device->sysattrs);
|
||||
udev_list_free(udev_device->tags);
|
||||
udev_list_free(udev_device->devlinks);
|
||||
|
||||
return mfree(udev_device);
|
||||
}
|
||||
|
@ -587,17 +631,17 @@ _public_ struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev
|
|||
!udev_device->devlinks_read) {
|
||||
const char *devlink;
|
||||
|
||||
udev_list_cleanup(&udev_device->devlinks);
|
||||
udev_list_cleanup(udev_device->devlinks);
|
||||
|
||||
FOREACH_DEVICE_DEVLINK(udev_device->device, devlink)
|
||||
if (!udev_list_entry_add(&udev_device->devlinks, devlink, NULL))
|
||||
if (!udev_list_entry_add(udev_device->devlinks, devlink, NULL))
|
||||
return_with_errno(NULL, ENOMEM);
|
||||
|
||||
udev_device->devlinks_read = true;
|
||||
udev_device->devlinks_generation = device_get_devlinks_generation(udev_device->device);
|
||||
}
|
||||
|
||||
return udev_list_get_entry(&udev_device->devlinks);
|
||||
return udev_list_get_entry(udev_device->devlinks);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -619,17 +663,17 @@ _public_ struct udev_list_entry *udev_device_get_properties_list_entry(struct ud
|
|||
!udev_device->properties_read) {
|
||||
const char *key, *value;
|
||||
|
||||
udev_list_cleanup(&udev_device->properties);
|
||||
udev_list_cleanup(udev_device->properties);
|
||||
|
||||
FOREACH_DEVICE_PROPERTY(udev_device->device, key, value)
|
||||
if (!udev_list_entry_add(&udev_device->properties, key, value))
|
||||
if (!udev_list_entry_add(udev_device->properties, key, value))
|
||||
return_with_errno(NULL, ENOMEM);
|
||||
|
||||
udev_device->properties_read = true;
|
||||
udev_device->properties_generation = device_get_properties_generation(udev_device->device);
|
||||
}
|
||||
|
||||
return udev_list_get_entry(&udev_device->properties);
|
||||
return udev_list_get_entry(udev_device->properties);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -739,16 +783,16 @@ _public_ struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_
|
|||
if (!udev_device->sysattrs_read) {
|
||||
const char *sysattr;
|
||||
|
||||
udev_list_cleanup(&udev_device->sysattrs);
|
||||
udev_list_cleanup(udev_device->sysattrs);
|
||||
|
||||
FOREACH_DEVICE_SYSATTR(udev_device->device, sysattr)
|
||||
if (!udev_list_entry_add(&udev_device->sysattrs, sysattr, NULL))
|
||||
if (!udev_list_entry_add(udev_device->sysattrs, sysattr, NULL))
|
||||
return_with_errno(NULL, ENOMEM);
|
||||
|
||||
udev_device->sysattrs_read = true;
|
||||
}
|
||||
|
||||
return udev_list_get_entry(&udev_device->sysattrs);
|
||||
return udev_list_get_entry(udev_device->sysattrs);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -794,17 +838,17 @@ _public_ struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_dev
|
|||
!udev_device->tags_read) {
|
||||
const char *tag;
|
||||
|
||||
udev_list_cleanup(&udev_device->tags);
|
||||
udev_list_cleanup(udev_device->tags);
|
||||
|
||||
FOREACH_DEVICE_TAG(udev_device->device, tag)
|
||||
if (!udev_list_entry_add(&udev_device->tags, tag, NULL))
|
||||
if (!udev_list_entry_add(udev_device->tags, tag, NULL))
|
||||
return_with_errno(NULL, ENOMEM);
|
||||
|
||||
udev_device->tags_read = true;
|
||||
udev_device->tags_generation = device_get_tags_generation(udev_device->device);
|
||||
}
|
||||
|
||||
return udev_list_get_entry(&udev_device->tags);
|
||||
return udev_list_get_entry(udev_device->tags);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -821,3 +865,9 @@ _public_ int udev_device_has_tag(struct udev_device *udev_device, const char *ta
|
|||
|
||||
return sd_device_has_tag(udev_device->device, tag) > 0;
|
||||
}
|
||||
|
||||
sd_device *udev_device_get_sd_device(struct udev_device *udev_device) {
|
||||
assert(udev_device);
|
||||
|
||||
return udev_device->device;
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
#include "device-enumerator-private.h"
|
||||
#include "device-util.h"
|
||||
#include "libudev-device-internal.h"
|
||||
#include "libudev-list-internal.h"
|
||||
|
||||
/**
|
||||
* SECTION:libudev-enumerate
|
||||
|
@ -34,7 +35,7 @@
|
|||
struct udev_enumerate {
|
||||
struct udev *udev;
|
||||
unsigned n_ref;
|
||||
struct udev_list devices_list;
|
||||
struct udev_list *devices_list;
|
||||
bool devices_uptodate:1;
|
||||
|
||||
sd_device_enumerator *enumerator;
|
||||
|
@ -50,6 +51,7 @@ struct udev_enumerate {
|
|||
**/
|
||||
_public_ struct udev_enumerate *udev_enumerate_new(struct udev *udev) {
|
||||
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
|
||||
_cleanup_(udev_list_freep) struct udev_list *list = NULL;
|
||||
struct udev_enumerate *udev_enumerate;
|
||||
int r;
|
||||
|
||||
|
@ -61,6 +63,10 @@ _public_ struct udev_enumerate *udev_enumerate_new(struct udev *udev) {
|
|||
if (r < 0)
|
||||
return_with_errno(NULL, r);
|
||||
|
||||
list = udev_list_new(false);
|
||||
if (!list)
|
||||
return_with_errno(NULL, ENOMEM);
|
||||
|
||||
udev_enumerate = new(struct udev_enumerate, 1);
|
||||
if (!udev_enumerate)
|
||||
return_with_errno(NULL, ENOMEM);
|
||||
|
@ -69,17 +75,16 @@ _public_ struct udev_enumerate *udev_enumerate_new(struct udev *udev) {
|
|||
.udev = udev,
|
||||
.n_ref = 1,
|
||||
.enumerator = TAKE_PTR(e),
|
||||
.devices_list = TAKE_PTR(list),
|
||||
};
|
||||
|
||||
udev_list_init(&udev_enumerate->devices_list, false);
|
||||
|
||||
return udev_enumerate;
|
||||
}
|
||||
|
||||
static struct udev_enumerate *udev_enumerate_free(struct udev_enumerate *udev_enumerate) {
|
||||
assert(udev_enumerate);
|
||||
|
||||
udev_list_cleanup(&udev_enumerate->devices_list);
|
||||
udev_list_free(udev_enumerate->devices_list);
|
||||
sd_device_enumerator_unref(udev_enumerate->enumerator);
|
||||
return mfree(udev_enumerate);
|
||||
}
|
||||
|
@ -134,7 +139,7 @@ _public_ struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enume
|
|||
if (!udev_enumerate->devices_uptodate) {
|
||||
sd_device *device;
|
||||
|
||||
udev_list_cleanup(&udev_enumerate->devices_list);
|
||||
udev_list_cleanup(udev_enumerate->devices_list);
|
||||
|
||||
FOREACH_DEVICE_AND_SUBSYSTEM(udev_enumerate->enumerator, device) {
|
||||
const char *syspath;
|
||||
|
@ -144,14 +149,14 @@ _public_ struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enume
|
|||
if (r < 0)
|
||||
return_with_errno(NULL, r);
|
||||
|
||||
if (!udev_list_entry_add(&udev_enumerate->devices_list, syspath, NULL))
|
||||
if (!udev_list_entry_add(udev_enumerate->devices_list, syspath, NULL))
|
||||
return_with_errno(NULL, ENOMEM);
|
||||
}
|
||||
|
||||
udev_enumerate->devices_uptodate = true;
|
||||
}
|
||||
|
||||
e = udev_list_get_entry(&udev_enumerate->devices_list);
|
||||
e = udev_list_get_entry(udev_enumerate->devices_list);
|
||||
if (!e)
|
||||
return_with_errno(NULL, ENODATA);
|
||||
|
||||
|
@ -168,12 +173,19 @@ _public_ struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enume
|
|||
* Returns: 0 on success, otherwise a negative error value.
|
||||
*/
|
||||
_public_ int udev_enumerate_add_match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem) {
|
||||
int r;
|
||||
|
||||
assert_return(udev_enumerate, -EINVAL);
|
||||
|
||||
if (!subsystem)
|
||||
return 0;
|
||||
|
||||
return sd_device_enumerator_add_match_subsystem(udev_enumerate->enumerator, subsystem, true);
|
||||
r = sd_device_enumerator_add_match_subsystem(udev_enumerate->enumerator, subsystem, true);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
udev_enumerate->devices_uptodate = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -186,12 +198,19 @@ _public_ int udev_enumerate_add_match_subsystem(struct udev_enumerate *udev_enum
|
|||
* Returns: 0 on success, otherwise a negative error value.
|
||||
*/
|
||||
_public_ int udev_enumerate_add_nomatch_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem) {
|
||||
int r;
|
||||
|
||||
assert_return(udev_enumerate, -EINVAL);
|
||||
|
||||
if (!subsystem)
|
||||
return 0;
|
||||
|
||||
return sd_device_enumerator_add_match_subsystem(udev_enumerate->enumerator, subsystem, false);
|
||||
r = sd_device_enumerator_add_match_subsystem(udev_enumerate->enumerator, subsystem, false);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
udev_enumerate->devices_uptodate = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -205,12 +224,19 @@ _public_ int udev_enumerate_add_nomatch_subsystem(struct udev_enumerate *udev_en
|
|||
* Returns: 0 on success, otherwise a negative error value.
|
||||
*/
|
||||
_public_ int udev_enumerate_add_match_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value) {
|
||||
int r;
|
||||
|
||||
assert_return(udev_enumerate, -EINVAL);
|
||||
|
||||
if (!sysattr)
|
||||
return 0;
|
||||
|
||||
return sd_device_enumerator_add_match_sysattr(udev_enumerate->enumerator, sysattr, value, true);
|
||||
r = sd_device_enumerator_add_match_sysattr(udev_enumerate->enumerator, sysattr, value, true);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
udev_enumerate->devices_uptodate = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -224,12 +250,19 @@ _public_ int udev_enumerate_add_match_sysattr(struct udev_enumerate *udev_enumer
|
|||
* Returns: 0 on success, otherwise a negative error value.
|
||||
*/
|
||||
_public_ int udev_enumerate_add_nomatch_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value) {
|
||||
int r;
|
||||
|
||||
assert_return(udev_enumerate, -EINVAL);
|
||||
|
||||
if (!sysattr)
|
||||
return 0;
|
||||
|
||||
return sd_device_enumerator_add_match_sysattr(udev_enumerate->enumerator, sysattr, value, false);
|
||||
r = sd_device_enumerator_add_match_sysattr(udev_enumerate->enumerator, sysattr, value, false);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
udev_enumerate->devices_uptodate = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -243,12 +276,19 @@ _public_ int udev_enumerate_add_nomatch_sysattr(struct udev_enumerate *udev_enum
|
|||
* Returns: 0 on success, otherwise a negative error value.
|
||||
*/
|
||||
_public_ int udev_enumerate_add_match_property(struct udev_enumerate *udev_enumerate, const char *property, const char *value) {
|
||||
int r;
|
||||
|
||||
assert_return(udev_enumerate, -EINVAL);
|
||||
|
||||
if (!property)
|
||||
return 0;
|
||||
|
||||
return sd_device_enumerator_add_match_property(udev_enumerate->enumerator, property, value);
|
||||
r = sd_device_enumerator_add_match_property(udev_enumerate->enumerator, property, value);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
udev_enumerate->devices_uptodate = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -261,12 +301,19 @@ _public_ int udev_enumerate_add_match_property(struct udev_enumerate *udev_enume
|
|||
* Returns: 0 on success, otherwise a negative error value.
|
||||
*/
|
||||
_public_ int udev_enumerate_add_match_tag(struct udev_enumerate *udev_enumerate, const char *tag) {
|
||||
int r;
|
||||
|
||||
assert_return(udev_enumerate, -EINVAL);
|
||||
|
||||
if (!tag)
|
||||
return 0;
|
||||
|
||||
return sd_device_enumerator_add_match_tag(udev_enumerate->enumerator, tag);
|
||||
r = sd_device_enumerator_add_match_tag(udev_enumerate->enumerator, tag);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
udev_enumerate->devices_uptodate = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -280,12 +327,19 @@ _public_ int udev_enumerate_add_match_tag(struct udev_enumerate *udev_enumerate,
|
|||
* Returns: 0 on success, otherwise a negative error value.
|
||||
*/
|
||||
_public_ int udev_enumerate_add_match_parent(struct udev_enumerate *udev_enumerate, struct udev_device *parent) {
|
||||
int r;
|
||||
|
||||
assert_return(udev_enumerate, -EINVAL);
|
||||
|
||||
if (!parent)
|
||||
return 0;
|
||||
|
||||
return sd_device_enumerator_add_match_parent(udev_enumerate->enumerator, parent->device);
|
||||
r = sd_device_enumerator_add_match_parent(udev_enumerate->enumerator, udev_device_get_sd_device(parent));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
udev_enumerate->devices_uptodate = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -307,9 +361,16 @@ _public_ int udev_enumerate_add_match_parent(struct udev_enumerate *udev_enumera
|
|||
* Returns: 0 on success, otherwise a negative error value.
|
||||
*/
|
||||
_public_ int udev_enumerate_add_match_is_initialized(struct udev_enumerate *udev_enumerate) {
|
||||
int r;
|
||||
|
||||
assert_return(udev_enumerate, -EINVAL);
|
||||
|
||||
return device_enumerator_add_match_is_initialized(udev_enumerate->enumerator);
|
||||
r = device_enumerator_add_match_is_initialized(udev_enumerate->enumerator);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
udev_enumerate->devices_uptodate = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -322,12 +383,19 @@ _public_ int udev_enumerate_add_match_is_initialized(struct udev_enumerate *udev
|
|||
* Returns: 0 on success, otherwise a negative error value.
|
||||
*/
|
||||
_public_ int udev_enumerate_add_match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname) {
|
||||
int r;
|
||||
|
||||
assert_return(udev_enumerate, -EINVAL);
|
||||
|
||||
if (!sysname)
|
||||
return 0;
|
||||
|
||||
return sd_device_enumerator_add_match_sysname(udev_enumerate->enumerator, sysname);
|
||||
r = sd_device_enumerator_add_match_sysname(udev_enumerate->enumerator, sysname);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
udev_enumerate->devices_uptodate = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -356,6 +424,7 @@ _public_ int udev_enumerate_add_syspath(struct udev_enumerate *udev_enumerate, c
|
|||
if (r < 0)
|
||||
return r;
|
||||
|
||||
udev_enumerate->devices_uptodate = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
struct udev_hwdb {
|
||||
unsigned n_ref;
|
||||
sd_hwdb *hwdb;
|
||||
struct udev_list properties_list;
|
||||
struct udev_list *properties_list;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -35,6 +35,7 @@ struct udev_hwdb {
|
|||
* Returns: a hwdb context.
|
||||
**/
|
||||
_public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
|
||||
_cleanup_(udev_list_freep) struct udev_list *list = NULL;
|
||||
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb_internal = NULL;
|
||||
struct udev_hwdb *hwdb;
|
||||
int r;
|
||||
|
@ -43,6 +44,10 @@ _public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
|
|||
if (r < 0)
|
||||
return_with_errno(NULL, r);
|
||||
|
||||
list = udev_list_new(true);
|
||||
if (!list)
|
||||
return_with_errno(NULL, ENOMEM);
|
||||
|
||||
hwdb = new(struct udev_hwdb, 1);
|
||||
if (!hwdb)
|
||||
return_with_errno(NULL, ENOMEM);
|
||||
|
@ -50,10 +55,9 @@ _public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
|
|||
*hwdb = (struct udev_hwdb) {
|
||||
.n_ref = 1,
|
||||
.hwdb = TAKE_PTR(hwdb_internal),
|
||||
.properties_list = TAKE_PTR(list),
|
||||
};
|
||||
|
||||
udev_list_init(&hwdb->properties_list, true);
|
||||
|
||||
return hwdb;
|
||||
}
|
||||
|
||||
|
@ -61,7 +65,7 @@ static struct udev_hwdb *udev_hwdb_free(struct udev_hwdb *hwdb) {
|
|||
assert(hwdb);
|
||||
|
||||
sd_hwdb_unref(hwdb->hwdb);
|
||||
udev_list_cleanup(&hwdb->properties_list);
|
||||
udev_list_free(hwdb->properties_list);
|
||||
return mfree(hwdb);
|
||||
}
|
||||
|
||||
|
@ -105,13 +109,13 @@ _public_ struct udev_list_entry *udev_hwdb_get_properties_list_entry(struct udev
|
|||
assert_return_errno(hwdb, NULL, EINVAL);
|
||||
assert_return_errno(modalias, NULL, EINVAL);
|
||||
|
||||
udev_list_cleanup(&hwdb->properties_list);
|
||||
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))
|
||||
if (!udev_list_entry_add(hwdb->properties_list, key, value))
|
||||
return_with_errno(NULL, ENOMEM);
|
||||
|
||||
e = udev_list_get_entry(&hwdb->properties_list);
|
||||
e = udev_list_get_entry(hwdb->properties_list);
|
||||
if (!e)
|
||||
return_with_errno(NULL, ENODATA);
|
||||
|
||||
|
|
|
@ -3,19 +3,14 @@
|
|||
|
||||
#include "libudev.h"
|
||||
|
||||
struct udev_list_node {
|
||||
struct udev_list_node *next, *prev;
|
||||
};
|
||||
#include "macro.h"
|
||||
|
||||
struct udev_list {
|
||||
struct udev_list_node node;
|
||||
struct udev_list_entry **entries;
|
||||
unsigned entries_cur;
|
||||
unsigned entries_max;
|
||||
bool unique;
|
||||
};
|
||||
struct udev_list;
|
||||
|
||||
void udev_list_init(struct udev_list *list, bool unique);
|
||||
struct udev_list *udev_list_new(bool unique);
|
||||
void udev_list_cleanup(struct udev_list *list);
|
||||
struct udev_list *udev_list_free(struct udev_list *list);
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_list *, udev_list_free);
|
||||
|
||||
struct udev_list_entry *udev_list_get_entry(struct udev_list *list);
|
||||
struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char *name, const char *value);
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "hashmap.h"
|
||||
#include "libudev-list-internal.h"
|
||||
#include "memory-util.h"
|
||||
#include "list.h"
|
||||
#include "sort-util.h"
|
||||
|
||||
/**
|
||||
* SECTION:libudev-list
|
||||
|
@ -23,209 +20,164 @@
|
|||
* contains a name, and optionally a value.
|
||||
*/
|
||||
struct udev_list_entry {
|
||||
struct udev_list_node node;
|
||||
struct udev_list *list;
|
||||
char *name;
|
||||
char *value;
|
||||
int num;
|
||||
|
||||
LIST_FIELDS(struct udev_list_entry, entries);
|
||||
};
|
||||
|
||||
/* the list's head points to itself if empty */
|
||||
static void udev_list_node_init(struct udev_list_node *list) {
|
||||
list->next = list;
|
||||
list->prev = list;
|
||||
}
|
||||
struct udev_list {
|
||||
Hashmap *unique_entries;
|
||||
LIST_HEAD(struct udev_list_entry, entries);
|
||||
bool unique:1;
|
||||
bool uptodate:1;
|
||||
};
|
||||
|
||||
static int udev_list_node_is_empty(struct udev_list_node *list) {
|
||||
return list->next == list;
|
||||
}
|
||||
|
||||
static void udev_list_node_insert_between(struct udev_list_node *new,
|
||||
struct udev_list_node *prev,
|
||||
struct udev_list_node *next) {
|
||||
next->prev = new;
|
||||
new->next = next;
|
||||
new->prev = prev;
|
||||
prev->next = new;
|
||||
}
|
||||
|
||||
static void udev_list_node_remove(struct udev_list_node *entry) {
|
||||
struct udev_list_node *prev = entry->prev;
|
||||
struct udev_list_node *next = entry->next;
|
||||
|
||||
next->prev = prev;
|
||||
prev->next = next;
|
||||
|
||||
entry->prev = NULL;
|
||||
entry->next = NULL;
|
||||
}
|
||||
|
||||
/* return list entry which embeds this node */
|
||||
static struct udev_list_entry *list_node_to_entry(struct udev_list_node *node) {
|
||||
return container_of(node, struct udev_list_entry, node);
|
||||
}
|
||||
|
||||
void udev_list_init(struct udev_list *list, bool unique) {
|
||||
memzero(list, sizeof(struct udev_list));
|
||||
list->unique = unique;
|
||||
udev_list_node_init(&list->node);
|
||||
}
|
||||
|
||||
/* insert entry into a list as the last element */
|
||||
static void udev_list_entry_append(struct udev_list_entry *new, struct udev_list *list) {
|
||||
/* inserting before the list head make the node the last node in the list */
|
||||
udev_list_node_insert_between(&new->node, list->node.prev, &list->node);
|
||||
new->list = list;
|
||||
}
|
||||
|
||||
/* insert entry into a list, before a given existing entry */
|
||||
static void udev_list_entry_insert_before(struct udev_list_entry *new, struct udev_list_entry *entry) {
|
||||
udev_list_node_insert_between(&new->node, entry->node.prev, &entry->node);
|
||||
new->list = entry->list;
|
||||
}
|
||||
|
||||
/* binary search in sorted array */
|
||||
static int list_search(struct udev_list *list, const char *name) {
|
||||
unsigned first, last;
|
||||
|
||||
first = 0;
|
||||
last = list->entries_cur;
|
||||
while (first < last) {
|
||||
unsigned i;
|
||||
int cmp;
|
||||
|
||||
i = (first + last)/2;
|
||||
cmp = strcmp(name, list->entries[i]->name);
|
||||
if (cmp < 0)
|
||||
last = i;
|
||||
else if (cmp > 0)
|
||||
first = i+1;
|
||||
else
|
||||
return i;
|
||||
}
|
||||
|
||||
/* not found, return negative insertion-index+1 */
|
||||
return -(first+1);
|
||||
}
|
||||
|
||||
struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char *name, const char *value) {
|
||||
struct udev_list_entry *entry;
|
||||
int i = 0;
|
||||
|
||||
if (list->unique) {
|
||||
/* lookup existing name or insertion-index */
|
||||
i = list_search(list, name);
|
||||
if (i >= 0) {
|
||||
entry = list->entries[i];
|
||||
|
||||
free(entry->value);
|
||||
if (!value) {
|
||||
entry->value = NULL;
|
||||
return entry;
|
||||
}
|
||||
entry->value = strdup(value);
|
||||
if (!entry->value)
|
||||
return NULL;
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
||||
/* add new name */
|
||||
entry = new0(struct udev_list_entry, 1);
|
||||
static struct udev_list_entry *udev_list_entry_free(struct udev_list_entry *entry) {
|
||||
if (!entry)
|
||||
return NULL;
|
||||
|
||||
entry->name = strdup(name);
|
||||
if (!entry->name)
|
||||
return mfree(entry);
|
||||
|
||||
if (value) {
|
||||
entry->value = strdup(value);
|
||||
if (!entry->value) {
|
||||
free(entry->name);
|
||||
return mfree(entry);
|
||||
}
|
||||
}
|
||||
|
||||
if (list->unique) {
|
||||
/* allocate or enlarge sorted array if needed */
|
||||
if (list->entries_cur >= list->entries_max) {
|
||||
struct udev_list_entry **entries;
|
||||
unsigned add;
|
||||
|
||||
add = list->entries_max;
|
||||
if (add < 1)
|
||||
add = 64;
|
||||
entries = reallocarray(list->entries, list->entries_max + add, sizeof(struct udev_list_entry *));
|
||||
if (!entries) {
|
||||
free(entry->name);
|
||||
free(entry->value);
|
||||
return mfree(entry);
|
||||
}
|
||||
list->entries = entries;
|
||||
list->entries_max += add;
|
||||
}
|
||||
|
||||
/* the negative i returned the insertion index */
|
||||
i = (-i)-1;
|
||||
|
||||
/* insert into sorted list */
|
||||
if ((unsigned)i < list->entries_cur)
|
||||
udev_list_entry_insert_before(entry, list->entries[i]);
|
||||
if (entry->list) {
|
||||
if (entry->list->unique)
|
||||
hashmap_remove(entry->list->unique_entries, entry->name);
|
||||
else
|
||||
udev_list_entry_append(entry, list);
|
||||
|
||||
/* insert into sorted array */
|
||||
memmove(&list->entries[i+1], &list->entries[i],
|
||||
(list->entries_cur - i) * sizeof(struct udev_list_entry *));
|
||||
list->entries[i] = entry;
|
||||
list->entries_cur++;
|
||||
} else
|
||||
udev_list_entry_append(entry, list);
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
static void udev_list_entry_delete(struct udev_list_entry *entry) {
|
||||
if (entry->list->entries) {
|
||||
int i;
|
||||
struct udev_list *list = entry->list;
|
||||
|
||||
/* remove entry from sorted array */
|
||||
i = list_search(list, entry->name);
|
||||
if (i >= 0) {
|
||||
memmove(&list->entries[i], &list->entries[i+1],
|
||||
((list->entries_cur-1) - i) * sizeof(struct udev_list_entry *));
|
||||
list->entries_cur--;
|
||||
}
|
||||
LIST_REMOVE(entries, entry->list->entries, entry);
|
||||
}
|
||||
|
||||
udev_list_node_remove(&entry->node);
|
||||
free(entry->name);
|
||||
free(entry->value);
|
||||
free(entry);
|
||||
|
||||
return mfree(entry);
|
||||
}
|
||||
|
||||
#define udev_list_entry_foreach_safe(entry, tmp, first) \
|
||||
for (entry = first, tmp = udev_list_entry_get_next(entry); \
|
||||
entry; \
|
||||
entry = tmp, tmp = udev_list_entry_get_next(tmp))
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_list_entry *, udev_list_entry_free);
|
||||
|
||||
struct udev_list *udev_list_new(bool unique) {
|
||||
struct udev_list *list;
|
||||
|
||||
list = new(struct udev_list, 1);
|
||||
if (!list)
|
||||
return NULL;
|
||||
|
||||
*list = (struct udev_list) {
|
||||
.unique = unique,
|
||||
};
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char *_name, const char *_value) {
|
||||
_cleanup_(udev_list_entry_freep) struct udev_list_entry *entry = NULL;
|
||||
_cleanup_free_ char *name = NULL, *value = NULL;
|
||||
int r;
|
||||
|
||||
assert(list);
|
||||
|
||||
name = strdup(_name);
|
||||
if (!name)
|
||||
return NULL;
|
||||
|
||||
if (_value) {
|
||||
value = strdup(_value);
|
||||
if (!value)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
entry = new(struct udev_list_entry, 1);
|
||||
if (!entry)
|
||||
return NULL;
|
||||
|
||||
*entry = (struct udev_list_entry) {
|
||||
.list = list,
|
||||
.name = TAKE_PTR(name),
|
||||
.value = TAKE_PTR(value),
|
||||
};
|
||||
|
||||
if (list->unique) {
|
||||
r = hashmap_ensure_allocated(&list->unique_entries, &string_hash_ops);
|
||||
if (r < 0)
|
||||
return NULL;
|
||||
|
||||
udev_list_entry_free(hashmap_get(list->unique_entries, entry->name));
|
||||
|
||||
r = hashmap_put(list->unique_entries, entry->name, entry);
|
||||
if (r < 0)
|
||||
return NULL;
|
||||
|
||||
list->uptodate = false;
|
||||
} else
|
||||
LIST_APPEND(entries, list->entries, entry);
|
||||
|
||||
return TAKE_PTR(entry);
|
||||
}
|
||||
|
||||
void udev_list_cleanup(struct udev_list *list) {
|
||||
struct udev_list_entry *entry_loop;
|
||||
struct udev_list_entry *entry_tmp;
|
||||
struct udev_list_entry *i, *n;
|
||||
|
||||
list->entries = mfree(list->entries);
|
||||
list->entries_cur = 0;
|
||||
list->entries_max = 0;
|
||||
udev_list_entry_foreach_safe(entry_loop, entry_tmp, udev_list_get_entry(list))
|
||||
udev_list_entry_delete(entry_loop);
|
||||
if (!list)
|
||||
return;
|
||||
|
||||
if (list->unique) {
|
||||
hashmap_clear_with_destructor(list->unique_entries, udev_list_entry_free);
|
||||
list->uptodate = false;
|
||||
} else
|
||||
LIST_FOREACH_SAFE(entries, i, n, list->entries)
|
||||
udev_list_entry_free(i);
|
||||
}
|
||||
|
||||
struct udev_list *udev_list_free(struct udev_list *list) {
|
||||
if (!list)
|
||||
return NULL;
|
||||
|
||||
udev_list_cleanup(list);
|
||||
hashmap_free(list->unique_entries);
|
||||
|
||||
return mfree(list);
|
||||
}
|
||||
|
||||
static int udev_list_entry_compare_func(struct udev_list_entry * const *a, struct udev_list_entry * const *b) {
|
||||
return strcmp((*a)->name, (*b)->name);
|
||||
}
|
||||
|
||||
struct udev_list_entry *udev_list_get_entry(struct udev_list *list) {
|
||||
if (udev_list_node_is_empty(&list->node))
|
||||
if (!list)
|
||||
return NULL;
|
||||
return list_node_to_entry(list->node.next);
|
||||
|
||||
if (list->unique && !list->uptodate) {
|
||||
size_t n;
|
||||
|
||||
LIST_HEAD_INIT(list->entries);
|
||||
|
||||
n = hashmap_size(list->unique_entries);
|
||||
if (n == 0)
|
||||
;
|
||||
else if (n == 1)
|
||||
LIST_PREPEND(entries, list->entries, hashmap_first(list->unique_entries));
|
||||
else {
|
||||
_cleanup_free_ struct udev_list_entry **buf = NULL;
|
||||
struct udev_list_entry *entry, **p;
|
||||
Iterator i;
|
||||
size_t j;
|
||||
|
||||
buf = new(struct udev_list_entry *, n);
|
||||
if (!buf)
|
||||
return NULL;
|
||||
|
||||
p = buf;
|
||||
HASHMAP_FOREACH(entry, list->unique_entries, i)
|
||||
*p++ = entry;
|
||||
|
||||
typesafe_qsort(buf, n, udev_list_entry_compare_func);
|
||||
|
||||
for (j = n; j > 0; j--)
|
||||
LIST_PREPEND(entries, list->entries, buf[j-1]);
|
||||
}
|
||||
|
||||
list->uptodate = true;
|
||||
}
|
||||
|
||||
return list->entries;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -237,15 +189,11 @@ struct udev_list_entry *udev_list_get_entry(struct udev_list *list) {
|
|||
* Returns: udev_list_entry, #NULL if no more entries are available.
|
||||
*/
|
||||
_public_ struct udev_list_entry *udev_list_entry_get_next(struct udev_list_entry *list_entry) {
|
||||
struct udev_list_node *next;
|
||||
|
||||
if (!list_entry)
|
||||
return NULL;
|
||||
next = list_entry->node.next;
|
||||
/* empty list or no more entries */
|
||||
if (next == &list_entry->list->node)
|
||||
if (list_entry->list->unique && !list_entry->list->uptodate)
|
||||
return NULL;
|
||||
return list_node_to_entry(next);
|
||||
return list_entry->entries_next;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -258,18 +206,11 @@ _public_ struct udev_list_entry *udev_list_entry_get_next(struct udev_list_entry
|
|||
* Returns: udev_list_entry, #NULL if no matching entry is found.
|
||||
*/
|
||||
_public_ struct udev_list_entry *udev_list_entry_get_by_name(struct udev_list_entry *list_entry, const char *name) {
|
||||
int i;
|
||||
|
||||
if (!list_entry)
|
||||
return NULL;
|
||||
|
||||
if (!list_entry->list->unique)
|
||||
if (!list_entry->list->unique || !list_entry->list->uptodate)
|
||||
return NULL;
|
||||
|
||||
i = list_search(list_entry->list, name);
|
||||
if (i < 0)
|
||||
return NULL;
|
||||
return list_entry->list->entries[i];
|
||||
return hashmap_get(list_entry->list->unique_entries, name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -433,19 +433,20 @@ static void test_util_resolve_subsys_kernel(void) {
|
|||
}
|
||||
|
||||
static void test_list(void) {
|
||||
struct udev_list list = {};
|
||||
_cleanup_(udev_list_freep) struct udev_list *list = NULL;
|
||||
struct udev_list_entry *e;
|
||||
|
||||
/* empty list */
|
||||
udev_list_init(&list, false);
|
||||
assert_se(!udev_list_get_entry(&list));
|
||||
assert_se(list = udev_list_new(false));
|
||||
assert_se(!udev_list_get_entry(list));
|
||||
list = udev_list_free(list);
|
||||
|
||||
/* unique == false */
|
||||
udev_list_init(&list, false);
|
||||
assert_se(udev_list_entry_add(&list, "aaa", "hoge"));
|
||||
assert_se(udev_list_entry_add(&list, "aaa", "hogehoge"));
|
||||
assert_se(udev_list_entry_add(&list, "bbb", "foo"));
|
||||
e = udev_list_get_entry(&list);
|
||||
assert_se(list = udev_list_new(false));
|
||||
assert_se(udev_list_entry_add(list, "aaa", "hoge"));
|
||||
assert_se(udev_list_entry_add(list, "aaa", "hogehoge"));
|
||||
assert_se(udev_list_entry_add(list, "bbb", "foo"));
|
||||
e = udev_list_get_entry(list);
|
||||
assert_se(e);
|
||||
assert_se(streq_ptr(udev_list_entry_get_name(e), "aaa"));
|
||||
assert_se(streq_ptr(udev_list_entry_get_value(e), "hoge"));
|
||||
|
@ -462,14 +463,14 @@ static void test_list(void) {
|
|||
assert_se(!udev_list_entry_get_by_name(e, "aaa"));
|
||||
assert_se(!udev_list_entry_get_by_name(e, "bbb"));
|
||||
assert_se(!udev_list_entry_get_by_name(e, "ccc"));
|
||||
udev_list_cleanup(&list);
|
||||
list = udev_list_free(list);
|
||||
|
||||
/* unique == true */
|
||||
udev_list_init(&list, true);
|
||||
assert_se(udev_list_entry_add(&list, "aaa", "hoge"));
|
||||
assert_se(udev_list_entry_add(&list, "aaa", "hogehoge"));
|
||||
assert_se(udev_list_entry_add(&list, "bbb", "foo"));
|
||||
e = udev_list_get_entry(&list);
|
||||
assert_se(list = udev_list_new(true));
|
||||
assert_se(udev_list_entry_add(list, "aaa", "hoge"));
|
||||
assert_se(udev_list_entry_add(list, "aaa", "hogehoge"));
|
||||
assert_se(udev_list_entry_add(list, "bbb", "foo"));
|
||||
e = udev_list_get_entry(list);
|
||||
assert_se(e);
|
||||
assert_se(streq_ptr(udev_list_entry_get_name(e), "aaa"));
|
||||
assert_se(streq_ptr(udev_list_entry_get_value(e), "hogehoge"));
|
||||
|
@ -487,7 +488,6 @@ static void test_list(void) {
|
|||
assert_se(streq_ptr(udev_list_entry_get_name(e), "aaa"));
|
||||
assert_se(streq_ptr(udev_list_entry_get_value(e), "hogehoge"));
|
||||
assert_se(!udev_list_entry_get_by_name(e, "ccc"));
|
||||
udev_list_cleanup(&list);
|
||||
}
|
||||
|
||||
static int parse_args(int argc, char *argv[], const char **syspath, const char **subsystem) {
|
||||
|
|
Loading…
Reference in a new issue