2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2012-10-27 20:59:01 +02:00
|
|
|
|
2018-11-16 03:44:17 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2014-12-03 17:32:40 +01:00
|
|
|
#include "sd-hwdb.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2014-12-03 17:32:40 +01:00
|
|
|
#include "hwdb-util.h"
|
2018-11-16 03:44:17 +01:00
|
|
|
#include "libudev-list-internal.h"
|
2012-10-27 20:59:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:libudev-hwdb
|
|
|
|
* @short_description: retrieve properties from the hardware database
|
|
|
|
*
|
2012-10-28 00:43:44 +02:00
|
|
|
* Libudev hardware database interface.
|
2012-10-27 20:59:01 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* udev_hwdb:
|
|
|
|
*
|
|
|
|
* Opaque object representing the hardware database.
|
|
|
|
*/
|
|
|
|
struct udev_hwdb {
|
2018-08-28 06:07:48 +02:00
|
|
|
unsigned n_ref;
|
2014-12-03 17:32:40 +01:00
|
|
|
sd_hwdb *hwdb;
|
2019-06-12 06:03:19 +02:00
|
|
|
struct udev_list *properties_list;
|
2012-10-27 20:59:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* udev_hwdb_new:
|
2018-08-24 06:55:25 +02:00
|
|
|
* @udev: udev library context (unused)
|
2012-10-27 20:59:01 +02:00
|
|
|
*
|
|
|
|
* Create a hardware database context to query properties for devices.
|
|
|
|
*
|
|
|
|
* Returns: a hwdb context.
|
|
|
|
**/
|
|
|
|
_public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
|
2019-06-12 06:03:19 +02:00
|
|
|
_cleanup_(udev_list_freep) struct udev_list *list = NULL;
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb_internal = NULL;
|
2012-10-27 20:59:01 +02:00
|
|
|
struct udev_hwdb *hwdb;
|
2014-12-03 17:32:40 +01:00
|
|
|
int r;
|
2012-10-27 20:59:01 +02:00
|
|
|
|
2014-12-03 17:32:40 +01:00
|
|
|
r = sd_hwdb_new(&hwdb_internal);
|
2018-11-21 06:06:41 +01:00
|
|
|
if (r < 0)
|
|
|
|
return_with_errno(NULL, r);
|
2012-10-27 20:59:01 +02:00
|
|
|
|
2019-06-12 06:03:19 +02:00
|
|
|
list = udev_list_new(true);
|
|
|
|
if (!list)
|
|
|
|
return_with_errno(NULL, ENOMEM);
|
|
|
|
|
2018-08-24 06:57:04 +02:00
|
|
|
hwdb = new(struct udev_hwdb, 1);
|
2018-11-21 06:06:41 +01:00
|
|
|
if (!hwdb)
|
|
|
|
return_with_errno(NULL, ENOMEM);
|
2012-10-27 20:59:01 +02:00
|
|
|
|
2018-08-24 06:57:04 +02:00
|
|
|
*hwdb = (struct udev_hwdb) {
|
|
|
|
.n_ref = 1,
|
|
|
|
.hwdb = TAKE_PTR(hwdb_internal),
|
2019-06-12 06:03:19 +02:00
|
|
|
.properties_list = TAKE_PTR(list),
|
2018-08-24 06:57:04 +02:00
|
|
|
};
|
2012-10-27 20:59:01 +02:00
|
|
|
|
|
|
|
return hwdb;
|
|
|
|
}
|
|
|
|
|
2018-08-28 06:07:48 +02:00
|
|
|
static struct udev_hwdb *udev_hwdb_free(struct udev_hwdb *hwdb) {
|
|
|
|
assert(hwdb);
|
|
|
|
|
|
|
|
sd_hwdb_unref(hwdb->hwdb);
|
2019-06-12 06:03:19 +02:00
|
|
|
udev_list_free(hwdb->properties_list);
|
2018-08-28 06:07:48 +02:00
|
|
|
return mfree(hwdb);
|
|
|
|
}
|
|
|
|
|
2012-10-27 20:59:01 +02:00
|
|
|
/**
|
|
|
|
* udev_hwdb_ref:
|
|
|
|
* @hwdb: context
|
|
|
|
*
|
|
|
|
* Take a reference of a hwdb context.
|
|
|
|
*
|
|
|
|
* Returns: the passed enumeration context
|
|
|
|
**/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* udev_hwdb_unref:
|
|
|
|
* @hwdb: context
|
|
|
|
*
|
|
|
|
* Drop a reference of a hwdb context. If the refcount reaches zero,
|
|
|
|
* all resources of the hwdb context will be released.
|
|
|
|
*
|
2013-11-19 01:15:31 +01:00
|
|
|
* Returns: #NULL
|
2012-10-27 20:59:01 +02:00
|
|
|
**/
|
2018-08-28 06:07:48 +02:00
|
|
|
DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(struct udev_hwdb, udev_hwdb, udev_hwdb_free);
|
2012-10-27 20:59:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* udev_hwdb_get_properties_list_entry:
|
|
|
|
* @hwdb: context
|
|
|
|
* @modalias: modalias string
|
|
|
|
* @flags: (unused)
|
|
|
|
*
|
|
|
|
* Lookup a matching device in the hardware database. The lookup key is a
|
|
|
|
* modalias string, whose formats are defined for the Linux kernel modules.
|
|
|
|
* Examples are: pci:v00008086d00001C2D*, usb:v04F2pB221*. The first entry
|
|
|
|
* of a list of retrieved properties is returned.
|
|
|
|
*
|
|
|
|
* Returns: a udev_list_entry.
|
|
|
|
*/
|
2018-10-19 20:00:46 +02:00
|
|
|
_public_ struct udev_list_entry *udev_hwdb_get_properties_list_entry(struct udev_hwdb *hwdb, const char *modalias, unsigned flags) {
|
2014-12-03 17:32:40 +01:00
|
|
|
const char *key, *value;
|
2017-09-09 22:31:09 +02:00
|
|
|
struct udev_list_entry *e;
|
2012-10-27 20:59:01 +02:00
|
|
|
|
2018-08-24 06:57:58 +02:00
|
|
|
assert_return_errno(hwdb, NULL, EINVAL);
|
|
|
|
assert_return_errno(modalias, NULL, EINVAL);
|
2012-10-27 20:59:01 +02:00
|
|
|
|
2019-06-12 06:03:19 +02:00
|
|
|
udev_list_cleanup(hwdb->properties_list);
|
2014-12-03 17:32:40 +01:00
|
|
|
|
2018-11-21 06:06:41 +01:00
|
|
|
SD_HWDB_FOREACH_PROPERTY(hwdb->hwdb, modalias, key, value)
|
2019-06-12 06:03:19 +02:00
|
|
|
if (!udev_list_entry_add(hwdb->properties_list, key, value))
|
2018-11-21 06:06:41 +01:00
|
|
|
return_with_errno(NULL, ENOMEM);
|
2014-12-03 17:32:40 +01:00
|
|
|
|
2019-06-12 06:03:19 +02:00
|
|
|
e = udev_list_get_entry(hwdb->properties_list);
|
2017-09-09 22:31:09 +02:00
|
|
|
if (!e)
|
2018-11-21 06:06:41 +01:00
|
|
|
return_with_errno(NULL, ENODATA);
|
2017-09-09 22:31:09 +02:00
|
|
|
|
|
|
|
return e;
|
2012-10-27 20:59:01 +02:00
|
|
|
}
|