summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: bb7b99f)
raw | patch | inline | side by side (parent: bb7b99f)
author | Sebastian Harl <sh@tokkee.org> | |
Tue, 6 Sep 2016 02:54:58 +0000 (22:54 -0400) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Tue, 6 Sep 2016 02:54:58 +0000 (22:54 -0400) |
src/core/plugin.c | patch | blob | history |
diff --git a/src/core/plugin.c b/src/core/plugin.c
index 0bf785d571a29b57ac9ecba7d9f34dd1dc6d6b5a..44ea48a06b53e38434cd23a6a9a951d689d48cf2 100644 (file)
--- a/src/core/plugin.c
+++ b/src/core/plugin.c
return ctx;
} /* ctx_create */
-static int
-plugin_cb_init(sdb_object_t *obj, va_list ap)
+/*
+ * plugin_init_ok:
+ * Checks whether the registration of a new plugin identified by 'obj' is
+ * okay. It consumes the first two arguments of 'ap'.
+ */
+static bool
+plugin_init_ok(sdb_object_t *obj, va_list ap)
{
sdb_llist_t **list = va_arg(ap, sdb_llist_t **);
- const char *type = va_arg(ap, const char *);
- void *callback = va_arg(ap, void *);
- sdb_object_t *ud = va_arg(ap, sdb_object_t *);
+ const char *type = va_arg(ap, const char *);
- assert(list);
- assert(type);
- assert(obj);
+ assert(list); assert(type);
if (sdb_llist_search_by_name(*list, obj->name)) {
sdb_log(SDB_LOG_WARNING, "core: %s callback '%s' "
"has already been registered. Ignoring newly "
"registered version.", type, obj->name);
- return -1;
+ return 0;
}
+ return 1;
+} /* plugin_init_ok */
+
+static int
+plugin_cb_init(sdb_object_t *obj, va_list ap)
+{
+ void *callback;
+ sdb_object_t *ud;
+
+ if (! plugin_init_ok(obj, ap))
+ return -1;
+
+ callback = va_arg(ap, void *);
+ ud = va_arg(ap, sdb_object_t *);
/* cb_ctx may be NULL if the plugin was not registered by a plugin */
static int
plugin_writer_init(sdb_object_t *obj, va_list ap)
{
- sdb_store_writer_t *impl = va_arg(ap, sdb_store_writer_t *);
- sdb_object_t *ud = va_arg(ap, sdb_object_t *);
+ sdb_store_writer_t *impl;
+ sdb_object_t *ud;
+
+ if (! plugin_init_ok(obj, ap))
+ return -1;
+ impl = va_arg(ap, sdb_store_writer_t *);
+ ud = va_arg(ap, sdb_object_t *);
assert(impl);
if ((! impl->store_host) || (! impl->store_service)
obj->name);
return -1;
}
- if (sdb_llist_search_by_name(writer_list, obj->name)) {
- sdb_log(SDB_LOG_WARNING, "core: store writer callback '%s' "
- "has already been registered. Ignoring newly "
- "registered version.", obj->name);
- return -1;
- }
/* ctx may be NULL if the callback was not registered by a plugin */
static int
plugin_reader_init(sdb_object_t *obj, va_list ap)
{
- sdb_store_reader_t *impl = va_arg(ap, sdb_store_reader_t *);
- sdb_object_t *ud = va_arg(ap, sdb_object_t *);
+ sdb_store_reader_t *impl;
+ sdb_object_t *ud;
+ if (! plugin_init_ok(obj, ap))
+ return -1;
+
+ impl = va_arg(ap, sdb_store_reader_t *);
+ ud = va_arg(ap, sdb_object_t *);
assert(impl);
if ((! impl->prepare_query) || (! impl->execute_query)) {
obj->name);
return -1;
}
- if (sdb_llist_search_by_name(reader_list, obj->name)) {
- sdb_log(SDB_LOG_WARNING, "core: store reader callback '%s' "
- "has already been registered. Ignoring newly "
- "registered version.", obj->name);
- return -1;
- }
/* ctx may be NULL if the callback was not registered by a plugin */
} /* plugin_get_name */
static int
-plugin_add_callback(sdb_llist_t **list, const char *type,
- const char *name, void *callback, sdb_object_t *user_data)
+plugin_add_impl(sdb_llist_t **list, sdb_type_t T, const char *type,
+ const char *name, void *impl, sdb_object_t *user_data)
{
sdb_object_t *obj;
- if ((! name) || (! callback))
+ if ((! name) || (! impl))
return -1;
assert(list);
if (! *list)
return -1;
- obj = sdb_object_create(name, callback_type,
- list, type, callback, user_data);
+ obj = sdb_object_create(name, T, list, type, impl, user_data);
if (! obj)
return -1;
sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
type, name);
return 0;
-} /* plugin_add_callback */
+} /* plugin_add_impl */
/*
* object meta-data
"config callback from outside a plugin");
return -1;
}
- return plugin_add_callback(&config_list, "config", ctx->info.plugin_name,
- (void *)callback, NULL);
+ return plugin_add_impl(&config_list, callback_type, "config",
+ ctx->info.plugin_name, (void *)callback, NULL);
} /* sdb_plugin_register_config */
int
sdb_object_t *user_data)
{
char cb_name[1024];
- return plugin_add_callback(&init_list, "init",
+ return plugin_add_impl(&init_list, callback_type, "init",
plugin_get_name(name, cb_name, sizeof(cb_name)),
(void *)callback, user_data);
} /* sdb_plugin_register_init */
sdb_object_t *user_data)
{
char cb_name[1024];
- return plugin_add_callback(&shutdown_list, "shutdown",
+ return plugin_add_impl(&shutdown_list, callback_type, "shutdown",
plugin_get_name(name, cb_name, sizeof(cb_name)),
(void *)callback, user_data);
} /* sdb_plugin_register_shutdown */
sdb_object_t *user_data)
{
char cb_name[1024];
- return plugin_add_callback(&log_list, "log",
+ return plugin_add_impl(&log_list, callback_type, "log",
plugin_get_name(name, cb_name, sizeof(cb_name)),
callback, user_data);
} /* sdb_plugin_register_log */
sdb_object_t *user_data)
{
char cb_name[1024];
- return plugin_add_callback(&cname_list, "cname",
+ return plugin_add_impl(&cname_list, callback_type, "cname",
plugin_get_name(name, cb_name, sizeof(cb_name)),
callback, user_data);
} /* sdb_plugin_register_cname */
sdb_plugin_register_ts_fetcher(const char *name,
sdb_plugin_fetch_ts_cb callback, sdb_object_t *user_data)
{
- return plugin_add_callback(&ts_fetcher_list, "time-series fetcher",
+ return plugin_add_impl(&ts_fetcher_list, callback_type, "time-series fetcher",
name, callback, user_data);
} /* sdb_plugin_register_ts_fetcher */
sdb_store_writer_t *writer, sdb_object_t *user_data)
{
char cb_name[1024];
- sdb_object_t *obj;
-
- if ((! name) || (! writer))
- return -1;
-
- if (! writer_list)
- writer_list = sdb_llist_create();
- if (! writer_list)
- return -1;
-
- plugin_get_name(name, cb_name, sizeof(cb_name));
-
- obj = sdb_object_create(cb_name, writer_type,
+ return plugin_add_impl(&writer_list, writer_type, "store writer",
+ plugin_get_name(name, cb_name, sizeof(cb_name)),
writer, user_data);
- if (! obj)
- return -1;
-
- if (sdb_llist_append(writer_list, obj)) {
- sdb_object_deref(obj);
- return -1;
- }
-
- /* pass control to the list */
- sdb_object_deref(obj);
-
- sdb_log(SDB_LOG_INFO, "core: Registered store writer callback '%s'.",
- cb_name);
- return 0;
} /* sdb_store_register_writer */
int
sdb_store_reader_t *reader, sdb_object_t *user_data)
{
char cb_name[1024];
- sdb_object_t *obj;
-
- if ((! name) || (! reader))
- return -1;
-
- if (! reader_list)
- reader_list = sdb_llist_create();
- if (! reader_list)
- return -1;
-
- plugin_get_name(name, cb_name, sizeof(cb_name));
-
- obj = sdb_object_create(cb_name, reader_type,
+ return plugin_add_impl(&reader_list, reader_type, "store reader",
+ plugin_get_name(name, cb_name, sizeof(cb_name)),
reader, user_data);
- if (! obj)
- return -1;
-
- if (sdb_llist_append(reader_list, obj)) {
- sdb_object_deref(obj);
- return -1;
- }
-
- /* pass control to the list */
- sdb_object_deref(obj);
-
- sdb_log(SDB_LOG_INFO, "core: Registered store reader callback '%s'.",
- cb_name);
- return 0;
} /* sdb_plugin_register_reader */
void