imperative.c 10 KB

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