X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;ds=sidebyside;f=discover%2Fdevice-handler.c;h=0d4349671dfa6c375f223cde0f43877c41ddad71;hb=a2f7b111a2073a4afaf75499ac4c2732b71ea801;hp=af48f5c6ea0648a81db4842c948011f6decde855;hpb=764741bda57e79ff4d30cfb5eb5d01f11a89c29e;p=petitboot diff --git a/discover/device-handler.c b/discover/device-handler.c index af48f5c..0d43496 100644 --- a/discover/device-handler.c +++ b/discover/device-handler.c @@ -10,19 +10,16 @@ #include #include #include -#include +#include #include #include "device-handler.h" #include "discover-server.h" +#include "event.h" #include "parser.h" #include "udev.h" #include "paths.h" -#define MOUNT_BIN "/bin/mount" - -#define UMOUNT_BIN "/bin/umount" - struct device_handler { struct discover_server *server; @@ -32,11 +29,6 @@ struct device_handler { struct list contexts; }; -struct mount_map { - char *device_path; - char *mount_point; -}; - /** * device_handler_add - Add a device to the handler device array. */ @@ -63,7 +55,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 +83,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 +133,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; @@ -193,15 +185,25 @@ static int mount_device(struct discover_context *ctx) pb_log("couldn't create mount directory %s: %s\n", ctx->mount_path, strerror(errno)); - argv[0] = MOUNT_BIN; + argv[0] = pb_system_apps.mount; argv[1] = ctx->device_path; argv[2] = ctx->mount_path; argv[3] = "-o"; 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] = pb_system_apps.mount; + 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; @@ -225,7 +227,8 @@ static int umount_device(struct discover_context *ctx) } if (pid == 0) { - execl(UMOUNT_BIN, UMOUNT_BIN, ctx->mount_path, NULL); + execl(pb_system_apps.umount, pb_system_apps.umount, + ctx->mount_path, NULL); exit(EXIT_FAILURE); } @@ -266,8 +269,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 +285,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 +314,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,22 +333,77 @@ static int handle_remove_event(struct device_handler *handler, return 0; } -int device_handler_event(struct device_handler *handler, - struct udev_event *event) +static int handle_add_user_event(struct device_handler *handler, + struct event *event) { - int rc; + 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); - switch (event->action) { - case UDEV_ACTION_ADD: - rc = handle_add_event(handler, event); - break; + 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); - case UDEV_ACTION_REMOVE: - rc = handle_remove_event(handler, event); - break; + talloc_free(device); + return 0; +} + +typedef int (*event_handler)(struct device_handler *, struct event *); + +static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = { + [EVENT_TYPE_UDEV] = { + [EVENT_ACTION_ADD] = handle_add_udev_event, + [EVENT_ACTION_REMOVE] = handle_remove_udev_event, + }, + [EVENT_TYPE_USER] = { + [EVENT_ACTION_ADD] = handle_add_user_event, + [EVENT_ACTION_REMOVE] = handle_remove_user_event, + } +}; + +int device_handler_event(struct device_handler *handler, + struct event *event) +{ + if (event->type >= EVENT_TYPE_MAX || + event->action >= EVENT_ACTION_MAX || + !handlers[event->type][event->action]) { + pb_log("%s unknown type/action: %d/%d\n", __func__, + event->type, event->action); + return 0; } - return rc; + return handlers[event->type][event->action](handler, event); } struct device_handler *device_handler_init(struct discover_server *server)