]> git.ozlabs.org Git - petitboot/blobdiff - discover/device-handler.c
protocol: Separate device add from boot-option add messages
[petitboot] / discover / device-handler.c
index e65eb61ebc2e353fb4880b4b59260c1f9c969a73..ab27b51b2eaf764d98a37b7df3185dd49d46a20b 100644 (file)
@@ -10,7 +10,7 @@
 #include <talloc/talloc.h>
 #include <list/list.h>
 #include <log/log.h>
-#include <pb-protocol/pb-protocol.h>
+#include <types/types.h>
 #include <system/system.h>
 
 #include "device-handler.h"
 #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,7 +187,7 @@ 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";
@@ -205,7 +198,7 @@ static int mount_device(struct discover_context *ctx)
 
                /* Retry mount without ro option. */
 
-               argv[0] = MOUNT_BIN;
+               argv[0] = pb_system_apps.mount;
                argv[1] = ctx->device_path;
                argv[2] = ctx->mount_path;
                argv[3] = NULL;
@@ -236,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);
        }
 
@@ -281,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;
 
@@ -317,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;
 }
@@ -331,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);
@@ -344,6 +342,7 @@ static int handle_remove_udev_event(struct device_handler *handler,
 static int handle_add_user_event(struct device_handler *handler,
                struct event *event)
 {
+       struct boot_option *opt;
        struct device *device;
 
        assert(event->device);
@@ -358,7 +357,10 @@ static int handle_add_user_event(struct device_handler *handler,
 
        parse_user_event(device, event);
 
-       discover_server_notify_add(handler->server, device);
+       discover_server_notify_device_add(handler->server, device);
+
+       list_for_each_entry(&device->boot_options, opt, list)
+               discover_server_notify_boot_option_add(handler->server, opt);
 
        /* add device to handler device array */
        device_handler_add(handler, device);
@@ -378,7 +380,7 @@ static int handle_remove_user_event(struct device_handler *handler,
        if (!device)
                return 0;
 
-       discover_server_notify_remove(handler->server, device);
+       discover_server_notify_device_remove(handler->server, device);
 
        /* remove device from handler device array */
        device_handler_remove(handler, device);
@@ -414,7 +416,8 @@ int device_handler_event(struct device_handler *handler,
        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;
 
@@ -422,6 +425,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);
 
@@ -437,3 +441,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);
+}