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