foreach.c 13 KB

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