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;
45 udev_monitor_unref(udev->monitor);
50 udev_unref(udev->udev);
57 static void udev_setup_device_params(struct udev_device *udev,
58 struct discover_device *dev)
60 struct udev_list_entry *list, *entry;
62 list = udev_device_get_properties_list_entry(udev);
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));
72 static int udev_handle_block_add(struct pb_udev *udev, struct udev_device *dev,
75 struct discover_device *ddev;
84 typestr = udev_device_get_devtype(dev);
86 pb_debug("udev_device_get_devtype failed\n");
90 if (!(!strcmp(typestr, "disk") || !strcmp(typestr, "partition"))) {
91 pb_log("SKIP %s: invalid type %s\n", name, typestr);
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);
103 cdrom = node && !!udev_device_get_property_value(dev, "ID_CDROM");
105 /* CDROMs require a little initialisation, to get
106 * petitboot-compatible tray behaviour */
108 if (!cdrom_media_present(node)) {
109 pb_log("SKIP: %s: no media present\n", name);
114 type = udev_device_get_property_value(dev, "ID_FS_TYPE");
116 pb_log("SKIP: %s: no ID_FS_TYPE property\n", name);
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");
124 ddev = device_lookup_by_uuid(udev->handler, uuid);
126 pb_log("SKIP: %s UUID [%s] already present (as %s)\n",
127 name, uuid, ddev->device->id);
132 ddev = discover_device_create(udev->handler, name);
134 ddev->device_path = talloc_strdup(ddev, node);
137 ddev->uuid = talloc_strdup(ddev, uuid);
138 prop = udev_device_get_property_value(dev, "ID_FS_LABEL");
140 ddev->label = talloc_strdup(ddev, prop);
141 ddev->device->type = cdrom ? DEVICE_TYPE_OPTICAL : DEVICE_TYPE_DISK;
143 udev_setup_device_params(dev, ddev);
145 device_handler_discover(udev->handler, ddev);
150 static int udev_handle_dev_add(struct pb_udev *udev, struct udev_device *dev)
155 name = udev_device_get_sysname(dev);
157 pb_debug("udev_device_get_sysname failed\n");
161 subsys = udev_device_get_subsystem(dev);
163 pb_debug("udev_device_get_subsystem failed\n");
167 if (device_lookup_by_id(udev->handler, name)) {
168 pb_debug("device %s is already present?\n", name);
172 if (!strcmp(subsys, "block")) {
173 return udev_handle_block_add(udev, dev, name);
176 pb_log("SKIP %s: unknown subsystem %s\n", name, subsys);
181 static int udev_handle_dev_remove(struct pb_udev *udev, struct udev_device *dev)
183 struct discover_device *ddev;
186 name = udev_device_get_sysname(dev);
188 pb_debug("udev_device_get_sysname failed\n");
192 ddev = device_lookup_by_id(udev->handler, name);
196 device_handler_remove(udev->handler, ddev);
201 /* returns true if further event processing should stop (eg., we've
204 static bool udev_handle_cdrom_events(struct pb_udev *udev,
205 struct udev_device *dev, struct discover_device *ddev)
209 node = udev_device_get_devnode(dev);
211 /* handle CDROM eject requests */
212 if (udev_device_get_property_value(dev, "DISK_EJECT_REQUEST")) {
215 pb_debug("udev: eject request\n");
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.
221 eject = ddev->mounted;
222 udev_handle_dev_remove(udev, dev);
232 if (udev_device_get_property_value(dev, "DISK_MEDIA_CHANGE")) {
233 if (cdrom_media_present(node))
234 udev_handle_dev_add(udev, dev);
236 udev_handle_dev_remove(udev, dev);
243 static int udev_handle_dev_change(struct pb_udev *udev, struct udev_device *dev)
245 struct discover_device *ddev;
249 name = udev_device_get_sysname(dev);
251 ddev = device_lookup_by_id(udev->handler, name);
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))
260 /* if this is a new device, treat it as an add */
262 rc = udev_handle_dev_add(udev, dev);
267 static int udev_handle_dev_action(struct udev_device *dev, const char *action)
269 struct pb_udev *udev = udev_get_userdata(udev_device_get_udev(dev));
270 struct udev_list_entry *list;
273 list = udev_device_get_properties_list_entry(dev);
274 name = udev_device_get_sysname(dev);
276 pb_debug("udev: action %s, device %s\n", action, name);
277 pb_debug("udev: properties:\n");
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));
283 if (!strcmp(action, "add"))
284 return udev_handle_dev_add(udev, dev);
286 else if (!strcmp(action, "remove"))
287 return udev_handle_dev_remove(udev, dev);
289 else if (!strcmp(action, "change"))
290 return udev_handle_dev_change(udev, dev);
295 static int udev_enumerate(struct udev *udev)
298 struct udev_list_entry *list, *entry;
299 struct udev_enumerate *enumerate;
301 enumerate = udev_enumerate_new(udev);
304 pb_log("udev_enumerate_new failed\n");
308 result = udev_enumerate_add_match_subsystem(enumerate, "block");
310 pb_log("udev_enumerate_add_match_subsystem failed\n");
314 result = udev_enumerate_add_match_is_initialized(enumerate);
316 pb_log("udev_enumerate_add_match_is_initialised failed\n");
320 udev_enumerate_scan_devices(enumerate);
322 list = udev_enumerate_get_list_entry(enumerate);
324 udev_list_entry_foreach(entry, list) {
326 struct udev_device *dev;
328 syspath = udev_list_entry_get_name(entry);
329 dev = udev_device_new_from_syspath(udev, syspath);
331 udev_handle_dev_action(dev, "add");
333 udev_device_unref(dev);
336 udev_enumerate_unref(enumerate);
340 udev_enumerate_unref(enumerate);
344 static int udev_setup_monitor(struct udev *udev, struct udev_monitor **monitor)
347 struct udev_monitor *m;
350 m = udev_monitor_new_from_netlink(udev, "udev");
353 pb_log("udev_monitor_new_from_netlink failed\n");
357 result = udev_monitor_set_receive_buffer_size(m, monitor_bufsize);
359 pb_log("udev_monitor_set_rx_bufsize(%d) failed\n",
363 result = udev_monitor_filter_add_match_subsystem_devtype(m, "block",
367 pb_log("udev_monitor_filter_add_match_subsystem_devtype failed\n");
371 result = udev_monitor_enable_receiving(m);
374 pb_log("udev_monitor_enable_receiving failed\n");
382 udev_monitor_unref(m);
387 * udev_process - waiter callback for monitor netlink.
390 static int udev_process(void *arg)
392 struct udev_monitor *monitor = arg;
393 struct udev_device *dev;
396 dev = udev_monitor_receive_device(monitor);
398 pb_log("udev_monitor_receive_device failed\n");
402 action = udev_device_get_action(dev);
405 pb_log("udev_device_get_action failed\n");
407 udev_handle_dev_action(dev, action);
410 udev_device_unref(dev);
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)
418 pb_log("libudev: %s %s:%d: ", fn, file, line);
419 vfprintf(pb_log_get_stream(), format, args);
422 struct pb_udev *udev_init(struct device_handler *handler,
423 struct waitset *waitset)
425 struct pb_udev *udev;
428 udev = talloc_zero(handler, struct pb_udev);
429 talloc_set_destructor(udev, udev_destructor);
430 udev->handler = handler;
432 udev->udev = udev_new();
435 pb_log("udev_new failed\n");
439 udev_set_userdata(udev->udev, udev);
441 udev_set_log_fn(udev->udev, udev_log_fn);
443 result = udev_setup_monitor(udev->udev, &udev->monitor);
447 result = udev_enumerate(udev->udev);
451 waiter_register_io(waitset, udev_monitor_get_fd(udev->monitor), WAIT_IN,
452 udev_process, udev->monitor);
454 pb_debug("%s: waiting on udev\n", __func__);
463 void udev_reinit(struct pb_udev *udev)
465 pb_log("udev: reinit requested, starting enumeration\n");
466 udev_enumerate(udev->udev);