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