]>
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 | |
8b516fde | 22 | #include "hwdb-util.h" |
07630cea | 23 | #include "libudev-private.h" |
2001208c KS |
24 | |
25 | /** | |
26 | * SECTION:libudev-hwdb | |
27 | * @short_description: retrieve properties from the hardware database | |
28 | * | |
f2d433e1 | 29 | * Libudev hardware database interface. |
2001208c KS |
30 | */ |
31 | ||
32 | /** | |
33 | * udev_hwdb: | |
34 | * | |
35 | * Opaque object representing the hardware database. | |
36 | */ | |
37 | struct udev_hwdb { | |
38 | struct udev *udev; | |
39 | int refcount; | |
40 | ||
8b516fde | 41 | sd_hwdb *hwdb; |
2001208c KS |
42 | |
43 | struct udev_list properties_list; | |
44 | }; | |
45 | ||
2001208c KS |
46 | /** |
47 | * udev_hwdb_new: | |
48 | * @udev: udev library context | |
49 | * | |
50 | * Create a hardware database context to query properties for devices. | |
51 | * | |
52 | * Returns: a hwdb context. | |
53 | **/ | |
54 | _public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) { | |
8b516fde | 55 | _cleanup_hwdb_unref_ sd_hwdb *hwdb_internal = NULL; |
2001208c | 56 | struct udev_hwdb *hwdb; |
8b516fde | 57 | int r; |
2001208c | 58 | |
8b516fde | 59 | assert_return(udev, NULL); |
33488f19 | 60 | |
8b516fde TG |
61 | r = sd_hwdb_new(&hwdb_internal); |
62 | if (r < 0) | |
2001208c | 63 | return NULL; |
2001208c | 64 | |
8b516fde TG |
65 | hwdb = new0(struct udev_hwdb, 1); |
66 | if (!hwdb) | |
2001208c | 67 | return NULL; |
2001208c | 68 | |
8b516fde TG |
69 | hwdb->refcount = 1; |
70 | hwdb->hwdb = hwdb_internal; | |
71 | hwdb_internal = NULL; | |
2001208c | 72 | |
8b516fde | 73 | udev_list_init(udev, &hwdb->properties_list, true); |
2001208c | 74 | |
2001208c KS |
75 | return hwdb; |
76 | } | |
77 | ||
78 | /** | |
79 | * udev_hwdb_ref: | |
80 | * @hwdb: context | |
81 | * | |
82 | * Take a reference of a hwdb context. | |
83 | * | |
84 | * Returns: the passed enumeration context | |
85 | **/ | |
86 | _public_ struct udev_hwdb *udev_hwdb_ref(struct udev_hwdb *hwdb) { | |
87 | if (!hwdb) | |
88 | return NULL; | |
89 | hwdb->refcount++; | |
90 | return hwdb; | |
91 | } | |
92 | ||
93 | /** | |
94 | * udev_hwdb_unref: | |
95 | * @hwdb: context | |
96 | * | |
97 | * Drop a reference of a hwdb context. If the refcount reaches zero, | |
98 | * all resources of the hwdb context will be released. | |
99 | * | |
725d7e6c | 100 | * Returns: #NULL |
2001208c KS |
101 | **/ |
102 | _public_ struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb) { | |
103 | if (!hwdb) | |
104 | return NULL; | |
105 | hwdb->refcount--; | |
106 | if (hwdb->refcount > 0) | |
725d7e6c | 107 | return NULL; |
8b516fde | 108 | sd_hwdb_unref(hwdb->hwdb); |
2001208c KS |
109 | udev_list_cleanup(&hwdb->properties_list); |
110 | free(hwdb); | |
111 | return NULL; | |
112 | } | |
113 | ||
2001208c KS |
114 | /** |
115 | * udev_hwdb_get_properties_list_entry: | |
116 | * @hwdb: context | |
117 | * @modalias: modalias string | |
118 | * @flags: (unused) | |
119 | * | |
120 | * Lookup a matching device in the hardware database. The lookup key is a | |
121 | * modalias string, whose formats are defined for the Linux kernel modules. | |
122 | * Examples are: pci:v00008086d00001C2D*, usb:v04F2pB221*. The first entry | |
123 | * of a list of retrieved properties is returned. | |
124 | * | |
125 | * Returns: a udev_list_entry. | |
126 | */ | |
127 | _public_ struct udev_list_entry *udev_hwdb_get_properties_list_entry(struct udev_hwdb *hwdb, const char *modalias, unsigned int flags) { | |
8b516fde | 128 | const char *key, *value; |
2001208c | 129 | |
8b516fde | 130 | if (!hwdb || !modalias) { |
2001208c KS |
131 | errno = EINVAL; |
132 | return NULL; | |
133 | } | |
134 | ||
9485d98d | 135 | udev_list_cleanup(&hwdb->properties_list); |
8b516fde | 136 | |
0b931571 | 137 | SD_HWDB_FOREACH_PROPERTY(hwdb->hwdb, modalias, key, value) { |
8b516fde TG |
138 | if (udev_list_entry_add(&hwdb->properties_list, key, value) == NULL) { |
139 | errno = ENOMEM; | |
140 | return NULL; | |
141 | } | |
2001208c | 142 | } |
8b516fde | 143 | |
2001208c KS |
144 | return udev_list_get_entry(&hwdb->properties_list); |
145 | } |