]> git.ozlabs.org Git - petitboot/blobdiff - discover/discover-server.c
discover: Handle and track plugin_options
[petitboot] / discover / discover-server.c
index 80919fdf3da68088f20f7bca696efa1a5c39e6aa..e2e87ca43618d86086a7b1a75172636399e8f202 100644 (file)
@@ -28,6 +28,7 @@ struct discover_server {
        struct waitset *waitset;
        struct waiter *waiter;
        struct list clients;
+       struct list status;
        struct device_handler *device_handler;
 };
 
@@ -138,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)
 {
@@ -157,7 +191,7 @@ static int write_device_remove_message(struct discover_server *server,
 }
 
 static int write_boot_status_message(struct discover_server *server,
-               struct client *client, const struct boot_status *status)
+               struct client *client, const struct status *status)
 {
        struct pb_protocol_message *message;
        int len;
@@ -216,6 +250,7 @@ static int discover_server_process_message(void *arg)
        struct boot_command *boot_command;
        struct client *client = arg;
        struct config *config;
+       char *url;
        int rc;
 
        message = pb_protocol_read_message(client, client->fd);
@@ -262,6 +297,13 @@ static int discover_server_process_message(void *arg)
                                config);
                break;
 
+       case PB_PROTOCOL_ACTION_ADD_URL:
+               url = pb_protocol_deserialise_string((void *) client, message);
+
+               device_handler_process_url(client->server->device_handler,
+                               url, NULL, NULL);
+               break;
+
        default:
                pb_log("%s: invalid action %d\n", __func__, message->action);
                return 0;
@@ -274,11 +316,12 @@ static int discover_server_process_message(void *arg)
 static int discover_server_process_connection(void *arg)
 {
        struct discover_server *server = arg;
-       int fd, rc, i, n_devices;
+       struct statuslog_entry *entry;
+       int fd, rc, i, n_devices, n_plugins;
        struct client *client;
 
        /* accept the incoming connection */
-       fd = accept(server->socket, NULL, 0);
+       fd = accept(server->socket, NULL, NULL);
        if (fd < 0) {
                pb_log("accept: %s\n", strerror(errno));
                return 0;
@@ -325,6 +368,19 @@ static int discover_server_process_connection(void *arg)
                }
        }
 
+       /* send status backlog to client */
+       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;
 }
 
@@ -358,10 +414,28 @@ void discover_server_notify_device_remove(struct discover_server *server,
 }
 
 void discover_server_notify_boot_status(struct discover_server *server,
-               struct boot_status *status)
+               struct status *status)
 {
+       struct statuslog_entry *entry;
        struct client *client;
 
+       /* Duplicate the status struct to add to the backlog */
+       entry = talloc(server, struct statuslog_entry);
+       if (!entry) {
+               pb_log("Failed to allocated saved status!\n");
+       } else {
+               entry->status = talloc(entry, struct status);
+               if (entry->status) {
+                       entry->status->type = status->type;
+                       entry->status->message = talloc_strdup(entry->status,
+                                                              status->message);
+                       entry->status->backlog = true;
+                       list_add_tail(&server->status, &entry->list);
+               } else {
+                       talloc_free(entry);
+               }
+       }
+
        list_for_each_entry(&server->clients, client, list)
                write_boot_status_message(server, client, status);
 }
@@ -384,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)
 {
@@ -402,6 +493,7 @@ struct discover_server *discover_server_init(struct waitset *waitset)
        server->waiter = NULL;
        server->waitset = waitset;
        list_init(&server->clients);
+       list_init(&server->status);
 
        unlink(PB_SOCKET_PATH);