load_module.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 <stddef.h>
  35. #include <limits.h>
  36. #include <string.h>
  37. #include <errno.h>
  38. #include <unistd.h>
  39. #include <dlfcn.h>
  40. #include <misc/balloc.h>
  41. #include <misc/concat_strings.h>
  42. #include <misc/strdup.h>
  43. #include <misc/offset.h>
  44. #include <misc/debug.h>
  45. #include <structure/LinkedList0.h>
  46. #include <ncd/NCDModule.h>
  47. #include <generated/blog_channel_ncd_load_module.h>
  48. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  49. #define ModuleGlobal(i) ((i)->m->group->group_state)
  50. struct global {
  51. LinkedList0 modules_list;
  52. };
  53. struct module {
  54. char *name;
  55. void *lib_handle;
  56. int ncdmodule_loaded;
  57. LinkedList0Node modules_list_node;
  58. };
  59. static struct module * find_module (const char *name, struct global *g)
  60. {
  61. for (LinkedList0Node *ln = LinkedList0_GetFirst(&g->modules_list); ln; ln = LinkedList0Node_Next(ln)) {
  62. struct module *mod = UPPER_OBJECT(ln, struct module, modules_list_node);
  63. if (!strcmp(mod->name, name)) {
  64. return mod;
  65. }
  66. }
  67. return NULL;
  68. }
  69. static struct module * module_init (const char *name, NCDModuleInst *i)
  70. {
  71. struct global *g = ModuleGlobal(i);
  72. ASSERT(!find_module(name, g))
  73. struct module *mod = BAlloc(sizeof(*mod));
  74. if (!mod) {
  75. ModuleLog(i, BLOG_ERROR, "BAlloc failed");
  76. goto fail0;
  77. }
  78. mod->name = b_strdup(name);
  79. if (!mod->name) {
  80. ModuleLog(i, BLOG_ERROR, "b_strdup failed");
  81. goto fail1;
  82. }
  83. mod->lib_handle = NULL;
  84. mod->ncdmodule_loaded = 0;
  85. LinkedList0_Prepend(&g->modules_list, &mod->modules_list_node);
  86. return mod;
  87. fail1:
  88. BFree(mod);
  89. fail0:
  90. return NULL;
  91. }
  92. static void module_free (struct module *mod, struct global *g)
  93. {
  94. LinkedList0_Remove(&g->modules_list, &mod->modules_list_node);
  95. if (mod->lib_handle) {
  96. if (dlclose(mod->lib_handle) != 0) {
  97. BLog(BLOG_ERROR, "dlclose failed");
  98. }
  99. }
  100. BFree(mod->name);
  101. BFree(mod);
  102. }
  103. static char * x_read_link (const char *path)
  104. {
  105. size_t size = 32;
  106. char *buf = BAlloc(size + 1);
  107. if (!buf) {
  108. goto fail0;
  109. }
  110. ssize_t link_size;
  111. while (1) {
  112. link_size = readlink(path, buf, size);
  113. if (link_size < 0) {
  114. goto fail1;
  115. }
  116. if (link_size >= 0 && link_size < size) {
  117. break;
  118. }
  119. if (size > SIZE_MAX / 2 || 2 * size > SIZE_MAX - 1) {
  120. goto fail1;
  121. }
  122. size *= 2;
  123. char *new_buf = BRealloc(buf, size + 1);
  124. if (!new_buf) {
  125. goto fail1;
  126. }
  127. buf = new_buf;
  128. }
  129. buf[link_size] = '\0';
  130. return buf;
  131. fail1:
  132. BFree(buf);
  133. fail0:
  134. return NULL;
  135. }
  136. static char * find_module_library (NCDModuleInst *i, const char *module_name)
  137. {
  138. char *ret = NULL;
  139. char *self = x_read_link("/proc/self/exe");
  140. if (!self) {
  141. ModuleLog(i, BLOG_ERROR, "failed to read /proc/self/exe");
  142. goto fail0;
  143. }
  144. char *slash = strrchr(self, '/');
  145. if (!slash) {
  146. ModuleLog(i, BLOG_ERROR, "contents of /proc/self/exe do not have a slash");
  147. goto fail1;
  148. }
  149. *slash = '\0';
  150. const char *paths[] = {"../lib/badvpn-ncd", "../mcvpn", NULL};
  151. size_t j;
  152. for (j = 0; paths[j]; j++) {
  153. char *module_path = concat_strings(6, self, "/", paths[j], "/libncdmodule_", module_name, ".so");
  154. if (!module_path) {
  155. ModuleLog(i, BLOG_ERROR, "concat_strings failed");
  156. goto fail1;
  157. }
  158. if (access(module_path, F_OK) == 0) {
  159. ret = module_path;
  160. break;
  161. }
  162. BFree(module_path);
  163. }
  164. if (!paths[j]) {
  165. ModuleLog(i, BLOG_ERROR, "failed to find module");
  166. }
  167. fail1:
  168. BFree(self);
  169. fail0:
  170. return ret;
  171. }
  172. static int func_globalinit (struct NCDInterpModuleGroup *group, const struct NCDModuleInst_iparams *params)
  173. {
  174. struct global *g = BAlloc(sizeof(*g));
  175. if (!g) {
  176. BLog(BLOG_ERROR, "BAlloc failed");
  177. return 0;
  178. }
  179. group->group_state = g;
  180. LinkedList0_Init(&g->modules_list);
  181. return 1;
  182. }
  183. static void func_globalfree (struct NCDInterpModuleGroup *group)
  184. {
  185. struct global *g = group->group_state;
  186. LinkedList0Node *ln;
  187. while ((ln = LinkedList0_GetFirst(&g->modules_list))) {
  188. struct module *mod = UPPER_OBJECT(ln, struct module, modules_list_node);
  189. module_free(mod, g);
  190. }
  191. BFree(g);
  192. }
  193. static void func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  194. {
  195. // check arguments
  196. NCDValRef name_arg;
  197. if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
  198. ModuleLog(i, BLOG_ERROR, "wrong arity");
  199. goto fail0;
  200. }
  201. if (!NCDVal_IsStringNoNulls(name_arg)) {
  202. ModuleLog(i, BLOG_ERROR, "wrong type");
  203. goto fail0;
  204. }
  205. struct module *mod = find_module(NCDVal_StringData(name_arg), ModuleGlobal(i));
  206. ASSERT(!mod || mod->lib_handle)
  207. if (!mod) {
  208. mod = module_init(NCDVal_StringData(name_arg), i);
  209. if (!mod) {
  210. ModuleLog(i, BLOG_ERROR, "module_init failed");
  211. goto fail0;
  212. }
  213. // find module library
  214. char *module_path = find_module_library(i, NCDVal_StringData(name_arg));
  215. if (!module_path) {
  216. module_free(mod, ModuleGlobal(i));
  217. goto fail0;
  218. }
  219. // load it as a dynamic library
  220. mod->lib_handle = dlopen(module_path, RTLD_NOW);
  221. BFree(module_path);
  222. if (!mod->lib_handle) {
  223. ModuleLog(i, BLOG_ERROR, "dlopen failed");
  224. module_free(mod, ModuleGlobal(i));
  225. goto fail0;
  226. }
  227. }
  228. if (!mod->ncdmodule_loaded) {
  229. // build name of NCDModuleGroup structure symbol
  230. char *group_symbol = concat_strings(2, "ncdmodule_", NCDVal_StringData(name_arg));
  231. if (!group_symbol) {
  232. ModuleLog(i, BLOG_ERROR, "concat_strings failed");
  233. goto fail0;
  234. }
  235. // resolve NCDModuleGroup structure symbol
  236. void *group = dlsym(mod->lib_handle, group_symbol);
  237. BFree(group_symbol);
  238. if (!group) {
  239. ModuleLog(i, BLOG_ERROR, "dlsym failed");
  240. goto fail0;
  241. }
  242. // load module group
  243. if (!NCDModuleInst_Backend_InterpLoadGroup(i, (struct NCDModuleGroup *)group)) {
  244. ModuleLog(i, BLOG_ERROR, "NCDModuleInst_Backend_InterpLoadGroup failed");
  245. goto fail0;
  246. }
  247. mod->ncdmodule_loaded = 1;
  248. }
  249. // signal up
  250. NCDModuleInst_Backend_Up(i);
  251. return;
  252. fail0:
  253. NCDModuleInst_Backend_DeadError(i);
  254. }
  255. static struct NCDModule modules[] = {
  256. {
  257. .type = "load_module",
  258. .func_new2 = func_new
  259. }, {
  260. .type = NULL
  261. }
  262. };
  263. const struct NCDModuleGroup ncdmodule_load_module = {
  264. .func_globalinit = func_globalinit,
  265. .func_globalfree = func_globalfree,
  266. .modules = modules
  267. };