diff mbox

[08/10] discover: Handle plugin install request

Message ID 20170623062709.16035-9-sam@mendozajonas.com
State Superseded
Headers show

Commit Message

Sam Mendoza-Jonas June 23, 2017, 6:27 a.m. UTC
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     | 55 +++++++++++++++++++++++++++++++++++++++++++
 discover/device-handler.h     |  2 ++
 discover/discover-server.c    |  6 +++++
 lib/pb-protocol/pb-protocol.h |  1 +
 4 files changed, 64 insertions(+)
diff mbox

Patch

diff --git a/discover/device-handler.c b/discover/device-handler.c
index 02a95d8..b72508b 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);
@@ -1345,6 +1346,60 @@  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)
+		handler->plugin_installing = false;
+	else
+		pb_log("%s: Missing data!\n", __func__);
+
+	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 {