discover/udev: Additional checks in udev_handle_block_add
[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;
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         ddev->device->type = cdrom ? DEVICE_TYPE_OPTICAL : DEVICE_TYPE_DISK;
171
172         udev_setup_device_params(dev, ddev);
173
174         /* Create a snapshot for all disks, unless it is an assembled RAID array */
175         if (ddev->device->type == DEVICE_TYPE_DISK &&
176             !udev_device_get_property_value(dev, "MD_LEVEL"))
177                 devmapper_init_snapshot(udev->handler, ddev);
178
179         device_handler_discover(udev->handler, ddev);
180
181         return 0;
182 }
183
184 static int udev_handle_dev_add(struct pb_udev *udev, struct udev_device *dev)
185 {
186         const char *subsys;
187         const char *name;
188
189         name = udev_device_get_sysname(dev);
190         if (!name) {
191                 pb_debug("udev_device_get_sysname failed\n");
192                 return -1;
193         }
194
195         subsys = udev_device_get_subsystem(dev);
196         if (!subsys) {
197                 pb_debug("udev_device_get_subsystem failed\n");
198                 return -1;
199         }
200
201         if (device_lookup_by_id(udev->handler, name)) {
202                 pb_debug("device %s is already present?\n", name);
203                 return -1;
204         }
205
206         if (!strcmp(subsys, "block")) {
207                 return udev_handle_block_add(udev, dev, name);
208         }
209
210         pb_log("SKIP %s: unknown subsystem %s\n", name, subsys);
211         return -1;
212 }
213
214
215 static int udev_handle_dev_remove(struct pb_udev *udev, struct udev_device *dev)
216 {
217         struct discover_device *ddev;
218         const char *name;
219
220         name = udev_device_get_sysname(dev);
221         if (!name) {
222                 pb_debug("udev_device_get_sysname failed\n");
223                 return -1;
224         }
225
226         ddev = device_lookup_by_id(udev->handler, name);
227         if (!ddev)
228                 return 0;
229
230         device_handler_remove(udev->handler, ddev);
231
232         return 0;
233 }
234
235 /* returns true if further event processing should stop (eg., we've
236  * ejected the cdrom)
237  */
238 static bool udev_handle_cdrom_events(struct pb_udev *udev,
239                 struct udev_device *dev, struct discover_device *ddev)
240 {
241         const char *node;
242
243         node = udev_device_get_devnode(dev);
244
245         /* handle CDROM eject requests */
246         if (udev_device_get_property_value(dev, "DISK_EJECT_REQUEST")) {
247                 bool eject = false;
248
249                 pb_debug("udev: eject request\n");
250
251                 /* If the device is mounted, cdrom_id's own eject request may
252                  * have failed. So, we'll need to do our own here.
253                  */
254                 if (ddev) {
255                         eject = ddev->mounted;
256                         udev_handle_dev_remove(udev, dev);
257                         return false;
258                 }
259
260                 if (eject)
261                         cdrom_eject(node);
262
263                 return true;
264         }
265
266         if (udev_device_get_property_value(dev, "DISK_MEDIA_CHANGE")) {
267                 if (cdrom_media_present(node))
268                         udev_handle_dev_add(udev, dev);
269                 else
270                         udev_handle_dev_remove(udev, dev);
271                 return true;
272         }
273
274         return false;
275 }
276
277 static int udev_handle_dev_change(struct pb_udev *udev, struct udev_device *dev)
278 {
279         struct discover_device *ddev;
280         const char *name;
281         int rc = 0;
282
283         name = udev_device_get_sysname(dev);
284
285         ddev = device_lookup_by_id(udev->handler, name);
286
287         /* if this is a CDROM device, process eject & media change requests;
288          * these may stop further processing */
289         if (!udev_device_get_property_value(dev, "ID_CDROM")) {
290                 if (udev_handle_cdrom_events(udev, dev, ddev))
291                         return 0;
292         }
293
294         /* if this is a new device, treat it as an add */
295         if (!ddev)
296                 rc = udev_handle_dev_add(udev, dev);
297
298         return rc;
299 }
300
301 static int udev_handle_dev_action(struct udev_device *dev, const char *action)
302 {
303         struct pb_udev *udev = udev_get_userdata(udev_device_get_udev(dev));
304         struct udev_list_entry *list;
305         const char *name;
306
307         list = udev_device_get_properties_list_entry(dev);
308         name = udev_device_get_sysname(dev);
309
310         pb_debug("udev: action %s, device %s\n", action, name);
311         pb_debug("udev: properties:\n");
312
313         for (; list; list = udev_list_entry_get_next(list))
314                 pb_debug("\t%-20s: %s\n", udev_list_entry_get_name(list),
315                                 udev_list_entry_get_value(list));
316
317         if (!strcmp(action, "add"))
318                 return udev_handle_dev_add(udev, dev);
319
320         else if (!strcmp(action, "remove"))
321                 return udev_handle_dev_remove(udev, dev);
322
323         else if (!strcmp(action, "change"))
324                 return udev_handle_dev_change(udev, dev);
325
326         return 0;
327 }
328
329 static int udev_enumerate(struct udev *udev)
330 {
331         int result;
332         struct udev_list_entry *list, *entry;
333         struct udev_enumerate *enumerate;
334
335         enumerate = udev_enumerate_new(udev);
336
337         if (!enumerate) {
338                 pb_log("udev_enumerate_new failed\n");
339                 return -1;
340         }
341
342         result = udev_enumerate_add_match_subsystem(enumerate, "block");
343         if (result) {
344                 pb_log("udev_enumerate_add_match_subsystem failed\n");
345                 goto fail;
346         }
347
348         result = udev_enumerate_add_match_is_initialized(enumerate);
349         if (result) {
350                 pb_log("udev_enumerate_add_match_is_initialised failed\n");
351                 goto fail;
352         }
353
354         udev_enumerate_scan_devices(enumerate);
355
356         list = udev_enumerate_get_list_entry(enumerate);
357
358         udev_list_entry_foreach(entry, list) {
359                 const char *syspath;
360                 struct udev_device *dev;
361
362                 syspath = udev_list_entry_get_name(entry);
363                 dev = udev_device_new_from_syspath(udev, syspath);
364
365                 udev_handle_dev_action(dev, "add");
366
367                 udev_device_unref(dev);
368         }
369
370         udev_enumerate_unref(enumerate);
371         return 0;
372
373 fail:
374         udev_enumerate_unref(enumerate);
375         return -1;
376 }
377
378 static int udev_setup_monitor(struct udev *udev, struct udev_monitor **monitor)
379 {
380         int result;
381         struct udev_monitor *m;
382
383         *monitor = NULL;
384         m = udev_monitor_new_from_netlink(udev, "udev");
385
386         if (!m) {
387                 pb_log("udev_monitor_new_from_netlink failed\n");
388                 goto out_err;
389         }
390
391         result = udev_monitor_set_receive_buffer_size(m, monitor_bufsize);
392         if (result) {
393                 pb_log("udev_monitor_set_rx_bufsize(%d) failed\n",
394                         monitor_bufsize);
395         }
396
397         result = udev_monitor_filter_add_match_subsystem_devtype(m, "block",
398                 NULL);
399
400         if (result) {
401                 pb_log("udev_monitor_filter_add_match_subsystem_devtype failed\n");
402                 goto out_err;
403         }
404
405         result = udev_monitor_enable_receiving(m);
406
407         if (result) {
408                 pb_log("udev_monitor_enable_receiving failed\n");
409                 goto out_err;
410         }
411
412         *monitor = m;
413         return 0;
414
415 out_err:
416         udev_monitor_unref(m);
417         return -1;
418 }
419
420 /*
421  * udev_process - waiter callback for monitor netlink.
422  */
423
424 static int udev_process(void *arg)
425 {
426         struct udev_monitor *monitor = arg;
427         struct udev_device *dev;
428         const char *action;
429
430         dev = udev_monitor_receive_device(monitor);
431         if (!dev) {
432                 pb_log("udev_monitor_receive_device failed\n");
433                 return -1;
434         }
435
436         action = udev_device_get_action(dev);
437
438         if (!action) {
439                 pb_log("udev_device_get_action failed\n");
440         } else {
441                 udev_handle_dev_action(dev, action);
442         }
443
444         udev_device_unref(dev);
445         return 0;
446 }
447
448 static void udev_log_fn(struct udev __attribute__((unused)) *udev,
449         int __attribute__((unused)) priority, const char *file, int line,
450         const char *fn, const char *format, va_list args)
451 {
452       pb_log("libudev: %s %s:%d: ", fn, file, line);
453       vfprintf(pb_log_get_stream(), format, args);
454 }
455
456 struct pb_udev *udev_init(struct device_handler *handler,
457                 struct waitset *waitset)
458 {
459         struct pb_udev *udev;
460         int result;
461
462         udev = talloc_zero(handler, struct pb_udev);
463         talloc_set_destructor(udev, udev_destructor);
464         udev->handler = handler;
465
466         udev->udev = udev_new();
467
468         if (!udev->udev) {
469                 pb_log("udev_new failed\n");
470                 goto fail;
471         }
472
473         udev_set_userdata(udev->udev, udev);
474
475         udev_set_log_fn(udev->udev, udev_log_fn);
476
477         result = udev_setup_monitor(udev->udev, &udev->monitor);
478         if (result)
479                 goto fail;
480
481         result = udev_enumerate(udev->udev);
482         if (result)
483                 goto fail;
484
485         waiter_register_io(waitset, udev_monitor_get_fd(udev->monitor), WAIT_IN,
486                 udev_process, udev->monitor);
487
488         pb_debug("%s: waiting on udev\n", __func__);
489
490         return udev;
491
492 fail:
493         talloc_free(udev);
494         return NULL;
495 }
496
497 void udev_reinit(struct pb_udev *udev)
498 {
499         pb_log("udev: reinit requested, starting enumeration\n");
500         udev_enumerate(udev->udev);
501 }