Debian Codegraph @

src/libudev/libudev-hwdb.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-hwdb.h"
4
5 #include "alloc-util.h"
6 #include "errno-util.h"
7 #include "libudev-list-internal.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_(udev_list_freep) struct udev_list *list = NULL;
37 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb_internal = NULL;
38 struct udev_hwdb *hwdb;
39 int r;
40
41 r = sd_hwdb_new(&hwdb_internal);
42 if (r < 0)
43 return_with_errno(NULL, r);
44
45 list = udev_list_new(true);
46 if (!list)
47 return_with_errno(NULL, ENOMEM);
48
49 hwdb = new(struct udev_hwdb, 1);
50 if (!hwdb)
51 return_with_errno(NULL, ENOMEM);
52
53 *hwdb = (struct udev_hwdb) {
54 .n_ref = 1,
55 .hwdb = TAKE_PTR(hwdb_internal),
56 .properties_list = TAKE_PTR(list),
57 };
58
59 return hwdb;
60 }
61
62 static struct udev_hwdb* udev_hwdb_free(struct udev_hwdb *hwdb) {
63 assert(hwdb);
64
65 sd_hwdb_unref(hwdb->hwdb);
66 udev_list_free(hwdb->properties_list);
67 return mfree(hwdb);
68 }
69
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 **/
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 *
86 * Returns: #NULL
87 **/
88 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(struct udev_hwdb, udev_hwdb, udev_hwdb_free);
89
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 flags) {
104 const char *key, *value;
105 struct udev_list_entry *e;
106
107 assert_return_errno(hwdb, NULL, EINVAL);
108 assert_return_errno(modalias, NULL, EINVAL);
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))
114 return_with_errno(NULL, ENOMEM);
115
116 e = udev_list_get_entry(hwdb->properties_list);
117 if (!e)
118 return_with_errno(NULL, ENODATA);
119
120 return e;
121 }