kplugin.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <dlfcn.h>
  7. #include <faux/str.h>
  8. #include <faux/list.h>
  9. #include <faux/conv.h>
  10. #include <faux/error.h>
  11. #include <klish/khelper.h>
  12. #include <klish/kplugin.h>
  13. #include <klish/ksym.h>
  14. #include <klish/kcontext_base.h>
  15. struct kplugin_s {
  16. char *name;
  17. char *id;
  18. char *file;
  19. char *conf;
  20. uint8_t major;
  21. uint8_t minor;
  22. void *dlhan; // dlopen() handler
  23. ksym_fn init_fn;
  24. ksym_fn fini_fn;
  25. void *udata; // User data
  26. faux_list_t *syms;
  27. };
  28. // Simple methods
  29. // Name
  30. KGET_STR(plugin, name);
  31. // ID
  32. KGET_STR(plugin, id);
  33. KSET_STR(plugin, id);
  34. // File
  35. KGET_STR(plugin, file);
  36. KSET_STR(plugin, file);
  37. // Conf
  38. KGET_STR(plugin, conf);
  39. KSET_STR(plugin, conf);
  40. // Version major number
  41. KGET(plugin, uint8_t, major);
  42. KSET(plugin, uint8_t, major);
  43. // Version minor number
  44. KGET(plugin, uint8_t, minor);
  45. KSET(plugin, uint8_t, minor);
  46. // User data
  47. KGET(plugin, void *, udata);
  48. KSET(plugin, void *, udata);
  49. // SYMs list
  50. KGET(plugin, faux_list_t *, syms);
  51. static KCMP_NESTED(plugin, sym, name);
  52. static KCMP_NESTED_BY_KEY(plugin, sym, name);
  53. KADD_NESTED(plugin, ksym_t *, syms);
  54. KFIND_NESTED(plugin, sym);
  55. KNESTED_LEN(plugin, syms);
  56. KNESTED_ITER(plugin, syms);
  57. KNESTED_EACH(plugin, ksym_t *, syms);
  58. kplugin_t *kplugin_new(const char *name)
  59. {
  60. kplugin_t *plugin = NULL;
  61. if (faux_str_is_empty(name))
  62. return NULL;
  63. plugin = faux_zmalloc(sizeof(*plugin));
  64. assert(plugin);
  65. if (!plugin)
  66. return NULL;
  67. // Initialize
  68. plugin->name = faux_str_dup(name);
  69. plugin->id = NULL;
  70. plugin->file = NULL;
  71. plugin->conf = NULL;
  72. plugin->major = 0;
  73. plugin->minor = 0;
  74. plugin->dlhan = NULL;
  75. plugin->init_fn = NULL;
  76. plugin->fini_fn = NULL;
  77. plugin->udata = NULL;
  78. // SYM list
  79. plugin->syms = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  80. kplugin_sym_compare, kplugin_sym_kcompare,
  81. (void (*)(void *))ksym_free);
  82. assert(plugin->syms);
  83. return plugin;
  84. }
  85. void kplugin_free(kplugin_t *plugin)
  86. {
  87. if (!plugin)
  88. return;
  89. faux_str_free(plugin->name);
  90. faux_str_free(plugin->id);
  91. faux_str_free(plugin->file);
  92. faux_str_free(plugin->conf);
  93. faux_list_free(plugin->syms);
  94. if (plugin->dlhan)
  95. dlclose(plugin->dlhan);
  96. faux_free(plugin);
  97. }
  98. bool_t kplugin_load(kplugin_t *plugin)
  99. {
  100. char *file_name = NULL;
  101. char *init_name = NULL;
  102. char *fini_name = NULL;
  103. char *major_name = NULL;
  104. char *minor_name = NULL;
  105. char *opt_global_name = NULL;
  106. int default_flags = RTLD_NOW; // By default it uses RTLD_LOCAL
  107. int flags = default_flags;
  108. const char *id = NULL;
  109. bool_t retcode = BOOL_FALSE;
  110. uint8_t *ver = NULL;
  111. assert(plugin);
  112. if (!plugin)
  113. return BOOL_FALSE;
  114. if (kplugin_id(plugin))
  115. id = kplugin_id(plugin);
  116. else
  117. id = kplugin_name(plugin);
  118. // Shared object file name
  119. if (kplugin_file(plugin))
  120. file_name = faux_str_dup(kplugin_file(plugin));
  121. else
  122. file_name = faux_str_sprintf(KPLUGIN_SONAME_FMT, id);
  123. // Symbol names
  124. major_name = faux_str_sprintf(KPLUGIN_MAJOR_FMT, id);
  125. minor_name = faux_str_sprintf(KPLUGIN_MINOR_FMT, id);
  126. init_name = faux_str_sprintf(KPLUGIN_INIT_FMT, id);
  127. fini_name = faux_str_sprintf(KPLUGIN_FINI_FMT, id);
  128. opt_global_name = faux_str_sprintf(KPLUGIN_OPT_GLOBAL_FMT, id);
  129. // Open shared object
  130. plugin->dlhan = dlopen(file_name, flags);
  131. if (!plugin->dlhan) {
  132. // fprintf(stderr, "Error: Can't open plugin \"%s\": %s\n",
  133. // this->name, dlerror());
  134. goto err;
  135. }
  136. // Get plugin version
  137. ver = (uint8_t *)dlsym(plugin->dlhan, major_name);
  138. if (!ver)
  139. goto err;
  140. kplugin_set_major(plugin, *ver);
  141. ver = (uint8_t *)dlsym(plugin->dlhan, minor_name);
  142. if (!ver)
  143. goto err;
  144. kplugin_set_minor(plugin, *ver);
  145. // Get plugin init function
  146. plugin->init_fn = dlsym(plugin->dlhan, init_name);
  147. if (!plugin->init_fn) {
  148. // fprintf(stderr, "Error: Can't get plugin \"%s\" init function: %s\n",
  149. // this->name, dlerror());
  150. goto err;
  151. }
  152. // Get plugin fini function
  153. plugin->fini_fn = dlsym(plugin->dlhan, fini_name);
  154. // Get plugin options
  155. ver = (uint8_t *)dlsym(plugin->dlhan, opt_global_name);
  156. if (ver && (*ver != 0))
  157. flags = flags | RTLD_GLOBAL;
  158. // Reopen shared object with plugin options if necessary
  159. if (flags != default_flags) {
  160. plugin->dlhan = dlopen(file_name, flags | RTLD_NOLOAD);
  161. // Decrement reference counter. Object will not be really closed.
  162. dlclose(plugin->dlhan);
  163. }
  164. retcode = BOOL_TRUE;
  165. err:
  166. faux_str_free(file_name);
  167. faux_str_free(major_name);
  168. faux_str_free(minor_name);
  169. faux_str_free(init_name);
  170. faux_str_free(fini_name);
  171. faux_str_free(opt_global_name);
  172. if (!retcode && plugin->dlhan)
  173. dlclose(plugin->dlhan);
  174. return retcode;
  175. }
  176. int kplugin_init(kplugin_t *plugin, kcontext_t *context)
  177. {
  178. assert(plugin);
  179. if (!plugin)
  180. return -1;
  181. assert(context);
  182. if (!context)
  183. return -1;
  184. if (!plugin->init_fn)
  185. return -1;
  186. // Be sure the context type is appropriate one
  187. kcontext_set_type(context, KCONTEXT_TYPE_PLUGIN_INIT);
  188. return plugin->init_fn(context);
  189. }
  190. int kplugin_fini(kplugin_t *plugin, kcontext_t *context)
  191. {
  192. assert(plugin);
  193. if (!plugin)
  194. return -1;
  195. assert(context);
  196. if (!context)
  197. return -1;
  198. if (!plugin->fini_fn)
  199. return 0; // Fini function is not mandatory so it's ok
  200. // Be sure the context type is appropriate one
  201. kcontext_set_type(context, KCONTEXT_TYPE_PLUGIN_FINI);
  202. return plugin->fini_fn(context);
  203. }