plugin.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. * SYM functions *
  19. **********************************************************/
  20. /*--------------------------------------------------------- */
  21. int clish_sym_compare(const void *first, const void *second)
  22. {
  23. const clish_sym_t *f = (const clish_sym_t *)first;
  24. const clish_sym_t *s = (const clish_sym_t *)second;
  25. return strcmp(f->name, s->name);
  26. }
  27. /*--------------------------------------------------------- */
  28. clish_sym_t *clish_sym_new(const char *name, void *func, int type)
  29. {
  30. clish_sym_t *this;
  31. this = malloc(sizeof(*this));
  32. this->name = lub_string_dup(name);
  33. this->func = func;
  34. this->type = type;
  35. this->permanent = BOOL_FALSE;
  36. return this;
  37. }
  38. /*--------------------------------------------------------- */
  39. void clish_sym_free(clish_sym_t *this)
  40. {
  41. if (!this)
  42. return;
  43. lub_string_free(this->name);
  44. free(this);
  45. }
  46. /*--------------------------------------------------------- */
  47. void clish_sym__set_func(clish_sym_t *this, void *func)
  48. {
  49. this->func = func;
  50. }
  51. /*--------------------------------------------------------- */
  52. void *clish_sym__get_func(clish_sym_t *this)
  53. {
  54. return this->func;
  55. }
  56. /*--------------------------------------------------------- */
  57. void clish_sym__set_permanent(clish_sym_t *this, bool_t permanent)
  58. {
  59. this->permanent = permanent;
  60. }
  61. /*--------------------------------------------------------- */
  62. bool_t clish_sym__get_permanent(clish_sym_t *this)
  63. {
  64. return this->permanent;
  65. }
  66. /*--------------------------------------------------------- */
  67. void clish_sym__set_name(clish_sym_t *this, const char *name)
  68. {
  69. lub_string_free(this->name);
  70. this->name = lub_string_dup(name);
  71. }
  72. /*--------------------------------------------------------- */
  73. char *clish_sym__get_name(clish_sym_t *this)
  74. {
  75. return this->name;
  76. }
  77. /*--------------------------------------------------------- */
  78. void clish_sym__set_plugin(clish_sym_t *this, clish_plugin_t *plugin)
  79. {
  80. this->plugin = plugin;
  81. }
  82. /*--------------------------------------------------------- */
  83. clish_plugin_t *clish_sym__get_plugin(clish_sym_t *this)
  84. {
  85. return this->plugin;
  86. }
  87. /*--------------------------------------------------------- */
  88. void clish_sym__set_type(clish_sym_t *this, int type)
  89. {
  90. this->type = type;
  91. }
  92. /*--------------------------------------------------------- */
  93. int clish_sym__get_type(clish_sym_t *this)
  94. {
  95. return this->type;
  96. }
  97. /*--------------------------------------------------------- */
  98. int clish_sym_clone(clish_sym_t *dst, clish_sym_t *src)
  99. {
  100. char *name;
  101. if (!dst || !src)
  102. return -1;
  103. name = dst->name;
  104. *dst = *src;
  105. dst->name = name;
  106. return 0;
  107. }
  108. /**********************************************************
  109. * PLUGIN functions *
  110. **********************************************************/
  111. /*--------------------------------------------------------- */
  112. clish_plugin_t *clish_plugin_new(const char *name)
  113. {
  114. clish_plugin_t *this;
  115. this = malloc(sizeof(*this));
  116. this->name = lub_string_dup(name);
  117. this->conf = NULL;
  118. this->alias = NULL;
  119. this->file = NULL;
  120. this->dlhan = NULL;
  121. /* Initialise the list of symbols */
  122. this->syms = lub_list_new(clish_sym_compare);
  123. /* Constructor and destructor */
  124. this->init = NULL;
  125. this->fini = NULL;
  126. return this;
  127. }
  128. /*--------------------------------------------------------- */
  129. void clish_plugin_free(clish_plugin_t *this, void *userdata)
  130. {
  131. lub_list_node_t *iter;
  132. if (!this)
  133. return;
  134. /* Execute destructor */
  135. if (this->fini)
  136. this->fini(userdata, this);
  137. lub_string_free(this->name);
  138. lub_string_free(this->alias);
  139. lub_string_free(this->file);
  140. lub_string_free(this->conf);
  141. /* Free symbol list */
  142. while ((iter = lub_list__get_head(this->syms))) {
  143. /* Remove the symbol from the list */
  144. lub_list_del(this->syms, iter);
  145. /* Free the instance */
  146. clish_sym_free((clish_sym_t *)lub_list_node__get_data(iter));
  147. lub_list_node_free(iter);
  148. }
  149. lub_list_free(this->syms);
  150. #ifdef HAVE_DLFCN_H
  151. if (this->dlhan)
  152. dlclose(this->dlhan);
  153. #endif
  154. free(this);
  155. }
  156. /*--------------------------------------------------------- */
  157. clish_sym_t *clish_plugin_add_generic(clish_plugin_t *this,
  158. void *func, const char *name, int type, bool_t permanent)
  159. {
  160. clish_sym_t *sym;
  161. if (!name || !func)
  162. return NULL;
  163. if (!(sym = clish_sym_new(name, func, type)))
  164. return NULL;
  165. clish_sym__set_plugin(sym, this);
  166. clish_sym__set_permanent(sym, permanent);
  167. lub_list_add(this->syms, sym);
  168. return sym;
  169. }
  170. /*--------------------------------------------------------- */
  171. clish_sym_t *clish_plugin_add_sym(clish_plugin_t *this,
  172. clish_hook_action_fn_t *func, const char *name)
  173. {
  174. return clish_plugin_add_generic(this, func,
  175. name, CLISH_SYM_TYPE_ACTION, BOOL_FALSE);
  176. }
  177. /*--------------------------------------------------------- */
  178. /* Add permanent symbol (can't be turned off by dry-run) */
  179. clish_sym_t *clish_plugin_add_psym(clish_plugin_t *this,
  180. clish_hook_action_fn_t *func, const char *name)
  181. {
  182. return clish_plugin_add_generic(this, func,
  183. name, CLISH_SYM_TYPE_ACTION, BOOL_TRUE);
  184. }
  185. /*--------------------------------------------------------- */
  186. clish_sym_t *clish_plugin_add_hook(clish_plugin_t *this,
  187. void *func, const char *name, int type)
  188. {
  189. return clish_plugin_add_generic(this, func,
  190. name, type, BOOL_FALSE);
  191. }
  192. /*--------------------------------------------------------- */
  193. clish_sym_t *clish_plugin_add_phook(clish_plugin_t *this,
  194. void *func, const char *name, int type)
  195. {
  196. return clish_plugin_add_generic(this, func,
  197. name, type, BOOL_TRUE);
  198. }
  199. /*--------------------------------------------------------- */
  200. void clish_plugin_add_fini(clish_plugin_t *this,
  201. clish_plugin_fini_t *fini)
  202. {
  203. this->fini = fini;
  204. }
  205. /*--------------------------------------------------------- */
  206. clish_sym_t *clish_plugin_get_sym(clish_plugin_t *this, const char *name, int type)
  207. {
  208. lub_list_node_t *iter;
  209. clish_sym_t *sym;
  210. /* Iterate elements */
  211. for(iter = lub_list__get_head(this->syms);
  212. iter; iter = lub_list_node__get_next(iter)) {
  213. int res;
  214. sym = (clish_sym_t *)lub_list_node__get_data(iter);
  215. res = strcmp(clish_sym__get_name(sym), name);
  216. if (!res && ((CLISH_SYM_TYPE_NONE == type) || (clish_sym__get_type(sym) == type)))
  217. return sym;
  218. if (res > 0) /* No chances to find name */
  219. break;
  220. }
  221. return NULL;
  222. }
  223. /*--------------------------------------------------------- */
  224. int clish_plugin_load(clish_plugin_t *this, void *userdata)
  225. {
  226. int res;
  227. char *file = NULL; /* Plugin so file name */
  228. char *init_name = NULL; /* Init function name */
  229. if (!this)
  230. return -1;
  231. assert(this->name);
  232. #ifdef HAVE_DLFCN_H
  233. if (this->file) {
  234. file = lub_string_dup(this->file);
  235. } else {
  236. lub_string_cat(&file, "clish_plugin_");
  237. lub_string_cat(&file, this->name);
  238. lub_string_cat(&file, ".so");
  239. }
  240. /* Open dynamic library */
  241. this->dlhan = dlopen(file, RTLD_NOW | RTLD_LOCAL);
  242. lub_string_free(file);
  243. if (!this->dlhan) {
  244. fprintf(stderr, "Error: Can't open plugin %s: %s\n",
  245. this->name, dlerror());
  246. return -1;
  247. }
  248. /* Get plugin init function */
  249. lub_string_cat(&init_name, CLISH_PLUGIN_INIT_NAME_PREFIX);
  250. lub_string_cat(&init_name, this->name);
  251. lub_string_cat(&init_name, CLISH_PLUGIN_INIT_NAME_SUFFIX);
  252. this->init = (clish_plugin_init_t *)dlsym(this->dlhan, init_name);
  253. lub_string_free(init_name);
  254. if (!this->init) {
  255. fprintf(stderr, "Error: Can't get plugin %s init function: %s\n",
  256. this->name, dlerror());
  257. return -1;
  258. }
  259. #else /* HAVE_DLFCN_H */
  260. /* We have no any dl functions. */
  261. #endif /* HAVE_DLFCN_H */
  262. /* Execute init function */
  263. if ((res = this->init(userdata, this)))
  264. fprintf(stderr, "Error: Plugin %s init retcode: %d\n",
  265. this->name, res);
  266. return res;
  267. }
  268. /*--------------------------------------------------------- */
  269. char *clish_plugin__get_name(const clish_plugin_t *this)
  270. {
  271. return this->name;
  272. }
  273. /*--------------------------------------------------------- */
  274. void clish_plugin__set_alias(clish_plugin_t *this, const char *alias)
  275. {
  276. lub_string_free(this->alias);
  277. this->alias = lub_string_dup(alias);
  278. }
  279. /*--------------------------------------------------------- */
  280. char *clish_plugin__get_alias(const clish_plugin_t *this)
  281. {
  282. return this->alias;
  283. }
  284. /*--------------------------------------------------------- */
  285. char *clish_plugin__get_pubname(const clish_plugin_t *this)
  286. {
  287. return (this->alias ? this->alias : this->name);
  288. }
  289. /*--------------------------------------------------------- */
  290. void clish_plugin__set_file(clish_plugin_t *this, const char *file)
  291. {
  292. lub_string_free(this->file);
  293. this->file = lub_string_dup(file);
  294. }
  295. /*--------------------------------------------------------- */
  296. char *clish_plugin__get_file(const clish_plugin_t *this)
  297. {
  298. return this->file;
  299. }
  300. /*--------------------------------------------------------- */
  301. void clish_plugin__set_conf(clish_plugin_t *this, const char *conf)
  302. {
  303. lub_string_free(this->conf);
  304. this->conf = lub_string_dup(conf);
  305. }
  306. /*--------------------------------------------------------- */
  307. char *clish_plugin__get_conf(const clish_plugin_t *this)
  308. {
  309. return this->conf;
  310. }
  311. /*--------------------------------------------------------- */