1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Kay Sievers <kay@vrfy.org>
15 #include "alloc-util.h"
16 #include "hwdb-util.h"
17 #include "parse-util.h"
18 #include "string-util.h"
19 #include "udev-util.h"
24 int udev_builtin_hwdb_lookup(struct udev_device
*dev
,
25 const char *prefix
, const char *modalias
,
26 const char *filter
, bool test
) {
27 _cleanup_free_
char *lookup
= NULL
;
28 const char *key
, *value
;
35 lookup
= strjoin(prefix
, modalias
);
41 SD_HWDB_FOREACH_PROPERTY(hwdb
, modalias
, key
, value
) {
42 if (filter
&& fnmatch(filter
, key
, FNM_NOESCAPE
) != 0)
45 if (udev_builtin_add_property(dev
, test
, key
, value
) < 0)
52 static const char *modalias_usb(struct udev_device
*dev
, char *s
, size_t size
) {
56 v
= udev_device_get_sysattr_value(dev
, "idVendor");
59 p
= udev_device_get_sysattr_value(dev
, "idProduct");
62 if (safe_atoux16(v
, &vn
) < 0)
64 if (safe_atoux16(p
, &pn
) < 0)
66 snprintf(s
, size
, "usb:v%04Xp%04X*", vn
, pn
);
70 static int udev_builtin_hwdb_search(struct udev_device
*dev
, struct udev_device
*srcdev
,
71 const char *subsystem
, const char *prefix
,
72 const char *filter
, bool test
) {
73 struct udev_device
*d
;
83 for (d
= srcdev
; d
&& !last
; d
= udev_device_get_parent(d
)) {
85 const char *modalias
= NULL
;
87 dsubsys
= udev_device_get_subsystem(d
);
91 /* look only at devices of a specific subsystem */
92 if (subsystem
&& !streq(dsubsys
, subsystem
))
95 modalias
= udev_device_get_property_value(d
, "MODALIAS");
97 if (streq(dsubsys
, "usb") && streq_ptr(udev_device_get_devtype(d
), "usb_device")) {
98 /* if the usb_device does not have a modalias, compose one */
100 modalias
= modalias_usb(d
, s
, sizeof(s
));
102 /* avoid looking at any parent device, they are usually just a USB hub */
109 r
= udev_builtin_hwdb_lookup(dev
, prefix
, modalias
, filter
, test
);
117 static int builtin_hwdb(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
118 static const struct option options
[] = {
119 { "filter", required_argument
, NULL
, 'f' },
120 { "device", required_argument
, NULL
, 'd' },
121 { "subsystem", required_argument
, NULL
, 's' },
122 { "lookup-prefix", required_argument
, NULL
, 'p' },
125 const char *filter
= NULL
;
126 const char *device
= NULL
;
127 const char *subsystem
= NULL
;
128 const char *prefix
= NULL
;
129 _cleanup_(udev_device_unrefp
) struct udev_device
*srcdev
= NULL
;
137 option
= getopt_long(argc
, argv
, "f:d:s:p:", options
, NULL
);
160 /* query a specific key given as argument */
162 if (udev_builtin_hwdb_lookup(dev
, prefix
, argv
[optind
], filter
, test
) > 0)
167 /* read data from another device than the device we will store the data */
169 srcdev
= udev_device_new_from_device_id(udev_device_get_udev(dev
), device
);
174 if (udev_builtin_hwdb_search(dev
, srcdev
, subsystem
, prefix
, filter
, test
) > 0)
179 /* called at udev startup and reload */
180 static int builtin_hwdb_init(struct udev
*udev
) {
186 r
= sd_hwdb_new(&hwdb
);
193 /* called on udev shutdown and reload request */
194 static void builtin_hwdb_exit(struct udev
*udev
) {
195 hwdb
= sd_hwdb_unref(hwdb
);
198 /* called every couple of seconds during event activity; 'true' if config has changed */
199 static bool builtin_hwdb_validate(struct udev
*udev
) {
200 return hwdb_validate(hwdb
);
203 const struct udev_builtin udev_builtin_hwdb
= {
206 .init
= builtin_hwdb_init
,
207 .exit
= builtin_hwdb_exit
,
208 .validate
= builtin_hwdb_validate
,
209 .help
= "Hardware database",