Cleanup --dry-run option code
[petitboot] / discover / device-handler.c
index 908409c6540ff52c71b3962d700486a38d309f76..6e03ef3fa159136b6015983a0054411cb7f126c8 100644 (file)
@@ -201,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;
@@ -284,11 +294,7 @@ static int handle_add_udev_event(struct device_handler *handler,
        ctx->id = talloc_strdup(ctx, event->device);
 
        devname = event_get_param(ctx->event, "DEVNAME");
-       if (!devname) {
-               pb_log("no devname for %s?\n", event->device);
-               return 0;
-       }
-
+       assert(devname);
        ctx->device_path = talloc_strdup(ctx, devname);
 
        rc = mount_device(ctx);
@@ -335,6 +341,52 @@ static int handle_remove_udev_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 event *event)
 {
@@ -356,6 +408,18 @@ int device_handler_event(struct device_handler *handler,
                }
                break;
        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);