]>
Commit | Line | Data |
---|---|---|
796b06c2 KS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
c3cfed0d | 4 | Copyright 2012 Kay Sievers <kay@vrfy.org> |
796b06c2 KS |
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 | ***/ | |
ccba91c7 LP |
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> | |
67410e9f | 26 | #include <fnmatch.h> |
796b06c2 | 27 | #include <getopt.h> |
ccba91c7 | 28 | |
3cf5266b | 29 | #include "udev.h" |
c532d8a0 | 30 | #include "sd-hwdb.h" |
ccba91c7 | 31 | |
c532d8a0 TG |
32 | #include "hwdb-util.h" |
33 | ||
34 | static sd_hwdb *hwdb; | |
ccba91c7 | 35 | |
67410e9f | 36 | int udev_builtin_hwdb_lookup(struct udev_device *dev, |
a4bbef09 KS |
37 | const char *prefix, const char *modalias, |
38 | const char *filter, bool test) { | |
c532d8a0 TG |
39 | _cleanup_free_ const char *lookup = NULL; |
40 | const char *key, *value; | |
59803c38 KS |
41 | int n = 0; |
42 | ||
33c770b1 KS |
43 | if (!hwdb) |
44 | return -ENOENT; | |
45 | ||
a4bbef09 | 46 | if (prefix) { |
a4bbef09 KS |
47 | lookup = strjoin(prefix, modalias, NULL); |
48 | if (!lookup) | |
49 | return -ENOMEM; | |
c532d8a0 TG |
50 | modalias = lookup; |
51 | } | |
a4bbef09 | 52 | |
c532d8a0 TG |
53 | SD_HWDB_FOREACH_PROPERTY(hwdb, modalias, key, value) { |
54 | if (filter && fnmatch(filter, key, FNM_NOESCAPE) != 0) | |
67410e9f KS |
55 | continue; |
56 | ||
c532d8a0 | 57 | if (udev_builtin_add_property(dev, test, key, value) < 0) |
59803c38 KS |
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 | ||
67410e9f | 84 | static int udev_builtin_hwdb_search(struct udev_device *dev, struct udev_device *srcdev, |
a4bbef09 KS |
85 | const char *subsystem, const char *prefix, |
86 | const char *filter, bool test) { | |
59803c38 KS |
87 | struct udev_device *d; |
88 | char s[16]; | |
77cf759e KS |
89 | bool last = false; |
90 | int r = 0; | |
59803c38 | 91 | |
77cf759e | 92 | for (d = srcdev; d && !last; d = udev_device_get_parent(d)) { |
59803c38 KS |
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 | ||
0238cf7c KS |
104 | modalias = udev_device_get_property_value(d, "MODALIAS"); |
105 | ||
77cf759e KS |
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 | } | |
59803c38 | 114 | |
59803c38 KS |
115 | if (!modalias) |
116 | continue; | |
67410e9f | 117 | |
77cf759e KS |
118 | r = udev_builtin_hwdb_lookup(dev, prefix, modalias, filter, test); |
119 | if (r > 0) | |
59803c38 KS |
120 | break; |
121 | } | |
122 | ||
77cf759e | 123 | return r; |
59803c38 KS |
124 | } |
125 | ||
2001208c KS |
126 | static int builtin_hwdb(struct udev_device *dev, int argc, char *argv[], bool test) { |
127 | static const struct option options[] = { | |
67410e9f KS |
128 | { "filter", required_argument, NULL, 'f' }, |
129 | { "device", required_argument, NULL, 'd' }, | |
2001208c | 130 | { "subsystem", required_argument, NULL, 's' }, |
a4bbef09 | 131 | { "lookup-prefix", required_argument, NULL, 'p' }, |
2001208c | 132 | {} |
796b06c2 | 133 | }; |
67410e9f KS |
134 | const char *filter = NULL; |
135 | const char *device = NULL; | |
59803c38 | 136 | const char *subsystem = NULL; |
a4bbef09 | 137 | const char *prefix = NULL; |
67410e9f | 138 | struct udev_device *srcdev; |
ccba91c7 | 139 | |
2001208c KS |
140 | if (!hwdb) |
141 | return EXIT_FAILURE; | |
ccba91c7 | 142 | |
2001208c KS |
143 | for (;;) { |
144 | int option; | |
ccba91c7 | 145 | |
a4bbef09 | 146 | option = getopt_long(argc, argv, "f:d:s:p:", options, NULL); |
2001208c KS |
147 | if (option == -1) |
148 | break; | |
ccba91c7 | 149 | |
2001208c | 150 | switch (option) { |
67410e9f KS |
151 | case 'f': |
152 | filter = optarg; | |
153 | break; | |
154 | ||
155 | case 'd': | |
156 | device = optarg; | |
157 | break; | |
158 | ||
2001208c | 159 | case 's': |
59803c38 | 160 | subsystem = optarg; |
2001208c | 161 | break; |
a4bbef09 KS |
162 | |
163 | case 'p': | |
164 | prefix = optarg; | |
165 | break; | |
912541b0 KS |
166 | } |
167 | } | |
ccba91c7 | 168 | |
6824690f KS |
169 | /* query a specific key given as argument */ |
170 | if (argv[optind]) { | |
a4bbef09 | 171 | if (udev_builtin_hwdb_lookup(dev, prefix, argv[optind], filter, test) > 0) |
6824690f KS |
172 | return EXIT_SUCCESS; |
173 | return EXIT_FAILURE; | |
174 | } | |
175 | ||
67410e9f KS |
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 | ||
beef8df8 KS |
184 | if (udev_builtin_hwdb_search(dev, srcdev, subsystem, prefix, filter, test) > 0) |
185 | return EXIT_SUCCESS; | |
186 | return EXIT_FAILURE; | |
796b06c2 | 187 | } |
ccba91c7 | 188 | |
796b06c2 | 189 | /* called at udev startup and reload */ |
9ec6e95b | 190 | static int builtin_hwdb_init(struct udev *udev) { |
c532d8a0 TG |
191 | int r; |
192 | ||
2001208c | 193 | if (hwdb) |
5b4d50ef | 194 | return 0; |
c532d8a0 TG |
195 | |
196 | r = sd_hwdb_new(&hwdb); | |
197 | if (r < 0) | |
198 | return r; | |
199 | ||
912541b0 | 200 | return 0; |
3cf5266b | 201 | } |
ccba91c7 | 202 | |
796b06c2 | 203 | /* called on udev shutdown and reload request */ |
9ec6e95b | 204 | static void builtin_hwdb_exit(struct udev *udev) { |
c532d8a0 | 205 | hwdb = sd_hwdb_unref(hwdb); |
ccba91c7 | 206 | } |
3cf5266b | 207 | |
796b06c2 | 208 | /* called every couple of seconds during event activity; 'true' if config has changed */ |
9ec6e95b | 209 | static bool builtin_hwdb_validate(struct udev *udev) { |
c532d8a0 | 210 | return hwdb_validate(hwdb); |
796b06c2 | 211 | } |
3cf5266b | 212 | |
796b06c2 KS |
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, | |
5ac0162c | 219 | .help = "Hardware database", |
3cf5266b | 220 | }; |