]>
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" |
ccba91c7 | 30 | |
2001208c | 31 | static struct udev_hwdb *hwdb; |
ccba91c7 | 32 | |
67410e9f KS |
33 | int udev_builtin_hwdb_lookup(struct udev_device *dev, |
34 | const char *modalias, const char *filter, bool test) { | |
59803c38 KS |
35 | struct udev_list_entry *entry; |
36 | int n = 0; | |
37 | ||
33c770b1 KS |
38 | if (!hwdb) |
39 | return -ENOENT; | |
40 | ||
59803c38 | 41 | udev_list_entry_foreach(entry, udev_hwdb_get_properties_list_entry(hwdb, modalias, 0)) { |
67410e9f KS |
42 | if (filter && fnmatch(filter, udev_list_entry_get_name(entry), FNM_NOESCAPE) != 0) |
43 | continue; | |
44 | ||
59803c38 KS |
45 | if (udev_builtin_add_property(dev, test, |
46 | udev_list_entry_get_name(entry), | |
47 | udev_list_entry_get_value(entry)) < 0) | |
48 | return -ENOMEM; | |
49 | n++; | |
50 | } | |
51 | return n; | |
52 | } | |
53 | ||
54 | static const char *modalias_usb(struct udev_device *dev, char *s, size_t size) { | |
55 | const char *v, *p; | |
56 | int vn, pn; | |
57 | ||
58 | v = udev_device_get_sysattr_value(dev, "idVendor"); | |
59 | if (!v) | |
60 | return NULL; | |
61 | p = udev_device_get_sysattr_value(dev, "idProduct"); | |
62 | if (!p) | |
63 | return NULL; | |
64 | vn = strtol(v, NULL, 16); | |
65 | if (vn <= 0) | |
66 | return NULL; | |
67 | pn = strtol(p, NULL, 16); | |
68 | if (pn <= 0) | |
69 | return NULL; | |
70 | snprintf(s, size, "usb:v%04Xp%04X*", vn, pn); | |
71 | return s; | |
72 | } | |
73 | ||
67410e9f KS |
74 | static int udev_builtin_hwdb_search(struct udev_device *dev, struct udev_device *srcdev, |
75 | const char *subsystem, const char *filter, bool test) { | |
59803c38 KS |
76 | struct udev_device *d; |
77 | char s[16]; | |
78 | int n = 0; | |
79 | ||
67410e9f | 80 | for (d = srcdev; d; d = udev_device_get_parent(d)) { |
59803c38 KS |
81 | const char *dsubsys; |
82 | const char *modalias = NULL; | |
83 | ||
84 | dsubsys = udev_device_get_subsystem(d); | |
85 | if (!dsubsys) | |
86 | continue; | |
87 | ||
88 | /* look only at devices of a specific subsystem */ | |
89 | if (subsystem && !streq(dsubsys, subsystem)) | |
90 | continue; | |
91 | ||
92 | /* the usb_device does not have a modalias, compose one */ | |
93 | if (streq(dsubsys, "usb")) | |
67410e9f | 94 | modalias = modalias_usb(d, s, sizeof(s)); |
59803c38 KS |
95 | |
96 | if (!modalias) | |
97 | modalias = udev_device_get_property_value(d, "MODALIAS"); | |
98 | ||
99 | if (!modalias) | |
100 | continue; | |
67410e9f KS |
101 | |
102 | n = udev_builtin_hwdb_lookup(dev, modalias, filter, test); | |
59803c38 KS |
103 | if (n > 0) |
104 | break; | |
105 | } | |
106 | ||
107 | return n; | |
108 | } | |
109 | ||
2001208c KS |
110 | static int builtin_hwdb(struct udev_device *dev, int argc, char *argv[], bool test) { |
111 | static const struct option options[] = { | |
67410e9f KS |
112 | { "filter", required_argument, NULL, 'f' }, |
113 | { "device", required_argument, NULL, 'd' }, | |
2001208c KS |
114 | { "subsystem", required_argument, NULL, 's' }, |
115 | {} | |
796b06c2 | 116 | }; |
67410e9f KS |
117 | const char *filter = NULL; |
118 | const char *device = NULL; | |
59803c38 | 119 | const char *subsystem = NULL; |
67410e9f | 120 | struct udev_device *srcdev; |
ccba91c7 | 121 | |
2001208c KS |
122 | if (!hwdb) |
123 | return EXIT_FAILURE; | |
ccba91c7 | 124 | |
2001208c KS |
125 | for (;;) { |
126 | int option; | |
ccba91c7 | 127 | |
2001208c KS |
128 | option = getopt_long(argc, argv, "s", options, NULL); |
129 | if (option == -1) | |
130 | break; | |
ccba91c7 | 131 | |
2001208c | 132 | switch (option) { |
67410e9f KS |
133 | case 'f': |
134 | filter = optarg; | |
135 | break; | |
136 | ||
137 | case 'd': | |
138 | device = optarg; | |
139 | break; | |
140 | ||
2001208c | 141 | case 's': |
59803c38 | 142 | subsystem = optarg; |
2001208c | 143 | break; |
912541b0 KS |
144 | } |
145 | } | |
ccba91c7 | 146 | |
67410e9f KS |
147 | /* read data from another device than the device we will store the data */ |
148 | if (device) { | |
149 | srcdev = udev_device_new_from_device_id(udev_device_get_udev(dev), device); | |
150 | if (!srcdev) | |
151 | return EXIT_FAILURE; | |
152 | } else | |
153 | srcdev = dev; | |
154 | ||
155 | if (udev_builtin_hwdb_search(dev, srcdev, subsystem, filter, test) < 0) | |
796b06c2 | 156 | return EXIT_FAILURE; |
796b06c2 KS |
157 | return EXIT_SUCCESS; |
158 | } | |
ccba91c7 | 159 | |
796b06c2 KS |
160 | /* called at udev startup and reload */ |
161 | static int builtin_hwdb_init(struct udev *udev) | |
162 | { | |
2001208c | 163 | if (hwdb) |
5b4d50ef | 164 | return 0; |
2001208c KS |
165 | hwdb = udev_hwdb_new(udev); |
166 | if (!hwdb) | |
167 | return -ENOMEM; | |
912541b0 | 168 | return 0; |
3cf5266b | 169 | } |
ccba91c7 | 170 | |
796b06c2 KS |
171 | /* called on udev shutdown and reload request */ |
172 | static void builtin_hwdb_exit(struct udev *udev) | |
3cf5266b | 173 | { |
2001208c | 174 | hwdb = udev_hwdb_unref(hwdb); |
ccba91c7 | 175 | } |
3cf5266b | 176 | |
796b06c2 KS |
177 | /* called every couple of seconds during event activity; 'true' if config has changed */ |
178 | static bool builtin_hwdb_validate(struct udev *udev) | |
3cf5266b | 179 | { |
2001208c | 180 | return udev_hwdb_validate(hwdb); |
796b06c2 | 181 | } |
3cf5266b | 182 | |
796b06c2 KS |
183 | const struct udev_builtin udev_builtin_hwdb = { |
184 | .name = "hwdb", | |
185 | .cmd = builtin_hwdb, | |
186 | .init = builtin_hwdb_init, | |
187 | .exit = builtin_hwdb_exit, | |
188 | .validate = builtin_hwdb_validate, | |
189 | .help = "hardware database", | |
3cf5266b | 190 | }; |