lib/efi: Cleanup read/write routines
[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 #include <process/process.h>
23
24 #include "event.h"
25 #include "udev.h"
26 #include "pb-discover.h"
27 #include "device-handler.h"
28 #include "cdrom.h"
29 #include "devmapper.h"
30 #include "network.h"
31
32 /* We set a default monitor buffer size, as we may not process monitor
33  * events while performing device discvoery. systemd uses a 128M buffer, so
34  * we'll do the same here */
35 static const int monitor_bufsize = 128 * 1024 * 1024;
36
37 struct pb_udev {
38         struct udev *udev;
39         struct udev_monitor *monitor;
40         struct device_handler *handler;
41 };
42
43 static int udev_destructor(void *p)
44 {
45         struct pb_udev *udev = p;
46
47         if (udev->monitor) {
48                 udev_monitor_unref(udev->monitor);
49                 udev->monitor = NULL;
50         }
51
52         if (udev->udev) {
53                 udev_unref(udev->udev);
54                 udev->udev = NULL;
55         }
56
57         return 0;
58 }
59
60 static void udev_setup_device_params(struct udev_device *udev,
61                 struct discover_device *dev)
62 {
63         struct udev_list_entry *list, *entry;
64
65         list = udev_device_get_properties_list_entry(udev);
66         if (!list)
67                 return;
68
69         udev_list_entry_foreach(entry, list)
70                 discover_device_set_param(dev,
71                                 udev_list_entry_get_name(entry),
72                                 udev_list_entry_get_value(entry));
73 }
74
75 /*
76  * Search for LVM logical volumes. If any exist they should be recognised
77  * by udev as normal.
78  * Normally this is handled in an init script, but on some platforms
79  * disks are slow enough to come up that we need to check again.
80  */
81 static void lvm_vg_search(struct device_handler *handler)
82 {
83         if (process_run_simple(handler, pb_system_apps.vgscan, "-qq", NULL))
84                 pb_log_fn("Failed to execute vgscan\n");
85
86         if (process_run_simple(handler, pb_system_apps.vgchange, "-ay", "-qq",
87                                 NULL))
88                 pb_log_fn("Failed to execute vgchange\n");
89 }
90
91 static int udev_handle_block_add(struct pb_udev *udev, struct udev_device *dev,
92                 const char *name)
93 {
94         char *devlinks = NULL, *link, *saveptr = NULL;
95         struct discover_device *ddev;
96         unsigned int i = 0;
97         const char *typestr;
98         const char *uuid;
99         const char *path;
100         const char *node;
101         const char *prop;
102         const char *type;
103         const char *devname;
104         const char *ignored_types[] = {
105                 "linux_raid_member",
106                 "swap",
107                 NULL,
108         };
109         bool cdrom, usb;
110
111         typestr = udev_device_get_devtype(dev);
112         if (!typestr) {
113                 pb_debug("udev_device_get_devtype failed\n");
114                 return -1;
115         }
116
117         if (!(!strcmp(typestr, "disk") || !strcmp(typestr, "partition"))) {
118                 pb_log("SKIP %s: invalid type %s\n", name, typestr);
119                 return 0;
120         }
121
122         node = udev_device_get_devnode(dev);
123         path = udev_device_get_devpath(dev);
124         if (path && strstr(path, "virtual/block/loop")) {
125                 pb_log("SKIP: %s: ignored (path=%s)\n", name, path);
126                 return 0;
127         }
128
129         if (path && strstr(path, "virtual/block/ram")) {
130                 device_handler_add_ramdisk(udev->handler, node);
131                 return 0;
132         }
133
134         cdrom = node && !!udev_device_get_property_value(dev, "ID_CDROM");
135         if (cdrom) {
136                 /* CDROMs require a little initialisation, to get
137                  * petitboot-compatible tray behaviour */
138                 cdrom_init(node);
139                 if (!cdrom_media_present(node)) {
140                         pb_log("SKIP: %s: no media present\n", name);
141                         return 0;
142                 }
143         }
144
145         /* Ignore any device mapper devices that aren't logical volumes */
146         devname = udev_device_get_property_value(dev, "DM_NAME");
147         if (devname && ! udev_device_get_property_value(dev, "DM_LV_NAME")) {
148                 pb_debug("SKIP: dm-device %s\n", devname);
149                 return 0;
150         }
151
152         type = udev_device_get_property_value(dev, "ID_FS_TYPE");
153         if (!type) {
154                 pb_log("SKIP: %s: no ID_FS_TYPE property\n", name);
155                 return 0;
156         }
157
158         while (ignored_types[i]) {
159                 if (!strncmp(type, ignored_types[i], strlen(ignored_types[i]))) {
160                         pb_log("SKIP: %s: ignore '%s' filesystem\n", name, type);
161                         return 0;
162                 }
163                 i++;
164         }
165
166         /* Search for LVM logical volumes if we see an LVM member */
167         if (strncmp(type, "LVM2_member", strlen("LVM2_member")) == 0) {
168                 lvm_vg_search(udev->handler);
169                 return 0;
170         }
171
172         /* We may see multipath devices; they'll have the same uuid as an
173          * existing device, so only parse the first. */
174         uuid = udev_device_get_property_value(dev, "ID_FS_UUID");
175         if (uuid) {
176                 ddev = device_lookup_by_uuid(udev->handler, uuid);
177                 if (ddev) {
178                         pb_log("SKIP: %s UUID [%s] already present (as %s)\n",
179                                         name, uuid, ddev->device->id);
180                         return 0;
181                 }
182         }
183
184         /* Use DM_NAME for logical volumes, or the device name otherwise */
185         ddev = discover_device_create(udev->handler, uuid, devname ?: name);
186
187         if (devname) {
188                 /*
189                  * For logical volumes udev_device_get_devnode() returns a path
190                  * of the form "/dev/dm-xx". These nodes names are not
191                  * persistent and are opaque to the user. Instead use the more
192                  * recognisable "/dev/mapper/lv-name" node if it is available.
193                  */
194                 devlinks = talloc_strdup(ddev,
195                                 udev_device_get_property_value(dev, "DEVLINKS"));
196                 link = devlinks ? strtok_r(devlinks, " ", &saveptr) : NULL;
197                 while (link) {
198                         if (strncmp(link, "/dev/mapper/",
199                                         strlen("/dev/mapper/")) == 0) {
200                                 node = link;
201                                 break;
202                         }
203                         link = strtok_r(NULL, " ", &saveptr);
204                 }
205         }
206
207         ddev->device_path = talloc_strdup(ddev, node);
208         talloc_free(devlinks);
209
210         if (uuid)
211                 ddev->uuid = talloc_strdup(ddev, uuid);
212         prop = udev_device_get_property_value(dev, "ID_FS_LABEL");
213         if (prop)
214                 ddev->label = talloc_strdup(ddev, prop);
215
216         usb = !!udev_device_get_property_value(dev, "ID_USB_DRIVER");
217         if (cdrom)
218                 ddev->device->type = DEVICE_TYPE_OPTICAL;
219         else
220                 ddev->device->type = usb ? DEVICE_TYPE_USB : DEVICE_TYPE_DISK;
221
222         udev_setup_device_params(dev, ddev);
223
224         /* Create a snapshot for all disk devices */
225         if ((ddev->device->type == DEVICE_TYPE_DISK ||
226              ddev->device->type == DEVICE_TYPE_USB))
227                 devmapper_init_snapshot(udev->handler, ddev);
228
229         device_handler_discover(udev->handler, ddev);
230
231         return 0;
232 }
233
234 /*
235  * Mark valid interfaces as 'ready'.
236  * The udev_enumerate_add_match_is_initialized() filter in udev_enumerate()
237  * ensures that any device we see is properly initialized by udev (eg. interface
238  * names); here we check that the properties are sane and mark the interface
239  * as ready for configuration in discover/network.
240  */
241 static int udev_check_interface_ready(struct device_handler *handler,
242                 struct udev_device *dev)
243 {
244         const char *name, *ifindex, *interface, *mac_name;
245         uint8_t *mac;
246         char byte[3];
247         unsigned int i, j;
248
249
250         name = udev_device_get_sysname(dev);
251         if (!name) {
252                 pb_debug("udev_device_get_sysname failed\n");
253                 return -1;
254         }
255
256         ifindex = udev_device_get_property_value(dev, "IFINDEX");
257         interface = udev_device_get_property_value(dev, "INTERFACE");
258         mac_name = udev_device_get_property_value(dev, "ID_NET_NAME_MAC");
259
260         /* Physical interfaces should have all of these properties */
261         if (!ifindex || !interface || !mac_name) {
262                 pb_debug("%s: interface %s missing properties\n",
263                                 __func__, name);
264                 return -1;
265         }
266
267         /* ID_NET_NAME_MAC format is enxMACADDR */
268         if (strlen(mac_name) < 15) {
269                 pb_debug("%s: Unexpected MAC format: %s\n",
270                                 __func__, mac_name);
271                 return -1;
272         }
273
274         mac = talloc_array(handler, uint8_t, HWADDR_SIZE);
275         if (!mac)
276                 return -1;
277
278         /*
279          * ID_NET_NAME_MAC is not a conventionally formatted MAC
280          * string - convert it before passing it to network.c
281          */
282         byte[2] = '\0';
283         for (i = strlen("enx"), j = 0;
284                         i < strlen(mac_name) && j < HWADDR_SIZE; i += 2) {
285                 memcpy(byte, &mac_name[i], 2);
286                 mac[j++] = strtoul(byte, NULL, 16);
287         }
288
289         network_mark_interface_ready(handler,
290                         atoi(ifindex), interface, mac, HWADDR_SIZE);
291
292         talloc_free(mac);
293         return 0;
294 }
295
296 static int udev_handle_dev_add(struct pb_udev *udev, struct udev_device *dev)
297 {
298         const char *subsys;
299         const char *name;
300
301         name = udev_device_get_sysname(dev);
302         if (!name) {
303                 pb_debug("udev_device_get_sysname failed\n");
304                 return -1;
305         }
306
307         subsys = udev_device_get_subsystem(dev);
308         if (!subsys) {
309                 pb_debug("udev_device_get_subsystem failed\n");
310                 return -1;
311         }
312
313         /* If we see a net device, check if it is ready to be used */
314         if (!strncmp(subsys, "net", strlen("net")))
315                 return udev_check_interface_ready(udev->handler, dev);
316
317         if (device_lookup_by_id(udev->handler, name)) {
318                 pb_debug("device %s is already present?\n", name);
319                 return -1;
320         }
321
322         if (!strcmp(subsys, "block")) {
323                 return udev_handle_block_add(udev, dev, name);
324         }
325
326         pb_log("SKIP %s: unknown subsystem %s\n", name, subsys);
327         return -1;
328 }
329
330
331 static int udev_handle_dev_remove(struct pb_udev *udev, struct udev_device *dev)
332 {
333         struct discover_device *ddev;
334         const char *name;
335
336         name = udev_device_get_sysname(dev);
337         if (!name) {
338                 pb_debug("udev_device_get_sysname failed\n");
339                 return -1;
340         }
341
342         ddev = device_lookup_by_id(udev->handler, name);
343         if (!ddev)
344                 return 0;
345
346         device_handler_remove(udev->handler, ddev);
347
348         return 0;
349 }
350
351 /* returns true if further event processing should stop (eg., we've
352  * ejected the cdrom)
353  */
354 static bool udev_handle_cdrom_events(struct pb_udev *udev,
355                 struct udev_device *dev, struct discover_device *ddev)
356 {
357         const char *node;
358         bool eject = false;
359
360         node = udev_device_get_devnode(dev);
361
362         /* handle CDROM eject requests */
363         if (udev_device_get_property_value(dev, "DISK_EJECT_REQUEST")) {
364                 pb_debug("udev: eject request\n");
365
366                 /* If the device is mounted, cdrom_id's own eject request may
367                  * have failed. So, we'll need to do our own here.
368                  */
369                 if (ddev) {
370                         eject = ddev->mounted;
371                         udev_handle_dev_remove(udev, dev);
372                 }
373
374                 if (eject)
375                         cdrom_eject(node);
376
377                 return true;
378         }
379
380         if (udev_device_get_property_value(dev, "DISK_MEDIA_CHANGE")) {
381                 if (cdrom_media_present(node))
382                         udev_handle_dev_add(udev, dev);
383                 else
384                         udev_handle_dev_remove(udev, dev);
385                 return true;
386         }
387
388         return false;
389 }
390
391 static int udev_handle_dev_change(struct pb_udev *udev, struct udev_device *dev)
392 {
393         struct discover_device *ddev;
394         const char *subsys;
395         const char *name;
396         int rc = 0;
397
398         name = udev_device_get_sysname(dev);
399         subsys = udev_device_get_subsystem(dev);
400
401         /* If we see a net device, check if it is ready to be used */
402         if (!strncmp(subsys, "net", strlen("net")))
403                 return udev_check_interface_ready(udev->handler, dev);
404
405         ddev = device_lookup_by_id(udev->handler, name);
406
407         /* if this is a CDROM device, process eject & media change requests;
408          * these may stop further processing */
409         if (!udev_device_get_property_value(dev, "ID_CDROM")) {
410                 if (udev_handle_cdrom_events(udev, dev, ddev))
411                         return 0;
412         }
413
414         /* if this is a new device, treat it as an add */
415         if (!ddev)
416                 rc = udev_handle_dev_add(udev, dev);
417
418         return rc;
419 }
420
421 static int udev_handle_dev_action(struct udev_device *dev, const char *action)
422 {
423         struct pb_udev *udev = udev_get_userdata(udev_device_get_udev(dev));
424         struct udev_list_entry *list;
425         const char *name;
426
427         list = udev_device_get_properties_list_entry(dev);
428         name = udev_device_get_sysname(dev);
429
430         pb_debug("udev: action %s, device %s\n", action, name);
431         pb_debug("udev: properties:\n");
432
433         for (; list; list = udev_list_entry_get_next(list))
434                 pb_debug("\t%-20s: %s\n", udev_list_entry_get_name(list),
435                                 udev_list_entry_get_value(list));
436
437         if (!strcmp(action, "add"))
438                 return udev_handle_dev_add(udev, dev);
439
440         else if (!strcmp(action, "remove"))
441                 return udev_handle_dev_remove(udev, dev);
442
443         else if (!strcmp(action, "change"))
444                 return udev_handle_dev_change(udev, dev);
445
446         return 0;
447 }
448
449 static int udev_enumerate(struct udev *udev)
450 {
451         int result;
452         struct udev_list_entry *list, *entry;
453         struct udev_enumerate *enumerate;
454
455         enumerate = udev_enumerate_new(udev);
456
457         if (!enumerate) {
458                 pb_log("udev_enumerate_new failed\n");
459                 return -1;
460         }
461
462         result = udev_enumerate_add_match_subsystem(enumerate, "block");
463         if (result) {
464                 pb_log("udev_enumerate_add_match_subsystem failed\n");
465                 goto fail;
466         }
467
468         result = udev_enumerate_add_match_subsystem(enumerate, "net");
469         if (result) {
470                 pb_log("udev_enumerate_add_match_subsystem failed\n");
471                 goto fail;
472         }
473
474         result = udev_enumerate_add_match_is_initialized(enumerate);
475         if (result) {
476                 pb_log("udev_enumerate_add_match_is_initialised failed\n");
477                 goto fail;
478         }
479
480         udev_enumerate_scan_devices(enumerate);
481
482         list = udev_enumerate_get_list_entry(enumerate);
483
484         udev_list_entry_foreach(entry, list) {
485                 const char *syspath;
486                 struct udev_device *dev;
487
488                 syspath = udev_list_entry_get_name(entry);
489                 dev = udev_device_new_from_syspath(udev, syspath);
490
491                 udev_handle_dev_action(dev, "add");
492
493                 udev_device_unref(dev);
494         }
495
496         udev_enumerate_unref(enumerate);
497         return 0;
498
499 fail:
500         udev_enumerate_unref(enumerate);
501         return -1;
502 }
503
504 static int udev_setup_monitor(struct udev *udev, struct udev_monitor **monitor)
505 {
506         int result;
507         struct udev_monitor *m;
508
509         *monitor = NULL;
510         m = udev_monitor_new_from_netlink(udev, "udev");
511
512         if (!m) {
513                 pb_log("udev_monitor_new_from_netlink failed\n");
514                 goto out_err;
515         }
516
517         result = udev_monitor_set_receive_buffer_size(m, monitor_bufsize);
518         if (result) {
519                 pb_log("udev_monitor_set_rx_bufsize(%d) failed\n",
520                         monitor_bufsize);
521         }
522
523         result = udev_monitor_filter_add_match_subsystem_devtype(m, "block",
524                 NULL);
525
526         if (result) {
527                 pb_log("udev_monitor_filter_add_match_subsystem_devtype failed\n");
528                 goto out_err;
529         }
530
531         result = udev_monitor_filter_add_match_subsystem_devtype(m, "net",
532                 NULL);
533
534         if (result) {
535                 pb_log("udev_monitor_filter_add_match_subsystem_devtype failed\n");
536                 goto out_err;
537         }
538
539         result = udev_monitor_enable_receiving(m);
540
541         if (result) {
542                 pb_log("udev_monitor_enable_receiving failed\n");
543                 goto out_err;
544         }
545
546         *monitor = m;
547         return 0;
548
549 out_err:
550         udev_monitor_unref(m);
551         return -1;
552 }
553
554 /*
555  * udev_process - waiter callback for monitor netlink.
556  */
557
558 static int udev_process(void *arg)
559 {
560         struct udev_monitor *monitor = arg;
561         struct udev_device *dev;
562         const char *action;
563
564         dev = udev_monitor_receive_device(monitor);
565         if (!dev) {
566                 pb_log("udev_monitor_receive_device failed\n");
567                 return -1;
568         }
569
570         action = udev_device_get_action(dev);
571
572         if (!action) {
573                 pb_log("udev_device_get_action failed\n");
574         } else {
575                 udev_handle_dev_action(dev, action);
576         }
577
578         udev_device_unref(dev);
579         return 0;
580 }
581
582 #ifdef UDEV_LOGGING
583 static void udev_log_fn(struct udev __attribute__((unused)) *udev,
584         int __attribute__((unused)) priority, const char *file, int line,
585         const char *fn, const char *format, va_list args)
586 {
587       pb_log("libudev: %s %s:%d: ", fn, file, line);
588       vfprintf(pb_log_get_stream(), format, args);
589 }
590 #endif
591
592 struct pb_udev *udev_init(struct device_handler *handler,
593                 struct waitset *waitset)
594 {
595         struct pb_udev *udev;
596         int result;
597
598         udev = talloc_zero(handler, struct pb_udev);
599         talloc_set_destructor(udev, udev_destructor);
600         udev->handler = handler;
601
602         udev->udev = udev_new();
603
604         if (!udev->udev) {
605                 pb_log("udev_new failed\n");
606                 goto fail;
607         }
608
609         udev_set_userdata(udev->udev, udev);
610
611 #ifdef UDEV_LOGGING
612         udev_set_log_fn(udev->udev, udev_log_fn);
613 #endif
614
615         result = udev_setup_monitor(udev->udev, &udev->monitor);
616         if (result)
617                 goto fail;
618
619         result = udev_enumerate(udev->udev);
620         if (result)
621                 goto fail;
622
623         waiter_register_io(waitset, udev_monitor_get_fd(udev->monitor), WAIT_IN,
624                 udev_process, udev->monitor);
625
626         pb_debug("%s: waiting on udev\n", __func__);
627
628         return udev;
629
630 fail:
631         talloc_free(udev);
632         return NULL;
633 }
634
635 void udev_reinit(struct pb_udev *udev)
636 {
637         pb_log("udev: reinit requested, starting enumeration\n");
638         udev_enumerate(udev->udev);
639 }