X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=discover%2Fdevice-handler.c;h=4ba7405f2de73f29ded296340ec310104393a8d6;hb=12a5c9153ce95ddde7fb14eaba9d779933a3637c;hp=908409c6540ff52c71b3962d700486a38d309f76;hpb=d7ce19907ff585e7fb6b950a40bd638d49c96a27;p=petitboot diff --git a/discover/device-handler.c b/discover/device-handler.c index 908409c..4ba7405 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" @@ -19,13 +19,11 @@ #include "parser.h" #include "udev.h" #include "paths.h" - -#define MOUNT_BIN "/bin/mount" - -#define UMOUNT_BIN "/bin/umount" +#include "boot.h" struct device_handler { struct discover_server *server; + int dry_run; struct device **devices; unsigned int n_devices; @@ -33,11 +31,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,15 +187,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; @@ -226,7 +229,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); } @@ -271,6 +275,7 @@ static int handle_add_udev_event(struct device_handler *handler, struct event *event) { struct discover_context *ctx; + struct boot_option *opt; const char *devname; int rc; @@ -284,11 +289,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); @@ -311,7 +312,10 @@ static int handle_add_udev_event(struct device_handler *handler, /* add device to handler device array */ device_handler_add(handler, ctx->device); - discover_server_notify_add(handler->server, ctx->device); + discover_server_notify_device_add(handler->server, ctx->device); + + list_for_each_entry(&ctx->device->boot_options, opt, list) + discover_server_notify_boot_option_add(handler->server, opt); return 0; } @@ -325,7 +329,7 @@ static int handle_remove_udev_event(struct device_handler *handler, if (!ctx) return 0; - discover_server_notify_remove(handler->server, ctx->device); + discover_server_notify_device_remove(handler->server, ctx->device); /* remove device from handler device array */ device_handler_remove(handler, ctx->device); @@ -335,37 +339,87 @@ static int handle_remove_udev_event(struct device_handler *handler, return 0; } -int device_handler_event(struct device_handler *handler, +static int handle_add_user_event(struct device_handler *handler, struct event *event) { - int rc = 0; + struct boot_option *opt; + struct device *device; - 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: - break; - default: - pb_log("%s unknown type: %d\n", __func__, event->type); - break; + assert(event->device); + + device = device_handler_find(handler, event->device); + + if (!device) { + device = talloc_zero(handler, struct device); + + if (!device) + goto fail; + + device->id = talloc_strdup(device, event->device); + list_init(&device->boot_options); + + /* add device to handler device array */ + device_handler_add(handler, device); + + discover_server_notify_device_add(handler->server, device); + } + + opt = parse_user_event(device, event); + discover_server_notify_boot_option_add(handler->server, opt); + + 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_device_remove(handler->server, device); + + /* remove device from handler device array */ + device_handler_remove(handler, device); + + 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) +struct device_handler *device_handler_init(struct discover_server *server, + int dry_run) { struct device_handler *handler; @@ -373,6 +427,7 @@ struct device_handler *device_handler_init(struct discover_server *server) handler->devices = NULL; handler->n_devices = 0; handler->server = server; + handler->dry_run = dry_run; list_init(&handler->contexts); @@ -388,3 +443,30 @@ void device_handler_destroy(struct device_handler *handler) { talloc_free(handler); } + +static struct boot_option *find_boot_option_by_id( + struct device_handler *handler, const char *id) +{ + unsigned int i; + + for (i = 0; i < handler->n_devices; i++) { + struct device *dev = handler->devices[i]; + struct boot_option *opt; + + list_for_each_entry(&dev->boot_options, opt, list) + if (!strcmp(opt->id, id)) + return opt; + } + + return NULL; +} + +void device_handler_boot(struct device_handler *handler, + struct boot_command *cmd) +{ + struct boot_option *opt; + + opt = find_boot_option_by_id(handler, cmd->option_id); + + boot(handler, opt, cmd, handler->dry_run); +}