foreach.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /**
  2. * @file foreach.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:
  25. * foreach(list list, string template, list args)
  26. *
  27. * Description:
  28. * Initializes a template process for each element of list, sequentially,
  29. * obeying to the usual execution model of NCD.
  30. * It's equivalent to (except for special variables):
  31. *
  32. * call(template, args);
  33. * ...
  34. * call(template, args); # one call() for every element of list
  35. *
  36. * Template process specials:
  37. *
  38. * _index - index of the list element corresponding to the template process,
  39. * as a decimal string, starting from zero
  40. * _elem - element of list corresponding to the template process
  41. * _caller.X - X as seen from the foreach() statement
  42. */
  43. #include <stdlib.h>
  44. #include <misc/balloc.h>
  45. #include <misc/string_begins_with.h>
  46. #include <system/BReactor.h>
  47. #include <ncd/NCDModule.h>
  48. #include <generated/blog_channel_ncd_foreach.h>
  49. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  50. #define ISTATE_WORKING 1
  51. #define ISTATE_UP 2
  52. #define ISTATE_WAITING 3
  53. #define ISTATE_TERMINATING 4
  54. #define ESTATE_FORGOTTEN 1
  55. #define ESTATE_DOWN 2
  56. #define ESTATE_UP 3
  57. #define ESTATE_WAITING 4
  58. #define ESTATE_TERMINATING 5
  59. struct element;
  60. struct instance {
  61. NCDModuleInst *i;
  62. char *template_name;
  63. NCDValue *args;
  64. BTimer timer;
  65. size_t num_elems;
  66. struct element *elems;
  67. size_t gp; // good pointer
  68. size_t ip; // initialized pointer
  69. int state;
  70. };
  71. struct element {
  72. struct instance *inst;
  73. size_t i;
  74. NCDValue *value;
  75. NCDModuleProcess process;
  76. int state;
  77. };
  78. static void assert_state (struct instance *o);
  79. static void work (struct instance *o);
  80. static void advance (struct instance *o);
  81. static void timer_handler (struct instance *o);
  82. static void element_process_handler_event (struct element *e, int event);
  83. static int element_process_func_getspecialvar (struct element *e, const char *name, NCDValue *out);
  84. static NCDModuleInst * element_process_func_getspecialobj (struct element *e, const char *name);
  85. static void instance_free (struct instance *o);
  86. static void assert_state (struct instance *o)
  87. {
  88. ASSERT(o->gp <= o->num_elems)
  89. ASSERT(o->ip <= o->num_elems)
  90. ASSERT(o->gp <= o->ip)
  91. // check GP
  92. for (size_t i = 0; i < o->gp; i++) {
  93. if (o->gp > 0 && i == o->gp - 1) {
  94. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  95. o->elems[i].state == ESTATE_WAITING)
  96. } else {
  97. ASSERT(o->elems[i].state == ESTATE_UP)
  98. }
  99. }
  100. // check IP
  101. size_t ip = o->num_elems;
  102. while (ip > 0 && o->elems[ip - 1].state == ESTATE_FORGOTTEN) {
  103. ip--;
  104. }
  105. ASSERT(o->ip == ip)
  106. }
  107. static void work (struct instance *o)
  108. {
  109. assert_state(o);
  110. // stop timer
  111. BReactor_RemoveTimer(o->i->reactor, &o->timer);
  112. if (o->state == ISTATE_WAITING) {
  113. return;
  114. }
  115. if (o->state == ISTATE_UP && !(o->gp == o->ip && o->gp == o->num_elems && (o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP))) {
  116. // signal down
  117. NCDModuleInst_Backend_Down(o->i);
  118. // set state waiting
  119. o->state = ISTATE_WAITING;
  120. return;
  121. }
  122. if (o->gp < o->ip) {
  123. // get last element
  124. struct element *le = &o->elems[o->ip - 1];
  125. ASSERT(le->state != ESTATE_FORGOTTEN)
  126. // start terminating if not already
  127. if (le->state != ESTATE_TERMINATING) {
  128. // request termination
  129. NCDModuleProcess_Terminate(&le->process);
  130. // set element state terminating
  131. le->state = ESTATE_TERMINATING;
  132. }
  133. return;
  134. }
  135. if (o->state == ISTATE_TERMINATING) {
  136. // finally die
  137. instance_free(o);
  138. return;
  139. }
  140. if (o->gp == o->num_elems && (o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)) {
  141. if (o->state == ISTATE_WORKING) {
  142. // signal up
  143. NCDModuleInst_Backend_Up(o->i);
  144. // set state up
  145. o->state = ISTATE_UP;
  146. }
  147. return;
  148. }
  149. if (o->gp > 0 && o->elems[o->gp - 1].state == ESTATE_WAITING) {
  150. // get last element
  151. struct element *le = &o->elems[o->gp - 1];
  152. // continue process
  153. NCDModuleProcess_Continue(&le->process);
  154. // set state down
  155. le->state = ESTATE_DOWN;
  156. return;
  157. }
  158. if (o->gp > 0 && o->elems[o->gp - 1].state == ESTATE_DOWN) {
  159. return;
  160. }
  161. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  162. advance(o);
  163. return;
  164. }
  165. static void advance (struct instance *o)
  166. {
  167. assert_state(o);
  168. ASSERT(o->gp == o->ip)
  169. ASSERT(o->gp < o->num_elems)
  170. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  171. ASSERT(o->elems[o->gp].state == ESTATE_FORGOTTEN)
  172. // get next element
  173. struct element *e = &o->elems[o->gp];
  174. // copy arguments
  175. NCDValue args;
  176. if (!NCDValue_InitCopy(&args, o->args)) {
  177. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  178. goto fail;
  179. }
  180. // init process
  181. if (!NCDModuleProcess_Init(&e->process, o->i, o->template_name, args, e, (NCDModuleProcess_handler_event)element_process_handler_event)) {
  182. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  183. NCDValue_Free(&args);
  184. goto fail;
  185. }
  186. // set special functions
  187. NCDModuleProcess_SetSpecialFuncs(&e->process,
  188. (NCDModuleProcess_func_getspecialvar)element_process_func_getspecialvar,
  189. (NCDModuleProcess_func_getspecialobj)element_process_func_getspecialobj);
  190. // set element state down
  191. e->state = ESTATE_DOWN;
  192. // increment GP and IP
  193. o->gp++;
  194. o->ip++;
  195. return;
  196. fail:
  197. // set timer
  198. BReactor_SetTimer(o->i->reactor, &o->timer);
  199. }
  200. static void timer_handler (struct instance *o)
  201. {
  202. assert_state(o);
  203. ASSERT(o->gp == o->ip)
  204. ASSERT(o->gp < o->num_elems)
  205. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  206. ASSERT(o->elems[o->gp].state == ESTATE_FORGOTTEN)
  207. advance(o);
  208. return;
  209. }
  210. static void element_process_handler_event (struct element *e, int event)
  211. {
  212. struct instance *o = e->inst;
  213. assert_state(o);
  214. ASSERT(e->i < o->ip)
  215. ASSERT(e->state != ESTATE_FORGOTTEN)
  216. switch (event) {
  217. case NCDMODULEPROCESS_EVENT_UP: {
  218. ASSERT(e->state == ESTATE_DOWN)
  219. ASSERT(o->gp == o->ip)
  220. ASSERT(o->gp == e->i + 1)
  221. // set element state up
  222. e->state = ESTATE_UP;
  223. } break;
  224. case NCDMODULEPROCESS_EVENT_DOWN: {
  225. ASSERT(e->state == ESTATE_UP)
  226. // set element state waiting
  227. e->state = ESTATE_WAITING;
  228. // bump down GP
  229. if (o->gp > e->i + 1) {
  230. o->gp = e->i + 1;
  231. }
  232. } break;
  233. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  234. ASSERT(e->state == ESTATE_TERMINATING)
  235. ASSERT(o->gp < o->ip)
  236. ASSERT(o->ip == e->i + 1)
  237. // free process
  238. NCDModuleProcess_Free(&e->process);
  239. // set element state forgotten
  240. e->state = ESTATE_FORGOTTEN;
  241. // decrement IP
  242. o->ip--;
  243. } break;
  244. default: ASSERT(0);
  245. }
  246. work(o);
  247. return;
  248. }
  249. static int element_process_func_getspecialvar (struct element *e, const char *name, NCDValue *out)
  250. {
  251. struct instance *o = e->inst;
  252. ASSERT(e->state != ESTATE_FORGOTTEN)
  253. if (e->i >= o->gp) {
  254. BLog(BLOG_ERROR, "tried to resolve variable %s but it's dirty", name);
  255. return 0;
  256. }
  257. if (!strcmp(name, "_index")) {
  258. char str[64];
  259. snprintf(str, sizeof(str), "%zu", e->i);
  260. if (!NCDValue_InitString(out, str)) {
  261. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  262. return 0;
  263. }
  264. return 1;
  265. }
  266. if (!strcmp(name, "_elem")) {
  267. if (!NCDValue_InitCopy(out, e->value)) {
  268. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  269. return 0;
  270. }
  271. return 1;
  272. }
  273. size_t p;
  274. if (p = string_begins_with(name, "_caller.")) {
  275. return NCDModuleInst_Backend_GetVar(o->i, name + p, out);
  276. }
  277. return 0;
  278. }
  279. static NCDModuleInst * element_process_func_getspecialobj (struct element *e, const char *name)
  280. {
  281. struct instance *o = e->inst;
  282. ASSERT(e->state != ESTATE_FORGOTTEN)
  283. if (e->i >= o->gp) {
  284. BLog(BLOG_ERROR, "tried to resolve object %s but it's dirty", name);
  285. return NULL;
  286. }
  287. size_t p;
  288. if (p = string_begins_with(name, "_caller.")) {
  289. return NCDModuleInst_Backend_GetObj(o->i, name + p);
  290. }
  291. return NULL;
  292. }
  293. static void func_new (NCDModuleInst *i)
  294. {
  295. // allocate instance
  296. struct instance *o = malloc(sizeof(*o));
  297. if (!o) {
  298. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  299. goto fail0;
  300. }
  301. NCDModuleInst_Backend_SetUser(i, o);
  302. // init arguments
  303. o->i = i;
  304. // read arguments
  305. NCDValue *arg_list;
  306. NCDValue *arg_template;
  307. NCDValue *arg_args;
  308. if (!NCDValue_ListRead(i->args, 3, &arg_list, &arg_template, &arg_args)) {
  309. ModuleLog(i, BLOG_ERROR, "wrong arity");
  310. goto fail1;
  311. }
  312. if (NCDValue_Type(arg_list) != NCDVALUE_LIST || NCDValue_Type(arg_template) != NCDVALUE_STRING ||
  313. NCDValue_Type(arg_args) != NCDVALUE_LIST
  314. ) {
  315. ModuleLog(i, BLOG_ERROR, "wrong type");
  316. goto fail1;
  317. }
  318. o->template_name = NCDValue_StringValue(arg_template);
  319. o->args = arg_args;
  320. // init timer
  321. BTimer_Init(&o->timer, 5000, (BTimer_handler)timer_handler, o);
  322. // count elements
  323. o->num_elems = NCDValue_ListCount(arg_list);
  324. // allocate elements
  325. if (!(o->elems = BAllocArray(o->num_elems, sizeof(o->elems[0])))) {
  326. ModuleLog(i, BLOG_ERROR, "BAllocArray failed");
  327. goto fail1;
  328. }
  329. NCDValue *ev = NCDValue_ListFirst(arg_list);
  330. for (size_t i = 0; i < o->num_elems; i++) {
  331. struct element *e = &o->elems[i];
  332. // set instance
  333. e->inst = o;
  334. // set index
  335. e->i = i;
  336. // set value
  337. e->value = ev;
  338. // set state forgotten
  339. e->state = ESTATE_FORGOTTEN;
  340. ev = NCDValue_ListNext(arg_list, ev);
  341. }
  342. // set GP and IP zero
  343. o->gp = 0;
  344. o->ip = 0;
  345. // set state working
  346. o->state = ISTATE_WORKING;
  347. work(o);
  348. return;
  349. fail1:
  350. free(o);
  351. fail0:
  352. NCDModuleInst_Backend_SetError(i);
  353. NCDModuleInst_Backend_Dead(i);
  354. }
  355. static void instance_free (struct instance *o)
  356. {
  357. NCDModuleInst *i = o->i;
  358. ASSERT(o->gp == 0)
  359. ASSERT(o->ip == 0)
  360. // free elements
  361. BFree(o->elems);
  362. // free timer
  363. BReactor_RemoveTimer(o->i->reactor, &o->timer);
  364. // free instance
  365. free(o);
  366. NCDModuleInst_Backend_Dead(i);
  367. }
  368. static void func_die (void *vo)
  369. {
  370. struct instance *o = vo;
  371. assert_state(o);
  372. ASSERT(o->state != ISTATE_TERMINATING)
  373. // set GP zero
  374. o->gp = 0;
  375. // set state terminating
  376. o->state = ISTATE_TERMINATING;
  377. work(o);
  378. return;
  379. }
  380. static void func_clean (void *vo)
  381. {
  382. struct instance *o = vo;
  383. if (o->state != ISTATE_WAITING) {
  384. return;
  385. }
  386. // set state working
  387. o->state = ISTATE_WORKING;
  388. work(o);
  389. return;
  390. }
  391. static const struct NCDModule modules[] = {
  392. {
  393. .type = "foreach",
  394. .func_new = func_new,
  395. .func_die = func_die,
  396. .func_clean = func_clean
  397. }, {
  398. .type = NULL
  399. }
  400. };
  401. const struct NCDModuleGroup ncdmodule_foreach = {
  402. .modules = modules
  403. };