Cleanup --dry-run option code
[petitboot] / discover / device-handler.c
index af48f5c6ea0648a81db4842c948011f6decde855..6e03ef3fa159136b6015983a0054411cb7f126c8 100644 (file)
@@ -15,6 +15,7 @@
 
 #include "device-handler.h"
 #include "discover-server.h"
+#include "event.h"
 #include "parser.h"
 #include "udev.h"
 #include "paths.h"
@@ -63,7 +64,7 @@ static void device_handler_remove(struct device_handler *handler,
                if (handler->devices[i] == device)
                        break;
 
-       if (i < handler->n_devices) {
+       if (i == handler->n_devices) {
                assert(0 && "unknown device");
                return;
        }
@@ -91,7 +92,7 @@ static struct device *device_handler_find(struct device_handler *handler,
                        && streq(handler->devices[i]->id, id))
                        return handler->devices[i];
 
-       assert(0 && "unknown device");
+       pb_log("%s: unknown device: %s\n", __func__, id);
        return NULL;
 }
 
@@ -141,7 +142,7 @@ static void setup_device_links(struct discover_context *ctx)
                char *enc, *dir, *path;
                const char *value;
 
-               value = udev_event_param(ctx->event, link->env);
+               value = event_get_param(ctx->event, link->env);
                if (!value || !*value)
                        continue;
 
@@ -200,8 +201,18 @@ static int mount_device(struct discover_context *ctx)
        argv[4] = "ro";
        argv[5] = NULL;
 
-       if (pb_run_cmd(argv))
-               goto out_rmdir;
+       if (pb_run_cmd(argv, 1, 0)) {
+
+               /* Retry mount without ro option. */
+
+               argv[0] = MOUNT_BIN;
+               argv[1] = ctx->device_path;
+               argv[2] = ctx->mount_path;
+               argv[3] = NULL;
+
+               if (pb_run_cmd(argv, 1, 0))
+                       goto out_rmdir;
+       }
 
        setup_device_links(ctx);
        return 0;
@@ -266,8 +277,8 @@ static int destroy_context(void *arg)
        return 0;
 }
 
-static int handle_add_event(struct device_handler *handler,
-               struct udev_event *event)
+static int handle_add_udev_event(struct device_handler *handler,
+               struct event *event)
 {
        struct discover_context *ctx;
        const char *devname;
@@ -282,12 +293,8 @@ static int handle_add_event(struct device_handler *handler,
 
        ctx->id = talloc_strdup(ctx, event->device);
 
-       devname = udev_event_param(ctx->event, "DEVNAME");
-       if (!devname) {
-               pb_log("no devname for %s?\n", event->device);
-               return 0;
-       }
-
+       devname = event_get_param(ctx->event, "DEVNAME");
+       assert(devname);
        ctx->device_path = talloc_strdup(ctx, devname);
 
        rc = mount_device(ctx);
@@ -315,8 +322,8 @@ static int handle_add_event(struct device_handler *handler,
        return 0;
 }
 
-static int handle_remove_event(struct device_handler *handler,
-               struct udev_event *event)
+static int handle_remove_udev_event(struct device_handler *handler,
+               struct event *event)
 {
        struct discover_context *ctx;
 
@@ -334,18 +341,88 @@ static int handle_remove_event(struct device_handler *handler,
        return 0;
 }
 
+static int handle_add_user_event(struct device_handler *handler,
+               struct event *event)
+{
+       struct device *device;
+
+       assert(event->device);
+
+       device = talloc_zero(handler, struct device);
+
+       if (!device)
+               goto fail;
+
+       device->id = talloc_strdup(device, event->device);
+       list_init(&device->boot_options);
+
+       parse_user_event(device, event);
+
+       discover_server_notify_add(handler->server, device);
+
+       /* add device to handler device array */
+       device_handler_add(handler, device);
+
+       return 0;
+
+fail:
+       talloc_free(device);
+       return 0;
+}
+
+static int handle_remove_user_event(struct device_handler *handler,
+               struct event *event)
+{
+       struct device *device = device_handler_find(handler, event->device);
+
+       if (!device)
+               return 0;
+
+       discover_server_notify_remove(handler->server, device);
+
+       /* remove device from handler device array */
+       device_handler_remove(handler, device);
+
+       talloc_free(device);
+       return 0;
+}
+
 int device_handler_event(struct device_handler *handler,
-               struct udev_event *event)
+               struct event *event)
 {
-       int rc;
+       int rc = 0;
 
-       switch (event->action) {
-       case UDEV_ACTION_ADD:
-               rc = handle_add_event(handler, event);
+       switch (event->type) {
+       case EVENT_TYPE_UDEV:
+               switch (event->action) {
+               case EVENT_ACTION_ADD:
+                       rc = handle_add_udev_event(handler, event);
+                       break;
+               case EVENT_ACTION_REMOVE:
+                       rc = handle_remove_udev_event(handler, event);
+                       break;
+               default:
+                       pb_log("%s unknown action: %d\n", __func__,
+                               event->action);
+                       break;
+               }
                break;
-
-       case UDEV_ACTION_REMOVE:
-               rc = handle_remove_event(handler, event);
+       case EVENT_TYPE_USER:
+               switch (event->action) {
+               case EVENT_ACTION_ADD:
+                       rc = handle_add_user_event(handler, event);
+                       break;
+               case EVENT_ACTION_REMOVE:
+                       rc = handle_remove_user_event(handler, event);
+                       break;
+               default:
+                       pb_log("%s unknown action: %d\n", __func__,
+                               event->action);
+                       break;
+               }
+               break;
+       default:
+               pb_log("%s unknown type: %d\n", __func__, event->type);
                break;
        }