imperative.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /**
  2. * @file imperative.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. * Imperative statement.
  25. *
  26. * Synopsis:
  27. * imperative(string init_template, list init_args, string deinit_template, list deinit_args, string deinit_timeout)
  28. *
  29. * Description:
  30. * Does the following, in order:
  31. * 1. Starts a template process from (init_template, init_args) and waits for it to
  32. * initialize completely.
  33. * 2. Initiates termination of the process and wait for it to terminate.
  34. * 3. Puts the statement UP, then waits for a statement termination request (which may
  35. * already have been received).
  36. * 4. Starts a template process from (deinit_template, deinit_args) and waits for it
  37. * to initialize completely, or for the timeout to elapse.
  38. * 5. Initiates termination of the process and wait for it to terminate.
  39. * 6. Terminates the statement.
  40. *
  41. * If init_template="<none>", steps (1-2) are skipped.
  42. * If deinit_template="<none>", steps (4-5) are skipped.
  43. */
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <misc/parse_number.h>
  47. #include <misc/string_begins_with.h>
  48. #include <ncd/NCDModule.h>
  49. #include <generated/blog_channel_ncd_imperative.h>
  50. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  51. #define STATE_INIT_WORKING 1
  52. #define STATE_INIT_CLEANING 2
  53. #define STATE_UP 3
  54. #define STATE_DEINIT_WORKING 4
  55. #define STATE_DEINIT_CLEANING 5
  56. struct instance {
  57. NCDModuleInst *i;
  58. char *deinit_template;
  59. NCDValue *deinit_args;
  60. BTimer deinit_timer;
  61. NCDModuleProcess process;
  62. int state;
  63. int dying;
  64. };
  65. static int start_process (struct instance *o, const char *templ, NCDValue *args, NCDModuleProcess_handler_event handler);
  66. static void go_deinit (struct instance *o);
  67. static void init_process_handler_event (struct instance *o, int event);
  68. static void deinit_process_handler_event (struct instance *o, int event);
  69. static int process_func_getspecialvar (struct instance *o, const char *name, NCDValue *out);
  70. static NCDModuleInst * process_func_getspecialobj (struct instance *o, const char *name);
  71. static void deinit_timer_handler (struct instance *o);
  72. static void instance_free (struct instance *o);
  73. static int start_process (struct instance *o, const char *templ, NCDValue *args, NCDModuleProcess_handler_event handler)
  74. {
  75. // copy arguments
  76. NCDValue args_copy;
  77. if (!NCDValue_InitCopy(&args_copy, args)) {
  78. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  79. goto fail;
  80. }
  81. // create process
  82. if (!NCDModuleProcess_Init(&o->process, o->i, templ, args_copy, o, handler)) {
  83. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  84. NCDValue_Free(&args_copy);
  85. goto fail;
  86. }
  87. // set special functions
  88. NCDModuleProcess_SetSpecialFuncs(&o->process,
  89. (NCDModuleProcess_func_getspecialvar)process_func_getspecialvar,
  90. (NCDModuleProcess_func_getspecialobj)process_func_getspecialobj);
  91. return 1;
  92. fail:
  93. return 0;
  94. }
  95. static void go_deinit (struct instance *o)
  96. {
  97. ASSERT(o->dying)
  98. // deinit is no-op?
  99. if (!strcmp(o->deinit_template, "<none>")) {
  100. instance_free(o);
  101. return;
  102. }
  103. // start deinit process
  104. if (!start_process(o, o->deinit_template, o->deinit_args, (NCDModuleProcess_handler_event)deinit_process_handler_event)) {
  105. instance_free(o);
  106. return;
  107. }
  108. // start timer
  109. BReactor_SetTimer(o->i->reactor, &o->deinit_timer);
  110. // set state deinit working
  111. o->state = STATE_DEINIT_WORKING;
  112. }
  113. static void init_process_handler_event (struct instance *o, int event)
  114. {
  115. switch (event) {
  116. case NCDMODULEPROCESS_EVENT_UP: {
  117. ASSERT(o->state == STATE_INIT_WORKING)
  118. // start terminating
  119. NCDModuleProcess_Terminate(&o->process);
  120. // set state init cleaning
  121. o->state = STATE_INIT_CLEANING;
  122. } break;
  123. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  124. ASSERT(o->state == STATE_INIT_CLEANING)
  125. // free process
  126. NCDModuleProcess_Free(&o->process);
  127. // were we requested to die aleady?
  128. if (o->dying) {
  129. go_deinit(o);
  130. return;
  131. }
  132. // signal up
  133. NCDModuleInst_Backend_Up(o->i);
  134. // set state up
  135. o->state = STATE_UP;
  136. } break;
  137. default: ASSERT(0);
  138. }
  139. }
  140. static void deinit_process_handler_event (struct instance *o, int event)
  141. {
  142. ASSERT(o->dying)
  143. switch (event) {
  144. case NCDMODULEPROCESS_EVENT_UP: {
  145. ASSERT(o->state == STATE_DEINIT_WORKING)
  146. // stop timer
  147. BReactor_RemoveTimer(o->i->reactor, &o->deinit_timer);
  148. // start terminating
  149. NCDModuleProcess_Terminate(&o->process);
  150. // set state deinit cleaning
  151. o->state = STATE_DEINIT_CLEANING;
  152. } break;
  153. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  154. ASSERT(o->state == STATE_DEINIT_CLEANING)
  155. // free process
  156. NCDModuleProcess_Free(&o->process);
  157. // die
  158. instance_free(o);
  159. return;
  160. } break;
  161. default: ASSERT(0);
  162. }
  163. }
  164. static int process_func_getspecialvar (struct instance *o, const char *name, NCDValue *out)
  165. {
  166. ASSERT(o->state != STATE_UP)
  167. size_t p;
  168. if (p = string_begins_with(name, "_caller.")) {
  169. return NCDModuleInst_Backend_GetVar(o->i, name + p, out);
  170. }
  171. return 0;
  172. }
  173. static NCDModuleInst * process_func_getspecialobj (struct instance *o, const char *name)
  174. {
  175. ASSERT(o->state != STATE_UP)
  176. size_t p;
  177. if (p = string_begins_with(name, "_caller.")) {
  178. return NCDModuleInst_Backend_GetObj(o->i, name + p);
  179. }
  180. return NULL;
  181. }
  182. static void deinit_timer_handler (struct instance *o)
  183. {
  184. ASSERT(o->state == STATE_DEINIT_WORKING)
  185. ModuleLog(o->i, BLOG_ERROR, "imperative deinit timeout elapsed");
  186. // start terminating
  187. NCDModuleProcess_Terminate(&o->process);
  188. // set state deinit cleaning
  189. o->state = STATE_DEINIT_CLEANING;
  190. }
  191. static void func_new (NCDModuleInst *i)
  192. {
  193. // allocate instance
  194. struct instance *o = malloc(sizeof(*o));
  195. if (!o) {
  196. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  197. goto fail0;
  198. }
  199. NCDModuleInst_Backend_SetUser(i, o);
  200. // init arguments
  201. o->i = i;
  202. // check arguments
  203. NCDValue *init_template_arg;
  204. NCDValue *init_args;
  205. NCDValue *deinit_template_arg;
  206. NCDValue *deinit_timeout_arg;
  207. if (!NCDValue_ListRead(i->args, 5, &init_template_arg, &init_args, &deinit_template_arg, &o->deinit_args, &deinit_timeout_arg)) {
  208. ModuleLog(i, BLOG_ERROR, "wrong arity");
  209. goto fail1;
  210. }
  211. if (NCDValue_Type(init_template_arg) != NCDVALUE_STRING || NCDValue_Type(init_args) != NCDVALUE_LIST ||
  212. NCDValue_Type(deinit_template_arg) != NCDVALUE_STRING || NCDValue_Type(o->deinit_args) != NCDVALUE_LIST ||
  213. NCDValue_Type(deinit_timeout_arg) != NCDVALUE_STRING) {
  214. ModuleLog(i, BLOG_ERROR, "wrong type");
  215. goto fail1;
  216. }
  217. char *init_template = NCDValue_StringValue(init_template_arg);
  218. o->deinit_template = NCDValue_StringValue(deinit_template_arg);
  219. // read timeout
  220. uintmax_t timeout;
  221. if (!parse_unsigned_integer(NCDValue_StringValue(deinit_timeout_arg), &timeout) || timeout > UINT64_MAX) {
  222. ModuleLog(i, BLOG_ERROR, "wrong timeout");
  223. goto fail1;
  224. }
  225. // init timer
  226. BTimer_Init(&o->deinit_timer, timeout, (BTimer_handler)deinit_timer_handler, o);
  227. if (!strcmp(init_template, "<none>")) {
  228. // signal up
  229. NCDModuleInst_Backend_Up(i);
  230. // set state up
  231. o->state = STATE_UP;
  232. } else {
  233. // start init process
  234. if (!start_process(o, init_template, init_args, (NCDModuleProcess_handler_event)init_process_handler_event)) {
  235. goto fail1;
  236. }
  237. // set state init working
  238. o->state = STATE_INIT_WORKING;
  239. }
  240. // set not dying
  241. o->dying = 0;
  242. return;
  243. fail1:
  244. free(o);
  245. fail0:
  246. NCDModuleInst_Backend_SetError(i);
  247. NCDModuleInst_Backend_Dead(i);
  248. }
  249. static void instance_free (struct instance *o)
  250. {
  251. NCDModuleInst *i = o->i;
  252. // free instance
  253. free(o);
  254. NCDModuleInst_Backend_Dead(i);
  255. }
  256. static void func_die (void *vo)
  257. {
  258. struct instance *o = vo;
  259. ASSERT(!o->dying)
  260. // set dying
  261. o->dying = 1;
  262. if (o->state == STATE_UP) {
  263. go_deinit(o);
  264. return;
  265. }
  266. }
  267. static const struct NCDModule modules[] = {
  268. {
  269. .type = "imperative",
  270. .func_new = func_new,
  271. .func_die = func_die
  272. }, {
  273. .type = NULL
  274. }
  275. };
  276. const struct NCDModuleGroup ncdmodule_imperative = {
  277. .modules = modules
  278. };