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