[RFC,7/9] discover: Add 'plugin' user-event

Message ID 20170215043541.10204-7-sam@mendozajonas.com
State RFC
Headers show

Commit Message

Samuel Mendoza-Jonas Feb. 15, 2017, 4:35 a.m.
Add a new user event to advertise installed pb-plugins and add them to
the device_handler. The intended use case for this is to enable
pb-plugin to inform pb-discover of new plugins as it installs them.

Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
---
 discover/event.c      |  2 ++
 discover/event.h      |  1 +
 discover/user-event.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++
 utils/pb-plugin       |  6 ++++++
 4 files changed, 66 insertions(+)

Patch

diff --git a/discover/event.c b/discover/event.c
index 1be19eb..047e928 100644
--- a/discover/event.c
+++ b/discover/event.c
@@ -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;
+	else if (streq(buf, "plugin"))
+		*action = EVENT_ACTION_PLUGIN;
 	else {
 		pb_log("%s: unknown action: %s\n", __func__, buf);
 		return -1;
diff --git a/discover/event.h b/discover/event.h
index a639d9c..1f6966c 100644
--- a/discover/event.h
+++ b/discover/event.h
@@ -14,6 +14,7 @@  enum event_action {
 	EVENT_ACTION_DHCP,
 	EVENT_ACTION_BOOT,
 	EVENT_ACTION_SYNC,
+	EVENT_ACTION_PLUGIN,
 	EVENT_ACTION_MAX,
 };
 
diff --git a/discover/user-event.c b/discover/user-event.c
index fb3fddb..98d5948 100644
--- a/discover/user-event.c
+++ b/discover/user-event.c
@@ -63,6 +63,8 @@  static const char *event_action_name(enum event_action action)
 		return "boot";
 	case EVENT_ACTION_SYNC:
 		return "sync";
+	case EVENT_ACTION_PLUGIN:
+		return "plugin";
 	default:
 		break;
 	}
@@ -486,6 +488,58 @@  static int user_event_sync(struct user_event *uev, struct event *event)
 	return 0;
 }
 
+/* Notification that a plugin has been installed. These are represented by a
+ * plugin_option struct which is added to the device_handler and then sent to
+ * clients.
+ */
+static int user_event_plugin(struct user_event *uev, struct event *event)
+{
+	struct device_handler *handler = uev->handler;
+	struct plugin_option *opt;
+	char *executable, *executables, *saveptr;
+
+	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)
 {
@@ -521,6 +575,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_PLUGIN:
+		result = user_event_plugin(uev, event);
+		break;
 	default:
 		break;
 	}
diff --git a/utils/pb-plugin b/utils/pb-plugin
index 1a03f23..2021e9d 100755
--- a/utils/pb-plugin
+++ b/utils/pb-plugin
@@ -231,6 +231,12 @@  do_install()
 		__create_wrapper "$__dest" "$binary"
 	done
 
+	pb-event plugin@local \
+		name=$PLUGIN_NAME id=$PLUGIN_ID version=$PLUGIN_VERSION \
+		vendor=$PLUGIN_VENDOR vendor_id=$PLUGIN_VENDOR_ID \
+		date=$PLUGIN_DATE executables="$PLUGIN_EXECUTABLES" \
+		source_file=$url
+
 	echo "Plugin installed"
 	plugin_info
 }