From 37feda62bd2835eabc779d275301a9b763521f7a Mon Sep 17 00:00:00 2001 From: Samuel Mendoza-Jonas Date: Fri, 16 Jun 2017 15:09:55 +1000 Subject: discover: Handle plugin install request 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 --- discover/device-handler.c | 56 +++++++++++++++++++++++++++++++++++++++++++ discover/device-handler.h | 2 ++ discover/discover-server.c | 6 +++++ lib/pb-protocol/pb-protocol.h | 1 + 4 files changed, 65 insertions(+) diff --git a/discover/device-handler.c b/discover/device-handler.c index 0c0a9a7..94a3f1c 100644 --- a/discover/device-handler.c +++ b/discover/device-handler.c @@ -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 /** diff --git a/discover/device-handler.h b/discover/device-handler.h index c1bbe7d..a95cc9d 100644 --- a/discover/device-handler.h +++ b/discover/device-handler.h @@ -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); diff --git a/discover/discover-server.c b/discover/discover-server.c index e2e87ca..57cf3b7 100644 --- a/discover/discover-server.c +++ b/discover/discover-server.c @@ -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; diff --git a/lib/pb-protocol/pb-protocol.h b/lib/pb-protocol/pb-protocol.h index ce876d0..250c2d1 100644 --- a/lib/pb-protocol/pb-protocol.h +++ b/lib/pb-protocol/pb-protocol.h @@ -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 { -- cgit v1.2.1