X-Git-Url: https://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=discover%2Fdevice-handler.c;h=0d4349671dfa6c375f223cde0f43877c41ddad71;hp=6457dce6b68c5acf39ff8b8e5525a25b7caf9612;hb=1b0b59295d0500764c5096753f7cd11bf3ab5df4;hpb=cff3fbc80d5b4dc473d724ac824376973c9ca1e7 diff --git a/discover/device-handler.c b/discover/device-handler.c index 6457dce..0d43496 100644 --- a/discover/device-handler.c +++ b/discover/device-handler.c @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include "device-handler.h" @@ -20,10 +20,6 @@ #include "udev.h" #include "paths.h" -#define MOUNT_BIN "/bin/mount" - -#define UMOUNT_BIN "/bin/umount" - struct device_handler { struct discover_server *server; @@ -33,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. */ @@ -194,18 +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)) - argv[3] = NULL; /* try without ro */ + 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)) - goto out_rmdir; + if (pb_run_cmd(argv, 1, 0)) + goto out_rmdir; + } setup_device_links(ctx); return 0; @@ -229,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); } @@ -287,11 +286,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); @@ -384,46 +379,31 @@ static int handle_remove_user_event(struct device_handler *handler, 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) { - int rc = 0; - - 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 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; + 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)