call.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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: call(string template_name, list args)
  25. *
  26. * Description:
  27. * Module which allows using a single statement to represent multiple statements
  28. * in a process template, allowing reuse of repetitive code.
  29. * The created template process can access variables and objects as seen from the
  30. * call statement via "_caller.variable".
  31. *
  32. * Variables:
  33. * Exposes variables as seen from the end of the called process template.
  34. *
  35. * Behavior in detail:
  36. * - On initialization, creates a new process from the template named
  37. * template_name, with arguments args.
  38. * - When all the statements in the created process go UP, transitions UP.
  39. * - When one of the statements is no longer UP, transitions DOWN. The
  40. * created process remais paused until the call statement receives the
  41. * clean signal, to wait for following statements to deinitialize.
  42. * - On deinitialization, initiates termination of the created process and waits
  43. * for all its statements to deinitialize.
  44. */
  45. #include <stdlib.h>
  46. #include <misc/string_begins_with.h>
  47. #include <ncd/NCDModule.h>
  48. #include <generated/blog_channel_ncd_call.h>
  49. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  50. #define STATE_WORKING 1
  51. #define STATE_UP 2
  52. #define STATE_WAITING 3
  53. #define STATE_TERMINATING 4
  54. struct instance {
  55. NCDModuleInst *i;
  56. NCDModuleProcess process;
  57. int state;
  58. };
  59. static void instance_free (struct instance *o);
  60. static void process_handler_event (struct instance *o, int event)
  61. {
  62. switch (event) {
  63. case NCDMODULEPROCESS_EVENT_UP: {
  64. ASSERT(o->state == STATE_WORKING)
  65. // signal up
  66. NCDModuleInst_Backend_Up(o->i);
  67. // set state up
  68. o->state = STATE_UP;
  69. } break;
  70. case NCDMODULEPROCESS_EVENT_DOWN: {
  71. ASSERT(o->state == STATE_UP)
  72. // signal down
  73. NCDModuleInst_Backend_Down(o->i);
  74. // set state waiting
  75. o->state = STATE_WAITING;
  76. } break;
  77. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  78. ASSERT(o->state == STATE_TERMINATING)
  79. // die finally
  80. instance_free(o);
  81. return;
  82. } break;
  83. default: ASSERT(0);
  84. }
  85. }
  86. static int process_func_getspecialvar (struct instance *o, const char *name, NCDValue *out)
  87. {
  88. size_t p;
  89. if (!(p = string_begins_with(name, "_caller."))) {
  90. return 0;
  91. }
  92. return NCDModuleInst_Backend_GetVar(o->i, name + p, out);
  93. }
  94. static NCDModuleInst * process_func_getspecialobj (struct instance *o, const char *name)
  95. {
  96. size_t p;
  97. if (!(p = string_begins_with(name, "_caller."))) {
  98. return NULL;
  99. }
  100. return NCDModuleInst_Backend_GetObj(o->i, name + p);
  101. }
  102. static void func_new (NCDModuleInst *i)
  103. {
  104. // allocate instance
  105. struct instance *o = malloc(sizeof(*o));
  106. if (!o) {
  107. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  108. goto fail0;
  109. }
  110. NCDModuleInst_Backend_SetUser(i, o);
  111. // init arguments
  112. o->i = i;
  113. // check arguments
  114. NCDValue *template_name_arg;
  115. NCDValue *args_arg;
  116. if (!NCDValue_ListRead(o->i->args, 2, &template_name_arg, &args_arg)) {
  117. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  118. goto fail1;
  119. }
  120. if (NCDValue_Type(template_name_arg) != NCDVALUE_STRING || NCDValue_Type(args_arg) != NCDVALUE_LIST) {
  121. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  122. goto fail1;
  123. }
  124. // copy arguments
  125. NCDValue args;
  126. if (!NCDValue_InitCopy(&args, args_arg)) {
  127. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  128. goto fail1;
  129. }
  130. // create process
  131. if (!NCDModuleProcess_Init(&o->process, o->i, NCDValue_StringValue(template_name_arg), args, o, (NCDModuleProcess_handler_event)process_handler_event)) {
  132. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  133. NCDValue_Free(&args);
  134. goto fail1;
  135. }
  136. // set special functions
  137. NCDModuleProcess_SetSpecialFuncs(&o->process,
  138. (NCDModuleProcess_func_getspecialvar)process_func_getspecialvar,
  139. (NCDModuleProcess_func_getspecialobj)process_func_getspecialobj);
  140. // set state working
  141. o->state = STATE_WORKING;
  142. return;
  143. fail1:
  144. free(o);
  145. fail0:
  146. NCDModuleInst_Backend_SetError(i);
  147. NCDModuleInst_Backend_Dead(i);
  148. }
  149. void instance_free (struct instance *o)
  150. {
  151. NCDModuleInst *i = o->i;
  152. // free process
  153. NCDModuleProcess_Free(&o->process);
  154. // free instance
  155. free(o);
  156. NCDModuleInst_Backend_Dead(i);
  157. }
  158. static void func_die (void *vo)
  159. {
  160. struct instance *o = vo;
  161. ASSERT(o->state != STATE_TERMINATING)
  162. // request process to terminate
  163. NCDModuleProcess_Terminate(&o->process);
  164. // set state terminating
  165. o->state = STATE_TERMINATING;
  166. }
  167. static void func_clean (void *vo)
  168. {
  169. struct instance *o = vo;
  170. if (o->state != STATE_WAITING) {
  171. return;
  172. }
  173. // allow process to continue
  174. NCDModuleProcess_Continue(&o->process);
  175. // set state working
  176. o->state = STATE_WORKING;
  177. }
  178. static int func_getvar (void *vo, const char *name, NCDValue *out)
  179. {
  180. struct instance *o = vo;
  181. ASSERT(o->state == STATE_UP)
  182. return NCDModuleProcess_GetVar(&o->process, name, out);
  183. }
  184. static NCDModuleInst * func_getobj (void *vo, const char *name)
  185. {
  186. struct instance *o = vo;
  187. ASSERT(o->state == STATE_UP)
  188. return NCDModuleProcess_GetObj(&o->process, name);
  189. }
  190. static const struct NCDModule modules[] = {
  191. {
  192. .type = "call",
  193. .func_new = func_new,
  194. .func_die = func_die,
  195. .func_clean = func_clean,
  196. .func_getvar = func_getvar,
  197. .func_getobj = func_getobj
  198. }, {
  199. .type = NULL
  200. }
  201. };
  202. const struct NCDModuleGroup ncdmodule_call = {
  203. .modules = modules
  204. };