plugin.c 6.8 KB

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