plugin.c 6.9 KB

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