Code

core: Automatically determine config callback names.
authorSebastian Harl <sh@tokkee.org>
Fri, 27 Jun 2014 18:11:24 +0000 (20:11 +0200)
committerSebastian Harl <sh@tokkee.org>
Fri, 27 Jun 2014 18:15:53 +0000 (20:15 +0200)
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.

src/backend/collectd/unixsock.c
src/backend/mk-livestatus.c
src/backend/puppet/store-configs.c
src/core/plugin.c
src/include/core/plugin.h
src/tools/sysdbd/configfile.c
t/integration/mock_plugin.c
t/integration/simple_query.sh

index 8ac829d117c85c5ccae04bba44be92da7aa13c0f..3a41fdf65e0e7d628d9a7e032412fb8f6f9e8649 100644 (file)
@@ -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_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 */
 
        return 0;
 } /* sdb_version_extra */
 
index e631ae7525acee7c8426563ed51c8531f5825a0f..b6dc1a7d1d810a09ea7ddb4d830c67ff4f85f851 100644 (file)
@@ -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_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 */
 
        return 0;
 } /* sdb_version_extra */
 
index c2f4d084048c71fbccb945b4f38dd753fda24d8d..409343fa1f0c33f449ccd969d4155654618e43b3 100644 (file)
@@ -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_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 */
 
        return 0;
 } /* sdb_version_extra */
 
index b3d5973178dffed19411af5d5acea45a99b718e3..282a5213a45e98cae33ae3c1fde14484972d972a 100644 (file)
@@ -689,9 +689,16 @@ sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
 } /* sdb_plugin_set_info */
 
 int
 } /* 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 */
 
                        (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)
        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);
                else
                        sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
                                        "a config callback.", name);
index 69c18b41d0ff2cd38d432852d2b1854fee9b8ff8..51d59051e5768ee5e73cb848b216876fadf1238c 100644 (file)
@@ -116,7 +116,7 @@ typedef int (*sdb_plugin_log_cb)(int prio, const char *msg,
  *  - a negative value else
  */
 int
  *  - 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:
 
 /*
  * 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
  * 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);
  */
 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:
 
 /*
  * 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
  *
  * Returns:
  *  - 0 on success
index 992801d2be80d559b5fcf5f41e6046ac1ad6e1e0..31bfd9bae4375c8ad86507615a010acee09e0943 100644 (file)
@@ -198,7 +198,7 @@ daemon_load_backend(oconfig_item_t *ci)
 {
        sdb_plugin_ctx_t ctx = SDB_PLUGIN_CTX_INIT;
 
 {
        sdb_plugin_ctx_t ctx = SDB_PLUGIN_CTX_INIT;
 
-       char  plugin_name[1024];
+       char plugin_name[1024];
        char *name;
 
        int i;
        char *name;
 
        int i;
@@ -235,6 +235,7 @@ daemon_load_backend(oconfig_item_t *ci)
 static int
 daemon_configure_plugin(oconfig_item_t *ci)
 {
 static int
 daemon_configure_plugin(oconfig_item_t *ci)
 {
+       char plugin_name[1024];
        char *name;
 
        assert(ci);
        char *name;
 
        assert(ci);
@@ -247,7 +248,11 @@ daemon_configure_plugin(oconfig_item_t *ci)
                return ERR_INVALID_ARG;
        }
 
                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[] = {
 } /* daemon_configure_backend */
 
 static token_parser_t token_parser_list[] = {
index 459a7e2de61f0ebda8670f22e253831402be7127..7e2b4a67dae3fc5e843a1eaf33cd307becc5762c 100644 (file)
@@ -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_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 */
 
        return 0;
 } /* sdb_module_init */
 
index 972d246065e2b598972848a11d00d5209f4ce9b1..22bd815a95f4733ef09c52f44b2996b9ca13de13 100755 (executable)
@@ -39,7 +39,7 @@ PluginDir "$PLUGIN_DIR"
 Interval 2
 
 LoadBackend mock_plugin
 Interval 2
 
 LoadBackend mock_plugin
-<Backend "test::integration::mock">
+<Backend "mock_plugin">
 </Backend>
 EOF
 
 </Backend>
 EOF