discover/discover-server: explicit #include <string.h>
[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("%s: Failed to execute vgscan\n", __func__);
85
86         if (process_run_simple(handler, pb_system_apps.vgchange, "-ay", "-qq",
87                                 NULL))
88                 pb_log("%s: Failed to execute vgchange\n", __func__);
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, *name_path, *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         name_path = udev_device_get_property_value(dev, "ID_NET_NAME_PATH");
257         ifindex = udev_device_get_property_value(dev, "IFINDEX");
258         interface = udev_device_get_property_value(dev, "INTERFACE");
259         mac_name = udev_device_get_property_value(dev, "ID_NET_NAME_MAC");
260
261         /* Physical interfaces should have all of these properties */
262         if (!name_path || !ifindex || !interface || !mac_name) {
263                 pb_debug("%s: interface %s missing properties\n",
264                                 __func__, name);
265                 return -1;
266         }
267
268         /* ID_NET_NAME_MAC format is enxMACADDR */
269         if (strlen(mac_name) < 15) {
270                 pb_debug("%s: Unexpected MAC format: %s\n",
271                                 __func__, mac_name);
272                 return -1;
273         }
274
275         mac = talloc_array(handler, uint8_t, HWADDR_SIZE);
276         if (!mac)
277                 return -1;
278
279         /*
280          * ID_NET_NAME_MAC is not a conventionally formatted MAC
281          * string - convert it before passing it to network.c
282          */
283         byte[2] = '\0';
284         for (i = strlen("enx"), j = 0;
285                         i < strlen(mac_name) && j < HWADDR_SIZE; i += 2) {
286                 memcpy(byte, &mac_name[i], 2);
287                 mac[j++] = strtoul(byte, NULL, 16);
288         }
289
290         network_mark_interface_ready(handler,
291                         atoi(ifindex), interface, mac, HWADDR_SIZE);
292
293         talloc_free(mac);
294         return 0;
295 }
296
297 static int udev_handle_dev_add(struct pb_udev *udev, struct udev_device *dev)
298 {
299         const char *subsys;
300         const char *name;
301
302         name = udev_device_get_sysname(dev);
303         if (!name) {
304                 pb_debug("udev_device_get_sysname failed\n");
305                 return -1;
306         }
307
308         subsys = udev_device_get_subsystem(dev);
309         if (!subsys) {
310                 pb_debug("udev_device_get_subsystem failed\n");
311                 return -1;
312         }
313
314         /* If we see a net device, check if it is ready to be used */
315         if (!strncmp(subsys, "net", strlen("net")))
316                 return udev_check_interface_ready(udev->handler, dev);
317
318         if (device_lookup_by_id(udev->handler, name)) {
319                 pb_debug("device %s is already present?\n", name);
320                 return -1;
321         }
322
323         if (!strcmp(subsys, "block")) {
324                 return udev_handle_block_add(udev, dev, name);
325         }
326
327         pb_log("SKIP %s: unknown subsystem %s\n", name, subsys);
328         return -1;
329 }
330
331
332 static int udev_handle_dev_remove(struct pb_udev *udev, struct udev_device *dev)
333 {
334         struct discover_device *ddev;
335         const char *name;
336
337         name = udev_device_get_sysname(dev);
338         if (!name) {
339                 pb_debug("udev_device_get_sysname failed\n");
340                 return -1;
341         }
342
343         ddev = device_lookup_by_id(udev->handler, name);
344         if (!ddev)
345                 return 0;
346
347         device_handler_remove(udev->handler, ddev);
348
349         return 0;
350 }
351
352 /* returns true if further event processing should stop (eg., we've
353  * ejected the cdrom)
354  */
355 static bool udev_handle_cdrom_events(struct pb_udev *udev,
356                 struct udev_device *dev, struct discover_device *ddev)
357 {
358         const char *node;
359         bool eject = false;
360
361         node = udev_device_get_devnode(dev);
362
363         /* handle CDROM eject requests */
364         if (udev_device_get_property_value(dev, "DISK_EJECT_REQUEST")) {
365                 pb_debug("udev: eject request\n");
366
367                 /* If the device is mounted, cdrom_id's own eject request may
368                  * have failed. So, we'll need to do our own here.
369                  */
370                 if (ddev) {
371                         eject = ddev->mounted;
372                         udev_handle_dev_remove(udev, dev);
373                 }
374
375                 if (eject)
376                         cdrom_eject(node);
377
378                 return true;
379         }
380
381         if (udev_device_get_property_value(dev, "DISK_MEDIA_CHANGE")) {
382                 if (cdrom_media_present(node))
383                         udev_handle_dev_add(udev, dev);
384                 else
385                         udev_handle_dev_remove(udev, dev);
386                 return true;
387         }
388
389         return false;
390 }
391
392 static int udev_handle_dev_change(struct pb_udev *udev, struct udev_device *dev)
393 {
394         struct discover_device *ddev;
395         const char *subsys;
396         const char *name;
397         int rc = 0;
398
399         name = udev_device_get_sysname(dev);
400         subsys = udev_device_get_subsystem(dev);
401
402         /* If we see a net device, check if it is ready to be used */
403         if (!strncmp(subsys, "net", strlen("net")))
404                 return udev_check_interface_ready(udev->handler, dev);
405
406         ddev = device_lookup_by_id(udev->handler, name);
407
408         /* if this is a CDROM device, process eject & media change requests;
409          * these may stop further processing */
410         if (!udev_device_get_property_value(dev, "ID_CDROM")) {
411                 if (udev_handle_cdrom_events(udev, dev, ddev))
412                         return 0;
413         }
414
415         /* if this is a new device, treat it as an add */
416         if (!ddev)
417                 rc = udev_handle_dev_add(udev, dev);
418
419         return rc;
420 }
421
422 static int udev_handle_dev_action(struct udev_device *dev, const char *action)
423 {
424         struct pb_udev *udev = udev_get_userdata(udev_device_get_udev(dev));
425         struct udev_list_entry *list;
426         const char *name;
427
428         list = udev_device_get_properties_list_entry(dev);
429         name = udev_device_get_sysname(dev);
430
431         pb_debug("udev: action %s, device %s\n", action, name);
432         pb_debug("udev: properties:\n");
433
434         for (; list; list = udev_list_entry_get_next(list))
435                 pb_debug("\t%-20s: %s\n", udev_list_entry_get_name(list),
436                                 udev_list_entry_get_value(list));
437
438         if (!strcmp(action, "add"))
439                 return udev_handle_dev_add(udev, dev);
440
441         else if (!strcmp(action, "remove"))
442                 return udev_handle_dev_remove(udev, dev);
443
444         else if (!strcmp(action, "change"))
445                 return udev_handle_dev_change(udev, dev);
446
447         return 0;
448 }
449
450 static int udev_enumerate(struct udev *udev)
451 {
452         int result;
453         struct udev_list_entry *list, *entry;
454         struct udev_enumerate *enumerate;
455
456         enumerate = udev_enumerate_new(udev);
457
458         if (!enumerate) {
459                 pb_log("udev_enumerate_new failed\n");
460                 return -1;
461         }
462
463         result = udev_enumerate_add_match_subsystem(enumerate, "block");
464         if (result) {
465                 pb_log("udev_enumerate_add_match_subsystem failed\n");
466                 goto fail;
467         }
468
469         result = udev_enumerate_add_match_subsystem(enumerate, "net");
470         if (result) {
471                 pb_log("udev_enumerate_add_match_subsystem failed\n");
472                 goto fail;
473         }
474
475         result = udev_enumerate_add_match_is_initialized(enumerate);
476         if (result) {
477                 pb_log("udev_enumerate_add_match_is_initialised failed\n");
478                 goto fail;
479         }
480
481         udev_enumerate_scan_devices(enumerate);
482
483         list = udev_enumerate_get_list_entry(enumerate);
484
485         udev_list_entry_foreach(entry, list) {
486                 const char *syspath;
487                 struct udev_device *dev;
488
489                 syspath = udev_list_entry_get_name(entry);
490                 dev = udev_device_new_from_syspath(udev, syspath);
491
492                 udev_handle_dev_action(dev, "add");
493
494                 udev_device_unref(dev);
495         }
496
497         udev_enumerate_unref(enumerate);
498         return 0;
499
500 fail:
501         udev_enumerate_unref(enumerate);
502         return -1;
503 }
504
505 static int udev_setup_monitor(struct udev *udev, struct udev_monitor **monitor)
506 {
507         int result;
508         struct udev_monitor *m;
509
510         *monitor = NULL;
511         m = udev_monitor_new_from_netlink(udev, "udev");
512
513         if (!m) {
514                 pb_log("udev_monitor_new_from_netlink failed\n");
515                 goto out_err;
516         }
517
518         result = udev_monitor_set_receive_buffer_size(m, monitor_bufsize);
519         if (result) {
520                 pb_log("udev_monitor_set_rx_bufsize(%d) failed\n",
521                         monitor_bufsize);
522         }
523
524         result = udev_monitor_filter_add_match_subsystem_devtype(m, "block",
525                 NULL);
526
527         if (result) {
528                 pb_log("udev_monitor_filter_add_match_subsystem_devtype failed\n");
529                 goto out_err;
530         }
531
532         result = udev_monitor_filter_add_match_subsystem_devtype(m, "net",
533                 NULL);
534
535         if (result) {
536                 pb_log("udev_monitor_filter_add_match_subsystem_devtype failed\n");
537                 goto out_err;
538         }
539
540         result = udev_monitor_enable_receiving(m);
541
542         if (result) {
543                 pb_log("udev_monitor_enable_receiving failed\n");
544                 goto out_err;
545         }
546
547         *monitor = m;
548         return 0;
549
550 out_err:
551         udev_monitor_unref(m);
552         return -1;
553 }
554
555 /*
556  * udev_process - waiter callback for monitor netlink.
557  */
558
559 static int udev_process(void *arg)
560 {
561         struct udev_monitor *monitor = arg;
562         struct udev_device *dev;
563         const char *action;
564
565         dev = udev_monitor_receive_device(monitor);
566         if (!dev) {
567                 pb_log("udev_monitor_receive_device failed\n");
568                 return -1;
569         }
570
571         action = udev_device_get_action(dev);
572
573         if (!action) {
574                 pb_log("udev_device_get_action failed\n");
575         } else {
576                 udev_handle_dev_action(dev, action);
577         }
578
579         udev_device_unref(dev);
580         return 0;
581 }
582
583 #ifdef UDEV_LOGGING
584 static void udev_log_fn(struct udev __attribute__((unused)) *udev,
585         int __attribute__((unused)) priority, const char *file, int line,
586         const char *fn, const char *format, va_list args)
587 {
588       pb_log("libudev: %s %s:%d: ", fn, file, line);
589       vfprintf(pb_log_get_stream(), format, args);
590 }
591 #endif
592
593 struct pb_udev *udev_init(struct device_handler *handler,
594                 struct waitset *waitset)
595 {
596         struct pb_udev *udev;
597         int result;
598
599         udev = talloc_zero(handler, struct pb_udev);
600         talloc_set_destructor(udev, udev_destructor);
601         udev->handler = handler;
602
603         udev->udev = udev_new();
604
605         if (!udev->udev) {
606                 pb_log("udev_new failed\n");
607                 goto fail;
608         }
609
610         udev_set_userdata(udev->udev, udev);
611
612 #ifdef UDEV_LOGGING
613         udev_set_log_fn(udev->udev, udev_log_fn);
614 #endif
615
616         result = udev_setup_monitor(udev->udev, &udev->monitor);
617         if (result)
618                 goto fail;
619
620         result = udev_enumerate(udev->udev);
621         if (result)
622                 goto fail;
623
624         waiter_register_io(waitset, udev_monitor_get_fd(udev->monitor), WAIT_IN,
625                 udev_process, udev->monitor);
626
627         pb_debug("%s: waiting on udev\n", __func__);
628
629         return udev;
630
631 fail:
632         talloc_free(udev);
633         return NULL;
634 }
635
636 void udev_reinit(struct pb_udev *udev)
637 {
638         pb_log("udev: reinit requested, starting enumeration\n");
639         udev_enumerate(udev->udev);
640 }