discover: Change parsers to explicitly request configuration files
[petitboot] / discover / udev.c
1
2 #define _GNU_SOURCE
3
4 #include <assert.h>
5 #include <errno.h>
6 #include <libudev.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <sys/socket.h>
12 #include <sys/types.h>
13 #include <sys/un.h>
14
15 #include <log/log.h>
16 #include <types/types.h>
17 #include <talloc/talloc.h>
18 #include <waiter/waiter.h>
19 #include <system/system.h>
20
21 #include "event.h"
22 #include "udev.h"
23 #include "pb-discover.h"
24 #include "device-handler.h"
25
26 struct pb_udev {
27         struct udev *udev;
28         struct udev_monitor *monitor;
29         struct device_handler *handler;
30 };
31
32 static int udev_destructor(void *p)
33 {
34         struct pb_udev *udev = p;
35
36         udev_monitor_unref(udev->monitor);
37         udev->monitor = NULL;
38
39         udev_unref(udev->udev);
40         udev->udev = NULL;
41
42         return 0;
43 }
44
45 static void udev_setup_device_params(struct udev_device *udev,
46                 struct discover_device *dev)
47 {
48         struct udev_list_entry *list, *entry;
49
50         list = udev_device_get_properties_list_entry(udev);
51         if (!list)
52                 return;
53
54         udev_list_entry_foreach(entry, list)
55                 discover_device_set_param(dev,
56                                 udev_list_entry_get_name(entry),
57                                 udev_list_entry_get_value(entry));
58 }
59
60 static int udev_handle_dev_add(struct pb_udev *udev, struct udev_device *dev)
61 {
62         struct discover_device *ddev;
63         const char *typestr;
64         const char *serial;
65         const char *path;
66         const char *name;
67         const char *prop;
68
69         name = udev_device_get_sysname(dev);
70         if (!name) {
71                 pb_debug("udev_device_get_sysname failed\n");
72                 return -1;
73         }
74
75         typestr = udev_device_get_devtype(dev);
76         if (!typestr) {
77                 pb_debug("udev_device_get_devtype failed\n");
78                 return -1;
79         }
80
81         if (!(!strcmp(typestr, "disk") || !strcmp(typestr, "partition"))) {
82                 pb_debug("SKIP %s: invalid type %s\n", name, typestr);
83                 return 0;
84         }
85
86         path = udev_device_get_devpath(dev);
87         if (path && (strstr(path, "virtual/block/loop")
88                         || strstr(path, "virtual/block/ram"))) {
89                 pb_debug("SKIP: %s: ignored (path=%s)\n", name, path);
90                 return 0;
91         }
92
93         /* We have enough info to create the device and start discovery */
94         ddev = device_lookup_by_id(udev->handler, name);
95         if (ddev) {
96                 pb_debug("device %s is already present?\n", name);
97                 return -1;
98         }
99
100         /* we may also see multipath devices; same dev nodes (hence id), but
101          * different serial numbers */
102         serial = udev_device_get_property_value(dev, "ID_SERIAL");
103         if (serial && device_lookup_by_serial(udev->handler, serial))
104                 return -1;
105
106         ddev = discover_device_create(udev->handler, name);
107
108         ddev->device_path = udev_device_get_devnode(dev);
109         prop = udev_device_get_property_value(dev, "ID_FS_UUID");
110         if (prop)
111                 ddev->uuid = talloc_strdup(ddev, prop);
112         prop = udev_device_get_property_value(dev, "ID_FS_LABEL");
113         if (prop)
114                 ddev->label = talloc_strdup(ddev, prop);
115         ddev->device->type = DEVICE_TYPE_DISK;
116
117         udev_setup_device_params(dev, ddev);
118
119         device_handler_discover(udev->handler, ddev);
120
121         return 0;
122 }
123
124 static int udev_handle_dev_remove(struct pb_udev *udev, struct udev_device *dev)
125 {
126         struct discover_device *ddev;
127         const char *name;
128
129         name = udev_device_get_sysname(dev);
130         if (!name) {
131                 pb_debug("udev_device_get_sysname failed\n");
132                 return -1;
133         }
134
135         ddev = device_lookup_by_id(udev->handler, name);
136         if (!ddev)
137                 return 0;
138
139         device_handler_remove(udev->handler, ddev);
140
141         return 0;
142 }
143 static int udev_handle_dev_action(struct udev_device *dev, const char *action)
144 {
145         struct pb_udev *udev = udev_get_userdata(udev_device_get_udev(dev));
146
147         if (!strcmp(action, "add"))
148                 return udev_handle_dev_add(udev, dev);
149
150         else if (!strcmp(action, "remove"))
151                 return udev_handle_dev_remove(udev, dev);
152
153         return 0;
154 }
155
156 static int udev_enumerate(struct udev *udev)
157 {
158         int result;
159         struct udev_list_entry *list, *entry;
160         struct udev_enumerate *enumerate;
161
162         enumerate = udev_enumerate_new(udev);
163
164         if (!enumerate) {
165                 pb_log("udev_enumerate_new failed\n");
166                 return -1;
167         }
168
169         result = udev_enumerate_add_match_subsystem(enumerate, "block");
170
171         if (result) {
172                 pb_log("udev_enumerate_add_match_subsystem failed\n");
173                 goto fail;
174         }
175
176         udev_enumerate_scan_devices(enumerate);
177
178         list = udev_enumerate_get_list_entry(enumerate);
179
180         if (!list) {
181                 pb_log("udev_enumerate_get_list_entry failed\n");
182                 goto fail;
183         }
184
185         udev_list_entry_foreach(entry, list) {
186                 const char *syspath;
187                 struct udev_device *dev;
188
189                 syspath = udev_list_entry_get_name(entry);
190                 dev = udev_device_new_from_syspath(udev, syspath);
191
192                 udev_handle_dev_action(dev, "add");
193
194                 udev_device_unref(dev);
195         }
196
197         udev_enumerate_unref(enumerate);
198         return 0;
199
200 fail:
201         udev_enumerate_unref(enumerate);
202         return -1;
203 }
204
205 static int udev_setup_monitor(struct udev *udev, struct udev_monitor **monitor)
206 {
207         int result;
208         struct udev_monitor *m;
209
210         *monitor = NULL;
211         m = udev_monitor_new_from_netlink(udev, "udev");
212
213         if (!m) {
214                 pb_log("udev_monitor_new_from_netlink failed\n");
215                 goto out_err;
216         }
217
218         result = udev_monitor_filter_add_match_subsystem_devtype(m, "block",
219                 NULL);
220
221         if (result) {
222                 pb_log("udev_monitor_filter_add_match_subsystem_devtype failed\n");
223                 goto out_err;
224         }
225
226         result = udev_monitor_enable_receiving(m);
227
228         if (result) {
229                 pb_log("udev_monitor_enable_receiving failed\n");
230                 goto out_err;
231         }
232
233         *monitor = m;
234         return 0;
235
236 out_err:
237         udev_monitor_unref(m);
238         return -1;
239 }
240
241 /*
242  * udev_process - waiter callback for monitor netlink.
243  */
244
245 static int udev_process(void *arg)
246 {
247         struct udev_monitor *monitor = arg;
248         struct udev_device *dev;
249         const char *action;
250         int result;
251
252         dev = udev_monitor_receive_device(monitor);
253
254         if (!dev) {
255                 pb_log("udev_monitor_receive_device failed\n");
256                 return -1;
257         }
258
259         action = udev_device_get_action(dev);
260
261         if (!action) {
262                 pb_log("udev_device_get_action failed\n");
263                 goto fail;
264         }
265
266         result = udev_handle_dev_action(dev, action);
267
268         udev_device_unref(dev);
269         return result;
270
271 fail:
272         udev_device_unref(dev);
273         return -1;
274 }
275
276 static void udev_log_fn(struct udev __attribute__((unused)) *udev,
277         int __attribute__((unused)) priority, const char *file, int line,
278         const char *fn, const char *format, va_list args)
279 {
280       pb_log("libudev: %s %s:%d: ", fn, file, line);
281       vfprintf(pb_log_get_stream(), format, args);
282 }
283
284 struct pb_udev *udev_init(struct waitset *waitset,
285         struct device_handler *handler)
286 {
287         int result;
288         struct pb_udev *udev = talloc(NULL, struct pb_udev);
289
290         talloc_set_destructor(udev, udev_destructor);
291         udev->handler = handler;
292
293         udev->udev = udev_new();
294
295         if (!udev->udev) {
296                 pb_log("udev_new failed\n");
297                 goto fail_new;
298         }
299
300         udev_set_userdata(udev->udev, udev);
301
302         udev_set_log_fn(udev->udev, udev_log_fn);
303
304         result = udev_enumerate(udev->udev);
305
306         if (result)
307                 goto fail_enumerate;
308
309         result = udev_setup_monitor(udev->udev, &udev->monitor);
310
311         if (result)
312                 goto fail_monitor;
313
314         waiter_register_io(waitset, udev_monitor_get_fd(udev->monitor), WAIT_IN,
315                 udev_process, udev->monitor);
316
317         pb_debug("%s: waiting on udev\n", __func__);
318
319         return udev;
320
321 fail_monitor:
322 fail_enumerate:
323         udev_unref(udev->udev);
324 fail_new:
325         talloc_free(udev);
326         return NULL;
327 }
328
329 void udev_destroy(struct pb_udev *udev)
330 {
331         talloc_free(udev);
332 }