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