discover: Handle plugin install request
authorSamuel Mendoza-Jonas <sam@mendozajonas.com>
Fri, 16 Jun 2017 05:09:55 +0000 (15:09 +1000)
committerSamuel Mendoza-Jonas <sam@mendozajonas.com>
Tue, 15 Aug 2017 03:38:17 +0000 (13:38 +1000)
Handle "_PLUGIN_INSTALL" requests from clients. Calling the pb-plugin
script from pb-discover ensures different clients don't trip over each
other. Successfully installed plugins are automatically communicated
back to clients once pb-plugin sends a 'plugin' user event.

Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
discover/device-handler.c
discover/device-handler.h
discover/discover-server.c
lib/pb-protocol/pb-protocol.h

index 0c0a9a78818bed53c0596390b927ebd571704e98..94a3f1c847f09fd3c26e22c88edbfb42ac365924 100644 (file)
@@ -87,6 +87,7 @@ struct device_handler {
 
        struct plugin_option    **plugins;
        unsigned int            n_plugins;
+       bool                    plugin_installing;
 };
 
 static int mount_device(struct discover_device *dev);
@@ -1406,6 +1407,61 @@ void device_handler_process_url(struct device_handler *handler,
        talloc_unlink(handler, ctx);
 }
 
+static void plugin_install_cb(struct process *process)
+{
+       struct device_handler *handler = process->data;
+
+       if (!handler) {
+               pb_log("%s: Missing data!\n", __func__);
+               return;
+       }
+
+       handler->plugin_installing = false;
+       if (process->exit_status) {
+               device_handler_status_err(handler, "Plugin failed to install!");
+               pb_log("Failed to install plugin:\n%s\n", process->stdout_buf);
+       }
+}
+
+void device_handler_install_plugin(struct device_handler *handler,
+               const char *plugin_file)
+{
+       struct process *p;
+       int result;
+
+       if (handler->plugin_installing) {
+               pb_log("Plugin install cancelled - install already running");
+               return;
+       }
+
+       p = process_create(handler);
+       if (!p) {
+               pb_log("install_plugin: Failed to create process\n");
+               return;
+       }
+
+       const char *argv[] = {
+               pb_system_apps.pb_plugin,
+               "install",
+               "auto",
+               plugin_file,
+               NULL
+       };
+
+       p->path = pb_system_apps.pb_plugin;
+       p->argv = argv;
+       p->exit_cb = plugin_install_cb;
+       p->data = handler;
+       p->keep_stdout = true;
+
+       result = process_run_async(p);
+
+       if (result)
+               device_handler_status_err(handler, "Could not install plugin");
+       else
+               handler->plugin_installing = true;
+}
+
 #ifndef PETITBOOT_TEST
 
 /**
index c1bbe7d3ae337bbcaf782138b8d52459c37496d5..a95cc9d75180eb7aeaaa7ce62acd78554644ecf9 100644 (file)
@@ -159,6 +159,8 @@ void device_handler_update_config(struct device_handler *handler,
                struct config *config);
 void device_handler_process_url(struct device_handler *handler,
                const char *url, const char *mac, const char *ip);
+void device_handler_install_plugin(struct device_handler *handler,
+               const char *plugin_file);
 void device_handler_reinit(struct device_handler *handler);
 
 int device_request_write(struct discover_device *dev, bool *release);
index e2e87ca43618d86086a7b1a75172636399e8f202..57cf3b78975f909c41a112af1a6f1fd4704e84e4 100644 (file)
@@ -304,6 +304,12 @@ static int discover_server_process_message(void *arg)
                                url, NULL, NULL);
                break;
 
+       case PB_PROTOCOL_ACTION_PLUGIN_INSTALL:
+               url = pb_protocol_deserialise_string((void *) client, message);
+
+               device_handler_install_plugin(client->server->device_handler,
+                               url);
+               break;
        default:
                pb_log("%s: invalid action %d\n", __func__, message->action);
                return 0;
index ce876d01786e2315b9cf474ffb9b74417fc4b5b4..250c2d16149467876d3cfc50ae5223a42a3a166c 100644 (file)
@@ -25,6 +25,7 @@ enum pb_protocol_action {
        PB_PROTOCOL_ACTION_ADD_URL              = 0xb,
        PB_PROTOCOL_ACTION_PLUGIN_OPTION_ADD    = 0xc,
        PB_PROTOCOL_ACTION_PLUGINS_REMOVE       = 0xd,
+       PB_PROTOCOL_ACTION_PLUGIN_INSTALL       = 0xe,
 };
 
 struct pb_protocol_message {