1
0

plugin.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * plugin.c
  3. */
  4. #ifdef HAVE_CONFIG_H
  5. #include "config.h"
  6. #endif /* HAVE_CONFIG_H */
  7. #include "private.h"
  8. #include "lub/string.h"
  9. #include "lub/list.h"
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <assert.h>
  14. #ifdef HAVE_DLFCN_H
  15. #include <dlfcn.h>
  16. #endif
  17. /*--------------------------------------------------------- */
  18. clish_plugin_t *clish_plugin_new(const char *name)
  19. {
  20. clish_plugin_t *this;
  21. this = malloc(sizeof(*this));
  22. this->name = lub_string_dup(name);
  23. this->conf = NULL;
  24. this->alias = NULL;
  25. this->file = NULL;
  26. this->builtin_flag = BOOL_FALSE; /* The plugin is shared object by default */
  27. this->dlhan = NULL;
  28. /* Initialise the list of symbols */
  29. this->syms = lub_list_new(clish_sym_compare);
  30. /* Constructor and destructor */
  31. this->init = NULL;
  32. this->fini = NULL;
  33. /* Flags */
  34. this->rtld_global = BOOL_FALSE; /* The dlopen() use RTLD_LOCAL by default */
  35. return this;
  36. }
  37. /*--------------------------------------------------------- */
  38. void clish_plugin_free(clish_plugin_t *this, void *userdata)
  39. {
  40. lub_list_node_t *iter;
  41. if (!this)
  42. return;
  43. /* Execute destructor */
  44. if (this->fini)
  45. this->fini(userdata, this);
  46. lub_string_free(this->name);
  47. lub_string_free(this->alias);
  48. lub_string_free(this->file);
  49. lub_string_free(this->conf);
  50. /* Free symbol list */
  51. while ((iter = lub_list__get_head(this->syms))) {
  52. /* Remove the symbol from the list */
  53. lub_list_del(this->syms, iter);
  54. /* Free the instance */
  55. clish_sym_free((clish_sym_t *)lub_list_node__get_data(iter));
  56. lub_list_node_free(iter);
  57. }
  58. lub_list_free(this->syms);
  59. #ifdef HAVE_DLFCN_H
  60. if (this->dlhan)
  61. dlclose(this->dlhan);
  62. #endif
  63. free(this);
  64. }
  65. /*--------------------------------------------------------- */
  66. clish_sym_t *clish_plugin_add_generic(clish_plugin_t *this,
  67. void *func, const char *name, int type, bool_t permanent)
  68. {
  69. clish_sym_t *sym;
  70. if (!name || !func)
  71. return NULL;
  72. if (!(sym = clish_sym_new(name, func, type)))
  73. return NULL;
  74. clish_sym__set_plugin(sym, this);
  75. clish_sym__set_permanent(sym, permanent);
  76. lub_list_add(this->syms, sym);
  77. return sym;
  78. }
  79. /*--------------------------------------------------------- */
  80. clish_sym_t *clish_plugin_add_sym(clish_plugin_t *this,
  81. clish_hook_action_fn_t *func, const char *name)
  82. {
  83. return clish_plugin_add_generic(this, func,
  84. name, CLISH_SYM_TYPE_ACTION, BOOL_FALSE);
  85. }
  86. /*--------------------------------------------------------- */
  87. /* Add permanent symbol (can't be turned off by dry-run) */
  88. clish_sym_t *clish_plugin_add_psym(clish_plugin_t *this,
  89. clish_hook_action_fn_t *func, const char *name)
  90. {
  91. return clish_plugin_add_generic(this, func,
  92. name, CLISH_SYM_TYPE_ACTION, BOOL_TRUE);
  93. }
  94. /*--------------------------------------------------------- */
  95. clish_sym_t *clish_plugin_add_osym(clish_plugin_t *this,
  96. clish_hook_oaction_fn_t *func, const char *name)
  97. {
  98. clish_sym_t *s;
  99. if (!(s = clish_plugin_add_generic(this, func,
  100. name, CLISH_SYM_TYPE_ACTION, BOOL_FALSE)))
  101. return s;
  102. clish_sym__set_api(s, CLISH_SYM_API_STDOUT);
  103. return s;
  104. }
  105. /*--------------------------------------------------------- */
  106. /* Add permanent symbol (can't be turned off by dry-run) */
  107. clish_sym_t *clish_plugin_add_posym(clish_plugin_t *this,
  108. clish_hook_oaction_fn_t *func, const char *name)
  109. {
  110. clish_sym_t *s;
  111. if (!(s = clish_plugin_add_generic(this, func,
  112. name, CLISH_SYM_TYPE_ACTION, BOOL_TRUE)))
  113. return s;
  114. clish_sym__set_api(s, CLISH_SYM_API_STDOUT);
  115. return s;
  116. }
  117. /*--------------------------------------------------------- */
  118. clish_sym_t *clish_plugin_add_hook(clish_plugin_t *this,
  119. void *func, const char *name, int type)
  120. {
  121. return clish_plugin_add_generic(this, func,
  122. name, type, BOOL_FALSE);
  123. }
  124. /*--------------------------------------------------------- */
  125. clish_sym_t *clish_plugin_add_phook(clish_plugin_t *this,
  126. void *func, const char *name, int type)
  127. {
  128. return clish_plugin_add_generic(this, func,
  129. name, type, BOOL_TRUE);
  130. }
  131. /*--------------------------------------------------------- */
  132. clish_sym_t *clish_plugin_get_sym(clish_plugin_t *this, const char *name, int type)
  133. {
  134. lub_list_node_t *iter;
  135. clish_sym_t *sym;
  136. /* Iterate elements */
  137. for(iter = lub_list__get_head(this->syms);
  138. iter; iter = lub_list_node__get_next(iter)) {
  139. int res;
  140. sym = (clish_sym_t *)lub_list_node__get_data(iter);
  141. res = strcmp(clish_sym__get_name(sym), name);
  142. if (!res && ((CLISH_SYM_TYPE_NONE == type) || (clish_sym__get_type(sym) == type)))
  143. return sym;
  144. if (res > 0) /* No chances to find name */
  145. break;
  146. }
  147. return NULL;
  148. }
  149. /*--------------------------------------------------------- */
  150. static int clish_plugin_load_shared(clish_plugin_t *this)
  151. {
  152. #ifdef HAVE_DLFCN_H
  153. char *file = NULL; /* Plugin so file name */
  154. char *init_name = NULL; /* Init function name */
  155. int flag = RTLD_NOW;
  156. if (this->file) {
  157. file = lub_string_dup(this->file);
  158. } else {
  159. lub_string_cat(&file, "clish_plugin_");
  160. lub_string_cat(&file, this->name);
  161. lub_string_cat(&file, ".so");
  162. }
  163. /* Open dynamic library */
  164. if (clish_plugin__get_rtld_global(this))
  165. flag |= RTLD_GLOBAL;
  166. else
  167. flag |= RTLD_LOCAL;
  168. this->dlhan = dlopen(file, flag);
  169. lub_string_free(file);
  170. if (!this->dlhan) {
  171. fprintf(stderr, "Error: Can't open plugin \"%s\": %s\n",
  172. this->name, dlerror());
  173. return -1;
  174. }
  175. /* Get plugin init function */
  176. lub_string_cat(&init_name, CLISH_PLUGIN_INIT_NAME_PREFIX);
  177. lub_string_cat(&init_name, this->name);
  178. lub_string_cat(&init_name, CLISH_PLUGIN_INIT_NAME_SUFFIX);
  179. this->init = (clish_plugin_init_t *)dlsym(this->dlhan, init_name);
  180. lub_string_free(init_name);
  181. if (!this->init) {
  182. fprintf(stderr, "Error: Can't get plugin \"%s\" init function: %s\n",
  183. this->name, dlerror());
  184. return -1;
  185. }
  186. return 0;
  187. #else /* HAVE_DLFCN_H */
  188. /* We have no any dl functions. */
  189. fprintf(stderr, "Error: Can't get plugin \"%s\" init function.\n",
  190. this->name);
  191. return -1;
  192. #endif /* HAVE_DLFCN_H */
  193. }
  194. /*--------------------------------------------------------- */
  195. int clish_plugin_load(clish_plugin_t *this, void *userdata)
  196. {
  197. int res;
  198. if (!this)
  199. return -1;
  200. assert(this->name);
  201. /* Builtin plugins already have init function. */
  202. if (!this->builtin_flag) {
  203. if (clish_plugin_load_shared(this) < 0)
  204. return -1;
  205. }
  206. if (!this->init) {
  207. fprintf(stderr, "Error: PLUGIN %s has no init function\n",
  208. this->name);
  209. return -1;
  210. }
  211. /* Execute init function */
  212. if ((res = this->init(userdata, this)))
  213. fprintf(stderr, "Error: Plugin %s init retcode: %d\n",
  214. this->name, res);
  215. return res;
  216. }
  217. CLISH_SET(plugin, clish_plugin_fini_t *, fini);
  218. CLISH_GET(plugin, clish_plugin_fini_t *, fini);
  219. CLISH_SET(plugin, clish_plugin_init_t *, init);
  220. CLISH_GET(plugin, clish_plugin_init_t *, init);
  221. CLISH_GET_STR(plugin, name);
  222. CLISH_SET_STR(plugin, alias);
  223. CLISH_GET_STR(plugin, alias);
  224. CLISH_SET_STR(plugin, file);
  225. CLISH_GET_STR(plugin, file);
  226. CLISH_SET_STR(plugin, conf);
  227. CLISH_GET_STR(plugin, conf);
  228. CLISH_SET(plugin, bool_t, builtin_flag);
  229. CLISH_GET(plugin, bool_t, builtin_flag);
  230. CLISH_SET(plugin, bool_t, rtld_global);
  231. CLISH_GET(plugin, bool_t, rtld_global);
  232. /*--------------------------------------------------------- */
  233. _CLISH_GET_STR(plugin, pubname)
  234. {
  235. assert(inst);
  236. return (inst->alias ? inst->alias : inst->name);
  237. }