udev-builtin-hwdb.c revision beef8df837fb0ef708143b34aa85a897400e8992
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2012 Kay Sievers <kay@vrfy.org>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
eef46c372f64f40dd75415b2c504c73138719c8dLennart Poetteringint udev_builtin_hwdb_lookup(struct udev_device *dev,
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering lookup = strjoin(prefix, modalias, NULL);
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering list = udev_hwdb_get_properties_list_entry(hwdb, lookup, 0);
0b452006de98294d1690f045f6ea2f7f6630ec3bRonny Chevalier list = udev_hwdb_get_properties_list_entry(hwdb, modalias, 0);
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering if (filter && fnmatch(filter, udev_list_entry_get_name(entry), FNM_NOESCAPE) != 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic const char *modalias_usb(struct udev_device *dev, char *s, size_t size) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *v, *p;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering v = udev_device_get_sysattr_value(dev, "idVendor");
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering p = udev_device_get_sysattr_value(dev, "idProduct");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering snprintf(s, size, "usb:v%04Xp%04X*", vn, pn);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int udev_builtin_hwdb_search(struct udev_device *dev, struct udev_device *srcdev,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *subsystem, const char *prefix,
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering for (d = srcdev; d; d = udev_device_get_parent(d)) {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering /* look only at devices of a specific subsystem */
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering if (subsystem && !streq(dsubsys, subsystem))
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering /* the usb_device does not have a modalias, compose one */
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering modalias = modalias_usb(d, s, sizeof(s));
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering modalias = udev_device_get_property_value(d, "MODALIAS");
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering n = udev_builtin_hwdb_lookup(dev, prefix, modalias, filter, test);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringstatic int builtin_hwdb(struct udev_device *dev, int argc, char *argv[], bool test) {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering { "filter", required_argument, NULL, 'f' },
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering { "device", required_argument, NULL, 'd' },
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering { "subsystem", required_argument, NULL, 's' },
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen { "lookup-prefix", required_argument, NULL, 'p' },
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen option = getopt_long(argc, argv, "f:d:s:p:", options, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* query a specific key given as argument */
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (udev_builtin_hwdb_lookup(dev, prefix, argv[optind], filter, test) > 0)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering /* read data from another device than the device we will store the data */
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering srcdev = udev_device_new_from_device_id(udev_device_get_udev(dev), device);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (udev_builtin_hwdb_search(dev, srcdev, subsystem, prefix, filter, test) > 0)
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering/* called at udev startup and reload */
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic int builtin_hwdb_init(struct udev *udev)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering/* called on udev shutdown and reload request */
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringstatic void builtin_hwdb_exit(struct udev *udev)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering/* called every couple of seconds during event activity; 'true' if config has changed */
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringstatic bool builtin_hwdb_validate(struct udev *udev)
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen return udev_hwdb_validate(hwdb);