discover: Add 'plugin' user-event
authorSamuel Mendoza-Jonas <sam@mendozajonas.com>
Tue, 25 Jul 2017 05:55:19 +0000 (15:55 +1000)
committerSamuel Mendoza-Jonas <sam@mendozajonas.com>
Tue, 15 Aug 2017 03:03:28 +0000 (13:03 +1000)
Add a new user event to advertise pb-plugins and add them to the
device_handler. Plugins described by this event can either be
uninstalled pb-plugin files or successfully installed pb-plugins
depending on the associated parameters.
The is primarily intended for use by the pb-plugin utility itself to
notify Petitboot as it operates on pb-plugin files.

Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
discover/event.c
discover/event.h
discover/user-event.c

index 1be19eb2a052bd79333c379cfa2d9d3fa5a0248e..047e9281fe8feabd8790d833780f4e9abe159bc6 100644 (file)
@@ -55,6 +55,8 @@ static int event_parse_ad_header(char *buf, int len, enum event_action *action,
                *action = EVENT_ACTION_BOOT;
        else if (streq(buf, "sync"))
                *action = EVENT_ACTION_SYNC;
                *action = EVENT_ACTION_BOOT;
        else if (streq(buf, "sync"))
                *action = EVENT_ACTION_SYNC;
+       else if (streq(buf, "plugin"))
+               *action = EVENT_ACTION_PLUGIN;
        else {
                pb_log("%s: unknown action: %s\n", __func__, buf);
                return -1;
        else {
                pb_log("%s: unknown action: %s\n", __func__, buf);
                return -1;
index a639d9cfa85b03dc87d749770a757f143aef2f48..1f6966c970ab54279b4d27747477dc1342b5a43d 100644 (file)
@@ -14,6 +14,7 @@ enum event_action {
        EVENT_ACTION_DHCP,
        EVENT_ACTION_BOOT,
        EVENT_ACTION_SYNC,
        EVENT_ACTION_DHCP,
        EVENT_ACTION_BOOT,
        EVENT_ACTION_SYNC,
+       EVENT_ACTION_PLUGIN,
        EVENT_ACTION_MAX,
 };
 
        EVENT_ACTION_MAX,
 };
 
index fb3fddb5c4bc5f7390f114ec7820f33d7ec45e77..77d28c116e635fb74698e49f0c59dfb1cb193335 100644 (file)
@@ -63,6 +63,8 @@ static const char *event_action_name(enum event_action action)
                return "boot";
        case EVENT_ACTION_SYNC:
                return "sync";
                return "boot";
        case EVENT_ACTION_SYNC:
                return "sync";
+       case EVENT_ACTION_PLUGIN:
+               return "plugin";
        default:
                break;
        }
        default:
                break;
        }
@@ -486,6 +488,111 @@ static int user_event_sync(struct user_event *uev, struct event *event)
        return 0;
 }
 
        return 0;
 }
 
+static int process_uninstalled_plugin(struct user_event *uev,
+               struct event *event)
+{
+       struct device_handler *handler = uev->handler;
+       struct discover_boot_option *file_opt;
+       struct discover_device *device;
+       struct discover_context *ctx;
+       const char *path;
+       struct resource *res;
+
+       if (!event_get_param(event, "path")) {
+               pb_log("Uninstalled pb-plugin event missing path param\n");
+               return -1;
+       }
+
+       device = device_lookup_by_name(handler, event->device);
+       if (!device) {
+               pb_log("Couldn't find device matching %s for plugin\n",
+                               event->device);
+               return -1;
+       }
+
+       ctx = device_handler_discover_context_create(handler, device);
+       file_opt = discover_boot_option_create(ctx, device);
+       file_opt->option->name = talloc_strdup(file_opt,
+                       event_get_param(event, "name"));
+       file_opt->option->id = talloc_asprintf(file_opt, "%s@%p",
+                       device->device->id, file_opt);
+       file_opt->option->type = DISCOVER_PLUGIN_OPTION;
+
+
+       path = event_get_param(event, "path");
+       /* path may be relative to root */
+       if (strncmp(device->mount_path, path, strlen(device->mount_path)) == 0) {
+               path += strlen(device->mount_path) + 1;
+       }
+
+       res = talloc(file_opt, struct resource);
+       resolve_resource_against_device(res, device, path);
+       file_opt->boot_image = res;
+
+       discover_context_add_boot_option(ctx, file_opt);
+       device_handler_discover_context_commit(handler, ctx);
+
+       return 0;
+}
+
+/*
+ * Notification of a plugin event. This can either be for an uninstalled plugin
+ * that pb-plugin has scanned, or the result of a plugin that pb-plugin has
+ * installed.
+ */
+static int user_event_plugin(struct user_event *uev, struct event *event)
+{
+       struct device_handler *handler = uev->handler;
+       char *executable, *executables, *saveptr;
+       struct plugin_option *opt;
+       const char *installed;
+
+       installed = event_get_param(event, "installed");
+       if (!installed || strncmp(installed, "no", strlen("no")) == 0)
+               return process_uninstalled_plugin(uev, event);
+
+       opt = talloc_zero(handler, struct plugin_option);
+       if (!opt)
+               return -1;
+       opt->name = talloc_strdup(opt, event_get_param(event, "name"));
+       opt->id = talloc_strdup(opt, event_get_param(event, "id"));
+       opt->version = talloc_strdup(opt, event_get_param(event, "version"));
+       opt->vendor = talloc_strdup(opt, event_get_param(event, "vendor"));
+       opt->vendor_id = talloc_strdup(opt, event_get_param(event, "vendor_id"));
+       opt->date = talloc_strdup(opt, event_get_param(event, "date"));
+       opt->plugin_file = talloc_strdup(opt,
+                       event_get_param(event, "source_file"));
+
+       executables = talloc_strdup(opt, event_get_param(event, "executables"));
+       if (!executables) {
+               talloc_free(opt);
+               return -1;
+       }
+
+       /*
+        * The 'executables' parameter is a space-delimited list of installed
+        * executables
+        */
+       executable = strtok_r(executables, " ", &saveptr);
+       while (executable) {
+               opt->executables = talloc_realloc(opt, opt->executables,
+                                                 char *, opt->n_executables + 1);
+               if (!opt->executables) {
+                       talloc_free(opt);
+                       return -1;
+               }
+               opt->executables[opt->n_executables++] = talloc_strdup(opt,
+                                                               executable);
+               executable = strtok_r(NULL, " ", &saveptr);
+       }
+
+       device_handler_add_plugin_option(handler, opt);
+
+       talloc_free(executables);
+
+       return 0;
+}
+
 static void user_event_handle_message(struct user_event *uev, char *buf,
        int len)
 {
 static void user_event_handle_message(struct user_event *uev, char *buf,
        int len)
 {
@@ -521,6 +628,9 @@ static void user_event_handle_message(struct user_event *uev, char *buf,
        case EVENT_ACTION_SYNC:
                result = user_event_sync(uev, event);
                break;
        case EVENT_ACTION_SYNC:
                result = user_event_sync(uev, event);
                break;
+       case EVENT_ACTION_PLUGIN:
+               result = user_event_plugin(uev, event);
+               break;
        default:
                break;
        }
        default:
                break;
        }