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