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