load_module.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * @file load_module.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Synopsis:
  32. * load_module(string name)
  33. */
  34. #include <limits.h>
  35. #include <string.h>
  36. #include <errno.h>
  37. #include <unistd.h>
  38. #include <dlfcn.h>
  39. #include <misc/balloc.h>
  40. #include <misc/concat_strings.h>
  41. #include <misc/strdup.h>
  42. #include <misc/offset.h>
  43. #include <misc/debug.h>
  44. #include <structure/LinkedList0.h>
  45. #include <ncd/module_common.h>
  46. #include <generated/blog_channel_ncd_load_module.h>
  47. struct global {
  48. LinkedList0 modules_list;
  49. };
  50. struct module {
  51. char *name;
  52. void *lib_handle;
  53. int ncdmodule_loaded;
  54. LinkedList0Node modules_list_node;
  55. };
  56. static struct module * find_module (const char *name, struct global *g)
  57. {
  58. for (LinkedList0Node *ln = LinkedList0_GetFirst(&g->modules_list); ln; ln = LinkedList0Node_Next(ln)) {
  59. struct module *mod = UPPER_OBJECT(ln, struct module, modules_list_node);
  60. if (!strcmp(mod->name, name)) {
  61. return mod;
  62. }
  63. }
  64. return NULL;
  65. }
  66. static struct module * module_init (const char *name, NCDModuleInst *i)
  67. {
  68. struct global *g = ModuleGlobal(i);
  69. ASSERT(!find_module(name, g))
  70. struct module *mod = BAlloc(sizeof(*mod));
  71. if (!mod) {
  72. ModuleLog(i, BLOG_ERROR, "BAlloc failed");
  73. goto fail0;
  74. }
  75. mod->name = b_strdup(name);
  76. if (!mod->name) {
  77. ModuleLog(i, BLOG_ERROR, "b_strdup failed");
  78. goto fail1;
  79. }
  80. mod->lib_handle = NULL;
  81. mod->ncdmodule_loaded = 0;
  82. LinkedList0_Prepend(&g->modules_list, &mod->modules_list_node);
  83. return mod;
  84. fail1:
  85. BFree(mod);
  86. fail0:
  87. return NULL;
  88. }
  89. static void module_free (struct module *mod, struct global *g)
  90. {
  91. LinkedList0_Remove(&g->modules_list, &mod->modules_list_node);
  92. if (mod->lib_handle) {
  93. if (dlclose(mod->lib_handle) != 0) {
  94. BLog(BLOG_ERROR, "dlclose failed");
  95. }
  96. }
  97. BFree(mod->name);
  98. BFree(mod);
  99. }
  100. static char * x_read_link (const char *path)
  101. {
  102. size_t size = 32;
  103. char *buf = BAlloc(size + 1);
  104. if (!buf) {
  105. goto fail0;
  106. }
  107. ssize_t link_size;
  108. while (1) {
  109. link_size = readlink(path, buf, size);
  110. if (link_size < 0) {
  111. goto fail1;
  112. }
  113. if (link_size >= 0 && link_size < size) {
  114. break;
  115. }
  116. if (size > SIZE_MAX / 2 || 2 * size > SIZE_MAX - 1) {
  117. goto fail1;
  118. }
  119. size *= 2;
  120. char *new_buf = BRealloc(buf, size + 1);
  121. if (!new_buf) {
  122. goto fail1;
  123. }
  124. buf = new_buf;
  125. }
  126. buf[link_size] = '\0';
  127. return buf;
  128. fail1:
  129. BFree(buf);
  130. fail0:
  131. return NULL;
  132. }
  133. static char * find_module_library (NCDModuleInst *i, const char *module_name)
  134. {
  135. char *ret = NULL;
  136. char *self = x_read_link("/proc/self/exe");
  137. if (!self) {
  138. ModuleLog(i, BLOG_ERROR, "failed to read /proc/self/exe");
  139. goto fail0;
  140. }
  141. char *slash = strrchr(self, '/');
  142. if (!slash) {
  143. ModuleLog(i, BLOG_ERROR, "contents of /proc/self/exe do not have a slash");
  144. goto fail1;
  145. }
  146. *slash = '\0';
  147. const char *paths[] = {"../lib/badvpn-ncd", "../mcvpn", NULL};
  148. size_t j;
  149. for (j = 0; paths[j]; j++) {
  150. char *module_path = concat_strings(6, self, "/", paths[j], "/libncdmodule_", module_name, ".so");
  151. if (!module_path) {
  152. ModuleLog(i, BLOG_ERROR, "concat_strings failed");
  153. goto fail1;
  154. }
  155. if (access(module_path, F_OK) == 0) {
  156. ret = module_path;
  157. break;
  158. }
  159. BFree(module_path);
  160. }
  161. if (!paths[j]) {
  162. ModuleLog(i, BLOG_ERROR, "failed to find module");
  163. }
  164. fail1:
  165. BFree(self);
  166. fail0:
  167. return ret;
  168. }
  169. static int func_globalinit (struct NCDInterpModuleGroup *group, const struct NCDModuleInst_iparams *params)
  170. {
  171. struct global *g = BAlloc(sizeof(*g));
  172. if (!g) {
  173. BLog(BLOG_ERROR, "BAlloc failed");
  174. return 0;
  175. }
  176. group->group_state = g;
  177. LinkedList0_Init(&g->modules_list);
  178. return 1;
  179. }
  180. static void func_globalfree (struct NCDInterpModuleGroup *group)
  181. {
  182. struct global *g = group->group_state;
  183. LinkedList0Node *ln;
  184. while ((ln = LinkedList0_GetFirst(&g->modules_list))) {
  185. struct module *mod = UPPER_OBJECT(ln, struct module, modules_list_node);
  186. module_free(mod, g);
  187. }
  188. BFree(g);
  189. }
  190. static void func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  191. {
  192. // check arguments
  193. NCDValRef name_arg;
  194. if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
  195. ModuleLog(i, BLOG_ERROR, "wrong arity");
  196. goto fail0;
  197. }
  198. if (!NCDVal_IsStringNoNulls(name_arg)) {
  199. ModuleLog(i, BLOG_ERROR, "wrong type");
  200. goto fail0;
  201. }
  202. NCDValNullTermString name_nts;
  203. if (!NCDVal_StringNullTerminate(name_arg, &name_nts)) {
  204. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  205. goto fail0;
  206. }
  207. struct module *mod = find_module(name_nts.data, ModuleGlobal(i));
  208. ASSERT(!mod || mod->lib_handle)
  209. if (!mod) {
  210. mod = module_init(name_nts.data, i);
  211. if (!mod) {
  212. ModuleLog(i, BLOG_ERROR, "module_init failed");
  213. goto fail1;
  214. }
  215. // find module library
  216. char *module_path = find_module_library(i, name_nts.data);
  217. if (!module_path) {
  218. module_free(mod, ModuleGlobal(i));
  219. goto fail1;
  220. }
  221. // load it as a dynamic library
  222. mod->lib_handle = dlopen(module_path, RTLD_NOW);
  223. BFree(module_path);
  224. if (!mod->lib_handle) {
  225. ModuleLog(i, BLOG_ERROR, "dlopen failed");
  226. module_free(mod, ModuleGlobal(i));
  227. goto fail1;
  228. }
  229. }
  230. if (!mod->ncdmodule_loaded) {
  231. // build name of NCDModuleGroup structure symbol
  232. char *group_symbol = concat_strings(2, "ncdmodule_", name_nts.data);
  233. if (!group_symbol) {
  234. ModuleLog(i, BLOG_ERROR, "concat_strings failed");
  235. goto fail1;
  236. }
  237. // resolve NCDModuleGroup structure symbol
  238. void *group = dlsym(mod->lib_handle, group_symbol);
  239. BFree(group_symbol);
  240. if (!group) {
  241. ModuleLog(i, BLOG_ERROR, "dlsym failed");
  242. goto fail1;
  243. }
  244. // load module group
  245. if (!NCDModuleInst_Backend_InterpLoadGroup(i, (struct NCDModuleGroup *)group)) {
  246. ModuleLog(i, BLOG_ERROR, "NCDModuleInst_Backend_InterpLoadGroup failed");
  247. goto fail1;
  248. }
  249. mod->ncdmodule_loaded = 1;
  250. }
  251. NCDValNullTermString_Free(&name_nts);
  252. // signal up
  253. NCDModuleInst_Backend_Up(i);
  254. return;
  255. fail1:
  256. NCDValNullTermString_Free(&name_nts);
  257. fail0:
  258. NCDModuleInst_Backend_DeadError(i);
  259. }
  260. static struct NCDModule modules[] = {
  261. {
  262. .type = "load_module",
  263. .func_new2 = func_new
  264. }, {
  265. .type = NULL
  266. }
  267. };
  268. const struct NCDModuleGroup ncdmodule_load_module = {
  269. .func_globalinit = func_globalinit,
  270. .func_globalfree = func_globalfree,
  271. .modules = modules
  272. };