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