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