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