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