]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-builtin-hwdb.c
treewide: use log_*_errno whenever %m is in the format string
[thirdparty/systemd.git] / src / udev / udev-builtin-hwdb.c
CommitLineData
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 31static struct udev_hwdb *hwdb;
ccba91c7 32
67410e9f 33int udev_builtin_hwdb_lookup(struct udev_device *dev,
a4bbef09
KS
34 const char *prefix, const char *modalias,
35 const char *filter, bool test) {
36 struct udev_list_entry *list;
59803c38
KS
37 struct udev_list_entry *entry;
38 int n = 0;
39
33c770b1
KS
40 if (!hwdb)
41 return -ENOENT;
42
a4bbef09
KS
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) {
67410e9f
KS
54 if (filter && fnmatch(filter, udev_list_entry_get_name(entry), FNM_NOESCAPE) != 0)
55 continue;
56
59803c38
KS
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
66static 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
67410e9f 86static int udev_builtin_hwdb_search(struct udev_device *dev, struct udev_device *srcdev,
a4bbef09
KS
87 const char *subsystem, const char *prefix,
88 const char *filter, bool test) {
59803c38
KS
89 struct udev_device *d;
90 char s[16];
77cf759e
KS
91 bool last = false;
92 int r = 0;
59803c38 93
77cf759e 94 for (d = srcdev; d && !last; d = udev_device_get_parent(d)) {
59803c38
KS
95 const char *dsubsys;
96 const char *modalias = NULL;
97
98 dsubsys = udev_device_get_subsystem(d);
99 if (!dsubsys)
100 continue;
101
102 /* look only at devices of a specific subsystem */
103 if (subsystem && !streq(dsubsys, subsystem))
104 continue;
105
0238cf7c
KS
106 modalias = udev_device_get_property_value(d, "MODALIAS");
107
77cf759e
KS
108 if (streq(dsubsys, "usb") && streq_ptr(udev_device_get_devtype(d), "usb_device")) {
109 /* if the usb_device does not have a modalias, compose one */
110 if (!modalias)
111 modalias = modalias_usb(d, s, sizeof(s));
112
113 /* avoid looking at any parent device, they are usually just a USB hub */
114 last = true;
115 }
59803c38 116
59803c38
KS
117 if (!modalias)
118 continue;
67410e9f 119
77cf759e
KS
120 r = udev_builtin_hwdb_lookup(dev, prefix, modalias, filter, test);
121 if (r > 0)
59803c38
KS
122 break;
123 }
124
77cf759e 125 return r;
59803c38
KS
126}
127
2001208c
KS
128static int builtin_hwdb(struct udev_device *dev, int argc, char *argv[], bool test) {
129 static const struct option options[] = {
67410e9f
KS
130 { "filter", required_argument, NULL, 'f' },
131 { "device", required_argument, NULL, 'd' },
2001208c 132 { "subsystem", required_argument, NULL, 's' },
a4bbef09 133 { "lookup-prefix", required_argument, NULL, 'p' },
2001208c 134 {}
796b06c2 135 };
67410e9f
KS
136 const char *filter = NULL;
137 const char *device = NULL;
59803c38 138 const char *subsystem = NULL;
a4bbef09 139 const char *prefix = NULL;
67410e9f 140 struct udev_device *srcdev;
ccba91c7 141
2001208c
KS
142 if (!hwdb)
143 return EXIT_FAILURE;
ccba91c7 144
2001208c
KS
145 for (;;) {
146 int option;
ccba91c7 147
a4bbef09 148 option = getopt_long(argc, argv, "f:d:s:p:", options, NULL);
2001208c
KS
149 if (option == -1)
150 break;
ccba91c7 151
2001208c 152 switch (option) {
67410e9f
KS
153 case 'f':
154 filter = optarg;
155 break;
156
157 case 'd':
158 device = optarg;
159 break;
160
2001208c 161 case 's':
59803c38 162 subsystem = optarg;
2001208c 163 break;
a4bbef09
KS
164
165 case 'p':
166 prefix = optarg;
167 break;
912541b0
KS
168 }
169 }
ccba91c7 170
6824690f
KS
171 /* query a specific key given as argument */
172 if (argv[optind]) {
a4bbef09 173 if (udev_builtin_hwdb_lookup(dev, prefix, argv[optind], filter, test) > 0)
6824690f
KS
174 return EXIT_SUCCESS;
175 return EXIT_FAILURE;
176 }
177
67410e9f
KS
178 /* read data from another device than the device we will store the data */
179 if (device) {
180 srcdev = udev_device_new_from_device_id(udev_device_get_udev(dev), device);
181 if (!srcdev)
182 return EXIT_FAILURE;
183 } else
184 srcdev = dev;
185
beef8df8
KS
186 if (udev_builtin_hwdb_search(dev, srcdev, subsystem, prefix, filter, test) > 0)
187 return EXIT_SUCCESS;
188 return EXIT_FAILURE;
796b06c2 189}
ccba91c7 190
796b06c2 191/* called at udev startup and reload */
9ec6e95b 192static int builtin_hwdb_init(struct udev *udev) {
2001208c 193 if (hwdb)
5b4d50ef 194 return 0;
2001208c
KS
195 hwdb = udev_hwdb_new(udev);
196 if (!hwdb)
197 return -ENOMEM;
912541b0 198 return 0;
3cf5266b 199}
ccba91c7 200
796b06c2 201/* called on udev shutdown and reload request */
9ec6e95b 202static void builtin_hwdb_exit(struct udev *udev) {
2001208c 203 hwdb = udev_hwdb_unref(hwdb);
ccba91c7 204}
3cf5266b 205
796b06c2 206/* called every couple of seconds during event activity; 'true' if config has changed */
9ec6e95b 207static bool builtin_hwdb_validate(struct udev *udev) {
2001208c 208 return udev_hwdb_validate(hwdb);
796b06c2 209}
3cf5266b 210
796b06c2
KS
211const struct udev_builtin udev_builtin_hwdb = {
212 .name = "hwdb",
213 .cmd = builtin_hwdb,
214 .init = builtin_hwdb_init,
215 .exit = builtin_hwdb_exit,
216 .validate = builtin_hwdb_validate,
217 .help = "hardware database",
3cf5266b 218};