2 This file is part of systemd.
4 Copyright 2012 Kay Sievers <kay@vrfy.org>
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.
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.
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/>.
27 #include "hwdb-util.h"
28 #include "string-util.h"
29 #include "udev-util.h"
34 int udev_builtin_hwdb_lookup(struct udev_device
*dev
,
35 const char *prefix
, const char *modalias
,
36 const char *filter
, bool test
) {
37 _cleanup_free_
char *lookup
= NULL
;
38 const char *key
, *value
;
45 lookup
= strjoin(prefix
, modalias
, NULL
);
51 SD_HWDB_FOREACH_PROPERTY(hwdb
, modalias
, key
, value
) {
52 if (filter
&& fnmatch(filter
, key
, FNM_NOESCAPE
) != 0)
55 if (udev_builtin_add_property(dev
, test
, key
, value
) < 0)
62 static const char *modalias_usb(struct udev_device
*dev
, char *s
, size_t size
) {
66 v
= udev_device_get_sysattr_value(dev
, "idVendor");
69 p
= udev_device_get_sysattr_value(dev
, "idProduct");
72 vn
= strtol(v
, NULL
, 16);
75 pn
= strtol(p
, NULL
, 16);
78 snprintf(s
, size
, "usb:v%04Xp%04X*", vn
, pn
);
82 static int udev_builtin_hwdb_search(struct udev_device
*dev
, struct udev_device
*srcdev
,
83 const char *subsystem
, const char *prefix
,
84 const char *filter
, bool test
) {
85 struct udev_device
*d
;
95 for (d
= srcdev
; d
&& !last
; d
= udev_device_get_parent(d
)) {
97 const char *modalias
= NULL
;
99 dsubsys
= udev_device_get_subsystem(d
);
103 /* look only at devices of a specific subsystem */
104 if (subsystem
&& !streq(dsubsys
, subsystem
))
107 modalias
= udev_device_get_property_value(d
, "MODALIAS");
109 if (streq(dsubsys
, "usb") && streq_ptr(udev_device_get_devtype(d
), "usb_device")) {
110 /* if the usb_device does not have a modalias, compose one */
112 modalias
= modalias_usb(d
, s
, sizeof(s
));
114 /* avoid looking at any parent device, they are usually just a USB hub */
121 r
= udev_builtin_hwdb_lookup(dev
, prefix
, modalias
, filter
, test
);
129 static int builtin_hwdb(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
130 static const struct option options
[] = {
131 { "filter", required_argument
, NULL
, 'f' },
132 { "device", required_argument
, NULL
, 'd' },
133 { "subsystem", required_argument
, NULL
, 's' },
134 { "lookup-prefix", required_argument
, NULL
, 'p' },
137 const char *filter
= NULL
;
138 const char *device
= NULL
;
139 const char *subsystem
= NULL
;
140 const char *prefix
= NULL
;
141 _cleanup_udev_device_unref_
struct udev_device
*srcdev
= NULL
;
149 option
= getopt_long(argc
, argv
, "f:d:s:p:", options
, NULL
);
172 /* query a specific key given as argument */
174 if (udev_builtin_hwdb_lookup(dev
, prefix
, argv
[optind
], filter
, test
) > 0)
179 /* read data from another device than the device we will store the data */
181 srcdev
= udev_device_new_from_device_id(udev_device_get_udev(dev
), device
);
186 if (udev_builtin_hwdb_search(dev
, srcdev
, subsystem
, prefix
, filter
, test
) > 0)
191 /* called at udev startup and reload */
192 static int builtin_hwdb_init(struct udev
*udev
) {
198 r
= sd_hwdb_new(&hwdb
);
205 /* called on udev shutdown and reload request */
206 static void builtin_hwdb_exit(struct udev
*udev
) {
207 hwdb
= sd_hwdb_unref(hwdb
);
210 /* called every couple of seconds during event activity; 'true' if config has changed */
211 static bool builtin_hwdb_validate(struct udev
*udev
) {
212 return hwdb_validate(hwdb
);
215 const struct udev_builtin udev_builtin_hwdb
= {
218 .init
= builtin_hwdb_init
,
219 .exit
= builtin_hwdb_exit
,
220 .validate
= builtin_hwdb_validate
,
221 .help
= "Hardware database",