1
0

plugin.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. static int clish_plugin_load_shared(clish_plugin_t *this)
  225. {
  226. #ifdef HAVE_DLFCN_H
  227. char *file = NULL; /* Plugin so file name */
  228. char *init_name = NULL; /* Init function name */
  229. if (this->file) {
  230. file = lub_string_dup(this->file);
  231. } else {
  232. lub_string_cat(&file, "clish_plugin_");
  233. lub_string_cat(&file, this->name);
  234. lub_string_cat(&file, ".so");
  235. }
  236. /* Open dynamic library */
  237. this->dlhan = dlopen(file, RTLD_NOW | RTLD_LOCAL);
  238. lub_string_free(file);
  239. if (!this->dlhan) {
  240. fprintf(stderr, "Error: Can't open plugin \"%s\": %s\n",
  241. this->name, dlerror());
  242. return -1;
  243. }
  244. /* Get plugin init function */
  245. lub_string_cat(&init_name, CLISH_PLUGIN_INIT_NAME_PREFIX);
  246. lub_string_cat(&init_name, this->name);
  247. lub_string_cat(&init_name, CLISH_PLUGIN_INIT_NAME_SUFFIX);
  248. this->init = (clish_plugin_init_t *)dlsym(this->dlhan, init_name);
  249. lub_string_free(init_name);
  250. if (!this->init) {
  251. fprintf(stderr, "Error: Can't get plugin \"%s\" init function: %s\n",
  252. this->name, dlerror());
  253. return -1;
  254. }
  255. return 0;
  256. #else /* HAVE_DLFCN_H */
  257. /* We have no any dl functions. */
  258. fprintf(stderr, "Error: Can't get plugin \"%s\" init function.\n",
  259. this->name);
  260. return -1;
  261. #endif /* HAVE_DLFCN_H */
  262. }
  263. /*--------------------------------------------------------- */
  264. static int clish_plugin_load_builtin(clish_plugin_t *this)
  265. {
  266. int i = 0;
  267. /* Search plugin in the list of builtin plugins */
  268. while (clish_plugin_builtin_list[i].name) {
  269. if (strcmp(clish_plugin_builtin_list[i].name, this->name))
  270. continue;
  271. this->init = clish_plugin_builtin_list[i].init;
  272. break;
  273. }
  274. if (this->init)
  275. return 0;
  276. return -1;
  277. }
  278. /*--------------------------------------------------------- */
  279. int clish_plugin_load(clish_plugin_t *this, void *userdata)
  280. {
  281. int res;
  282. if (!this)
  283. return -1;
  284. assert(this->name);
  285. /* Firstly try to find builtin plugin and if there is
  286. no such builtin plugin then try to find plugin
  287. shared object */
  288. if (clish_plugin_load_builtin(this) < 0) {
  289. if (clish_plugin_load_shared(this) < 0)
  290. return -1;
  291. }
  292. /* Execute init function */
  293. if ((res = this->init(userdata, this)))
  294. fprintf(stderr, "Error: Plugin %s init retcode: %d\n",
  295. this->name, res);
  296. return res;
  297. }
  298. /*--------------------------------------------------------- */
  299. char *clish_plugin__get_name(const clish_plugin_t *this)
  300. {
  301. return this->name;
  302. }
  303. /*--------------------------------------------------------- */
  304. void clish_plugin__set_alias(clish_plugin_t *this, const char *alias)
  305. {
  306. lub_string_free(this->alias);
  307. this->alias = lub_string_dup(alias);
  308. }
  309. /*--------------------------------------------------------- */
  310. char *clish_plugin__get_alias(const clish_plugin_t *this)
  311. {
  312. return this->alias;
  313. }
  314. /*--------------------------------------------------------- */
  315. char *clish_plugin__get_pubname(const clish_plugin_t *this)
  316. {
  317. return (this->alias ? this->alias : this->name);
  318. }
  319. /*--------------------------------------------------------- */
  320. void clish_plugin__set_file(clish_plugin_t *this, const char *file)
  321. {
  322. lub_string_free(this->file);
  323. this->file = lub_string_dup(file);
  324. }
  325. /*--------------------------------------------------------- */
  326. char *clish_plugin__get_file(const clish_plugin_t *this)
  327. {
  328. return this->file;
  329. }
  330. /*--------------------------------------------------------- */
  331. void clish_plugin__set_conf(clish_plugin_t *this, const char *conf)
  332. {
  333. lub_string_free(this->conf);
  334. this->conf = lub_string_dup(conf);
  335. }
  336. /*--------------------------------------------------------- */
  337. char *clish_plugin__get_conf(const clish_plugin_t *this)
  338. {
  339. return this->conf;
  340. }
  341. /*--------------------------------------------------------- */