call.c 5.8 KB

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