From patchwork Fri Jun 23 06:27:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Mendoza-Jonas X-Patchwork-Id: 779807 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wv7mh5WGDz9s81 for ; Fri, 23 Jun 2017 16:28:04 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="HbjxuaM5"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3wv7mh4Gf7zDqlG for ; Fri, 23 Jun 2017 16:28:04 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="HbjxuaM5"; dkim-atps=neutral X-Original-To: petitboot@lists.ozlabs.org Delivered-To: petitboot@lists.ozlabs.org Received: from mendozajonas.com (mendozajonas.com [188.166.185.233]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3wv7m72jY7zDqjL for ; Fri, 23 Jun 2017 16:27:35 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="HbjxuaM5"; dkim-atps=neutral Received: from v4.ozlabs.ibm.com (unknown [122.99.82.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) (Authenticated sender: sam@mendozajonas.com) by mendozajonas.com (Postfix) with ESMTPSA id E5F0C1441B4; Fri, 23 Jun 2017 14:27:31 +0800 (SGT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mendozajonas.com; s=mail; t=1498199252; bh=pub0FTplZ+9wwN2WdFHtOkJfnAHgd1cXZs4wnle96Cs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HbjxuaM5R2/HHTpvqUeJQ1W9+4XzZSW1X61+4p0xwfqRY5YUuGAb7epa5NjetOnMk FfkCagmkq0LSo6v9lUS7wZuy4pOCWoSmA3x36dGeXPcVa/7DeVpeuYDZx/BDzFVhdj Zc2Ycp88XUaPR5gihuDAmaVlczvfW8LljNNWzpKY= From: Samuel Mendoza-Jonas To: petitboot@lists.ozlabs.org Subject: [PATCH 06/10] discover: Handle and track plugin_options Date: Fri, 23 Jun 2017 16:27:05 +1000 Message-Id: <20170623062709.16035-7-sam@mendozajonas.com> X-Mailer: git-send-email 2.13.1 In-Reply-To: <20170623062709.16035-1-sam@mendozajonas.com> References: <20170623062709.16035-1-sam@mendozajonas.com> X-BeenThere: petitboot@lists.ozlabs.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Petitboot bootloader development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Samuel Mendoza-Jonas MIME-Version: 1.0 Errors-To: petitboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Petitboot" Track plugin_options in the device_handler. Plugins can be added with device_handler_add_plugin_option() and accessed via device_handler_get_plugin(). Extend discover_server to support the new 'add' and 'remove' pb-protocol actions and advertise new plugins to connecting clients. Signed-off-by: Samuel Mendoza-Jonas --- discover/device-handler.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++ discover/device-handler.h | 5 ++++ discover/discover-server.c | 61 +++++++++++++++++++++++++++++++++++++++++++++- discover/discover-server.h | 5 ++++ test/parser/Makefile.am | 1 + test/parser/handler.c | 6 +++++ test/parser/user-event.c | 13 ++++++++++ 7 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 test/parser/user-event.c diff --git a/discover/device-handler.c b/discover/device-handler.c index ec1eee3..02a95d8 100644 --- a/discover/device-handler.c +++ b/discover/device-handler.c @@ -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) { @@ -384,6 +409,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); } @@ -1365,6 +1399,33 @@ 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 (strncmp(opt->id, tmp->id, strlen(opt->id)) == 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; diff --git a/discover/device-handler.h b/discover/device-handler.h index 133eff3..c1bbe7d 100644 --- a/discover/device-handler.h +++ b/discover/device-handler.h @@ -80,6 +80,9 @@ void device_handler_destroy(struct device_handler *devices); int device_handler_get_device_count(const struct device_handler *handler); const struct discover_device *device_handler_get_device( const struct device_handler *handler, unsigned int index); +int device_handler_get_plugin_count(const struct device_handler *handler); +const struct plugin_option *device_handler_get_plugin( + const struct device_handler *handler, unsigned int index); struct network *device_handler_get_network( const struct device_handler *handler); @@ -126,6 +129,8 @@ struct discover_boot_option *discover_boot_option_create( struct discover_device *dev); void discover_context_add_boot_option(struct discover_context *ctx, struct discover_boot_option *opt); +void device_handler_add_plugin_option(struct device_handler *handler, + struct plugin_option *opt); int device_handler_user_event(struct device_handler *handler, struct event *event); diff --git a/discover/discover-server.c b/discover/discover-server.c index a3087b3..e2e87ca 100644 --- a/discover/discover-server.c +++ b/discover/discover-server.c @@ -139,6 +139,39 @@ static int write_boot_option_add_message(struct discover_server *server, return client_write_message(server, client, message); } +static int write_plugin_option_add_message(struct discover_server *server, + struct client *client, const struct plugin_option *opt) +{ + struct pb_protocol_message *message; + int len; + + len = pb_protocol_plugin_option_len(opt); + + message = pb_protocol_create_message(client, + PB_PROTOCOL_ACTION_PLUGIN_OPTION_ADD, len); + if (!message) + return -1; + + pb_protocol_serialise_plugin_option(opt, message->payload, len); + + return client_write_message(server, client, message); +} + +static int write_plugins_remove_message(struct discover_server *server, + struct client *client) +{ + struct pb_protocol_message *message; + + message = pb_protocol_create_message(client, + PB_PROTOCOL_ACTION_PLUGINS_REMOVE, 0); + if (!message) + return -1; + + /* No payload so nothing to serialise */ + + return client_write_message(server, client, message); +} + static int write_device_remove_message(struct discover_server *server, struct client *client, char *dev_id) { @@ -284,7 +317,7 @@ static int discover_server_process_connection(void *arg) { struct discover_server *server = arg; struct statuslog_entry *entry; - int fd, rc, i, n_devices; + int fd, rc, i, n_devices, n_plugins; struct client *client; /* accept the incoming connection */ @@ -339,6 +372,15 @@ static int discover_server_process_connection(void *arg) list_for_each_entry(&server->status, entry, list) write_boot_status_message(server, client, entry->status); + /* send installed plugins to client */ + n_plugins = device_handler_get_plugin_count(server->device_handler); + for (i = 0; i < n_plugins; i++) { + const struct plugin_option *plugin; + + plugin = device_handler_get_plugin(server->device_handler, i); + write_plugin_option_add_message(server, client, plugin); + } + return 0; } @@ -416,6 +458,23 @@ void discover_server_notify_config(struct discover_server *server, write_config_message(server, client, config); } +void discover_server_notify_plugin_option_add(struct discover_server *server, + struct plugin_option *opt) +{ + struct client *client; + + list_for_each_entry(&server->clients, client, list) + write_plugin_option_add_message(server, client, opt); +} + +void discover_server_notify_plugins_remove(struct discover_server *server) +{ + struct client *client; + + list_for_each_entry(&server->clients, client, list) + write_plugins_remove_message(server, client); +} + void discover_server_set_device_source(struct discover_server *server, struct device_handler *handler) { diff --git a/discover/discover-server.h b/discover/discover-server.h index 97d07e6..9f3aa62 100644 --- a/discover/discover-server.h +++ b/discover/discover-server.h @@ -7,6 +7,8 @@ struct discover_server; struct device_handler; struct boot_option; struct status; +struct plugin_option; +struct boot_status; struct system_info; struct device; struct config; @@ -30,4 +32,7 @@ void discover_server_notify_system_info(struct discover_server *server, const struct system_info *sysinfo); void discover_server_notify_config(struct discover_server *server, const struct config *config); +void discover_server_notify_plugin_option_add(struct discover_server *server, + struct plugin_option *option); +void discover_server_notify_plugins_remove(struct discover_server *server); #endif /* _DISCOVER_SERVER_H */ diff --git a/test/parser/Makefile.am b/test/parser/Makefile.am index 9a6be6d..fa644fe 100644 --- a/test/parser/Makefile.am +++ b/test/parser/Makefile.am @@ -100,6 +100,7 @@ test_parser_libtest_ro_SOURCES = \ test/parser/utils.c \ test/parser/handler.c \ test/parser/network.c \ + test/parser/user-event.c \ test/parser/parser-test.h \ discover/yaboot-parser.c \ discover/kboot-parser.c \ diff --git a/test/parser/handler.c b/test/parser/handler.c index e356407..321e0ca 100644 --- a/test/parser/handler.c +++ b/test/parser/handler.c @@ -7,6 +7,7 @@ #include "device-handler.h" struct network; +struct client; typedef void (*boot_status_fn)(void *arg, struct status *); @@ -102,3 +103,8 @@ void boot_cancel(struct boot_task *task) { (void)task; } + +void discover_server_notify_plugins_remove(struct discover_server *server) +{ + (void)server; +} diff --git a/test/parser/user-event.c b/test/parser/user-event.c new file mode 100644 index 0000000..43d15b7 --- /dev/null +++ b/test/parser/user-event.c @@ -0,0 +1,13 @@ + +#include + +#include + +struct device_handler; + +void device_handler_add_plugin_option(struct device_handler *handler, + struct plugin_option *opt) +{ + (void)handler; + (void)opt; +}