Browse Source

Implement plugin.c functions

Serj Kalichev 11 years ago
parent
commit
206a4a1987
4 changed files with 39 additions and 6 deletions
  1. 1 1
      clish/plugin.h
  2. 24 2
      clish/plugin/plugin.c
  3. 1 0
      clish/plugin/private.h
  4. 13 3
      configure.ac

+ 1 - 1
clish/plugin.h

@@ -17,7 +17,7 @@ typedef int clish_plugin_init_t(clish_plugin_t *plugin);
 
 clish_plugin_t *clish_plugin_new(const char *name, const char *file);
 void clish_plugin_free(clish_plugin_t *instance);
-int clish_plugin_load(clish_plugin_t *instance);
+void *clish_plugin_load(clish_plugin_t *instance);
 clish_plugin_fn_t *clish_plugin_dlsym(clish_plugin_t *instance,
 	const char *name);
 int clish_plugin_sym(clish_plugin_t *instance,

+ 24 - 2
clish/plugin/plugin.c

@@ -9,6 +9,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <assert.h>
+#include <dlfcn.h>
 
 /**********************************************************
  * SYM functions                                          *
@@ -63,6 +64,7 @@ clish_plugin_t *clish_plugin_new(const char *name, const char *file)
 		this->name = lub_string_dup(name);
 	else
 		this->name = NULL;
+	this->dlhan = NULL;
 	/* Initialise the list of symbols */
 	this->syms = lub_list_new(clish_sym_compare);
 
@@ -89,6 +91,8 @@ void clish_plugin_free(clish_plugin_t *this)
 		lub_list_node_free(iter);
 	}
 	lub_list_free(this->syms);
+	if (this->dlhan)
+		dlclose(this->dlhan);
 
 	free(this);
 }
@@ -118,17 +122,35 @@ clish_plugin_fn_t *clish_plugin_dlsym(clish_plugin_t *this, const char *name)
 	/* Iterate elements */
 	for(iter = lub_list__get_head(this->syms);
 		iter; iter = lub_list_node__get_next(iter)) {
+		int res;
 		sym = (clish_sym_t *)lub_list_node__get_data(iter);
-
+		res = strcmp(sym->name, name);
+		if (!res)
+			return sym->func;
+		if (res > 0) /* No chances to find name */
+			break;
 	}
 
 	return NULL;
 }
 
 /*--------------------------------------------------------- */
-int clish_plugin_load(clish_plugin_t *this)
+void *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)))
+		return NULL;
+	plugin_init = (clish_plugin_init_t *)dlsym(this->dlhan, CLISH_PLUGIN_INIT);
+	if (!plugin_init) {
+		dlclose(this->dlhan);
+		this->dlhan = NULL;
+		return NULL;
+	}
+	plugin_init(this);
 
 	return 0;
 }

+ 1 - 0
clish/plugin/private.h

@@ -18,4 +18,5 @@ struct clish_plugin_s {
 	char *file; /* Plugin file name. Must be unique. */
 	char *name; /* Local plugin name. Can be used in builtin ref. */
 	lub_list_t *syms; /* List of plugin symbols */
+	void *dlhan;
 };

+ 13 - 3
configure.ac

@@ -1,8 +1,8 @@
 #                                               -*- Autoconf -*-
 # Process this file with autoconf to produce a configure script.
-m4_define([MAJOR_VERSION], 1)
-m4_define([MINOR_VERSION], 6)
-m4_define([MICRO_VERSION], 2)
+m4_define([MAJOR_VERSION], 2)
+m4_define([MINOR_VERSION], 0)
+m4_define([MICRO_VERSION], 0)
 
 AC_PREREQ(2.59)
 AC_INIT([klish],
@@ -444,5 +444,15 @@ AC_CHECK_HEADERS(grp.h, [],
 AC_CHECK_FUNCS(chroot, [],
     AC_MSG_WARN([chroot() not found: the choot is not supported]))
 
+################################
+# Check for dlopen
+################################
+AC_CHECK_HEADERS(dlfcn.h, [
+        AC_SEARCH_LIBS([dlopen], [dl dld], [], [
+          AC_MSG_WARN([unable to find the dlopen() function])
+        ])
+    ],
+    AC_MSG_WARN([dlfcn.h not found: the dl operations is not supported]))
+
 AC_CONFIG_FILES(Makefile)
 AC_OUTPUT