]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-hwdb.c
225e0265a4a3d83933196cae624624a9d892f7ce
[thirdparty/systemd.git] / src / udev / udev-builtin-hwdb.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <fnmatch.h>
5 #include <getopt.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #include "sd-hwdb.h"
10
11 #include "alloc-util.h"
12 #include "device-util.h"
13 #include "hwdb-util.h"
14 #include "parse-util.h"
15 #include "string-util.h"
16 #include "udev-builtin.h"
17
18 static sd_hwdb *hwdb;
19
20 int udev_builtin_hwdb_lookup(sd_device *dev,
21 const char *prefix, const char *modalias,
22 const char *filter, bool test) {
23 _cleanup_free_ char *lookup = NULL;
24 const char *key, *value;
25 int n = 0, r;
26
27 if (!hwdb)
28 return -ENOENT;
29
30 if (prefix) {
31 lookup = strjoin(prefix, modalias);
32 if (!lookup)
33 return -ENOMEM;
34 modalias = lookup;
35 }
36
37 SD_HWDB_FOREACH_PROPERTY(hwdb, modalias, key, value) {
38 if (filter && fnmatch(filter, key, FNM_NOESCAPE) != 0)
39 continue;
40
41 r = udev_builtin_add_property(dev, test, key, value);
42 if (r < 0)
43 return r;
44 n++;
45 }
46 return n;
47 }
48
49 static const char *modalias_usb(sd_device *dev, char *s, size_t size) {
50 const char *v, *p;
51 uint16_t vn, pn;
52
53 if (sd_device_get_sysattr_value(dev, "idVendor", &v) < 0)
54 return NULL;
55 if (sd_device_get_sysattr_value(dev, "idProduct", &p) < 0)
56 return NULL;
57 if (safe_atoux16(v, &vn) < 0)
58 return NULL;
59 if (safe_atoux16(p, &pn) < 0)
60 return NULL;
61 snprintf(s, size, "usb:v%04Xp%04X*", vn, pn);
62 return s;
63 }
64
65 static int udev_builtin_hwdb_search(sd_device *dev, sd_device *srcdev,
66 const char *subsystem, const char *prefix,
67 const char *filter, bool test) {
68 sd_device *d;
69 char s[16];
70 bool last = false;
71 int r = 0;
72
73 assert(dev);
74
75 if (!srcdev)
76 srcdev = dev;
77
78 for (d = srcdev; d; ) {
79 const char *dsubsys, *devtype, *modalias = NULL;
80
81 if (sd_device_get_subsystem(d, &dsubsys) < 0)
82 goto next;
83
84 /* look only at devices of a specific subsystem */
85 if (subsystem && !streq(dsubsys, subsystem))
86 goto next;
87
88 (void) sd_device_get_property_value(d, "MODALIAS", &modalias);
89
90 if (streq(dsubsys, "usb") &&
91 sd_device_get_devtype(d, &devtype) >= 0 &&
92 streq(devtype, "usb_device")) {
93 /* if the usb_device does not have a modalias, compose one */
94 if (!modalias)
95 modalias = modalias_usb(d, s, sizeof(s));
96
97 /* avoid looking at any parent device, they are usually just a USB hub */
98 last = true;
99 }
100
101 if (!modalias)
102 goto next;
103
104 r = udev_builtin_hwdb_lookup(dev, prefix, modalias, filter, test);
105 if (r > 0)
106 break;
107
108 if (last)
109 break;
110 next:
111 if (sd_device_get_parent(d, &d) < 0)
112 break;
113 }
114
115 return r;
116 }
117
118 static int builtin_hwdb(sd_device *dev, int argc, char *argv[], bool test) {
119 static const struct option options[] = {
120 { "filter", required_argument, NULL, 'f' },
121 { "device", required_argument, NULL, 'd' },
122 { "subsystem", required_argument, NULL, 's' },
123 { "lookup-prefix", required_argument, NULL, 'p' },
124 {}
125 };
126 const char *filter = NULL;
127 const char *device = NULL;
128 const char *subsystem = NULL;
129 const char *prefix = NULL;
130 _cleanup_(sd_device_unrefp) sd_device *srcdev = NULL;
131 int r;
132
133 if (!hwdb)
134 return -EINVAL;
135
136 for (;;) {
137 int option;
138
139 option = getopt_long(argc, argv, "f:d:s:p:", options, NULL);
140 if (option == -1)
141 break;
142
143 switch (option) {
144 case 'f':
145 filter = optarg;
146 break;
147
148 case 'd':
149 device = optarg;
150 break;
151
152 case 's':
153 subsystem = optarg;
154 break;
155
156 case 'p':
157 prefix = optarg;
158 break;
159 }
160 }
161
162 /* query a specific key given as argument */
163 if (argv[optind]) {
164 r = udev_builtin_hwdb_lookup(dev, prefix, argv[optind], filter, test);
165 if (r < 0)
166 return log_device_debug_errno(dev, r, "Failed to lookup hwdb: %m");
167 if (r == 0)
168 return log_device_debug_errno(dev, SYNTHETIC_ERRNO(ENODATA), "No entry found from hwdb.");
169 return r;
170 }
171
172 /* read data from another device than the device we will store the data */
173 if (device) {
174 r = sd_device_new_from_device_id(&srcdev, device);
175 if (r < 0)
176 return log_device_debug_errno(dev, r, "Failed to create sd_device object '%s': %m", device);
177 }
178
179 r = udev_builtin_hwdb_search(dev, srcdev, subsystem, prefix, filter, test);
180 if (r < 0)
181 return log_device_debug_errno(dev, r, "Failed to lookup hwdb: %m");
182 if (r == 0)
183 return log_device_debug_errno(dev, SYNTHETIC_ERRNO(ENODATA), "No entry found from hwdb.");
184 return r;
185 }
186
187 /* called at udev startup and reload */
188 static int builtin_hwdb_init(void) {
189 int r;
190
191 if (hwdb)
192 return 0;
193
194 r = sd_hwdb_new(&hwdb);
195 if (r < 0)
196 return r;
197
198 return 0;
199 }
200
201 /* called on udev shutdown and reload request */
202 static void builtin_hwdb_exit(void) {
203 hwdb = sd_hwdb_unref(hwdb);
204 }
205
206 /* called every couple of seconds during event activity; 'true' if config has changed */
207 static bool builtin_hwdb_validate(void) {
208 return hwdb_validate(hwdb);
209 }
210
211 const struct udev_builtin udev_builtin_hwdb = {
212 .name = "hwdb",
213 .cmd = builtin_hwdb,
214 .init = builtin_hwdb_init,
215 .exit = builtin_hwdb_exit,
216 .validate = builtin_hwdb_validate,
217 .help = "Hardware database",
218 };