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