plugin.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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->api = CLISH_SYM_API_SIMPLE;
  36. this->permanent = BOOL_FALSE;
  37. return this;
  38. }
  39. /*--------------------------------------------------------- */
  40. void clish_sym_free(clish_sym_t *this)
  41. {
  42. if (!this)
  43. return;
  44. lub_string_free(this->name);
  45. free(this);
  46. }
  47. /*--------------------------------------------------------- */
  48. void clish_sym__set_func(clish_sym_t *this, void *func)
  49. {
  50. this->func = func;
  51. }
  52. /*--------------------------------------------------------- */
  53. void *clish_sym__get_func(clish_sym_t *this)
  54. {
  55. return this->func;
  56. }
  57. /*--------------------------------------------------------- */
  58. void clish_sym__set_permanent(clish_sym_t *this, bool_t permanent)
  59. {
  60. this->permanent = permanent;
  61. }
  62. /*--------------------------------------------------------- */
  63. bool_t clish_sym__get_permanent(clish_sym_t *this)
  64. {
  65. return this->permanent;
  66. }
  67. /*--------------------------------------------------------- */
  68. void clish_sym__set_name(clish_sym_t *this, const char *name)
  69. {
  70. lub_string_free(this->name);
  71. this->name = lub_string_dup(name);
  72. }
  73. /*--------------------------------------------------------- */
  74. char *clish_sym__get_name(clish_sym_t *this)
  75. {
  76. return this->name;
  77. }
  78. /*--------------------------------------------------------- */
  79. void clish_sym__set_plugin(clish_sym_t *this, clish_plugin_t *plugin)
  80. {
  81. this->plugin = plugin;
  82. }
  83. /*--------------------------------------------------------- */
  84. clish_plugin_t *clish_sym__get_plugin(clish_sym_t *this)
  85. {
  86. return this->plugin;
  87. }
  88. /*--------------------------------------------------------- */
  89. void clish_sym__set_type(clish_sym_t *this, int type)
  90. {
  91. this->type = type;
  92. }
  93. /*--------------------------------------------------------- */
  94. int clish_sym__get_type(const clish_sym_t *this)
  95. {
  96. return this->type;
  97. }
  98. /*--------------------------------------------------------- */
  99. void clish_sym__set_api(clish_sym_t *this, clish_sym_api_e api)
  100. {
  101. this->api = api;
  102. }
  103. /*--------------------------------------------------------- */
  104. clish_sym_api_e clish_sym__get_api(const clish_sym_t *this)
  105. {
  106. return this->api;
  107. }
  108. /*--------------------------------------------------------- */
  109. int clish_sym_clone(clish_sym_t *dst, clish_sym_t *src)
  110. {
  111. char *name;
  112. if (!dst || !src)
  113. return -1;
  114. name = dst->name;
  115. *dst = *src;
  116. dst->name = name;
  117. return 0;
  118. }
  119. /**********************************************************
  120. * PLUGIN functions *
  121. **********************************************************/
  122. /*--------------------------------------------------------- */
  123. clish_plugin_t *clish_plugin_new(const char *name)
  124. {
  125. clish_plugin_t *this;
  126. this = malloc(sizeof(*this));
  127. this->name = lub_string_dup(name);
  128. this->conf = NULL;
  129. this->alias = NULL;
  130. this->file = NULL;
  131. this->builtin_flag = BOOL_FALSE; /* The plugin is shared object by default */
  132. this->dlhan = NULL;
  133. /* Initialise the list of symbols */
  134. this->syms = lub_list_new(clish_sym_compare);
  135. /* Constructor and destructor */
  136. this->init = NULL;
  137. this->fini = NULL;
  138. return this;
  139. }
  140. /*--------------------------------------------------------- */
  141. void clish_plugin_free(clish_plugin_t *this, void *userdata)
  142. {
  143. lub_list_node_t *iter;
  144. if (!this)
  145. return;
  146. /* Execute destructor */
  147. if (this->fini)
  148. this->fini(userdata, this);
  149. lub_string_free(this->name);
  150. lub_string_free(this->alias);
  151. lub_string_free(this->file);
  152. lub_string_free(this->conf);
  153. /* Free symbol list */
  154. while ((iter = lub_list__get_head(this->syms))) {
  155. /* Remove the symbol from the list */
  156. lub_list_del(this->syms, iter);
  157. /* Free the instance */
  158. clish_sym_free((clish_sym_t *)lub_list_node__get_data(iter));
  159. lub_list_node_free(iter);
  160. }
  161. lub_list_free(this->syms);
  162. #ifdef HAVE_DLFCN_H
  163. if (this->dlhan)
  164. dlclose(this->dlhan);
  165. #endif
  166. free(this);
  167. }
  168. /*--------------------------------------------------------- */
  169. clish_sym_t *clish_plugin_add_generic(clish_plugin_t *this,
  170. void *func, const char *name, int type, bool_t permanent)
  171. {
  172. clish_sym_t *sym;
  173. if (!name || !func)
  174. return NULL;
  175. if (!(sym = clish_sym_new(name, func, type)))
  176. return NULL;
  177. clish_sym__set_plugin(sym, this);
  178. clish_sym__set_permanent(sym, permanent);
  179. lub_list_add(this->syms, sym);
  180. return sym;
  181. }
  182. /*--------------------------------------------------------- */
  183. clish_sym_t *clish_plugin_add_sym(clish_plugin_t *this,
  184. clish_hook_action_fn_t *func, const char *name)
  185. {
  186. return clish_plugin_add_generic(this, func,
  187. name, CLISH_SYM_TYPE_ACTION, BOOL_FALSE);
  188. }
  189. /*--------------------------------------------------------- */
  190. /* Add permanent symbol (can't be turned off by dry-run) */
  191. clish_sym_t *clish_plugin_add_psym(clish_plugin_t *this,
  192. clish_hook_action_fn_t *func, const char *name)
  193. {
  194. return clish_plugin_add_generic(this, func,
  195. name, CLISH_SYM_TYPE_ACTION, BOOL_TRUE);
  196. }
  197. /*--------------------------------------------------------- */
  198. clish_sym_t *clish_plugin_add_osym(clish_plugin_t *this,
  199. clish_hook_oaction_fn_t *func, const char *name)
  200. {
  201. clish_sym_t *s;
  202. if (!(s = clish_plugin_add_generic(this, func,
  203. name, CLISH_SYM_TYPE_ACTION, BOOL_FALSE)))
  204. return s;
  205. clish_sym__set_api(s, CLISH_SYM_API_STDOUT);
  206. return s;
  207. }
  208. /*--------------------------------------------------------- */
  209. /* Add permanent symbol (can't be turned off by dry-run) */
  210. clish_sym_t *clish_plugin_add_posym(clish_plugin_t *this,
  211. clish_hook_oaction_fn_t *func, const char *name)
  212. {
  213. clish_sym_t *s;
  214. if (!(s = clish_plugin_add_generic(this, func,
  215. name, CLISH_SYM_TYPE_ACTION, BOOL_TRUE)))
  216. return s;
  217. clish_sym__set_api(s, CLISH_SYM_API_STDOUT);
  218. return s;
  219. }
  220. /*--------------------------------------------------------- */
  221. clish_sym_t *clish_plugin_add_hook(clish_plugin_t *this,
  222. void *func, const char *name, int type)
  223. {
  224. return clish_plugin_add_generic(this, func,
  225. name, type, BOOL_FALSE);
  226. }
  227. /*--------------------------------------------------------- */
  228. clish_sym_t *clish_plugin_add_phook(clish_plugin_t *this,
  229. void *func, const char *name, int type)
  230. {
  231. return clish_plugin_add_generic(this, func,
  232. name, type, BOOL_TRUE);
  233. }
  234. /*--------------------------------------------------------- */
  235. void clish_plugin_add_fini(clish_plugin_t *this,
  236. clish_plugin_fini_t *fini)
  237. {
  238. this->fini = fini;
  239. }
  240. /*--------------------------------------------------------- */
  241. clish_plugin_fini_t * clish_plugin_get_fini(clish_plugin_t *this)
  242. {
  243. return this->fini;
  244. }
  245. /*--------------------------------------------------------- */
  246. void clish_plugin_add_init(clish_plugin_t *this,
  247. clish_plugin_init_t *init)
  248. {
  249. this->init = init;
  250. }
  251. /*--------------------------------------------------------- */
  252. clish_plugin_init_t * clish_plugin_get_init(clish_plugin_t *this)
  253. {
  254. return this->init;
  255. }
  256. /*--------------------------------------------------------- */
  257. clish_sym_t *clish_plugin_get_sym(clish_plugin_t *this, const char *name, int type)
  258. {
  259. lub_list_node_t *iter;
  260. clish_sym_t *sym;
  261. /* Iterate elements */
  262. for(iter = lub_list__get_head(this->syms);
  263. iter; iter = lub_list_node__get_next(iter)) {
  264. int res;
  265. sym = (clish_sym_t *)lub_list_node__get_data(iter);
  266. res = strcmp(clish_sym__get_name(sym), name);
  267. if (!res && ((CLISH_SYM_TYPE_NONE == type) || (clish_sym__get_type(sym) == type)))
  268. return sym;
  269. if (res > 0) /* No chances to find name */
  270. break;
  271. }
  272. return NULL;
  273. }
  274. /*--------------------------------------------------------- */
  275. static int clish_plugin_load_shared(clish_plugin_t *this)
  276. {
  277. #ifdef HAVE_DLFCN_H
  278. char *file = NULL; /* Plugin so file name */
  279. char *init_name = NULL; /* Init function name */
  280. if (this->file) {
  281. file = lub_string_dup(this->file);
  282. } else {
  283. lub_string_cat(&file, "clish_plugin_");
  284. lub_string_cat(&file, this->name);
  285. lub_string_cat(&file, ".so");
  286. }
  287. /* Open dynamic library */
  288. this->dlhan = dlopen(file, RTLD_NOW | RTLD_LOCAL);
  289. lub_string_free(file);
  290. if (!this->dlhan) {
  291. fprintf(stderr, "Error: Can't open plugin \"%s\": %s\n",
  292. this->name, dlerror());
  293. return -1;
  294. }
  295. /* Get plugin init function */
  296. lub_string_cat(&init_name, CLISH_PLUGIN_INIT_NAME_PREFIX);
  297. lub_string_cat(&init_name, this->name);
  298. lub_string_cat(&init_name, CLISH_PLUGIN_INIT_NAME_SUFFIX);
  299. this->init = (clish_plugin_init_t *)dlsym(this->dlhan, init_name);
  300. lub_string_free(init_name);
  301. if (!this->init) {
  302. fprintf(stderr, "Error: Can't get plugin \"%s\" init function: %s\n",
  303. this->name, dlerror());
  304. return -1;
  305. }
  306. return 0;
  307. #else /* HAVE_DLFCN_H */
  308. /* We have no any dl functions. */
  309. fprintf(stderr, "Error: Can't get plugin \"%s\" init function.\n",
  310. this->name);
  311. return -1;
  312. #endif /* HAVE_DLFCN_H */
  313. }
  314. /*--------------------------------------------------------- */
  315. int clish_plugin_load(clish_plugin_t *this, void *userdata)
  316. {
  317. int res;
  318. if (!this)
  319. return -1;
  320. assert(this->name);
  321. /* Builtin plugins already have init function. */
  322. if (!this->builtin_flag) {
  323. if (clish_plugin_load_shared(this) < 0)
  324. return -1;
  325. }
  326. if (!this->init) {
  327. fprintf(stderr, "Error: PLUGIN %s has no init function\n",
  328. this->name);
  329. return -1;
  330. }
  331. /* Execute init function */
  332. if ((res = this->init(userdata, this)))
  333. fprintf(stderr, "Error: Plugin %s init retcode: %d\n",
  334. this->name, res);
  335. return res;
  336. }
  337. /*--------------------------------------------------------- */
  338. char *clish_plugin__get_name(const clish_plugin_t *this)
  339. {
  340. return this->name;
  341. }
  342. /*--------------------------------------------------------- */
  343. void clish_plugin__set_alias(clish_plugin_t *this, const char *alias)
  344. {
  345. lub_string_free(this->alias);
  346. this->alias = lub_string_dup(alias);
  347. }
  348. /*--------------------------------------------------------- */
  349. char *clish_plugin__get_alias(const clish_plugin_t *this)
  350. {
  351. return this->alias;
  352. }
  353. /*--------------------------------------------------------- */
  354. char *clish_plugin__get_pubname(const clish_plugin_t *this)
  355. {
  356. return (this->alias ? this->alias : this->name);
  357. }
  358. /*--------------------------------------------------------- */
  359. void clish_plugin__set_file(clish_plugin_t *this, const char *file)
  360. {
  361. lub_string_free(this->file);
  362. this->file = lub_string_dup(file);
  363. }
  364. /*--------------------------------------------------------- */
  365. char *clish_plugin__get_file(const clish_plugin_t *this)
  366. {
  367. return this->file;
  368. }
  369. /*--------------------------------------------------------- */
  370. void clish_plugin__set_builtin_flag(clish_plugin_t *this, bool_t builtin_flag)
  371. {
  372. this->builtin_flag = builtin_flag;
  373. }
  374. /*--------------------------------------------------------- */
  375. bool_t clish_plugin__get_builtin_flag(const clish_plugin_t *this)
  376. {
  377. return this->builtin_flag;
  378. }
  379. /*--------------------------------------------------------- */
  380. void clish_plugin__set_conf(clish_plugin_t *this, const char *conf)
  381. {
  382. lub_string_free(this->conf);
  383. this->conf = lub_string_dup(conf);
  384. }
  385. /*--------------------------------------------------------- */
  386. char *clish_plugin__get_conf(const clish_plugin_t *this)
  387. {
  388. return this->conf;
  389. }
  390. /*--------------------------------------------------------- */