rimp_call.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * @file rimp_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. * Reverse imperative call.
  25. *
  26. * Synopsis:
  27. * rimp_call(string template_name, list args)
  28. * rimp_call_timeout(string template_name, list args, string timeout_ms)
  29. *
  30. * Description:
  31. * Goes up immediately. On deinitialization, does the following, in order:
  32. * 1. starts a template process from the given template and arguments
  33. * and waits for it to completely initialize, or for the timeout to
  34. * elapse, then
  35. * 2. requests termination of the process and waits for it to terminate,
  36. * then finally
  37. * 3. deinitializes.
  38. *
  39. * WARNING: if there's a bug in the NCD program and the started template
  40. * process never initializes completely, rimp_call() will never
  41. * terminate, and you will have to kill the NCD process by force.
  42. */
  43. #include <stdlib.h>
  44. #include <misc/parse_number.h>
  45. #include <misc/string_begins_with.h>
  46. #include <ncd/NCDModule.h>
  47. #include <generated/blog_channel_ncd_rimp_call.h>
  48. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  49. #define STATE_UP 1
  50. #define STATE_WORKING 2
  51. #define STATE_CLEANING 3
  52. struct instance {
  53. NCDModuleInst *i;
  54. char *template_name;
  55. NCDValue *args;
  56. int have_timeout;
  57. BTimer timer;
  58. NCDModuleProcess process;
  59. int state;
  60. };
  61. static void instance_free (struct instance *o);
  62. static void process_handler_event (struct instance *o, int event)
  63. {
  64. switch (event) {
  65. case NCDMODULEPROCESS_EVENT_UP: {
  66. ASSERT(o->state == STATE_WORKING)
  67. // stop timer
  68. if (o->have_timeout) {
  69. BReactor_RemoveTimer(o->i->reactor, &o->timer);
  70. }
  71. // start terminating
  72. NCDModuleProcess_Terminate(&o->process);
  73. // set state cleaning
  74. o->state = STATE_CLEANING;
  75. } break;
  76. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  77. ASSERT(o->state == STATE_CLEANING)
  78. // free process
  79. NCDModuleProcess_Free(&o->process);
  80. // free instance
  81. instance_free(o);
  82. return;
  83. } break;
  84. default: ASSERT(0);
  85. }
  86. }
  87. static int process_func_getspecialvar (struct instance *o, const char *name, NCDValue *out)
  88. {
  89. ASSERT(o->state == STATE_WORKING || o->state == STATE_CLEANING)
  90. size_t p;
  91. if (p = string_begins_with(name, "_caller.")) {
  92. return NCDModuleInst_Backend_GetVar(o->i, name + p, out);
  93. }
  94. return 0;
  95. }
  96. static NCDModuleInst * process_func_getspecialobj (struct instance *o, const char *name)
  97. {
  98. ASSERT(o->state == STATE_WORKING || o->state == STATE_CLEANING)
  99. size_t p;
  100. if (p = string_begins_with(name, "_caller.")) {
  101. return NCDModuleInst_Backend_GetObj(o->i, name + p);
  102. }
  103. return NULL;
  104. }
  105. static void timer_handler (struct instance *o)
  106. {
  107. ASSERT(o->have_timeout)
  108. ASSERT(o->state == STATE_WORKING)
  109. ModuleLog(o->i, BLOG_ERROR, "rimp_call timeout elapsed");
  110. // start terminating
  111. NCDModuleProcess_Terminate(&o->process);
  112. // set state cleaning
  113. o->state = STATE_CLEANING;
  114. }
  115. static void func_new (NCDModuleInst *i)
  116. {
  117. // allocate instance
  118. struct instance *o = malloc(sizeof(*o));
  119. if (!o) {
  120. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  121. goto fail0;
  122. }
  123. NCDModuleInst_Backend_SetUser(i, o);
  124. // init arguments
  125. o->i = i;
  126. // check arguments
  127. NCDValue *template_name_arg;
  128. if (!NCDValue_ListRead(i->args, 2, &template_name_arg, &o->args)) {
  129. ModuleLog(i, BLOG_ERROR, "wrong arity");
  130. goto fail1;
  131. }
  132. if (NCDValue_Type(template_name_arg) != NCDVALUE_STRING || NCDValue_Type(o->args) != NCDVALUE_LIST) {
  133. ModuleLog(i, BLOG_ERROR, "wrong type");
  134. goto fail1;
  135. }
  136. o->template_name = NCDValue_StringValue(template_name_arg);
  137. // set have no timeout
  138. o->have_timeout = 0;
  139. // signal up
  140. NCDModuleInst_Backend_Up(i);
  141. // set state up
  142. o->state = STATE_UP;
  143. return;
  144. fail1:
  145. free(o);
  146. fail0:
  147. NCDModuleInst_Backend_SetError(i);
  148. NCDModuleInst_Backend_Dead(i);
  149. }
  150. static void func_new_timeout (NCDModuleInst *i)
  151. {
  152. // allocate instance
  153. struct instance *o = malloc(sizeof(*o));
  154. if (!o) {
  155. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  156. goto fail0;
  157. }
  158. NCDModuleInst_Backend_SetUser(i, o);
  159. // init arguments
  160. o->i = i;
  161. // check arguments
  162. NCDValue *template_name_arg;
  163. NCDValue *timeout_arg;
  164. if (!NCDValue_ListRead(i->args, 3, &template_name_arg, &o->args, &timeout_arg)) {
  165. ModuleLog(i, BLOG_ERROR, "wrong arity");
  166. goto fail1;
  167. }
  168. if (NCDValue_Type(template_name_arg) != NCDVALUE_STRING || NCDValue_Type(o->args) != NCDVALUE_LIST ||
  169. NCDValue_Type(timeout_arg) != NCDVALUE_STRING) {
  170. ModuleLog(i, BLOG_ERROR, "wrong type");
  171. goto fail1;
  172. }
  173. o->template_name = NCDValue_StringValue(template_name_arg);
  174. // parse timeout
  175. uintmax_t timeout;
  176. if (!parse_unsigned_integer(NCDValue_StringValue(timeout_arg), &timeout) || timeout > UINT64_MAX) {
  177. ModuleLog(i, BLOG_ERROR, "wrong timeout");
  178. goto fail1;
  179. }
  180. // set have timeout
  181. o->have_timeout = 1;
  182. // init timer
  183. BTimer_Init(&o->timer, timeout, (BTimer_handler)timer_handler, o);
  184. // signal up
  185. NCDModuleInst_Backend_Up(i);
  186. // set state up
  187. o->state = STATE_UP;
  188. return;
  189. fail1:
  190. free(o);
  191. fail0:
  192. NCDModuleInst_Backend_SetError(i);
  193. NCDModuleInst_Backend_Dead(i);
  194. }
  195. void instance_free (struct instance *o)
  196. {
  197. NCDModuleInst *i = o->i;
  198. // free instance
  199. free(o);
  200. NCDModuleInst_Backend_Dead(i);
  201. }
  202. static void func_die (void *vo)
  203. {
  204. struct instance *o = vo;
  205. ASSERT(o->state == STATE_UP)
  206. // copy arguments
  207. NCDValue args;
  208. if (!NCDValue_InitCopy(&args, o->args)) {
  209. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  210. goto fail;
  211. }
  212. // create process
  213. if (!NCDModuleProcess_Init(&o->process, o->i, o->template_name, args, o, (NCDModuleProcess_handler_event)process_handler_event)) {
  214. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  215. NCDValue_Free(&args);
  216. goto fail;
  217. }
  218. // set special functions
  219. NCDModuleProcess_SetSpecialFuncs(&o->process,
  220. (NCDModuleProcess_func_getspecialvar)process_func_getspecialvar,
  221. (NCDModuleProcess_func_getspecialobj)process_func_getspecialobj);
  222. // start timer if used
  223. if (o->have_timeout) {
  224. BReactor_SetTimer(o->i->reactor, &o->timer);
  225. }
  226. // set state working
  227. o->state = STATE_WORKING;
  228. return;
  229. fail:
  230. instance_free(o);
  231. }
  232. static const struct NCDModule modules[] = {
  233. {
  234. .type = "rimp_call",
  235. .func_new = func_new,
  236. .func_die = func_die
  237. }, {
  238. .type = "rimp_call_timeout",
  239. .func_new = func_new_timeout,
  240. .func_die = func_die
  241. }, {
  242. .type = NULL
  243. }
  244. };
  245. const struct NCDModuleGroup ncdmodule_rimp_call = {
  246. .modules = modules
  247. };