Browse Source

Change plugin init prototype. RTLD_LOCAL while dlopen

Serj Kalichev 11 years ago
parent
commit
1b2ddac0cb
3 changed files with 12 additions and 9 deletions
  1. 6 3
      clish/plugin.h
  2. 3 5
      clish/plugin/plugin.c
  3. 3 1
      clish/shell/shell_plugin.c

+ 6 - 3
clish/plugin.h

@@ -28,12 +28,14 @@ typedef struct clish_plugin_s clish_plugin_t;
 /* Name of init function within plugin */
 #define CLISH_PLUGIN_INIT_FNAME clish_plugin_init
 #define CLISH_PLUGIN_INIT_NAME "clish_plugin_init"
-#define CLISH_PLUGIN_INIT_FUNC(name) int name(clish_plugin_t *plugin)
+#define CLISH_PLUGIN_INIT_FUNC(name) int name(void *clish_shell, clish_plugin_t *plugin)
 #define CLISH_PLUGIN_INIT CLISH_PLUGIN_INIT_FUNC(CLISH_PLUGIN_INIT_FNAME)
+#define CLISH_PLUGIN_FINI(name) int name(void *clish_shell, clish_plugin_t *plugin)
 
-#define CLISH_PLUGIN_SYM(name) int name(void *clish_context, const char *script, char **out)
 #define CLISH_HOOK_INIT(name) int name(void *clish_context)
 #define CLISH_HOOK_FINI(name) int name(void *clish_context)
+
+#define CLISH_PLUGIN_SYM(name) int name(void *clish_context, const char *script, char **out)
 #define CLISH_HOOK_ACCESS(name) int name(void *clish_context, const char *access)
 #define CLISH_HOOK_CONFIG(name) int name(void *clish_context)
 #define CLISH_HOOK_LOG(name) int name(void *clish_context, const char *line, int retcode)
@@ -47,6 +49,7 @@ typedef struct clish_plugin_s clish_plugin_t;
 /* typedef void clish_shell_cmd_line_fn_t(clish_context_t *context, const char *cmd_line); */
 
 typedef CLISH_PLUGIN_INIT_FUNC(clish_plugin_init_t);
+typedef CLISH_PLUGIN_FINI(clish_plugin_fini_t);
 typedef CLISH_PLUGIN_SYM(clish_hook_action_fn_t);
 typedef CLISH_HOOK_INIT(clish_hook_init_fn_t);
 typedef CLISH_HOOK_FINI(clish_hook_fini_fn_t);
@@ -78,7 +81,7 @@ int clish_sym_clone(clish_sym_t *dst, clish_sym_t *src);
 
 clish_plugin_t *clish_plugin_new(const char *name, const char *file);
 void clish_plugin_free(clish_plugin_t *instance);
-void *clish_plugin_load(clish_plugin_t *instance);
+clish_plugin_init_t *clish_plugin_load(clish_plugin_t *instance);
 clish_sym_t *clish_plugin_get_sym(clish_plugin_t *instance,
 	const char *name, int type);
 clish_sym_t *clish_plugin_add_generic(clish_plugin_t *instance,

+ 3 - 5
clish/plugin/plugin.c

@@ -247,24 +247,22 @@ clish_sym_t *clish_plugin_get_sym(clish_plugin_t *this, const char *name, int ty
 }
 
 /*--------------------------------------------------------- */
-void *clish_plugin_load(clish_plugin_t *this)
+clish_plugin_init_t *clish_plugin_load(clish_plugin_t *this)
 {
 	clish_plugin_init_t *plugin_init;
 
 	if (!this)
 		return NULL;
 
-	if (!(this->dlhan = dlopen(this->file, RTLD_NOW | RTLD_GLOBAL)))
+	if (!(this->dlhan = dlopen(this->file, RTLD_NOW | RTLD_LOCAL)))
 		return NULL;
 	plugin_init = (clish_plugin_init_t *)dlsym(this->dlhan, CLISH_PLUGIN_INIT_NAME);
 	if (!plugin_init) {
 		dlclose(this->dlhan);
 		this->dlhan = NULL;
-		return NULL;
 	}
-	plugin_init(this);
 
-	return this->dlhan;
+	return plugin_init;
 }
 
 /*--------------------------------------------------------- */

+ 3 - 1
clish/shell/shell_plugin.c

@@ -21,6 +21,7 @@ int clish_shell_load_plugins(clish_shell_t *this)
 {
 	lub_list_node_t *iter;
 	clish_plugin_t *plugin;
+	clish_plugin_init_t *plugin_init;
 
 	assert(this);
 
@@ -28,11 +29,12 @@ int clish_shell_load_plugins(clish_shell_t *this)
 	for(iter = lub_list__get_head(this->plugins);
 		iter; iter = lub_list_node__get_next(iter)) {
 		plugin = (clish_plugin_t *)lub_list_node__get_data(iter);
-		if (!clish_plugin_load(plugin)) {
+		if (!(plugin_init = clish_plugin_load(plugin))) {
 			fprintf(stderr, "Error: Can't load plugin %s.\n",
 				clish_plugin__get_file(plugin));
 			return -1;
 		}
+		plugin_init(this, plugin);
 #ifdef DEBUG
 		clish_plugin_dump(plugin);
 #endif