discover: Change parsers to explicitly request configuration files
[petitboot] / discover / udev.c
index 9d628407ca761bd3894c29ad122d6cffbf2fa974..48ab745b4f371ac410048dc3ab7b93f9dc3ff4fb 100644 (file)
 
 #define _GNU_SOURCE
 
-#include <stdlib.h>
+#include <assert.h>
+#include <errno.h>
+#include <libudev.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
-#include <errno.h>
-#include <sys/types.h>
 #include <sys/socket.h>
+#include <sys/types.h>
 #include <sys/un.h>
 
+#include <log/log.h>
+#include <types/types.h>
 #include <talloc/talloc.h>
 #include <waiter/waiter.h>
-#include <log/log.h>
+#include <system/system.h>
 
+#include "event.h"
 #include "udev.h"
 #include "pb-discover.h"
 #include "device-handler.h"
 
-#define PBOOT_DEVICE_SOCKET "/tmp/petitboot.udev"
-
-#define max(a, b) ((a) > (b) ? (a) : (b))
-
-struct udev {
+struct pb_udev {
+       struct udev *udev;
+       struct udev_monitor *monitor;
        struct device_handler *handler;
-       int socket;
 };
 
-static void parse_event_params(struct udev_event *event, char *buf, int len)
+static int udev_destructor(void *p)
 {
-       int param_len, name_len, value_len;
-       struct param *param;
-       char *sep;
+       struct pb_udev *udev = p;
 
-       for (; len > 0; len -= param_len + 1, buf += param_len + 1) {
+       udev_monitor_unref(udev->monitor);
+       udev->monitor = NULL;
 
-               /* find the length of the whole parameter */
-               param_len = strnlen(buf, len);
-               if (!param_len) {
-                       /* multiple NULs? skip over */
-                       param_len = 1;
-                       continue;
-               }
+       udev_unref(udev->udev);
+       udev->udev = NULL;
 
-               /* find the separator */
-               sep = memchr(buf, '=', param_len);
-               if (!sep)
-                       continue;
+       return 0;
+}
 
-               name_len = sep - buf;
-               value_len = param_len - name_len - 1;
+static void udev_setup_device_params(struct udev_device *udev,
+               struct discover_device *dev)
+{
+       struct udev_list_entry *list, *entry;
 
-               /* update the params array */
-               event->params = talloc_realloc(event, event->params,
-                                       struct param, ++event->n_params);
-               param = &event->params[event->n_params - 1];
+       list = udev_device_get_properties_list_entry(udev);
+       if (!list)
+               return;
 
-               param->name = talloc_strndup(event, buf, name_len);
-               param->value = talloc_strndup(event, sep + 1, value_len);
-       }
+       udev_list_entry_foreach(entry, list)
+               discover_device_set_param(dev,
+                               udev_list_entry_get_name(entry),
+                               udev_list_entry_get_value(entry));
 }
 
-const char *udev_event_param(struct udev_event *event, const char *name)
+static int udev_handle_dev_add(struct pb_udev *udev, struct udev_device *dev)
 {
-       int i;
+       struct discover_device *ddev;
+       const char *typestr;
+       const char *serial;
+       const char *path;
+       const char *name;
+       const char *prop;
+
+       name = udev_device_get_sysname(dev);
+       if (!name) {
+               pb_debug("udev_device_get_sysname failed\n");
+               return -1;
+       }
 
-       for (i = 0; i < event->n_params; i++)
-               if (!strcasecmp(event->params[i].name, name))
-                       return event->params[i].value;
+       typestr = udev_device_get_devtype(dev);
+       if (!typestr) {
+               pb_debug("udev_device_get_devtype failed\n");
+               return -1;
+       }
 
-       return NULL;
+       if (!(!strcmp(typestr, "disk") || !strcmp(typestr, "partition"))) {
+               pb_debug("SKIP %s: invalid type %s\n", name, typestr);
+               return 0;
+       }
+
+       path = udev_device_get_devpath(dev);
+       if (path && (strstr(path, "virtual/block/loop")
+                       || strstr(path, "virtual/block/ram"))) {
+               pb_debug("SKIP: %s: ignored (path=%s)\n", name, path);
+               return 0;
+       }
+
+       /* We have enough info to create the device and start discovery */
+       ddev = device_lookup_by_id(udev->handler, name);
+       if (ddev) {
+               pb_debug("device %s is already present?\n", name);
+               return -1;
+       }
+
+       /* we may also see multipath devices; same dev nodes (hence id), but
+        * different serial numbers */
+       serial = udev_device_get_property_value(dev, "ID_SERIAL");
+       if (serial && device_lookup_by_serial(udev->handler, serial))
+               return -1;
+
+       ddev = discover_device_create(udev->handler, name);
+
+       ddev->device_path = udev_device_get_devnode(dev);
+       prop = udev_device_get_property_value(dev, "ID_FS_UUID");
+       if (prop)
+               ddev->uuid = talloc_strdup(ddev, prop);
+       prop = udev_device_get_property_value(dev, "ID_FS_LABEL");
+       if (prop)
+               ddev->label = talloc_strdup(ddev, prop);
+       ddev->device->type = DEVICE_TYPE_DISK;
+
+       udev_setup_device_params(dev, ddev);
+
+       device_handler_discover(udev->handler, ddev);
+
+       return 0;
 }
 
-static void print_event(struct udev_event *event)
+static int udev_handle_dev_remove(struct pb_udev *udev, struct udev_device *dev)
 {
-       const char *action, *params[] = {
-               "DEVNAME", "ID_TYPE", "ID_BUS", "ID_FS_UUID", "ID_FS_LABEL",
-               NULL,
-       };
-       int i;
+       struct discover_device *ddev;
+       const char *name;
+
+       name = udev_device_get_sysname(dev);
+       if (!name) {
+               pb_debug("udev_device_get_sysname failed\n");
+               return -1;
+       }
+
+       ddev = device_lookup_by_id(udev->handler, name);
+       if (!ddev)
+               return 0;
 
-       action = event->action == UDEV_ACTION_ADD ? "add" : "remove";
+       device_handler_remove(udev->handler, ddev);
 
-       pb_log("udev %s event:\n", action);
-       printf("\tdevice: %s\n", event->device);
+       return 0;
+}
+static int udev_handle_dev_action(struct udev_device *dev, const char *action)
+{
+       struct pb_udev *udev = udev_get_userdata(udev_device_get_udev(dev));
 
-       for (i = 0; params[i]; i++)
-               printf("\t%-12s => %s\n",
-                               params[i], udev_event_param(event, params[i]));
+       if (!strcmp(action, "add"))
+               return udev_handle_dev_add(udev, dev);
 
+       else if (!strcmp(action, "remove"))
+               return udev_handle_dev_remove(udev, dev);
+
+       return 0;
 }
 
-static void handle_udev_message(struct udev *udev, char *buf, int len)
+static int udev_enumerate(struct udev *udev)
 {
-       char *sep, *device;
-       enum udev_action action;
-       struct udev_event *event;
-       int device_len;
-
-       /* we should see an <action>@<device>\0 at the head of the buffer */
-       sep = strchr(buf, '@');
-       if (!sep)
-               return;
+       int result;
+       struct udev_list_entry *list, *entry;
+       struct udev_enumerate *enumerate;
 
-       /* terminate the action string */
-       *sep = '\0';
-       len -= sep - buf + 1;
+       enumerate = udev_enumerate_new(udev);
 
-       if (!strcmp(buf, "add")) {
-               action = UDEV_ACTION_ADD;
+       if (!enumerate) {
+               pb_log("udev_enumerate_new failed\n");
+               return -1;
+       }
 
-       } else if (!strcmp(buf, "remove")) {
-               action = UDEV_ACTION_REMOVE;
+       result = udev_enumerate_add_match_subsystem(enumerate, "block");
 
-       } else {
-               return;
+       if (result) {
+               pb_log("udev_enumerate_add_match_subsystem failed\n");
+               goto fail;
        }
 
-       /* initialise the device string */
-       device = sep + 1;
-       device_len = strnlen(device, len);
-       if (!device_len)
-               return;
+       udev_enumerate_scan_devices(enumerate);
+
+       list = udev_enumerate_get_list_entry(enumerate);
+
+       if (!list) {
+               pb_log("udev_enumerate_get_list_entry failed\n");
+               goto fail;
+       }
 
-       /* now we have an action and a device, we can construct an event */
-       event = talloc(udev, struct udev_event);
-       event->action = action;
-       event->device = talloc_strndup(event, device, device_len);
-       event->n_params = 0;
-       event->params = NULL;
+       udev_list_entry_foreach(entry, list) {
+               const char *syspath;
+               struct udev_device *dev;
 
-       len -= device_len + 1;
-       parse_event_params(event, device + device_len + 1, len);
+               syspath = udev_list_entry_get_name(entry);
+               dev = udev_device_new_from_syspath(udev, syspath);
 
-       print_event(event);
+               udev_handle_dev_action(dev, "add");
 
-       device_handler_event(udev->handler, event);
+               udev_device_unref(dev);
+       }
 
-       talloc_free(event);
+       udev_enumerate_unref(enumerate);
+       return 0;
 
-       return;
+fail:
+       udev_enumerate_unref(enumerate);
+       return -1;
 }
 
-static int udev_process(void *arg)
+static int udev_setup_monitor(struct udev *udev, struct udev_monitor **monitor)
 {
-       struct udev *udev = arg;
-       char buf[4096];
-       int len;
+       int result;
+       struct udev_monitor *m;
 
-       len = recvfrom(udev->socket, buf, sizeof(buf), 0, NULL, NULL);
+       *monitor = NULL;
+       m = udev_monitor_new_from_netlink(udev, "udev");
 
-       if (len < 0) {
-               pb_log("udev socket read failed: %s", strerror(errno));
-               return -1;
+       if (!m) {
+               pb_log("udev_monitor_new_from_netlink failed\n");
+               goto out_err;
        }
 
-       if (len == 0)
-               return 0;
+       result = udev_monitor_filter_add_match_subsystem_devtype(m, "block",
+               NULL);
+
+       if (result) {
+               pb_log("udev_monitor_filter_add_match_subsystem_devtype failed\n");
+               goto out_err;
+       }
+
+       result = udev_monitor_enable_receiving(m);
 
-       handle_udev_message(udev, buf, len);
+       if (result) {
+               pb_log("udev_monitor_enable_receiving failed\n");
+               goto out_err;
+       }
 
+       *monitor = m;
        return 0;
+
+out_err:
+       udev_monitor_unref(m);
+       return -1;
 }
 
-static int udev_destructor(void *p)
+/*
+ * udev_process - waiter callback for monitor netlink.
+ */
+
+static int udev_process(void *arg)
 {
-       struct udev *udev = p;
+       struct udev_monitor *monitor = arg;
+       struct udev_device *dev;
+       const char *action;
+       int result;
 
-       if (udev->socket >= 0)
-               close(udev->socket);
+       dev = udev_monitor_receive_device(monitor);
 
-       return 0;
+       if (!dev) {
+               pb_log("udev_monitor_receive_device failed\n");
+               return -1;
+       }
+
+       action = udev_device_get_action(dev);
+
+       if (!action) {
+               pb_log("udev_device_get_action failed\n");
+               goto fail;
+       }
+
+       result = udev_handle_dev_action(dev, action);
+
+       udev_device_unref(dev);
+       return result;
+
+fail:
+       udev_device_unref(dev);
+       return -1;
 }
 
-struct udev *udev_init(struct device_handler *handler)
+static void udev_log_fn(struct udev __attribute__((unused)) *udev,
+       int __attribute__((unused)) priority, const char *file, int line,
+       const char *fn, const char *format, va_list args)
 {
-       struct sockaddr_un addr;
-       struct udev *udev;
-
-       unlink(PBOOT_DEVICE_SOCKET);
+      pb_log("libudev: %s %s:%d: ", fn, file, line);
+      vfprintf(pb_log_get_stream(), format, args);
+}
 
-       udev = talloc(NULL, struct udev);
+struct pb_udev *udev_init(struct waitset *waitset,
+       struct device_handler *handler)
+{
+       int result;
+       struct pb_udev *udev = talloc(NULL, struct pb_udev);
 
+       talloc_set_destructor(udev, udev_destructor);
        udev->handler = handler;
 
-       udev->socket = socket(PF_UNIX, SOCK_DGRAM, 0);
-       if (udev->socket < 0) {
-               pb_log("Error creating udev socket: %s\n", strerror(errno));
-               goto out_err;
+       udev->udev = udev_new();
+
+       if (!udev->udev) {
+               pb_log("udev_new failed\n");
+               goto fail_new;
        }
 
-       talloc_set_destructor(udev, udev_destructor);
+       udev_set_userdata(udev->udev, udev);
 
-       addr.sun_family = AF_UNIX;
-       strcpy(addr.sun_path, PBOOT_DEVICE_SOCKET);
+       udev_set_log_fn(udev->udev, udev_log_fn);
 
-       if (bind(udev->socket, (struct sockaddr *)&addr, sizeof(addr))) {
-               pb_log("Error binding udev socket: %s\n", strerror(errno));
-               goto out_err;
-       }
+       result = udev_enumerate(udev->udev);
+
+       if (result)
+               goto fail_enumerate;
+
+       result = udev_setup_monitor(udev->udev, &udev->monitor);
 
-       waiter_register(udev->socket, WAIT_IN, udev_process, udev);
+       if (result)
+               goto fail_monitor;
+
+       waiter_register_io(waitset, udev_monitor_get_fd(udev->monitor), WAIT_IN,
+               udev_process, udev->monitor);
+
+       pb_debug("%s: waiting on udev\n", __func__);
 
        return udev;
 
-out_err:
+fail_monitor:
+fail_enumerate:
+       udev_unref(udev->udev);
+fail_new:
        talloc_free(udev);
        return NULL;
 }
 
-void udev_destroy(struct udev *udev)
+void udev_destroy(struct pb_udev *udev)
 {
        talloc_free(udev);
 }