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