]> git.ozlabs.org Git - petitboot/blobdiff - discover/device-handler.c
discover/device-handler: Scan devices for plugins
[petitboot] / discover / device-handler.c
index 38584440801963e193a7aee2ad9c7a62cb89f9d3..0c0a9a78818bed53c0596390b927ebd571704e98 100644 (file)
@@ -84,6 +84,9 @@ struct device_handler {
 
        struct list             progress;
        unsigned int            n_progress;
+
+       struct plugin_option    **plugins;
+       unsigned int            n_plugins;
 };
 
 static int mount_device(struct discover_device *dev);
@@ -126,6 +129,28 @@ const struct discover_device *device_handler_get_device(
        return handler->devices[index];
 }
 
+/**
+ * device_handler_get_plugin_count - Get the count of current handler plugins.
+ */
+int device_handler_get_plugin_count(const struct device_handler *handler)
+{
+       return handler->n_plugins;
+}
+
+/**
+ * discover_handler_get_plugin - Get a handler plugin by index.
+ */
+const struct plugin_option *device_handler_get_plugin(
+       const struct device_handler *handler, unsigned int index)
+{
+       if (index >= handler->n_plugins) {
+               assert(0 && "bad index");
+               return NULL;
+       }
+
+       return handler->plugins[index];
+}
+
 struct network *device_handler_get_network(
                const struct device_handler *handler)
 {
@@ -385,6 +410,15 @@ void device_handler_reinit(struct device_handler *handler)
        handler->ramdisks = NULL;
        handler->n_ramdisks = 0;
 
+       /* drop any known plugins */
+       for (i = 0; i < handler->n_plugins; i++)
+               talloc_free(handler->plugins[i]);
+       talloc_free(handler->plugins);
+       handler->plugins = NULL;
+       handler->n_plugins = 0;
+
+       discover_server_notify_plugins_remove(handler->server);
+
        device_handler_reinit_sources(handler);
 }
 
@@ -603,6 +637,21 @@ void device_handler_status_download(struct device_handler *handler,
        }
 }
 
+static void device_handler_plugin_scan_device(struct device_handler *handler,
+               struct discover_device *dev)
+{
+       int rc;
+
+       pb_debug("Scanning %s for plugin files\n", dev->device->id);
+
+       rc = process_run_simple(handler, pb_system_apps.pb_plugin,
+                               "scan", dev->mount_path,
+                               NULL);
+       if (rc)
+               pb_log("Error from pb-plugin scan %s\n",
+                               dev->mount_path);
+}
+
 void device_handler_status_download_remove(struct device_handler *handler,
                struct process_info *procinfo)
 {
@@ -628,7 +677,8 @@ static void countdown_status(struct device_handler *handler,
 
        status.type = STATUS_INFO;
        status.message = talloc_asprintf(handler,
-                       _("Booting in %d sec: %s"), sec, opt->option->name);
+                       _("Booting in %d sec: [%s] %s"), sec,
+                       opt->device->device->id, opt->option->name);
        status.backlog = false;
 
        device_handler_status(handler, &status);
@@ -1068,6 +1118,9 @@ int device_handler_discover(struct device_handler *handler,
        device_handler_discover_context_commit(handler, ctx);
 
        process_boot_option_queue(handler);
+
+       /* Check this device for pb-plugins */
+       device_handler_plugin_scan_device(handler, dev);
 out:
        talloc_unlink(handler, ctx);
 
@@ -1407,6 +1460,36 @@ void device_handler_discover_context_commit(struct device_handler *handler,
        }
 }
 
+void device_handler_add_plugin_option(struct device_handler *handler,
+               struct plugin_option *opt)
+{
+       struct plugin_option *tmp;
+       unsigned int i;
+
+       for (i = 0; i < handler->n_plugins; i++) {
+               tmp = handler->plugins[i];
+               /* If both id and version match, ignore */
+               if (strncmp(opt->id, tmp->id, strlen(opt->id)) == 0 &&
+                               strncmp(opt->version, tmp->version,
+                                       strlen(opt->version) == 0)) {
+                       pb_log("discover: Plugin '%s' already exists, ignoring\n",
+                                       opt->id);
+                       return;
+               }
+       }
+
+       handler->plugins = talloc_realloc(handler, handler->plugins,
+                       struct plugin_option *, handler->n_plugins + 1);
+       if (!handler->plugins) {
+               pb_log("Failed to allocate memory for new plugin\n");
+               handler->n_plugins = 0;
+               return;
+       }
+
+       handler->plugins[handler->n_plugins++] = opt;
+       discover_server_notify_plugin_option_add(handler->server, opt);
+}
+
 static void device_handler_update_lang(const char *lang)
 {
        const char *cur_lang;
@@ -1424,15 +1507,15 @@ static void device_handler_update_lang(const char *lang)
 static int device_handler_init_sources(struct device_handler *handler)
 {
        /* init our device sources: udev, network and user events */
-       handler->udev = udev_init(handler, handler->waitset);
-       if (!handler->udev)
-               return -1;
-
        handler->network = network_init(handler, handler->waitset,
                        handler->dry_run);
        if (!handler->network)
                return -1;
 
+       handler->udev = udev_init(handler, handler->waitset);
+       if (!handler->udev)
+               return -1;
+
        handler->user_event = user_event_init(handler, handler->waitset);
        if (!handler->user_event)
                return -1;
@@ -1451,11 +1534,11 @@ static void device_handler_reinit_sources(struct device_handler *handler)
 
        system_info_reinit();
 
-       udev_reinit(handler->udev);
-
        network_shutdown(handler->network);
        handler->network = network_init(handler, handler->waitset,
                        handler->dry_run);
+
+       udev_reinit(handler->udev);
 }
 
 static inline const char *get_device_path(struct discover_device *dev)