1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "device-util.h"
13 #include "hwdb-util.h"
14 #include "parse-util.h"
15 #include "string-util.h"
16 #include "udev-builtin.h"
20 int udev_builtin_hwdb_lookup(sd_device
*dev
,
21 const char *prefix
, const char *modalias
,
22 const char *filter
, bool test
) {
23 _cleanup_free_
char *lookup
= NULL
;
24 const char *key
, *value
;
31 lookup
= strjoin(prefix
, modalias
);
37 SD_HWDB_FOREACH_PROPERTY(hwdb
, modalias
, key
, value
) {
38 if (filter
&& fnmatch(filter
, key
, FNM_NOESCAPE
) != 0)
41 r
= udev_builtin_add_property(dev
, test
, key
, value
);
49 static const char *modalias_usb(sd_device
*dev
, char *s
, size_t size
) {
53 if (sd_device_get_sysattr_value(dev
, "idVendor", &v
) < 0)
55 if (sd_device_get_sysattr_value(dev
, "idProduct", &p
) < 0)
57 if (safe_atoux16(v
, &vn
) < 0)
59 if (safe_atoux16(p
, &pn
) < 0)
61 snprintf(s
, size
, "usb:v%04Xp%04X*", vn
, pn
);
65 static int udev_builtin_hwdb_search(sd_device
*dev
, sd_device
*srcdev
,
66 const char *subsystem
, const char *prefix
,
67 const char *filter
, bool test
) {
78 for (d
= srcdev
; d
; ) {
79 const char *dsubsys
, *devtype
, *modalias
= NULL
;
81 if (sd_device_get_subsystem(d
, &dsubsys
) < 0)
84 /* look only at devices of a specific subsystem */
85 if (subsystem
&& !streq(dsubsys
, subsystem
))
88 (void) sd_device_get_property_value(d
, "MODALIAS", &modalias
);
90 if (streq(dsubsys
, "usb") &&
91 sd_device_get_devtype(d
, &devtype
) >= 0 &&
92 streq(devtype
, "usb_device")) {
93 /* if the usb_device does not have a modalias, compose one */
95 modalias
= modalias_usb(d
, s
, sizeof(s
));
97 /* avoid looking at any parent device, they are usually just a USB hub */
104 r
= udev_builtin_hwdb_lookup(dev
, prefix
, modalias
, filter
, test
);
111 if (sd_device_get_parent(d
, &d
) < 0)
118 static int builtin_hwdb(sd_device
*dev
, int argc
, char *argv
[], bool test
) {
119 static const struct option options
[] = {
120 { "filter", required_argument
, NULL
, 'f' },
121 { "device", required_argument
, NULL
, 'd' },
122 { "subsystem", required_argument
, NULL
, 's' },
123 { "lookup-prefix", required_argument
, NULL
, 'p' },
126 const char *filter
= NULL
;
127 const char *device
= NULL
;
128 const char *subsystem
= NULL
;
129 const char *prefix
= NULL
;
130 _cleanup_(sd_device_unrefp
) sd_device
*srcdev
= NULL
;
139 option
= getopt_long(argc
, argv
, "f:d:s:p:", options
, NULL
);
162 /* query a specific key given as argument */
164 r
= udev_builtin_hwdb_lookup(dev
, prefix
, argv
[optind
], filter
, test
);
166 return log_device_debug_errno(dev
, r
, "Failed to lookup hwdb: %m");
168 return log_device_debug_errno(dev
, SYNTHETIC_ERRNO(ENOENT
), "No entry found from hwdb.");
172 /* read data from another device than the device we will store the data */
174 r
= sd_device_new_from_device_id(&srcdev
, device
);
176 return log_device_debug_errno(dev
, r
, "Failed to create sd_device object '%s': %m", device
);
179 r
= udev_builtin_hwdb_search(dev
, srcdev
, subsystem
, prefix
, filter
, test
);
181 return log_device_debug_errno(dev
, r
, "Failed to lookup hwdb: %m");
183 return log_device_debug_errno(dev
, SYNTHETIC_ERRNO(ENOENT
), "No entry found from hwdb.");
187 /* called at udev startup and reload */
188 static int builtin_hwdb_init(void) {
194 r
= sd_hwdb_new(&hwdb
);
201 /* called on udev shutdown and reload request */
202 static void builtin_hwdb_exit(void) {
203 hwdb
= sd_hwdb_unref(hwdb
);
206 /* called every couple of seconds during event activity; 'true' if config has changed */
207 static bool builtin_hwdb_validate(void) {
208 return hwdb_validate(hwdb
);
211 const struct udev_builtin udev_builtin_hwdb
= {
214 .init
= builtin_hwdb_init
,
215 .exit
= builtin_hwdb_exit
,
216 .validate
= builtin_hwdb_validate
,
217 .help
= "Hardware database",