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