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