plugin.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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_action_fn_t *func, const char *name)
  200. {
  201. clish_sym_t *s;
  202. if (!(s = clish_plugin_add_sym(this, func, name)))
  203. return s;
  204. clish_sym__set_api(s, CLISH_SYM_API_STDOUT);
  205. return s;
  206. }
  207. /*--------------------------------------------------------- */
  208. /* Add permanent symbol (can't be turned off by dry-run) */
  209. clish_sym_t *clish_plugin_add_posym(clish_plugin_t *this,
  210. clish_hook_action_fn_t *func, const char *name)
  211. {
  212. clish_sym_t *s;
  213. if (!(s = clish_plugin_add_psym(this, func, name)))
  214. return s;
  215. clish_sym__set_api(s, CLISH_SYM_API_STDOUT);
  216. return s;
  217. }
  218. /*--------------------------------------------------------- */
  219. clish_sym_t *clish_plugin_add_hook(clish_plugin_t *this,
  220. void *func, const char *name, int type)
  221. {
  222. return clish_plugin_add_generic(this, func,
  223. name, type, BOOL_FALSE);
  224. }
  225. /*--------------------------------------------------------- */
  226. clish_sym_t *clish_plugin_add_phook(clish_plugin_t *this,
  227. void *func, const char *name, int type)
  228. {
  229. return clish_plugin_add_generic(this, func,
  230. name, type, BOOL_TRUE);
  231. }
  232. /*--------------------------------------------------------- */
  233. void clish_plugin_add_fini(clish_plugin_t *this,
  234. clish_plugin_fini_t *fini)
  235. {
  236. this->fini = fini;
  237. }
  238. /*--------------------------------------------------------- */
  239. clish_plugin_fini_t * clish_plugin_get_fini(clish_plugin_t *this)
  240. {
  241. return this->fini;
  242. }
  243. /*--------------------------------------------------------- */
  244. void clish_plugin_add_init(clish_plugin_t *this,
  245. clish_plugin_init_t *init)
  246. {
  247. this->init = init;
  248. }
  249. /*--------------------------------------------------------- */
  250. clish_plugin_init_t * clish_plugin_get_init(clish_plugin_t *this)
  251. {
  252. return this->init;
  253. }
  254. /*--------------------------------------------------------- */
  255. clish_sym_t *clish_plugin_get_sym(clish_plugin_t *this, const char *name, int type)
  256. {
  257. lub_list_node_t *iter;
  258. clish_sym_t *sym;
  259. /* Iterate elements */
  260. for(iter = lub_list__get_head(this->syms);
  261. iter; iter = lub_list_node__get_next(iter)) {
  262. int res;
  263. sym = (clish_sym_t *)lub_list_node__get_data(iter);
  264. res = strcmp(clish_sym__get_name(sym), name);
  265. if (!res && ((CLISH_SYM_TYPE_NONE == type) || (clish_sym__get_type(sym) == type)))
  266. return sym;
  267. if (res > 0) /* No chances to find name */
  268. break;
  269. }
  270. return NULL;
  271. }
  272. /*--------------------------------------------------------- */
  273. static int clish_plugin_load_shared(clish_plugin_t *this)
  274. {
  275. #ifdef HAVE_DLFCN_H
  276. char *file = NULL; /* Plugin so file name */
  277. char *init_name = NULL; /* Init function name */
  278. if (this->file) {
  279. file = lub_string_dup(this->file);
  280. } else {
  281. lub_string_cat(&file, "clish_plugin_");
  282. lub_string_cat(&file, this->name);
  283. lub_string_cat(&file, ".so");
  284. }
  285. /* Open dynamic library */
  286. this->dlhan = dlopen(file, RTLD_NOW | RTLD_LOCAL);
  287. lub_string_free(file);
  288. if (!this->dlhan) {
  289. fprintf(stderr, "Error: Can't open plugin \"%s\": %s\n",
  290. this->name, dlerror());
  291. return -1;
  292. }
  293. /* Get plugin init function */
  294. lub_string_cat(&init_name, CLISH_PLUGIN_INIT_NAME_PREFIX);
  295. lub_string_cat(&init_name, this->name);
  296. lub_string_cat(&init_name, CLISH_PLUGIN_INIT_NAME_SUFFIX);
  297. this->init = (clish_plugin_init_t *)dlsym(this->dlhan, init_name);
  298. lub_string_free(init_name);
  299. if (!this->init) {
  300. fprintf(stderr, "Error: Can't get plugin \"%s\" init function: %s\n",
  301. this->name, dlerror());
  302. return -1;
  303. }
  304. return 0;
  305. #else /* HAVE_DLFCN_H */
  306. /* We have no any dl functions. */
  307. fprintf(stderr, "Error: Can't get plugin \"%s\" init function.\n",
  308. this->name);
  309. return -1;
  310. #endif /* HAVE_DLFCN_H */
  311. }
  312. /*--------------------------------------------------------- */
  313. int clish_plugin_load(clish_plugin_t *this, void *userdata)
  314. {
  315. int res;
  316. if (!this)
  317. return -1;
  318. assert(this->name);
  319. /* Builtin plugins already have init function. */
  320. if (!this->builtin_flag) {
  321. if (clish_plugin_load_shared(this) < 0)
  322. return -1;
  323. }
  324. if (!this->init) {
  325. fprintf(stderr, "Error: PLUGIN %s has no init function\n",
  326. this->name);
  327. return -1;
  328. }
  329. /* Execute init function */
  330. if ((res = this->init(userdata, this)))
  331. fprintf(stderr, "Error: Plugin %s init retcode: %d\n",
  332. this->name, res);
  333. return res;
  334. }
  335. /*--------------------------------------------------------- */
  336. char *clish_plugin__get_name(const clish_plugin_t *this)
  337. {
  338. return this->name;
  339. }
  340. /*--------------------------------------------------------- */
  341. void clish_plugin__set_alias(clish_plugin_t *this, const char *alias)
  342. {
  343. lub_string_free(this->alias);
  344. this->alias = lub_string_dup(alias);
  345. }
  346. /*--------------------------------------------------------- */
  347. char *clish_plugin__get_alias(const clish_plugin_t *this)
  348. {
  349. return this->alias;
  350. }
  351. /*--------------------------------------------------------- */
  352. char *clish_plugin__get_pubname(const clish_plugin_t *this)
  353. {
  354. return (this->alias ? this->alias : this->name);
  355. }
  356. /*--------------------------------------------------------- */
  357. void clish_plugin__set_file(clish_plugin_t *this, const char *file)
  358. {
  359. lub_string_free(this->file);
  360. this->file = lub_string_dup(file);
  361. }
  362. /*--------------------------------------------------------- */
  363. char *clish_plugin__get_file(const clish_plugin_t *this)
  364. {
  365. return this->file;
  366. }
  367. /*--------------------------------------------------------- */
  368. void clish_plugin__set_builtin_flag(clish_plugin_t *this, bool_t builtin_flag)
  369. {
  370. this->builtin_flag = builtin_flag;
  371. }
  372. /*--------------------------------------------------------- */
  373. bool_t clish_plugin__get_builtin_flag(const clish_plugin_t *this)
  374. {
  375. return this->builtin_flag;
  376. }
  377. /*--------------------------------------------------------- */
  378. void clish_plugin__set_conf(clish_plugin_t *this, const char *conf)
  379. {
  380. lub_string_free(this->conf);
  381. this->conf = lub_string_dup(conf);
  382. }
  383. /*--------------------------------------------------------- */
  384. char *clish_plugin__get_conf(const clish_plugin_t *this)
  385. {
  386. return this->conf;
  387. }
  388. /*--------------------------------------------------------- */