chiark / gitweb /
234448cdf1126712f15e8e73c548cfffc3f28f3f
[elogind.git] / src / udev / udev-builtin-hwdb.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2012 Kay Sievers <kay@vrfy.org>
5
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.
10
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.
15
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/>.
18 ***/
19
20 #include <stdio.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <inttypes.h>
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <getopt.h>
27
28 #include "udev.h"
29
30 static struct udev_hwdb *hwdb;
31
32 int udev_builtin_hwdb_lookup(struct udev_device *dev, const char *modalias, bool test) {
33         struct udev_list_entry *entry;
34         int n = 0;
35
36         udev_list_entry_foreach(entry, udev_hwdb_get_properties_list_entry(hwdb, modalias, 0)) {
37                 if (udev_builtin_add_property(dev, test,
38                                               udev_list_entry_get_name(entry),
39                                               udev_list_entry_get_value(entry)) < 0)
40                         return -ENOMEM;
41                 n++;
42         }
43         return n;
44 }
45
46 static const char *modalias_usb(struct udev_device *dev, char *s, size_t size) {
47         const char *v, *p;
48         int vn, pn;
49
50         v = udev_device_get_sysattr_value(dev, "idVendor");
51         if (!v)
52                 return NULL;
53         p = udev_device_get_sysattr_value(dev, "idProduct");
54         if (!p)
55                 return NULL;
56         vn = strtol(v, NULL, 16);
57         if (vn <= 0)
58                 return NULL;
59         pn = strtol(p, NULL, 16);
60         if (pn <= 0)
61                 return NULL;
62         snprintf(s, size, "usb:v%04Xp%04X*", vn, pn);
63         return s;
64 }
65
66 static int udev_builtin_hwdb_search(struct udev_device *dev, const char *subsystem, bool test) {
67         struct udev_device *d;
68         char s[16];
69         int n = 0;
70
71         for (d = dev; d; d = udev_device_get_parent(d)) {
72                 const char *dsubsys;
73                 const char *modalias = NULL;
74
75                 dsubsys = udev_device_get_subsystem(d);
76                 if (!dsubsys)
77                         continue;
78
79                 /* look only at devices of a specific subsystem */
80                 if (subsystem && !streq(dsubsys, subsystem))
81                         continue;
82
83                 /* the usb_device does not have a modalias, compose one */
84                 if (streq(dsubsys, "usb"))
85                         modalias = modalias_usb(dev, s, sizeof(s));
86
87                 if (!modalias)
88                         modalias = udev_device_get_property_value(d, "MODALIAS");
89
90                 if (!modalias)
91                         continue;
92                 n = udev_builtin_hwdb_lookup(dev, modalias, test);
93                 if (n > 0)
94                         break;
95         }
96
97         return n;
98 }
99
100 static int builtin_hwdb(struct udev_device *dev, int argc, char *argv[], bool test) {
101         static const struct option options[] = {
102                 { "subsystem", required_argument, NULL, 's' },
103                 {}
104         };
105         const char *subsystem = NULL;
106
107         if (!hwdb)
108                 return EXIT_FAILURE;
109
110         for (;;) {
111                 int option;
112
113                 option = getopt_long(argc, argv, "s", options, NULL);
114                 if (option == -1)
115                         break;
116
117                 switch (option) {
118                 case 's':
119                         subsystem = optarg;
120                         break;
121                 }
122         }
123
124         if (udev_builtin_hwdb_search(dev, subsystem, test) < 0)
125                 return EXIT_FAILURE;
126         return EXIT_SUCCESS;
127 }
128
129 /* called at udev startup and reload */
130 static int builtin_hwdb_init(struct udev *udev)
131 {
132         if (hwdb)
133                 return 0;
134         hwdb = udev_hwdb_new(udev);
135         if (!hwdb)
136                 return -ENOMEM;
137         return 0;
138 }
139
140 /* called on udev shutdown and reload request */
141 static void builtin_hwdb_exit(struct udev *udev)
142 {
143         hwdb = udev_hwdb_unref(hwdb);
144 }
145
146 /* called every couple of seconds during event activity; 'true' if config has changed */
147 static bool builtin_hwdb_validate(struct udev *udev)
148 {
149         return udev_hwdb_validate(hwdb);
150 }
151
152 const struct udev_builtin udev_builtin_hwdb = {
153         .name = "hwdb",
154         .cmd = builtin_hwdb,
155         .init = builtin_hwdb_init,
156         .exit = builtin_hwdb_exit,
157         .validate = builtin_hwdb_validate,
158         .help = "hardware database",
159 };