0b0582a3aa10227767e359e693c4b43fec272388nd/***
bfb214f8ec1b83a124e7a8caa495e3e8f19e9c75nd This file is part of systemd.
bfb214f8ec1b83a124e7a8caa495e3e8f19e9c75nd
0b0582a3aa10227767e359e693c4b43fec272388nd Copyright Tom Gundersen <teg@jklm.no>
bfb214f8ec1b83a124e7a8caa495e3e8f19e9c75nd
bfb214f8ec1b83a124e7a8caa495e3e8f19e9c75nd systemd is free software; you can redistribute it and/or modify it
bfb214f8ec1b83a124e7a8caa495e3e8f19e9c75nd under the terms of the GNU Lesser General Public License as published by
bfb214f8ec1b83a124e7a8caa495e3e8f19e9c75nd the Free Software Foundation; either version 2.1 of the License, or
bfb214f8ec1b83a124e7a8caa495e3e8f19e9c75nd (at your option) any later version.
bfb214f8ec1b83a124e7a8caa495e3e8f19e9c75nd
bfb214f8ec1b83a124e7a8caa495e3e8f19e9c75nd systemd is distributed in the hope that it will be useful, but
bfb214f8ec1b83a124e7a8caa495e3e8f19e9c75nd WITHOUT ANY WARRANTY; without even the implied warranty of
bfb214f8ec1b83a124e7a8caa495e3e8f19e9c75nd MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
bfb214f8ec1b83a124e7a8caa495e3e8f19e9c75nd Lesser General Public License for more details.
bfb214f8ec1b83a124e7a8caa495e3e8f19e9c75nd
bfb214f8ec1b83a124e7a8caa495e3e8f19e9c75nd You should have received a copy of the GNU Lesser General Public License
bfb214f8ec1b83a124e7a8caa495e3e8f19e9c75nd along with systemd; If not, see <http://www.gnu.org/licenses/>.
bfb214f8ec1b83a124e7a8caa495e3e8f19e9c75nd***/
0b0582a3aa10227767e359e693c4b43fec272388nd
60ca0709311658bc8604806c62c6e33534921b3bnd#include "sd-hwdb.h"
0b0582a3aa10227767e359e693c4b43fec272388nd
60ca0709311658bc8604806c62c6e33534921b3bnd#include "alloc-util.h"
60ca0709311658bc8604806c62c6e33534921b3bnd#include "hwdb-util.h"
0b0582a3aa10227767e359e693c4b43fec272388nd#include "libudev-private.h"
0b0582a3aa10227767e359e693c4b43fec272388nd
45de0209f6fdd98b9961f9641f85d572e14e3edcnd/**
60ca0709311658bc8604806c62c6e33534921b3bnd * SECTION:libudev-hwdb
60ca0709311658bc8604806c62c6e33534921b3bnd * @short_description: retrieve properties from the hardware database
0b0582a3aa10227767e359e693c4b43fec272388nd *
0b0582a3aa10227767e359e693c4b43fec272388nd * Libudev hardware database interface.
0b0582a3aa10227767e359e693c4b43fec272388nd */
60ca0709311658bc8604806c62c6e33534921b3bnd
60ca0709311658bc8604806c62c6e33534921b3bnd/**
60ca0709311658bc8604806c62c6e33534921b3bnd * udev_hwdb:
60ca0709311658bc8604806c62c6e33534921b3bnd *
578d284908c082afe20159da2b8967b504a33e9dnd * Opaque object representing the hardware database.
*/
struct udev_hwdb {
struct udev *udev;
int refcount;
sd_hwdb *hwdb;
struct udev_list properties_list;
};
/**
* udev_hwdb_new:
* @udev: udev library context
*
* Create a hardware database context to query properties for devices.
*
* Returns: a hwdb context.
**/
_public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb_internal = NULL;
struct udev_hwdb *hwdb;
int r;
assert_return(udev, NULL);
r = sd_hwdb_new(&hwdb_internal);
if (r < 0)
return NULL;
hwdb = new0(struct udev_hwdb, 1);
if (!hwdb)
return NULL;
hwdb->refcount = 1;
hwdb->hwdb = hwdb_internal;
hwdb_internal = NULL;
udev_list_init(udev, &hwdb->properties_list, true);
return hwdb;
}
/**
* udev_hwdb_ref:
* @hwdb: context
*
* Take a reference of a hwdb context.
*
* Returns: the passed enumeration context
**/
_public_ struct udev_hwdb *udev_hwdb_ref(struct udev_hwdb *hwdb) {
if (!hwdb)
return NULL;
hwdb->refcount++;
return hwdb;
}
/**
* 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.
*
* Returns: #NULL
**/
_public_ struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb) {
if (!hwdb)
return NULL;
hwdb->refcount--;
if (hwdb->refcount > 0)
return NULL;
sd_hwdb_unref(hwdb->hwdb);
udev_list_cleanup(&hwdb->properties_list);
free(hwdb);
return NULL;
}
/**
* 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.
*/
_public_ struct udev_list_entry *udev_hwdb_get_properties_list_entry(struct udev_hwdb *hwdb, const char *modalias, unsigned int flags) {
const char *key, *value;
if (!hwdb || !modalias) {
errno = EINVAL;
return NULL;
}
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) == NULL) {
errno = ENOMEM;
return NULL;
}
}
return udev_list_get_entry(&hwdb->properties_list);
}