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