1
0

plugin.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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->builtin_flag = BOOL_FALSE; /* The plugin is shared object by default */
  121. this->dlhan = NULL;
  122. /* Initialise the list of symbols */
  123. this->syms = lub_list_new(clish_sym_compare);
  124. /* Constructor and destructor */
  125. this->init = NULL;
  126. this->fini = NULL;
  127. return this;
  128. }
  129. /*--------------------------------------------------------- */
  130. void clish_plugin_free(clish_plugin_t *this, void *userdata)
  131. {
  132. lub_list_node_t *iter;
  133. if (!this)
  134. return;
  135. /* Execute destructor */
  136. if (this->fini)
  137. this->fini(userdata, this);
  138. lub_string_free(this->name);
  139. lub_string_free(this->alias);
  140. lub_string_free(this->file);
  141. lub_string_free(this->conf);
  142. /* Free symbol list */
  143. while ((iter = lub_list__get_head(this->syms))) {
  144. /* Remove the symbol from the list */
  145. lub_list_del(this->syms, iter);
  146. /* Free the instance */
  147. clish_sym_free((clish_sym_t *)lub_list_node__get_data(iter));
  148. lub_list_node_free(iter);
  149. }
  150. lub_list_free(this->syms);
  151. #ifdef HAVE_DLFCN_H
  152. if (this->dlhan)
  153. dlclose(this->dlhan);
  154. #endif
  155. free(this);
  156. }
  157. /*--------------------------------------------------------- */
  158. clish_sym_t *clish_plugin_add_generic(clish_plugin_t *this,
  159. void *func, const char *name, int type, bool_t permanent)
  160. {
  161. clish_sym_t *sym;
  162. if (!name || !func)
  163. return NULL;
  164. if (!(sym = clish_sym_new(name, func, type)))
  165. return NULL;
  166. clish_sym__set_plugin(sym, this);
  167. clish_sym__set_permanent(sym, permanent);
  168. lub_list_add(this->syms, sym);
  169. return sym;
  170. }
  171. /*--------------------------------------------------------- */
  172. clish_sym_t *clish_plugin_add_sym(clish_plugin_t *this,
  173. clish_hook_action_fn_t *func, const char *name)
  174. {
  175. return clish_plugin_add_generic(this, func,
  176. name, CLISH_SYM_TYPE_ACTION, BOOL_FALSE);
  177. }
  178. /*--------------------------------------------------------- */
  179. /* Add permanent symbol (can't be turned off by dry-run) */
  180. clish_sym_t *clish_plugin_add_psym(clish_plugin_t *this,
  181. clish_hook_action_fn_t *func, const char *name)
  182. {
  183. return clish_plugin_add_generic(this, func,
  184. name, CLISH_SYM_TYPE_ACTION, BOOL_TRUE);
  185. }
  186. /*--------------------------------------------------------- */
  187. clish_sym_t *clish_plugin_add_hook(clish_plugin_t *this,
  188. void *func, const char *name, int type)
  189. {
  190. return clish_plugin_add_generic(this, func,
  191. name, type, BOOL_FALSE);
  192. }
  193. /*--------------------------------------------------------- */
  194. clish_sym_t *clish_plugin_add_phook(clish_plugin_t *this,
  195. void *func, const char *name, int type)
  196. {
  197. return clish_plugin_add_generic(this, func,
  198. name, type, BOOL_TRUE);
  199. }
  200. /*--------------------------------------------------------- */
  201. void clish_plugin_add_fini(clish_plugin_t *this,
  202. clish_plugin_fini_t *fini)
  203. {
  204. this->fini = fini;
  205. }
  206. /*--------------------------------------------------------- */
  207. clish_plugin_fini_t * clish_plugin_get_fini(clish_plugin_t *this)
  208. {
  209. return this->fini;
  210. }
  211. /*--------------------------------------------------------- */
  212. void clish_plugin_add_init(clish_plugin_t *this,
  213. clish_plugin_init_t *init)
  214. {
  215. this->init = init;
  216. }
  217. /*--------------------------------------------------------- */
  218. clish_plugin_init_t * clish_plugin_get_init(clish_plugin_t *this)
  219. {
  220. return this->init;
  221. }
  222. /*--------------------------------------------------------- */
  223. clish_sym_t *clish_plugin_get_sym(clish_plugin_t *this, const char *name, int type)
  224. {
  225. lub_list_node_t *iter;
  226. clish_sym_t *sym;
  227. /* Iterate elements */
  228. for(iter = lub_list__get_head(this->syms);
  229. iter; iter = lub_list_node__get_next(iter)) {
  230. int res;
  231. sym = (clish_sym_t *)lub_list_node__get_data(iter);
  232. res = strcmp(clish_sym__get_name(sym), name);
  233. if (!res && ((CLISH_SYM_TYPE_NONE == type) || (clish_sym__get_type(sym) == type)))
  234. return sym;
  235. if (res > 0) /* No chances to find name */
  236. break;
  237. }
  238. return NULL;
  239. }
  240. /*--------------------------------------------------------- */
  241. static int clish_plugin_load_shared(clish_plugin_t *this)
  242. {
  243. #ifdef HAVE_DLFCN_H
  244. char *file = NULL; /* Plugin so file name */
  245. char *init_name = NULL; /* Init function name */
  246. if (this->file) {
  247. file = lub_string_dup(this->file);
  248. } else {
  249. lub_string_cat(&file, "clish_plugin_");
  250. lub_string_cat(&file, this->name);
  251. lub_string_cat(&file, ".so");
  252. }
  253. /* Open dynamic library */
  254. this->dlhan = dlopen(file, RTLD_NOW | RTLD_LOCAL);
  255. lub_string_free(file);
  256. if (!this->dlhan) {
  257. fprintf(stderr, "Error: Can't open plugin \"%s\": %s\n",
  258. this->name, dlerror());
  259. return -1;
  260. }
  261. /* Get plugin init function */
  262. lub_string_cat(&init_name, CLISH_PLUGIN_INIT_NAME_PREFIX);
  263. lub_string_cat(&init_name, this->name);
  264. lub_string_cat(&init_name, CLISH_PLUGIN_INIT_NAME_SUFFIX);
  265. this->init = (clish_plugin_init_t *)dlsym(this->dlhan, init_name);
  266. lub_string_free(init_name);
  267. if (!this->init) {
  268. fprintf(stderr, "Error: Can't get plugin \"%s\" init function: %s\n",
  269. this->name, dlerror());
  270. return -1;
  271. }
  272. return 0;
  273. #else /* HAVE_DLFCN_H */
  274. /* We have no any dl functions. */
  275. fprintf(stderr, "Error: Can't get plugin \"%s\" init function.\n",
  276. this->name);
  277. return -1;
  278. #endif /* HAVE_DLFCN_H */
  279. }
  280. /*--------------------------------------------------------- */
  281. int clish_plugin_load(clish_plugin_t *this, void *userdata)
  282. {
  283. int res;
  284. if (!this)
  285. return -1;
  286. assert(this->name);
  287. /* Builtin plugins already have init function. */
  288. if (!this->builtin_flag) {
  289. if (clish_plugin_load_shared(this) < 0)
  290. return -1;
  291. }
  292. if (!this->init) {
  293. fprintf(stderr, "Error: PLUGIN %s has no init function\n",
  294. this->name);
  295. return -1;
  296. }
  297. /* Execute init function */
  298. if ((res = this->init(userdata, this)))
  299. fprintf(stderr, "Error: Plugin %s init retcode: %d\n",
  300. this->name, res);
  301. return res;
  302. }
  303. /*--------------------------------------------------------- */
  304. char *clish_plugin__get_name(const clish_plugin_t *this)
  305. {
  306. return this->name;
  307. }
  308. /*--------------------------------------------------------- */
  309. void clish_plugin__set_alias(clish_plugin_t *this, const char *alias)
  310. {
  311. lub_string_free(this->alias);
  312. this->alias = lub_string_dup(alias);
  313. }
  314. /*--------------------------------------------------------- */
  315. char *clish_plugin__get_alias(const clish_plugin_t *this)
  316. {
  317. return this->alias;
  318. }
  319. /*--------------------------------------------------------- */
  320. char *clish_plugin__get_pubname(const clish_plugin_t *this)
  321. {
  322. return (this->alias ? this->alias : this->name);
  323. }
  324. /*--------------------------------------------------------- */
  325. void clish_plugin__set_file(clish_plugin_t *this, const char *file)
  326. {
  327. lub_string_free(this->file);
  328. this->file = lub_string_dup(file);
  329. }
  330. /*--------------------------------------------------------- */
  331. char *clish_plugin__get_file(const clish_plugin_t *this)
  332. {
  333. return this->file;
  334. }
  335. /*--------------------------------------------------------- */
  336. void clish_plugin__set_builtin_flag(clish_plugin_t *this, bool_t builtin_flag)
  337. {
  338. this->builtin_flag = builtin_flag;
  339. }
  340. /*--------------------------------------------------------- */
  341. bool_t clish_plugin__get_builtin_flag(const clish_plugin_t *this)
  342. {
  343. return this->builtin_flag;
  344. }
  345. /*--------------------------------------------------------- */
  346. void clish_plugin__set_conf(clish_plugin_t *this, const char *conf)
  347. {
  348. lub_string_free(this->conf);
  349. this->conf = lub_string_dup(conf);
  350. }
  351. /*--------------------------------------------------------- */
  352. char *clish_plugin__get_conf(const clish_plugin_t *this)
  353. {
  354. return this->conf;
  355. }
  356. /*--------------------------------------------------------- */