call.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**
  2. * @file call.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Synopsis:
  25. * callrefhere()
  26. *
  27. * Description:
  28. * Exposes variables and objects to call() statements as seen from this
  29. * callrefhere() statement.
  30. *
  31. * Synopsis:
  32. * call(string template_name, list args)
  33. * callhefhere::call(string template_name, list args)
  34. *
  35. * Description:
  36. * Module which allows using a single statement to represent multiple statements
  37. * in a process template, allowing reuse of repetitive code.
  38. * The created template process can access variables and objects as seen from the
  39. * call statement via "_caller.variable".
  40. * The second form also exposes variables and objects from the corresponding
  41. * callrefhere() statement via "_ref.variable".
  42. * If template_name is "<none>", then the call() is a no-op - it goes up
  43. * immediately and immediately terminates on request.
  44. *
  45. * Variables:
  46. * Exposes variables as seen from the end of the called process template.
  47. *
  48. * Behavior in detail (assuming template_name is not "<none>"):
  49. * - On initialization, creates a new process from the template named
  50. * template_name, with arguments args.
  51. * - When all the statements in the created process go UP, transitions UP.
  52. * - When one of the statements is no longer UP, transitions DOWN. The
  53. * created process remais paused until the call statement receives the
  54. * clean signal, to wait for following statements to deinitialize.
  55. * - On deinitialization, initiates termination of the created process and waits
  56. * for all its statements to deinitialize.
  57. */
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include <misc/string_begins_with.h>
  61. #include <misc/offset.h>
  62. #include <structure/LinkedList0.h>
  63. #include <ncd/NCDModule.h>
  64. #include <generated/blog_channel_ncd_call.h>
  65. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  66. #define STATE_WORKING 1
  67. #define STATE_UP 2
  68. #define STATE_WAITING 3
  69. #define STATE_TERMINATING 4
  70. #define STATE_NONE 5
  71. struct callrefhere_instance {
  72. NCDModuleInst *i;
  73. LinkedList0 calls_list;
  74. };
  75. struct instance {
  76. NCDModuleInst *i;
  77. NCDModuleProcess process;
  78. int state;
  79. struct callrefhere_instance *crh;
  80. LinkedList0Node calls_list_node;
  81. };
  82. static void instance_free (struct instance *o);
  83. static void process_handler_event (struct instance *o, int event)
  84. {
  85. switch (event) {
  86. case NCDMODULEPROCESS_EVENT_UP: {
  87. ASSERT(o->state == STATE_WORKING)
  88. // signal up
  89. NCDModuleInst_Backend_Up(o->i);
  90. // set state up
  91. o->state = STATE_UP;
  92. } break;
  93. case NCDMODULEPROCESS_EVENT_DOWN: {
  94. ASSERT(o->state == STATE_UP)
  95. // signal down
  96. NCDModuleInst_Backend_Down(o->i);
  97. // set state waiting
  98. o->state = STATE_WAITING;
  99. } break;
  100. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  101. ASSERT(o->state == STATE_TERMINATING)
  102. // die finally
  103. instance_free(o);
  104. return;
  105. } break;
  106. default: ASSERT(0);
  107. }
  108. }
  109. static int process_func_getspecialvar (struct instance *o, const char *name, NCDValue *out)
  110. {
  111. size_t p;
  112. if (p = string_begins_with(name, "_caller.")) {
  113. return NCDModuleInst_Backend_GetVar(o->i, name + p, out);
  114. }
  115. if (o->crh && (p = string_begins_with(name, "_ref."))) {
  116. return NCDModuleInst_Backend_GetVar(o->crh->i, name + p, out);
  117. }
  118. return 0;
  119. }
  120. static NCDModuleInst * process_func_getspecialobj (struct instance *o, const char *name)
  121. {
  122. size_t p;
  123. if (p = string_begins_with(name, "_caller.")) {
  124. return NCDModuleInst_Backend_GetObj(o->i, name + p);
  125. }
  126. if (o->crh && (p = string_begins_with(name, "_ref."))) {
  127. return NCDModuleInst_Backend_GetObj(o->crh->i, name + p);
  128. }
  129. return NULL;
  130. }
  131. static void callrefhere_func_new (NCDModuleInst *i)
  132. {
  133. // allocate instance
  134. struct callrefhere_instance *o = malloc(sizeof(*o));
  135. if (!o) {
  136. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  137. goto fail0;
  138. }
  139. NCDModuleInst_Backend_SetUser(i, o);
  140. // set arguments
  141. o->i = i;
  142. // init calls list
  143. LinkedList0_Init(&o->calls_list);
  144. // signal up
  145. NCDModuleInst_Backend_Up(o->i);
  146. return;
  147. fail0:
  148. NCDModuleInst_Backend_SetError(i);
  149. NCDModuleInst_Backend_Dead(i);
  150. }
  151. static void callrefhere_func_die (void *vo)
  152. {
  153. struct callrefhere_instance *o = vo;
  154. NCDModuleInst *i = o->i;
  155. // disconnect calls
  156. while (!LinkedList0_IsEmpty(&o->calls_list)) {
  157. struct instance *inst = UPPER_OBJECT(LinkedList0_GetFirst(&o->calls_list), struct instance, calls_list_node);
  158. ASSERT(inst->crh == o)
  159. LinkedList0_Remove(&o->calls_list, &inst->calls_list_node);
  160. inst->crh = NULL;
  161. }
  162. // free instance
  163. free(o);
  164. NCDModuleInst_Backend_Dead(i);
  165. }
  166. static void func_new (NCDModuleInst *i)
  167. {
  168. // allocate instance
  169. struct instance *o = malloc(sizeof(*o));
  170. if (!o) {
  171. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  172. goto fail0;
  173. }
  174. NCDModuleInst_Backend_SetUser(i, o);
  175. // init arguments
  176. o->i = i;
  177. // check arguments
  178. NCDValue *template_name_arg;
  179. NCDValue *args_arg;
  180. if (!NCDValue_ListRead(o->i->args, 2, &template_name_arg, &args_arg)) {
  181. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  182. goto fail1;
  183. }
  184. if (NCDValue_Type(template_name_arg) != NCDVALUE_STRING || NCDValue_Type(args_arg) != NCDVALUE_LIST) {
  185. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  186. goto fail1;
  187. }
  188. char *template_name = NCDValue_StringValue(template_name_arg);
  189. // calling none?
  190. if (!strcmp(template_name, "<none>")) {
  191. // signal up
  192. NCDModuleInst_Backend_Up(o->i);
  193. // set state none
  194. o->state = STATE_NONE;
  195. } else {
  196. // copy arguments
  197. NCDValue args;
  198. if (!NCDValue_InitCopy(&args, args_arg)) {
  199. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  200. goto fail1;
  201. }
  202. // create process
  203. if (!NCDModuleProcess_Init(&o->process, o->i, template_name, args, o, (NCDModuleProcess_handler_event)process_handler_event)) {
  204. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  205. NCDValue_Free(&args);
  206. goto fail1;
  207. }
  208. // set special functions
  209. NCDModuleProcess_SetSpecialFuncs(&o->process,
  210. (NCDModuleProcess_func_getspecialvar)process_func_getspecialvar,
  211. (NCDModuleProcess_func_getspecialobj)process_func_getspecialobj);
  212. // set callrefhere
  213. o->crh = (o->i->method_object ? o->i->method_object->inst_user : NULL);
  214. // add to callrefhere's calls list
  215. if (o->crh) {
  216. LinkedList0_Prepend(&o->crh->calls_list, &o->calls_list_node);
  217. }
  218. // set state working
  219. o->state = STATE_WORKING;
  220. }
  221. return;
  222. fail1:
  223. free(o);
  224. fail0:
  225. NCDModuleInst_Backend_SetError(i);
  226. NCDModuleInst_Backend_Dead(i);
  227. }
  228. void instance_free (struct instance *o)
  229. {
  230. NCDModuleInst *i = o->i;
  231. if (o->state != STATE_NONE) {
  232. // remove from callrefhere's calls list
  233. if (o->crh) {
  234. LinkedList0_Remove(&o->crh->calls_list, &o->calls_list_node);
  235. }
  236. // free process
  237. NCDModuleProcess_Free(&o->process);
  238. }
  239. // free instance
  240. free(o);
  241. NCDModuleInst_Backend_Dead(i);
  242. }
  243. static void func_die (void *vo)
  244. {
  245. struct instance *o = vo;
  246. ASSERT(o->state != STATE_TERMINATING)
  247. // if none, die now
  248. if (o->state == STATE_NONE) {
  249. instance_free(o);
  250. return;
  251. }
  252. // request process to terminate
  253. NCDModuleProcess_Terminate(&o->process);
  254. // set state terminating
  255. o->state = STATE_TERMINATING;
  256. }
  257. static void func_clean (void *vo)
  258. {
  259. struct instance *o = vo;
  260. if (o->state != STATE_WAITING) {
  261. return;
  262. }
  263. // allow process to continue
  264. NCDModuleProcess_Continue(&o->process);
  265. // set state working
  266. o->state = STATE_WORKING;
  267. }
  268. static int func_getvar (void *vo, const char *name, NCDValue *out)
  269. {
  270. struct instance *o = vo;
  271. if (o->state == STATE_NONE) {
  272. return 0;
  273. }
  274. return NCDModuleProcess_GetVar(&o->process, name, out);
  275. }
  276. static NCDModuleInst * func_getobj (void *vo, const char *name)
  277. {
  278. struct instance *o = vo;
  279. if (o->state == STATE_NONE) {
  280. return NULL;
  281. }
  282. return NCDModuleProcess_GetObj(&o->process, name);
  283. }
  284. static const struct NCDModule modules[] = {
  285. {
  286. .type = "callrefhere",
  287. .func_new = callrefhere_func_new,
  288. .func_die = callrefhere_func_die
  289. }, {
  290. .type = "call",
  291. .func_new = func_new,
  292. .func_die = func_die,
  293. .func_clean = func_clean,
  294. .func_getvar = func_getvar,
  295. .func_getobj = func_getobj,
  296. .can_resolve_when_down = 1
  297. }, {
  298. .type = "callrefhere::call",
  299. .func_new = func_new,
  300. .func_die = func_die,
  301. .func_clean = func_clean,
  302. .func_getvar = func_getvar,
  303. .func_getobj = func_getobj,
  304. .can_resolve_when_down = 1
  305. }, {
  306. .type = NULL
  307. }
  308. };
  309. const struct NCDModuleGroup ncdmodule_call = {
  310. .modules = modules
  311. };