diff mbox series

[3/7] trace-cmd,libtraceevent: Check for plugin options duplication

Message ID 20200122150002.763233-4-tz.stoyanov@gmail.com (mailing list archive)
State New
Headers show
Series trace-cmd,libtraceevent: Rework of plugin options APIs | expand

Commit Message

Tzvetomir Stoyanov (VMware) Jan. 22, 2020, 2:59 p.m. UTC
When loading new plugin options, check is an option with the same
name and plugin is already registered. The API
 tep_plugin_add_options()
is modified to return errors is these cases:
  - The "plugin" field in the option's description is not set,
    return -EINVAL
  - An option with the same name and plugin is already registered
    return -EBUSY

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 lib/traceevent/event-plugin.c | 50 +++++++++++++++++++++--------------
 1 file changed, 30 insertions(+), 20 deletions(-)
diff mbox series

Patch

diff --git a/lib/traceevent/event-plugin.c b/lib/traceevent/event-plugin.c
index 8c48ccf..e53b09c 100644
--- a/lib/traceevent/event-plugin.c
+++ b/lib/traceevent/event-plugin.c
@@ -202,6 +202,26 @@  update_option(const char *file, struct tep_plugin_option *option)
 	return ret;
 }
 
+static struct tep_plugin_option *
+find_registered_option(const char *plugin, const char *option)
+{
+	struct registered_plugin_options *reg;
+	struct tep_plugin_option *op;
+
+	for (reg = registered_options; reg; reg = reg->next) {
+		for (op = reg->options; op->name; op++) {
+			if (plugin && strcmp(plugin, op->plugin) != 0)
+				continue;
+			if (strcmp(option, op->name) != 0)
+				continue;
+
+			return op;
+		}
+	}
+
+	return NULL;
+}
+
 /**
  * tep_plugin_add_options - Add a set of options by a plugin
  * @name: The name of the plugin adding the options
@@ -213,6 +233,16 @@  int tep_plugin_add_options(const char *name,
 			   struct tep_plugin_option *options)
 {
 	struct registered_plugin_options *reg;
+	struct tep_plugin_option *option;
+
+	option = options;
+	while (option && option->name) {
+		if (!option->plugin)
+			return -EINVAL;
+		if (find_registered_option(name, option->name))
+			return -EBUSY;
+		option++;
+	}
 
 	reg = malloc(sizeof(*reg));
 	if (!reg)
@@ -262,26 +292,6 @@  static void parse_option_name(char **option, char **plugin)
 	}
 }
 
-static struct tep_plugin_option *
-find_registered_option(const char *plugin, const char *option)
-{
-	struct registered_plugin_options *reg;
-	struct tep_plugin_option *op;
-
-	for (reg = registered_options; reg; reg = reg->next) {
-		for (op = reg->options; op->name; op++) {
-			if (plugin && strcmp(plugin, op->plugin) != 0)
-				continue;
-			if (strcmp(option, op->name) != 0)
-				continue;
-
-			return op;
-		}
-	}
-
-	return NULL;
-}
-
 static int process_option(const char *plugin, const char *option, const char *val)
 {
 	struct tep_plugin_option *op;