]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libudev/libudev-hwdb.c
libudev-hwdb: use structured initializer
[thirdparty/systemd.git] / src / libudev / libudev-hwdb.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
2001208c 2
8b516fde 3#include "sd-hwdb.h"
07630cea 4
b5efdb8a 5#include "alloc-util.h"
8b516fde 6#include "hwdb-util.h"
07630cea 7#include "libudev-private.h"
2001208c
KS
8
9/**
10 * SECTION:libudev-hwdb
11 * @short_description: retrieve properties from the hardware database
12 *
f2d433e1 13 * Libudev hardware database interface.
2001208c
KS
14 */
15
16/**
17 * udev_hwdb:
18 *
19 * Opaque object representing the hardware database.
20 */
21struct udev_hwdb {
3c6ac219 22 unsigned n_ref;
8b516fde 23 sd_hwdb *hwdb;
2001208c
KS
24 struct udev_list properties_list;
25};
26
2001208c
KS
27/**
28 * udev_hwdb_new:
b485aa58 29 * @udev: udev library context (unused)
2001208c
KS
30 *
31 * Create a hardware database context to query properties for devices.
32 *
33 * Returns: a hwdb context.
34 **/
35_public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
4afd3348 36 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb_internal = NULL;
2001208c 37 struct udev_hwdb *hwdb;
8b516fde 38 int r;
2001208c 39
8b516fde 40 r = sd_hwdb_new(&hwdb_internal);
309f631d
LP
41 if (r < 0) {
42 errno = -r;
2001208c 43 return NULL;
309f631d 44 }
2001208c 45
68b80b85 46 hwdb = new(struct udev_hwdb, 1);
309f631d
LP
47 if (!hwdb) {
48 errno = ENOMEM;
2001208c 49 return NULL;
309f631d 50 }
2001208c 51
68b80b85
YW
52 *hwdb = (struct udev_hwdb) {
53 .n_ref = 1,
54 .hwdb = TAKE_PTR(hwdb_internal),
55 };
2001208c 56
8b516fde 57 udev_list_init(udev, &hwdb->properties_list, true);
2001208c 58
2001208c
KS
59 return hwdb;
60}
61
3c6ac219
YW
62static struct udev_hwdb *udev_hwdb_free(struct udev_hwdb *hwdb) {
63 assert(hwdb);
64
65 sd_hwdb_unref(hwdb->hwdb);
66 udev_list_cleanup(&hwdb->properties_list);
67 return mfree(hwdb);
68}
69
2001208c
KS
70/**
71 * udev_hwdb_ref:
72 * @hwdb: context
73 *
74 * Take a reference of a hwdb context.
75 *
76 * Returns: the passed enumeration context
77 **/
2001208c
KS
78
79/**
80 * udev_hwdb_unref:
81 * @hwdb: context
82 *
83 * Drop a reference of a hwdb context. If the refcount reaches zero,
84 * all resources of the hwdb context will be released.
85 *
725d7e6c 86 * Returns: #NULL
2001208c 87 **/
3c6ac219 88DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(struct udev_hwdb, udev_hwdb, udev_hwdb_free);
2001208c 89
2001208c
KS
90/**
91 * udev_hwdb_get_properties_list_entry:
92 * @hwdb: context
93 * @modalias: modalias string
94 * @flags: (unused)
95 *
96 * Lookup a matching device in the hardware database. The lookup key is a
97 * modalias string, whose formats are defined for the Linux kernel modules.
98 * Examples are: pci:v00008086d00001C2D*, usb:v04F2pB221*. The first entry
99 * of a list of retrieved properties is returned.
100 *
101 * Returns: a udev_list_entry.
102 */
103_public_ struct udev_list_entry *udev_hwdb_get_properties_list_entry(struct udev_hwdb *hwdb, const char *modalias, unsigned int flags) {
8b516fde 104 const char *key, *value;
309f631d 105 struct udev_list_entry *e;
2001208c 106
8b516fde 107 if (!hwdb || !modalias) {
2001208c
KS
108 errno = EINVAL;
109 return NULL;
110 }
111
9485d98d 112 udev_list_cleanup(&hwdb->properties_list);
8b516fde 113
0b931571 114 SD_HWDB_FOREACH_PROPERTY(hwdb->hwdb, modalias, key, value) {
8b516fde
TG
115 if (udev_list_entry_add(&hwdb->properties_list, key, value) == NULL) {
116 errno = ENOMEM;
117 return NULL;
118 }
2001208c 119 }
8b516fde 120
309f631d
LP
121 e = udev_list_get_entry(&hwdb->properties_list);
122 if (!e)
123 errno = ENODATA;
124
125 return e;
2001208c 126}