Code

plugin: Split sdb_plugin_load() into multiple functions.
authorSebastian Harl <sh@tokkee.org>
Sat, 19 Apr 2014 09:33:47 +0000 (11:33 +0200)
committerSebastian Harl <sh@tokkee.org>
Sat, 19 Apr 2014 09:35:38 +0000 (11:35 +0200)
… to make it easier to reuse parts of the function.

src/core/plugin.c

index 1c07538125506ebbc2e49952af0be351e58456db..929abaee8467cf4bc7f419c324cdd7c6b46a339f 100644 (file)
@@ -384,60 +384,41 @@ static sdb_type_t sdb_plugin_collector_cb_type = {
 };
 
 static int
-plugin_add_callback(sdb_llist_t **list, const char *type,
-               const char *name, void *callback, sdb_object_t *user_data)
+module_init(const char *name, lt_dlhandle lh, sdb_plugin_info_t *info)
 {
-       sdb_object_t *obj;
-
-       if ((! name) || (! callback))
-               return -1;
-
-       assert(list);
-
-       if (! *list)
-               *list = sdb_llist_create();
-       if (! *list)
-               return -1;
+       int (*mod_init)(sdb_plugin_info_t *);
+       int status;
 
-       obj = sdb_object_create(name, sdb_plugin_cb_type,
-                       list, type, callback, user_data);
-       if (! obj)
+       mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
+       if (! mod_init) {
+               sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
+                               "could not find symbol 'sdb_module_init'", name);
                return -1;
+       }
 
-       if (sdb_llist_append(*list, obj)) {
-               sdb_object_deref(obj);
+       status = mod_init(info);
+       if (status) {
+               sdb_log(SDB_LOG_ERR, "core: Failed to initialize "
+                               "module '%s'", name);
+               plugin_unregister_by_name(name);
                return -1;
        }
-
-       /* pass control to the list */
-       sdb_object_deref(obj);
-
-       sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
-                       type, name);
        return 0;
-} /* plugin_add_callback */
+} /* module_init */
 
-/*
- * public API
- */
-
-int
-sdb_plugin_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
+static int
+module_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
 {
        char  base_name[name ? strlen(name) + 1 : 1];
        const char *name_ptr;
        char *tmp;
 
        char filename[1024];
-       ctx_t *ctx;
-
        lt_dlhandle lh;
 
-       int (*mod_init)(sdb_plugin_info_t *);
-       int status;
+       ctx_t *ctx;
 
-       if ((! name) || (! *name))
-               return -1;
+       int status;
 
        base_name[0] = '\0';
        name_ptr = name;
@@ -449,13 +430,6 @@ sdb_plugin_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
        }
        strcat(base_name, name_ptr);
 
-       ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
-       if (ctx) {
-               /* plugin already loaded */
-               ++ctx->use_cnt;
-               return 0;
-       }
-
        snprintf(filename, sizeof(filename), "%s/%s.so",
                        PKGLIBDIR, base_name);
        filename[sizeof(filename) - 1] = '\0';
@@ -494,21 +468,9 @@ sdb_plugin_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
        if (plugin_ctx)
                ctx->public = *plugin_ctx;
 
-       mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
-       if (! mod_init) {
-               sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
-                               "could not find symbol 'sdb_module_init'", name);
+       if ((status = module_init(name, lh, &ctx->info))) {
                sdb_object_deref(SDB_OBJ(ctx));
-               return -1;
-       }
-
-       status = mod_init(&ctx->info);
-       if (status) {
-               sdb_log(SDB_LOG_ERR, "core: Failed to initialize "
-                               "module '%s'", name);
-               plugin_unregister_by_name(ctx->info.plugin_name);
-               sdb_object_deref(SDB_OBJ(ctx));
-               return -1;
+               return status;
        }
 
        /* compare minor version */
@@ -520,16 +482,6 @@ sdb_plugin_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
                                name, SDB_VERSION_DECODE(ctx->info.version),
                                SDB_VERSION_DECODE(SDB_VERSION));
 
-       if (! all_plugins) {
-               if (! (all_plugins = sdb_llist_create())) {
-                       sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
-                                       "internal error while creating linked list", name);
-                       plugin_unregister_by_name(ctx->info.plugin_name);
-                       sdb_object_deref(SDB_OBJ(ctx));
-                       return -1;
-               }
-       }
-
        sdb_llist_append(all_plugins, SDB_OBJ(ctx));
 
        sdb_log(SDB_LOG_INFO, "core: Successfully loaded "
@@ -545,6 +497,70 @@ sdb_plugin_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
        /* reset */
        ctx_set(NULL);
        return 0;
+} /* module_load */
+
+static int
+plugin_add_callback(sdb_llist_t **list, const char *type,
+               const char *name, void *callback, sdb_object_t *user_data)
+{
+       sdb_object_t *obj;
+
+       if ((! name) || (! callback))
+               return -1;
+
+       assert(list);
+
+       if (! *list)
+               *list = sdb_llist_create();
+       if (! *list)
+               return -1;
+
+       obj = sdb_object_create(name, sdb_plugin_cb_type,
+                       list, type, callback, user_data);
+       if (! obj)
+               return -1;
+
+       if (sdb_llist_append(*list, obj)) {
+               sdb_object_deref(obj);
+               return -1;
+       }
+
+       /* pass control to the list */
+       sdb_object_deref(obj);
+
+       sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
+                       type, name);
+       return 0;
+} /* plugin_add_callback */
+
+/*
+ * public API
+ */
+
+int
+sdb_plugin_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx)
+{
+       ctx_t *ctx;
+
+       if ((! name) || (! *name))
+               return -1;
+
+       if (! all_plugins) {
+               if (! (all_plugins = sdb_llist_create())) {
+                       sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
+                                       "internal error while creating linked list", name);
+                       return -1;
+               }
+       }
+
+       ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
+       if (ctx) {
+               /* plugin already loaded */
+               ++ctx->use_cnt;
+               return 0;
+       }
+
+       return module_load(name, plugin_ctx);
 } /* sdb_plugin_load */
 
 int