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
) {
50 const char *v
, *p
, *n
= NULL
;
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 (void) sd_device_get_sysattr_value(dev
, "product", &n
);
63 snprintf(s
, size
, "usb:v%04Xp%04X:%s", vn
, pn
, strempty(n
));
67 static int udev_builtin_hwdb_search(sd_device
*dev
, sd_device
*srcdev
,
68 const char *subsystem
, const char *prefix
,
69 const char *filter
, bool test
) {
79 for (sd_device
*d
= srcdev
; d
; ) {
80 const char *dsubsys
, *devtype
, *modalias
= NULL
;
82 if (sd_device_get_subsystem(d
, &dsubsys
) < 0)
85 /* look only at devices of a specific subsystem */
86 if (subsystem
&& !streq(dsubsys
, subsystem
))
89 (void) sd_device_get_property_value(d
, "MODALIAS", &modalias
);
91 if (streq(dsubsys
, "usb") &&
92 sd_device_get_devtype(d
, &devtype
) >= 0 &&
93 streq(devtype
, "usb_device")) {
94 /* if the usb_device does not have a modalias, compose one */
96 modalias
= modalias_usb(d
, s
, sizeof(s
));
98 /* avoid looking at any parent device, they are usually just a USB hub */
105 log_device_debug(dev
, "hwdb modalias key: \"%s\"", modalias
);
107 r
= udev_builtin_hwdb_lookup(dev
, prefix
, modalias
, filter
, test
);
114 if (sd_device_get_parent(d
, &d
) < 0)
121 static int builtin_hwdb(sd_device
*dev
, int argc
, char *argv
[], bool test
) {
122 static const struct option options
[] = {
123 { "filter", required_argument
, NULL
, 'f' },
124 { "device", required_argument
, NULL
, 'd' },
125 { "subsystem", required_argument
, NULL
, 's' },
126 { "lookup-prefix", required_argument
, NULL
, 'p' },
129 const char *filter
= NULL
;
130 const char *device
= NULL
;
131 const char *subsystem
= NULL
;
132 const char *prefix
= NULL
;
133 _cleanup_(sd_device_unrefp
) sd_device
*srcdev
= NULL
;
142 option
= getopt_long(argc
, argv
, "f:d:s:p:", options
, NULL
);
165 /* query a specific key given as argument */
167 r
= udev_builtin_hwdb_lookup(dev
, prefix
, argv
[optind
], filter
, test
);
169 return log_device_debug_errno(dev
, r
, "Failed to look up hwdb: %m");
171 return log_device_debug_errno(dev
, SYNTHETIC_ERRNO(ENODATA
), "No entry found from hwdb.");
175 /* read data from another device than the device we will store the data */
177 r
= sd_device_new_from_device_id(&srcdev
, device
);
179 return log_device_debug_errno(dev
, r
, "Failed to create sd_device object '%s': %m", device
);
182 r
= udev_builtin_hwdb_search(dev
, srcdev
, subsystem
, prefix
, filter
, test
);
184 return log_device_debug_errno(dev
, r
, "Failed to look up hwdb: %m");
186 return log_device_debug_errno(dev
, SYNTHETIC_ERRNO(ENODATA
), "No entry found from hwdb.");
190 /* called at udev startup and reload */
191 static int builtin_hwdb_init(void) {
197 r
= sd_hwdb_new(&hwdb
);
204 /* called on udev shutdown and reload request */
205 static void builtin_hwdb_exit(void) {
206 hwdb
= sd_hwdb_unref(hwdb
);
209 /* called every couple of seconds during event activity; 'true' if config has changed */
210 static bool builtin_hwdb_validate(void) {
211 return hwdb_validate(hwdb
);
214 const UdevBuiltin udev_builtin_hwdb
= {
217 .init
= builtin_hwdb_init
,
218 .exit
= builtin_hwdb_exit
,
219 .validate
= builtin_hwdb_validate
,
220 .help
= "Hardware database",