From: Sebastian Harl Date: Fri, 27 Jun 2014 18:11:24 +0000 (+0200) Subject: core: Automatically determine config callback names. X-Git-Tag: sysdb-0.2.0~24 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=5ef6f70a8eae3e4ee1a839e10b2d2037382463e0;p=sysdb.git core: Automatically determine config callback names. The plugin name (as stored in the plugin context) is used for that purpose, thus, ensuring that Plugin/Backend blocks will always belong to the respective LoadPlugin/LoadBackend options of the same name. --- diff --git a/src/backend/collectd/unixsock.c b/src/backend/collectd/unixsock.c index 8ac829d..3a41fdf 100644 --- a/src/backend/collectd/unixsock.c +++ b/src/backend/collectd/unixsock.c @@ -381,7 +381,7 @@ sdb_module_init(sdb_plugin_info_t *info) sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION); sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION); - sdb_plugin_register_config("collectd::unixsock", sdb_collectd_config); + sdb_plugin_register_config(sdb_collectd_config); return 0; } /* sdb_version_extra */ diff --git a/src/backend/mk-livestatus.c b/src/backend/mk-livestatus.c index e631ae7..b6dc1a7 100644 --- a/src/backend/mk-livestatus.c +++ b/src/backend/mk-livestatus.c @@ -331,7 +331,7 @@ sdb_module_init(sdb_plugin_info_t *info) sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION); sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION); - sdb_plugin_register_config("mk-livestatus", sdb_livestatus_config); + sdb_plugin_register_config(sdb_livestatus_config); return 0; } /* sdb_version_extra */ diff --git a/src/backend/puppet/store-configs.c b/src/backend/puppet/store-configs.c index c2f4d08..409343f 100644 --- a/src/backend/puppet/store-configs.c +++ b/src/backend/puppet/store-configs.c @@ -381,8 +381,7 @@ sdb_module_init(sdb_plugin_info_t *info) sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION); sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION); - sdb_plugin_register_config("puppet::store-configs", - sdb_puppet_stcfg_config); + sdb_plugin_register_config(sdb_puppet_stcfg_config); return 0; } /* sdb_version_extra */ diff --git a/src/core/plugin.c b/src/core/plugin.c index b3d5973..282a521 100644 --- a/src/core/plugin.c +++ b/src/core/plugin.c @@ -689,9 +689,16 @@ sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...) } /* sdb_plugin_set_info */ int -sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback) +sdb_plugin_register_config(sdb_plugin_config_cb callback) { - return plugin_add_callback(&config_list, "init", name, + ctx_t *ctx = ctx_get(); + + if (! ctx) { + sdb_log(SDB_LOG_ERR, "core: Invalid attempt to register a " + "config callback from outside a plugin"); + return -1; + } + return plugin_add_callback(&config_list, "init", ctx->info.plugin_name, (void *)callback, NULL); } /* sdb_plugin_register_config */ @@ -829,7 +836,9 @@ sdb_plugin_configure(const char *name, oconfig_item_t *ci) if (! plugin) { ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name)); if (! ctx) - sdb_log(SDB_LOG_ERR, "core: Plugin '%s' not loaded.", name); + sdb_log(SDB_LOG_ERR, "core: Cannot configure unknown " + "plugin '%s'. Missing 'LoadPlugin \"%s\"'?", + name, name); else sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register " "a config callback.", name); diff --git a/src/include/core/plugin.h b/src/include/core/plugin.h index 69c18b4..51d5905 100644 --- a/src/include/core/plugin.h +++ b/src/include/core/plugin.h @@ -116,7 +116,7 @@ typedef int (*sdb_plugin_log_cb)(int prio, const char *msg, * - a negative value else */ int -sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback); +sdb_plugin_register_config(sdb_plugin_config_cb callback); /* * sdb_plugin_register_init: @@ -225,7 +225,7 @@ sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback, * every plugin care about it. * * If non-NULL, sdb_plugin_set_ctx stores the previous context in the location - * pointed to be 'old'. + * pointed to by 'old'. */ sdb_plugin_ctx_t sdb_plugin_get_ctx(void); @@ -234,8 +234,8 @@ sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old); /* * sdb_plugin_configure: - * Configure the plugin called 'name' (according to the registered config - * callback) using the config tree 'ci'. + * Configure the plugin called 'name' using the config tree 'ci'. The plugin + * name is the same as the one used when loading the plugin. * * Returns: * - 0 on success diff --git a/src/tools/sysdbd/configfile.c b/src/tools/sysdbd/configfile.c index 992801d..31bfd9b 100644 --- a/src/tools/sysdbd/configfile.c +++ b/src/tools/sysdbd/configfile.c @@ -198,7 +198,7 @@ daemon_load_backend(oconfig_item_t *ci) { sdb_plugin_ctx_t ctx = SDB_PLUGIN_CTX_INIT; - char plugin_name[1024]; + char plugin_name[1024]; char *name; int i; @@ -235,6 +235,7 @@ daemon_load_backend(oconfig_item_t *ci) static int daemon_configure_plugin(oconfig_item_t *ci) { + char plugin_name[1024]; char *name; assert(ci); @@ -247,7 +248,11 @@ daemon_configure_plugin(oconfig_item_t *ci) return ERR_INVALID_ARG; } - return sdb_plugin_configure(name, ci); + if (!strcasecmp(ci->key, "Backend")) + snprintf(plugin_name, sizeof(plugin_name), "Backend::%s", name); + else + strncpy(plugin_name, name, sizeof(plugin_name)); + return sdb_plugin_configure(plugin_name, ci); } /* daemon_configure_backend */ static token_parser_t token_parser_list[] = { diff --git a/t/integration/mock_plugin.c b/t/integration/mock_plugin.c index 459a7e2..7e2b4a6 100644 --- a/t/integration/mock_plugin.c +++ b/t/integration/mock_plugin.c @@ -203,7 +203,7 @@ sdb_module_init(sdb_plugin_info_t *info) sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION); sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION); - sdb_plugin_register_config("test::integration::mock", mock_config); + sdb_plugin_register_config(mock_config); return 0; } /* sdb_module_init */ diff --git a/t/integration/simple_query.sh b/t/integration/simple_query.sh index 972d246..22bd815 100755 --- a/t/integration/simple_query.sh +++ b/t/integration/simple_query.sh @@ -39,7 +39,7 @@ PluginDir "$PLUGIN_DIR" Interval 2 LoadBackend mock_plugin - + EOF