Recognise storage devices on USB bus
[petitboot] / discover / udev.c
1
2 #if defined(HAVE_CONFIG_H)
3 #include "config.h"
4 #endif
5
6 #include <assert.h>
7 #include <errno.h>
8 #include <libudev.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <sys/socket.h>
14 #include <sys/types.h>
15 #include <sys/un.h>
16
17 #include <log/log.h>
18 #include <types/types.h>
19 #include <talloc/talloc.h>
20 #include <waiter/waiter.h>
21 #include <system/system.h>
22
23 #include "event.h"
24 #include "udev.h"
25 #include "pb-discover.h"
26 #include "device-handler.h"
27 #include "cdrom.h"
28 #include "devmapper.h"
29
30 /* We set a default monitor buffer size, as we may not process monitor
31  * events while performing device discvoery. systemd uses a 128M buffer, so
32  * we'll do the same here */
33 static const int monitor_bufsize = 128 * 1024 * 1024;
34
35 struct pb_udev {
36         struct udev *udev;
37         struct udev_monitor *monitor;
38         struct device_handler *handler;
39 };
40
41 static int udev_destructor(void *p)
42 {
43         struct pb_udev *udev = p;
44
45         if (udev->monitor) {
46                 udev_monitor_unref(udev->monitor);
47                 udev->monitor = NULL;
48         }
49
50         if (udev->udev) {
51                 udev_unref(udev->udev);
52                 udev->udev = NULL;
53         }
54
55         return 0;
56 }
57
58 static void udev_setup_device_params(struct udev_device *udev,
59                 struct discover_device *dev)
60 {
61         struct udev_list_entry *list, *entry;
62
63         list = udev_device_get_properties_list_entry(udev);
64         if (!list)
65                 return;
66
67         udev_list_entry_foreach(entry, list)
68                 discover_device_set_param(dev,
69                                 udev_list_entry_get_name(entry),
70                                 udev_list_entry_get_value(entry));
71 }
72
73 static int udev_handle_block_add(struct pb_udev *udev, struct udev_device *dev,
74                 const char *name)
75 {
76         struct discover_device *ddev;
77         unsigned int i = 0;
78         const char *typestr;
79         const char *uuid;
80         const char *path;
81         const char *node;
82         const char *prop;
83         const char *type;
84         const char *devname;
85         const char *ignored_types[] = {
86                 "linux_raid_member",
87                 "swap",
88                 "LVM2_member",
89                 NULL,
90         };
91         bool cdrom, usb;
92
93         typestr = udev_device_get_devtype(dev);
94         if (!typestr) {
95                 pb_debug("udev_device_get_devtype failed\n");
96                 return -1;
97         }
98
99         if (!(!strcmp(typestr, "disk") || !strcmp(typestr, "partition"))) {
100                 pb_log("SKIP %s: invalid type %s\n", name, typestr);
101                 return 0;
102         }
103
104         node = udev_device_get_devnode(dev);
105         path = udev_device_get_devpath(dev);
106         if (path && strstr(path, "virtual/block/loop")) {
107                 pb_log("SKIP: %s: ignored (path=%s)\n", name, path);
108                 return 0;
109         }
110
111         if (path && strstr(path, "virtual/block/ram")) {
112                 device_handler_add_ramdisk(udev->handler, node);
113                 return 0;
114         }
115
116         cdrom = node && !!udev_device_get_property_value(dev, "ID_CDROM");
117         if (cdrom) {
118                 /* CDROMs require a little initialisation, to get
119                  * petitboot-compatible tray behaviour */
120                 cdrom_init(node);
121                 if (!cdrom_media_present(node)) {
122                         pb_log("SKIP: %s: no media present\n", name);
123                         return 0;
124                 }
125         }
126
127         /* If our environment's udev can recognise them explictly skip any
128          * device mapper devices we encounter */
129         devname = udev_device_get_property_value(dev, "DM_NAME");
130         if (devname) {
131                 pb_debug("SKIP: dm-device %s\n", devname);
132                 return 0;
133         }
134
135         type = udev_device_get_property_value(dev, "ID_FS_TYPE");
136         if (!type) {
137                 pb_log("SKIP: %s: no ID_FS_TYPE property\n", name);
138                 return 0;
139         }
140
141         while (ignored_types[i]) {
142                 if (!strncmp(type, ignored_types[i], strlen(ignored_types[i]))) {
143                         pb_log("SKIP: %s: ignore '%s' filesystem\n", name, type);
144                         return 0;
145                 }
146                 i++;
147         }
148
149         /* We may see multipath devices; they'll have the same uuid as an
150          * existing device, so only parse the first. */
151         uuid = udev_device_get_property_value(dev, "ID_FS_UUID");
152         if (uuid) {
153                 ddev = device_lookup_by_uuid(udev->handler, uuid);
154                 if (ddev) {
155                         pb_log("SKIP: %s UUID [%s] already present (as %s)\n",
156                                         name, uuid, ddev->device->id);
157                         return 0;
158                 }
159         }
160
161         ddev = discover_device_create(udev->handler, name);
162
163         ddev->device_path = talloc_strdup(ddev, node);
164
165         if (uuid)
166                 ddev->uuid = talloc_strdup(ddev, uuid);
167         prop = udev_device_get_property_value(dev, "ID_FS_LABEL");
168         if (prop)
169                 ddev->label = talloc_strdup(ddev, prop);
170
171         usb = !!udev_device_get_property_value(dev, "ID_USB_DRIVER");
172         if (cdrom)
173                 ddev->device->type = DEVICE_TYPE_OPTICAL;
174         else
175                 ddev->device->type = usb ? DEVICE_TYPE_USB : DEVICE_TYPE_DISK;
176
177         udev_setup_device_params(dev, ddev);
178
179         /* Create a snapshot for all disks, unless it is an assembled RAID array */
180         if ((ddev->device->type == DEVICE_TYPE_DISK ||
181              ddev->device->type == DEVICE_TYPE_USB) &&
182             !udev_device_get_property_value(dev, "MD_LEVEL"))
183                 devmapper_init_snapshot(udev->handler, ddev);
184
185         device_handler_discover(udev->handler, ddev);
186
187         return 0;
188 }
189
190 static int udev_handle_dev_add(struct pb_udev *udev, struct udev_device *dev)
191 {
192         const char *subsys;
193         const char *name;
194
195         name = udev_device_get_sysname(dev);
196         if (!name) {
197                 pb_debug("udev_device_get_sysname failed\n");
198                 return -1;
199         }
200
201         subsys = udev_device_get_subsystem(dev);
202         if (!subsys) {
203                 pb_debug("udev_device_get_subsystem failed\n");
204                 return -1;
205         }
206
207         if (device_lookup_by_id(udev->handler, name)) {
208                 pb_debug("device %s is already present?\n", name);
209                 return -1;
210         }
211
212         if (!strcmp(subsys, "block")) {
213                 return udev_handle_block_add(udev, dev, name);
214         }
215
216         pb_log("SKIP %s: unknown subsystem %s\n", name, subsys);
217         return -1;
218 }
219
220
221 static int udev_handle_dev_remove(struct pb_udev *udev, struct udev_device *dev)
222 {
223         struct discover_device *ddev;
224         const char *name;
225
226         name = udev_device_get_sysname(dev);
227         if (!name) {
228                 pb_debug("udev_device_get_sysname failed\n");
229                 return -1;
230         }
231
232         ddev = device_lookup_by_id(udev->handler, name);
233         if (!ddev)
234                 return 0;
235
236         device_handler_remove(udev->handler, ddev);
237
238         return 0;
239 }
240
241 /* returns true if further event processing should stop (eg., we've
242  * ejected the cdrom)
243  */
244 static bool udev_handle_cdrom_events(struct pb_udev *udev,
245                 struct udev_device *dev, struct discover_device *ddev)
246 {
247         const char *node;
248
249         node = udev_device_get_devnode(dev);
250
251         /* handle CDROM eject requests */
252         if (udev_device_get_property_value(dev, "DISK_EJECT_REQUEST")) {
253                 bool eject = false;
254
255                 pb_debug("udev: eject request\n");
256
257                 /* If the device is mounted, cdrom_id's own eject request may
258                  * have failed. So, we'll need to do our own here.
259                  */
260                 if (ddev) {
261                         eject = ddev->mounted;
262                         udev_handle_dev_remove(udev, dev);
263                         return false;
264                 }
265
266                 if (eject)
267                         cdrom_eject(node);
268
269                 return true;
270         }
271
272         if (udev_device_get_property_value(dev, "DISK_MEDIA_CHANGE")) {
273                 if (cdrom_media_present(node))
274                         udev_handle_dev_add(udev, dev);
275                 else
276                         udev_handle_dev_remove(udev, dev);
277                 return true;
278         }
279
280         return false;
281 }
282
283 static int udev_handle_dev_change(struct pb_udev *udev, struct udev_device *dev)
284 {
285         struct discover_device *ddev;
286         const char *name;
287         int rc = 0;
288
289         name = udev_device_get_sysname(dev);
290
291         ddev = device_lookup_by_id(udev->handler, name);
292
293         /* if this is a CDROM device, process eject & media change requests;
294          * these may stop further processing */
295         if (!udev_device_get_property_value(dev, "ID_CDROM")) {
296                 if (udev_handle_cdrom_events(udev, dev, ddev))
297                         return 0;
298         }
299
300         /* if this is a new device, treat it as an add */
301         if (!ddev)
302                 rc = udev_handle_dev_add(udev, dev);
303
304         return rc;
305 }
306
307 static int udev_handle_dev_action(struct udev_device *dev, const char *action)
308 {
309         struct pb_udev *udev = udev_get_userdata(udev_device_get_udev(dev));
310         struct udev_list_entry *list;
311         const char *name;
312
313         list = udev_device_get_properties_list_entry(dev);
314         name = udev_device_get_sysname(dev);
315
316         pb_debug("udev: action %s, device %s\n", action, name);
317         pb_debug("udev: properties:\n");
318
319         for (; list; list = udev_list_entry_get_next(list))
320                 pb_debug("\t%-20s: %s\n", udev_list_entry_get_name(list),
321                                 udev_list_entry_get_value(list));
322
323         if (!strcmp(action, "add"))
324                 return udev_handle_dev_add(udev, dev);
325
326         else if (!strcmp(action, "remove"))
327                 return udev_handle_dev_remove(udev, dev);
328
329         else if (!strcmp(action, "change"))
330                 return udev_handle_dev_change(udev, dev);
331
332         return 0;
333 }
334
335 static int udev_enumerate(struct udev *udev)
336 {
337         int result;
338         struct udev_list_entry *list, *entry;
339         struct udev_enumerate *enumerate;
340
341         enumerate = udev_enumerate_new(udev);
342
343         if (!enumerate) {
344                 pb_log("udev_enumerate_new failed\n");
345                 return -1;
346         }
347
348         result = udev_enumerate_add_match_subsystem(enumerate, "block");
349         if (result) {
350                 pb_log("udev_enumerate_add_match_subsystem failed\n");
351                 goto fail;
352         }
353
354         result = udev_enumerate_add_match_is_initialized(enumerate);
355         if (result) {
356                 pb_log("udev_enumerate_add_match_is_initialised failed\n");
357                 goto fail;
358         }
359
360         udev_enumerate_scan_devices(enumerate);
361
362         list = udev_enumerate_get_list_entry(enumerate);
363
364         udev_list_entry_foreach(entry, list) {
365                 const char *syspath;
366                 struct udev_device *dev;
367
368                 syspath = udev_list_entry_get_name(entry);
369                 dev = udev_device_new_from_syspath(udev, syspath);
370
371                 udev_handle_dev_action(dev, "add");
372
373                 udev_device_unref(dev);
374         }
375
376         udev_enumerate_unref(enumerate);
377         return 0;
378
379 fail:
380         udev_enumerate_unref(enumerate);
381         return -1;
382 }
383
384 static int udev_setup_monitor(struct udev *udev, struct udev_monitor **monitor)
385 {
386         int result;
387         struct udev_monitor *m;
388
389         *monitor = NULL;
390         m = udev_monitor_new_from_netlink(udev, "udev");
391
392         if (!m) {
393                 pb_log("udev_monitor_new_from_netlink failed\n");
394                 goto out_err;
395         }
396
397         result = udev_monitor_set_receive_buffer_size(m, monitor_bufsize);
398         if (result) {
399                 pb_log("udev_monitor_set_rx_bufsize(%d) failed\n",
400                         monitor_bufsize);
401         }
402
403         result = udev_monitor_filter_add_match_subsystem_devtype(m, "block",
404                 NULL);
405
406         if (result) {
407                 pb_log("udev_monitor_filter_add_match_subsystem_devtype failed\n");
408                 goto out_err;
409         }
410
411         result = udev_monitor_enable_receiving(m);
412
413         if (result) {
414                 pb_log("udev_monitor_enable_receiving failed\n");
415                 goto out_err;
416         }
417
418         *monitor = m;
419         return 0;
420
421 out_err:
422         udev_monitor_unref(m);
423         return -1;
424 }
425
426 /*
427  * udev_process - waiter callback for monitor netlink.
428  */
429
430 static int udev_process(void *arg)
431 {
432         struct udev_monitor *monitor = arg;
433         struct udev_device *dev;
434         const char *action;
435
436         dev = udev_monitor_receive_device(monitor);
437         if (!dev) {
438                 pb_log("udev_monitor_receive_device failed\n");
439                 return -1;
440         }
441
442         action = udev_device_get_action(dev);
443
444         if (!action) {
445                 pb_log("udev_device_get_action failed\n");
446         } else {
447                 udev_handle_dev_action(dev, action);
448         }
449
450         udev_device_unref(dev);
451         return 0;
452 }
453
454 static void udev_log_fn(struct udev __attribute__((unused)) *udev,
455         int __attribute__((unused)) priority, const char *file, int line,
456         const char *fn, const char *format, va_list args)
457 {
458       pb_log("libudev: %s %s:%d: ", fn, file, line);
459       vfprintf(pb_log_get_stream(), format, args);
460 }
461
462 struct pb_udev *udev_init(struct device_handler *handler,
463                 struct waitset *waitset)
464 {
465         struct pb_udev *udev;
466         int result;
467
468         udev = talloc_zero(handler, struct pb_udev);
469         talloc_set_destructor(udev, udev_destructor);
470         udev->handler = handler;
471
472         udev->udev = udev_new();
473
474         if (!udev->udev) {
475                 pb_log("udev_new failed\n");
476                 goto fail;
477         }
478
479         udev_set_userdata(udev->udev, udev);
480
481         udev_set_log_fn(udev->udev, udev_log_fn);
482
483         result = udev_setup_monitor(udev->udev, &udev->monitor);
484         if (result)
485                 goto fail;
486
487         result = udev_enumerate(udev->udev);
488         if (result)
489                 goto fail;
490
491         waiter_register_io(waitset, udev_monitor_get_fd(udev->monitor), WAIT_IN,
492                 udev_process, udev->monitor);
493
494         pb_debug("%s: waiting on udev\n", __func__);
495
496         return udev;
497
498 fail:
499         talloc_free(udev);
500         return NULL;
501 }
502
503 void udev_reinit(struct pb_udev *udev)
504 {
505         pb_log("udev: reinit requested, starting enumeration\n");
506         udev_enumerate(udev->udev);
507 }