plugin.c 12 KB

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