]>
Commit | Line | Data |
---|---|---|
2001208c KS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
8b516fde | 4 | Copyright Tom Gundersen <teg@jklm.no> |
2001208c KS |
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 | ||
8b516fde | 20 | #include "sd-hwdb.h" |
07630cea | 21 | |
b5efdb8a | 22 | #include "alloc-util.h" |
8b516fde | 23 | #include "hwdb-util.h" |
07630cea | 24 | #include "libudev-private.h" |
2001208c KS |
25 | |
26 | /** | |
27 | * SECTION:libudev-hwdb | |
28 | * @short_description: retrieve properties from the hardware database | |
29 | * | |
f2d433e1 | 30 | * Libudev hardware database interface. |
2001208c KS |
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 | ||
8b516fde | 42 | sd_hwdb *hwdb; |
2001208c KS |
43 | |
44 | struct udev_list properties_list; | |
45 | }; | |
46 | ||
2001208c KS |
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) { | |
4afd3348 | 56 | _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb_internal = NULL; |
2001208c | 57 | struct udev_hwdb *hwdb; |
8b516fde | 58 | int r; |
2001208c | 59 | |
309f631d | 60 | assert_return_errno(udev, NULL, EINVAL); |
33488f19 | 61 | |
8b516fde | 62 | r = sd_hwdb_new(&hwdb_internal); |
309f631d LP |
63 | if (r < 0) { |
64 | errno = -r; | |
2001208c | 65 | return NULL; |
309f631d | 66 | } |
2001208c | 67 | |
8b516fde | 68 | hwdb = new0(struct udev_hwdb, 1); |
309f631d LP |
69 | if (!hwdb) { |
70 | errno = ENOMEM; | |
2001208c | 71 | return NULL; |
309f631d | 72 | } |
2001208c | 73 | |
8b516fde TG |
74 | hwdb->refcount = 1; |
75 | hwdb->hwdb = hwdb_internal; | |
76 | hwdb_internal = NULL; | |
2001208c | 77 | |
8b516fde | 78 | udev_list_init(udev, &hwdb->properties_list, true); |
2001208c | 79 | |
2001208c KS |
80 | return hwdb; |
81 | } | |
82 | ||
83 | /** | |
84 | * udev_hwdb_ref: | |
85 | * @hwdb: context | |
86 | * | |
87 | * Take a reference of a hwdb context. | |
88 | * | |
89 | * Returns: the passed enumeration context | |
90 | **/ | |
91 | _public_ struct udev_hwdb *udev_hwdb_ref(struct udev_hwdb *hwdb) { | |
92 | if (!hwdb) | |
93 | return NULL; | |
94 | hwdb->refcount++; | |
95 | return hwdb; | |
96 | } | |
97 | ||
98 | /** | |
99 | * udev_hwdb_unref: | |
100 | * @hwdb: context | |
101 | * | |
102 | * Drop a reference of a hwdb context. If the refcount reaches zero, | |
103 | * all resources of the hwdb context will be released. | |
104 | * | |
725d7e6c | 105 | * Returns: #NULL |
2001208c KS |
106 | **/ |
107 | _public_ struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb) { | |
108 | if (!hwdb) | |
109 | return NULL; | |
110 | hwdb->refcount--; | |
111 | if (hwdb->refcount > 0) | |
725d7e6c | 112 | return NULL; |
8b516fde | 113 | sd_hwdb_unref(hwdb->hwdb); |
2001208c KS |
114 | udev_list_cleanup(&hwdb->properties_list); |
115 | free(hwdb); | |
116 | return NULL; | |
117 | } | |
118 | ||
2001208c KS |
119 | /** |
120 | * udev_hwdb_get_properties_list_entry: | |
121 | * @hwdb: context | |
122 | * @modalias: modalias string | |
123 | * @flags: (unused) | |
124 | * | |
125 | * Lookup a matching device in the hardware database. The lookup key is a | |
126 | * modalias string, whose formats are defined for the Linux kernel modules. | |
127 | * Examples are: pci:v00008086d00001C2D*, usb:v04F2pB221*. The first entry | |
128 | * of a list of retrieved properties is returned. | |
129 | * | |
130 | * Returns: a udev_list_entry. | |
131 | */ | |
132 | _public_ struct udev_list_entry *udev_hwdb_get_properties_list_entry(struct udev_hwdb *hwdb, const char *modalias, unsigned int flags) { | |
8b516fde | 133 | const char *key, *value; |
309f631d | 134 | struct udev_list_entry *e; |
2001208c | 135 | |
8b516fde | 136 | if (!hwdb || !modalias) { |
2001208c KS |
137 | errno = EINVAL; |
138 | return NULL; | |
139 | } | |
140 | ||
9485d98d | 141 | udev_list_cleanup(&hwdb->properties_list); |
8b516fde | 142 | |
0b931571 | 143 | SD_HWDB_FOREACH_PROPERTY(hwdb->hwdb, modalias, key, value) { |
8b516fde TG |
144 | if (udev_list_entry_add(&hwdb->properties_list, key, value) == NULL) { |
145 | errno = ENOMEM; | |
146 | return NULL; | |
147 | } | |
2001208c | 148 | } |
8b516fde | 149 | |
309f631d LP |
150 | e = udev_list_get_entry(&hwdb->properties_list); |
151 | if (!e) | |
152 | errno = ENODATA; | |
153 | ||
154 | return e; | |
2001208c | 155 | } |