2 #if defined(HAVE_CONFIG_H)
13 #include <sys/socket.h>
14 #include <sys/types.h>
18 #include <types/types.h>
19 #include <talloc/talloc.h>
20 #include <waiter/waiter.h>
21 #include <system/system.h>
25 #include "pb-discover.h"
26 #include "device-handler.h"
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;
36 struct udev_monitor *monitor;
37 struct device_handler *handler;
40 static int udev_destructor(void *p)
42 struct pb_udev *udev = p;
44 udev_monitor_unref(udev->monitor);
47 udev_unref(udev->udev);
53 static void udev_setup_device_params(struct udev_device *udev,
54 struct discover_device *dev)
56 struct udev_list_entry *list, *entry;
58 list = udev_device_get_properties_list_entry(udev);
62 udev_list_entry_foreach(entry, list)
63 discover_device_set_param(dev,
64 udev_list_entry_get_name(entry),
65 udev_list_entry_get_value(entry));
68 static int udev_handle_block_add(struct pb_udev *udev, struct udev_device *dev,
71 struct discover_device *ddev;
80 typestr = udev_device_get_devtype(dev);
82 pb_debug("udev_device_get_devtype failed\n");
86 if (!(!strcmp(typestr, "disk") || !strcmp(typestr, "partition"))) {
87 pb_log("SKIP %s: invalid type %s\n", name, typestr);
91 node = udev_device_get_devnode(dev);
92 path = udev_device_get_devpath(dev);
93 if (path && (strstr(path, "virtual/block/loop")
94 || strstr(path, "virtual/block/ram"))) {
95 pb_log("SKIP: %s: ignored (path=%s)\n", name, path);
99 cdrom = node && !!udev_device_get_property_value(dev, "ID_CDROM");
101 /* CDROMs require a little initialisation, to get
102 * petitboot-compatible tray behaviour */
104 if (!cdrom_media_present(node)) {
105 pb_log("SKIP: %s: no media present\n", name);
110 type = udev_device_get_property_value(dev, "ID_FS_TYPE");
112 pb_log("SKIP: %s: no ID_FS_TYPE property\n", name);
116 /* We may see multipath devices; they'll have the same uuid as an
117 * existing device, so only parse the first. */
118 uuid = udev_device_get_property_value(dev, "ID_FS_UUID");
120 ddev = device_lookup_by_uuid(udev->handler, uuid);
122 pb_log("SKIP: %s UUID [%s] already present (as %s)\n",
123 name, uuid, ddev->device->id);
128 ddev = discover_device_create(udev->handler, name);
130 ddev->device_path = talloc_strdup(ddev, node);
133 ddev->uuid = talloc_strdup(ddev, uuid);
134 prop = udev_device_get_property_value(dev, "ID_FS_LABEL");
136 ddev->label = talloc_strdup(ddev, prop);
137 ddev->device->type = cdrom ? DEVICE_TYPE_OPTICAL : DEVICE_TYPE_DISK;
139 udev_setup_device_params(dev, ddev);
141 device_handler_discover(udev->handler, ddev);
146 static int udev_handle_dev_add(struct pb_udev *udev, struct udev_device *dev)
151 name = udev_device_get_sysname(dev);
153 pb_debug("udev_device_get_sysname failed\n");
157 subsys = udev_device_get_subsystem(dev);
159 pb_debug("udev_device_get_subsystem failed\n");
163 if (device_lookup_by_id(udev->handler, name)) {
164 pb_debug("device %s is already present?\n", name);
168 if (!strcmp(subsys, "block")) {
169 return udev_handle_block_add(udev, dev, name);
172 pb_log("SKIP %s: unknown subsystem %s\n", name, subsys);
177 static int udev_handle_dev_remove(struct pb_udev *udev, struct udev_device *dev)
179 struct discover_device *ddev;
182 name = udev_device_get_sysname(dev);
184 pb_debug("udev_device_get_sysname failed\n");
188 ddev = device_lookup_by_id(udev->handler, name);
192 device_handler_remove(udev->handler, ddev);
197 /* returns true if further event processing should stop (eg., we've
200 static bool udev_handle_cdrom_events(struct pb_udev *udev,
201 struct udev_device *dev, struct discover_device *ddev)
205 node = udev_device_get_devnode(dev);
207 /* handle CDROM eject requests */
208 if (udev_device_get_property_value(dev, "DISK_EJECT_REQUEST")) {
211 pb_debug("udev: eject request\n");
213 /* If the device is mounted, cdrom_id's own eject request may
214 * have failed. So, we'll need to do our own here.
217 eject = ddev->mounted;
218 udev_handle_dev_remove(udev, dev);
228 if (udev_device_get_property_value(dev, "DISK_MEDIA_CHANGE")) {
229 if (cdrom_media_present(node))
230 udev_handle_dev_add(udev, dev);
232 udev_handle_dev_remove(udev, dev);
239 static int udev_handle_dev_change(struct pb_udev *udev, struct udev_device *dev)
241 struct discover_device *ddev;
245 name = udev_device_get_sysname(dev);
247 ddev = device_lookup_by_id(udev->handler, name);
249 /* if this is a CDROM device, process eject & media change requests;
250 * these may stop further processing */
251 if (!udev_device_get_property_value(dev, "ID_CDROM")) {
252 if (udev_handle_cdrom_events(udev, dev, ddev))
256 /* if this is a new device, treat it as an add */
258 rc = udev_handle_dev_add(udev, dev);
263 static int udev_handle_dev_action(struct udev_device *dev, const char *action)
265 struct pb_udev *udev = udev_get_userdata(udev_device_get_udev(dev));
269 struct udev_list_entry *list;
272 list = udev_device_get_properties_list_entry(dev);
273 name = udev_device_get_sysname(dev);
275 pb_debug("%s: action %s, device %s\n", __func__, action, name);
276 pb_debug("%s properties:\n", __func__);
278 for (; list; list = udev_list_entry_get_next(list))
279 pb_log("\t%-20s: %s\n", udev_list_entry_get_name(list),
280 udev_list_entry_get_value(list));
284 if (!strcmp(action, "add"))
285 return udev_handle_dev_add(udev, dev);
287 else if (!strcmp(action, "remove"))
288 return udev_handle_dev_remove(udev, dev);
290 else if (!strcmp(action, "change"))
291 return udev_handle_dev_change(udev, dev);
296 static int udev_enumerate(struct udev *udev)
299 struct udev_list_entry *list, *entry;
300 struct udev_enumerate *enumerate;
302 enumerate = udev_enumerate_new(udev);
305 pb_log("udev_enumerate_new failed\n");
309 result = udev_enumerate_add_match_subsystem(enumerate, "block");
311 pb_log("udev_enumerate_add_match_subsystem failed\n");
315 result = udev_enumerate_add_match_is_initialized(enumerate);
317 pb_log("udev_enumerate_add_match_is_initialised failed\n");
321 udev_enumerate_scan_devices(enumerate);
323 list = udev_enumerate_get_list_entry(enumerate);
326 pb_log("udev_enumerate_get_list_entry failed\n");
330 udev_list_entry_foreach(entry, list) {
332 struct udev_device *dev;
334 syspath = udev_list_entry_get_name(entry);
335 dev = udev_device_new_from_syspath(udev, syspath);
337 udev_handle_dev_action(dev, "add");
339 udev_device_unref(dev);
342 udev_enumerate_unref(enumerate);
346 udev_enumerate_unref(enumerate);
350 static int udev_setup_monitor(struct udev *udev, struct udev_monitor **monitor)
353 struct udev_monitor *m;
356 m = udev_monitor_new_from_netlink(udev, "udev");
359 pb_log("udev_monitor_new_from_netlink failed\n");
363 result = udev_monitor_set_receive_buffer_size(m, monitor_bufsize);
365 pb_log("udev_monitor_set_rx_bufsize(%d) failed\n",
369 result = udev_monitor_filter_add_match_subsystem_devtype(m, "block",
373 pb_log("udev_monitor_filter_add_match_subsystem_devtype failed\n");
377 result = udev_monitor_enable_receiving(m);
380 pb_log("udev_monitor_enable_receiving failed\n");
388 udev_monitor_unref(m);
393 * udev_process - waiter callback for monitor netlink.
396 static int udev_process(void *arg)
398 struct udev_monitor *monitor = arg;
399 struct udev_device *dev;
402 dev = udev_monitor_receive_device(monitor);
404 pb_log("udev_monitor_receive_device failed\n");
408 action = udev_device_get_action(dev);
411 pb_log("udev_device_get_action failed\n");
413 udev_handle_dev_action(dev, action);
416 udev_device_unref(dev);
420 static void udev_log_fn(struct udev __attribute__((unused)) *udev,
421 int __attribute__((unused)) priority, const char *file, int line,
422 const char *fn, const char *format, va_list args)
424 pb_log("libudev: %s %s:%d: ", fn, file, line);
425 vfprintf(pb_log_get_stream(), format, args);
428 struct pb_udev *udev_init(struct waitset *waitset,
429 struct device_handler *handler)
432 struct pb_udev *udev = talloc(NULL, struct pb_udev);
434 talloc_set_destructor(udev, udev_destructor);
435 udev->handler = handler;
437 udev->udev = udev_new();
440 pb_log("udev_new failed\n");
444 udev_set_userdata(udev->udev, udev);
446 udev_set_log_fn(udev->udev, udev_log_fn);
448 result = udev_setup_monitor(udev->udev, &udev->monitor);
452 result = udev_enumerate(udev->udev);
456 waiter_register_io(waitset, udev_monitor_get_fd(udev->monitor), WAIT_IN,
457 udev_process, udev->monitor);
459 pb_debug("%s: waiting on udev\n", __func__);
464 udev_monitor_unref(udev->monitor);
466 udev_unref(udev->udev);
472 void udev_destroy(struct pb_udev *udev)