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