]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev-hwdb.c
Merge pull request #1607 from keszybz/lz4-remove-v1
[thirdparty/systemd.git] / src / libudev / libudev-hwdb.c
1 /***
2 This file is part of systemd.
3
4 Copyright Tom Gundersen <teg@jklm.no>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "sd-hwdb.h"
21
22 #include "alloc-util.h"
23 #include "hwdb-util.h"
24 #include "libudev-private.h"
25
26 /**
27 * SECTION:libudev-hwdb
28 * @short_description: retrieve properties from the hardware database
29 *
30 * Libudev hardware database interface.
31 */
32
33 /**
34 * udev_hwdb:
35 *
36 * Opaque object representing the hardware database.
37 */
38 struct udev_hwdb {
39 struct udev *udev;
40 int refcount;
41
42 sd_hwdb *hwdb;
43
44 struct udev_list properties_list;
45 };
46
47 /**
48 * udev_hwdb_new:
49 * @udev: udev library context
50 *
51 * Create a hardware database context to query properties for devices.
52 *
53 * Returns: a hwdb context.
54 **/
55 _public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
56 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb_internal = NULL;
57 struct udev_hwdb *hwdb;
58 int r;
59
60 assert_return(udev, NULL);
61
62 r = sd_hwdb_new(&hwdb_internal);
63 if (r < 0)
64 return NULL;
65
66 hwdb = new0(struct udev_hwdb, 1);
67 if (!hwdb)
68 return NULL;
69
70 hwdb->refcount = 1;
71 hwdb->hwdb = hwdb_internal;
72 hwdb_internal = NULL;
73
74 udev_list_init(udev, &hwdb->properties_list, true);
75
76 return hwdb;
77 }
78
79 /**
80 * udev_hwdb_ref:
81 * @hwdb: context
82 *
83 * Take a reference of a hwdb context.
84 *
85 * Returns: the passed enumeration context
86 **/
87 _public_ struct udev_hwdb *udev_hwdb_ref(struct udev_hwdb *hwdb) {
88 if (!hwdb)
89 return NULL;
90 hwdb->refcount++;
91 return hwdb;
92 }
93
94 /**
95 * udev_hwdb_unref:
96 * @hwdb: context
97 *
98 * Drop a reference of a hwdb context. If the refcount reaches zero,
99 * all resources of the hwdb context will be released.
100 *
101 * Returns: #NULL
102 **/
103 _public_ struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb) {
104 if (!hwdb)
105 return NULL;
106 hwdb->refcount--;
107 if (hwdb->refcount > 0)
108 return NULL;
109 sd_hwdb_unref(hwdb->hwdb);
110 udev_list_cleanup(&hwdb->properties_list);
111 free(hwdb);
112 return NULL;
113 }
114
115 /**
116 * udev_hwdb_get_properties_list_entry:
117 * @hwdb: context
118 * @modalias: modalias string
119 * @flags: (unused)
120 *
121 * Lookup a matching device in the hardware database. The lookup key is a
122 * modalias string, whose formats are defined for the Linux kernel modules.
123 * Examples are: pci:v00008086d00001C2D*, usb:v04F2pB221*. The first entry
124 * of a list of retrieved properties is returned.
125 *
126 * Returns: a udev_list_entry.
127 */
128 _public_ struct udev_list_entry *udev_hwdb_get_properties_list_entry(struct udev_hwdb *hwdb, const char *modalias, unsigned int flags) {
129 const char *key, *value;
130
131 if (!hwdb || !modalias) {
132 errno = EINVAL;
133 return NULL;
134 }
135
136 udev_list_cleanup(&hwdb->properties_list);
137
138 SD_HWDB_FOREACH_PROPERTY(hwdb->hwdb, modalias, key, value) {
139 if (udev_list_entry_add(&hwdb->properties_list, key, value) == NULL) {
140 errno = ENOMEM;
141 return NULL;
142 }
143 }
144
145 return udev_list_get_entry(&hwdb->properties_list);
146 }