NCDInterpreter.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. /**
  2. * @file NCDInterpreter.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. #include <stdint.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <limits.h>
  33. #include <stdarg.h>
  34. #include <misc/offset.h>
  35. #include <misc/balloc.h>
  36. #include <misc/expstring.h>
  37. #include <base/BLog.h>
  38. #include <ncd/NCDSugar.h>
  39. #include <ncd/modules/modules.h>
  40. #include "NCDInterpreter.h"
  41. #include <generated/blog_channel_ncd.h>
  42. #define SSTATE_CHILD 0
  43. #define SSTATE_ADULT 1
  44. #define SSTATE_DYING 2
  45. #define SSTATE_FORGOTTEN 3
  46. #define PSTATE_WORKING 0
  47. #define PSTATE_UP 1
  48. #define PSTATE_WAITING 2
  49. #define PSTATE_TERMINATING 3
  50. struct statement {
  51. NCDModuleInst inst;
  52. NCDValMem args_mem;
  53. int mem_size;
  54. int i;
  55. };
  56. struct process {
  57. NCDInterpreter *interp;
  58. BReactor *reactor;
  59. NCDInterpProcess *iprocess;
  60. NCDModuleProcess *module_process;
  61. BSmallTimer wait_timer;
  62. BSmallPending work_job;
  63. LinkedList1Node list_node; // node in processes
  64. int ap;
  65. int fp;
  66. int num_statements;
  67. unsigned int error:1;
  68. unsigned int have_alloc:1;
  69. #ifndef NDEBUG
  70. int state;
  71. #endif
  72. struct statement statements[];
  73. };
  74. static void start_terminate (NCDInterpreter *interp, int exit_code);
  75. static char * implode_id_strings (NCDInterpreter *interp, const NCD_string_id_t *names, size_t num_names, char del);
  76. static void clear_process_cache (NCDInterpreter *interp);
  77. static struct process * process_allocate (NCDInterpreter *interp, NCDInterpProcess *iprocess);
  78. static void process_release (struct process *p, int no_push);
  79. static void process_assert_statements_cleared (struct process *p);
  80. static int process_new (NCDInterpreter *interp, NCDInterpProcess *iprocess, NCDModuleProcess *module_process);
  81. static void process_free (struct process *p, NCDModuleProcess **out_mp);
  82. static void process_set_state (struct process *p, int state);
  83. static void process_start_terminating (struct process *p);
  84. static int process_have_child (struct process *p);
  85. static void process_assert_pointers (struct process *p);
  86. static void process_logfunc (struct process *p);
  87. static void process_log (struct process *p, int level, const char *fmt, ...);
  88. static void process_work_job_handler_working (struct process *p);
  89. static void process_work_job_handler_up (struct process *p);
  90. static void process_work_job_handler_waiting (struct process *p);
  91. static void process_work_job_handler_terminating (struct process *p);
  92. static int replace_placeholders_callback (void *arg, int plid, NCDValMem *mem, NCDValRef *out);
  93. static void process_advance (struct process *p);
  94. static void process_wait_timer_handler (BSmallTimer *timer);
  95. static int process_find_object (struct process *p, int pos, NCD_string_id_t name, NCDObject *out_object);
  96. static int process_resolve_object_expr (struct process *p, int pos, const NCD_string_id_t *names, size_t num_names, NCDObject *out_object);
  97. static int process_resolve_variable_expr (struct process *p, int pos, const NCD_string_id_t *names, size_t num_names, NCDValMem *mem, NCDValRef *out_value);
  98. static void statement_logfunc (struct statement *ps);
  99. static void statement_log (struct statement *ps, int level, const char *fmt, ...);
  100. static struct process * statement_process (struct statement *ps);
  101. static int statement_mem_is_allocated (struct statement *ps);
  102. static int statement_mem_size (struct statement *ps);
  103. static int statement_allocate_memory (struct statement *ps, int alloc_size);
  104. static void statement_instance_func_event (NCDModuleInst *inst, int event);
  105. static int statement_instance_func_getobj (NCDModuleInst *inst, NCD_string_id_t objname, NCDObject *out_object);
  106. static int statement_instance_func_initprocess (void *vinterp, NCDModuleProcess *mp, NCD_string_id_t template_name);
  107. static void statement_instance_logfunc (NCDModuleInst *inst);
  108. static void statement_instance_func_interp_exit (void *vinterp, int exit_code);
  109. static int statement_instance_func_interp_getargs (void *vinterp, NCDValMem *mem, NCDValRef *out_value);
  110. static btime_t statement_instance_func_interp_getretrytime (void *vinterp);
  111. static void process_moduleprocess_func_event (struct process *p, int event);
  112. static int process_moduleprocess_func_getobj (struct process *p, NCD_string_id_t name, NCDObject *out_object);
  113. #define STATEMENT_LOG(ps, channel, ...) if (BLog_WouldLog(BLOG_CURRENT_CHANNEL, channel)) statement_log(ps, channel, __VA_ARGS__)
  114. int NCDInterpreter_Init (NCDInterpreter *o, NCDProgram program, struct NCDInterpreter_params params)
  115. {
  116. ASSERT(!NCDProgram_ContainsElemType(&program, NCDPROGRAMELEM_INCLUDE));
  117. ASSERT(!NCDProgram_ContainsElemType(&program, NCDPROGRAMELEM_INCLUDE_GUARD));
  118. ASSERT(params.handler_finished);
  119. ASSERT(params.num_extra_args >= 0);
  120. ASSERT(params.reactor);
  121. #ifndef BADVPN_NO_PROCESS
  122. ASSERT(params.manager);
  123. #endif
  124. #ifndef BADVPN_NO_UDEV
  125. ASSERT(params.umanager);
  126. #endif
  127. #ifndef BADVPN_NO_RANDOM
  128. ASSERT(params.random2);
  129. #endif
  130. // set params
  131. o->params = params;
  132. // set not terminating
  133. o->terminating = 0;
  134. // set program
  135. o->program = program;
  136. // init string index
  137. if (!NCDStringIndex_Init(&o->string_index)) {
  138. BLog(BLOG_ERROR, "NCDStringIndex_Init failed");
  139. goto fail0;
  140. }
  141. // init module index
  142. if (!NCDModuleIndex_Init(&o->mindex, &o->string_index)) {
  143. BLog(BLOG_ERROR, "NCDModuleIndex_Init failed");
  144. goto fail2;
  145. }
  146. // init pointers to global resources in out struct NCDModuleInst_iparams.
  147. // Don't initialize any callback at this point as these must not be called
  148. // from globalinit functions of modules.
  149. o->module_iparams.reactor = params.reactor;
  150. #ifndef BADVPN_NO_PROCESS
  151. o->module_iparams.manager = params.manager;
  152. #endif
  153. #ifndef BADVPN_NO_UDEV
  154. o->module_iparams.umanager = params.umanager;
  155. #endif
  156. #ifndef BADVPN_NO_RANDOM
  157. o->module_iparams.random2 = params.random2;
  158. #endif
  159. o->module_iparams.string_index = &o->string_index;
  160. // add module groups to index and allocate string id's for base_type's
  161. for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
  162. if (!NCDModuleIndex_AddGroup(&o->mindex, *g, &o->module_iparams, &o->string_index)) {
  163. BLog(BLOG_ERROR, "NCDModuleIndex_AddGroup failed");
  164. goto fail3;
  165. }
  166. }
  167. // desugar
  168. if (!NCDSugar_Desugar(&o->program)) {
  169. BLog(BLOG_ERROR, "NCDSugar_Desugar failed");
  170. goto fail3;
  171. }
  172. // init placeholder database
  173. if (!NCDPlaceholderDb_Init(&o->placeholder_db, &o->string_index)) {
  174. BLog(BLOG_ERROR, "NCDPlaceholderDb_Init failed");
  175. goto fail3;
  176. }
  177. // init interp program
  178. if (!NCDInterpProg_Init(&o->iprogram, &o->program, &o->string_index, &o->placeholder_db, &o->mindex)) {
  179. BLog(BLOG_ERROR, "NCDInterpProg_Init failed");
  180. goto fail5;
  181. }
  182. // init the rest of the module parameters structures
  183. o->module_params.func_event = statement_instance_func_event;
  184. o->module_params.func_getobj = statement_instance_func_getobj;
  185. o->module_params.logfunc = (BLog_logfunc)statement_instance_logfunc;
  186. o->module_params.iparams = &o->module_iparams;
  187. o->module_iparams.user = o;
  188. o->module_iparams.func_initprocess = statement_instance_func_initprocess;
  189. o->module_iparams.func_interp_exit = statement_instance_func_interp_exit;
  190. o->module_iparams.func_interp_getargs = statement_instance_func_interp_getargs;
  191. o->module_iparams.func_interp_getretrytime = statement_instance_func_interp_getretrytime;
  192. // init processes list
  193. LinkedList1_Init(&o->processes);
  194. // init processes
  195. for (NCDProgramElem *elem = NCDProgram_FirstElem(&o->program); elem; elem = NCDProgram_NextElem(&o->program, elem)) {
  196. ASSERT(NCDProgramElem_Type(elem) == NCDPROGRAMELEM_PROCESS)
  197. NCDProcess *p = NCDProgramElem_Process(elem);
  198. if (NCDProcess_IsTemplate(p)) {
  199. continue;
  200. }
  201. // get string id for process name
  202. NCD_string_id_t name_id = NCDStringIndex_Lookup(&o->string_index, NCDProcess_Name(p));
  203. ASSERT(name_id >= 0)
  204. // find iprocess
  205. NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&o->iprogram, name_id);
  206. ASSERT(iprocess)
  207. if (!process_new(o, iprocess, NULL)) {
  208. BLog(BLOG_ERROR, "failed to initialize process, exiting");
  209. goto fail7;
  210. }
  211. }
  212. DebugObject_Init(&o->d_obj);
  213. return 1;
  214. fail7:;
  215. // free processes
  216. LinkedList1Node *ln;
  217. while (ln = LinkedList1_GetFirst(&o->processes)) {
  218. struct process *p = UPPER_OBJECT(ln, struct process, list_node);
  219. BSmallPending_Unset(&p->work_job, BReactor_PendingGroup(o->params.reactor));
  220. NCDModuleProcess *mp;
  221. process_free(p, &mp);
  222. ASSERT(!mp)
  223. }
  224. // clear process cache (process_free() above may push to cache)
  225. clear_process_cache(o);
  226. // free interp program
  227. NCDInterpProg_Free(&o->iprogram);
  228. fail5:
  229. // free placeholder database
  230. NCDPlaceholderDb_Free(&o->placeholder_db);
  231. fail3:
  232. // free module index
  233. NCDModuleIndex_Free(&o->mindex);
  234. fail2:
  235. // free string index
  236. NCDStringIndex_Free(&o->string_index);
  237. fail0:
  238. // free program AST
  239. NCDProgram_Free(&o->program);
  240. return 0;
  241. }
  242. void NCDInterpreter_Free (NCDInterpreter *o)
  243. {
  244. DebugObject_Free(&o->d_obj);
  245. // any process that exists must be completely uninitialized
  246. // free processes
  247. LinkedList1Node *ln;
  248. while (ln = LinkedList1_GetFirst(&o->processes)) {
  249. struct process *p = UPPER_OBJECT(ln, struct process, list_node);
  250. BSmallPending_Unset(&p->work_job, BReactor_PendingGroup(o->params.reactor));
  251. NCDModuleProcess *mp;
  252. process_free(p, &mp);
  253. ASSERT(!mp)
  254. }
  255. // clear process cache
  256. clear_process_cache(o);
  257. // free interp program
  258. NCDInterpProg_Free(&o->iprogram);
  259. // free placeholder database
  260. NCDPlaceholderDb_Free(&o->placeholder_db);
  261. // free module index
  262. NCDModuleIndex_Free(&o->mindex);
  263. // free string index
  264. NCDStringIndex_Free(&o->string_index);
  265. // free program AST
  266. NCDProgram_Free(&o->program);
  267. }
  268. void NCDInterpreter_RequestShutdown (NCDInterpreter *o, int exit_code)
  269. {
  270. DebugObject_Access(&o->d_obj);
  271. start_terminate(o, exit_code);
  272. }
  273. void start_terminate (NCDInterpreter *interp, int exit_code)
  274. {
  275. // remember exit code
  276. interp->main_exit_code = exit_code;
  277. // if we're already terminating, there's nothing to do
  278. if (interp->terminating) {
  279. return;
  280. }
  281. // set the terminating flag
  282. interp->terminating = 1;
  283. // if there are no processes, we're done
  284. if (LinkedList1_IsEmpty(&interp->processes)) {
  285. interp->params.handler_finished(interp->params.user, interp->main_exit_code);
  286. return;
  287. }
  288. // start terminating non-template processes
  289. for (LinkedList1Node *ln = LinkedList1_GetFirst(&interp->processes); ln; ln = LinkedList1Node_Next(ln)) {
  290. struct process *p = UPPER_OBJECT(ln, struct process, list_node);
  291. if (p->module_process) {
  292. continue;
  293. }
  294. process_start_terminating(p);
  295. }
  296. }
  297. char * implode_id_strings (NCDInterpreter *interp, const NCD_string_id_t *names, size_t num_names, char del)
  298. {
  299. ExpString str;
  300. if (!ExpString_Init(&str)) {
  301. goto fail0;
  302. }
  303. int is_first = 1;
  304. while (num_names > 0) {
  305. if (!is_first && !ExpString_AppendChar(&str, del)) {
  306. goto fail1;
  307. }
  308. const char *name_str = NCDStringIndex_Value(&interp->string_index, *names);
  309. if (!ExpString_Append(&str, name_str)) {
  310. goto fail1;
  311. }
  312. names++;
  313. num_names--;
  314. is_first = 0;
  315. }
  316. return ExpString_Get(&str);
  317. fail1:
  318. ExpString_Free(&str);
  319. fail0:
  320. return NULL;
  321. }
  322. void clear_process_cache (NCDInterpreter *interp)
  323. {
  324. for (NCDProgramElem *elem = NCDProgram_FirstElem(&interp->program); elem; elem = NCDProgram_NextElem(&interp->program, elem)) {
  325. ASSERT(NCDProgramElem_Type(elem) == NCDPROGRAMELEM_PROCESS)
  326. NCDProcess *ast_p = NCDProgramElem_Process(elem);
  327. NCD_string_id_t name_id = NCDStringIndex_Lookup(&interp->string_index, NCDProcess_Name(ast_p));
  328. NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&interp->iprogram, name_id);
  329. struct process *p;
  330. while (p = NCDInterpProcess_CachePull(iprocess)) {
  331. process_release(p, 1);
  332. }
  333. }
  334. }
  335. struct process * process_allocate (NCDInterpreter *interp, NCDInterpProcess *iprocess)
  336. {
  337. ASSERT(iprocess)
  338. // try to pull from cache
  339. struct process *p = NCDInterpProcess_CachePull(iprocess);
  340. if (p) {
  341. goto allocated;
  342. }
  343. // get number of statements
  344. int num_statements = NCDInterpProcess_NumStatements(iprocess);
  345. // get size of preallocated memory
  346. int mem_size = NCDInterpProcess_PreallocSize(iprocess);
  347. if (mem_size < 0) {
  348. goto fail0;
  349. }
  350. // start with size of process structure
  351. size_t alloc_size = sizeof(struct process);
  352. // add size of statements array
  353. if (num_statements > SIZE_MAX / sizeof(struct statement)) {
  354. goto fail0;
  355. }
  356. if (!BSizeAdd(&alloc_size, num_statements * sizeof(struct statement))) {
  357. goto fail0;
  358. }
  359. // align for preallocated memory
  360. if (!BSizeAlign(&alloc_size, BMAX_ALIGN)) {
  361. goto fail0;
  362. }
  363. size_t mem_off = alloc_size;
  364. // add size of preallocated memory
  365. if (mem_size > SIZE_MAX || !BSizeAdd(&alloc_size, mem_size)) {
  366. goto fail0;
  367. }
  368. // allocate memory
  369. p = BAlloc(alloc_size);
  370. if (!p) {
  371. goto fail0;
  372. }
  373. // set variables
  374. p->interp = interp;
  375. p->reactor = interp->params.reactor;
  376. p->iprocess = iprocess;
  377. p->ap = 0;
  378. p->fp = 0;
  379. p->num_statements = num_statements;
  380. p->error = 0;
  381. p->have_alloc = 0;
  382. // init statements
  383. char *mem = (char *)p + mem_off;
  384. for (int i = 0; i < num_statements; i++) {
  385. struct statement *ps = &p->statements[i];
  386. ps->i = i;
  387. ps->inst.istate = SSTATE_FORGOTTEN;
  388. ps->mem_size = NCDInterpProcess_StatementPreallocSize(iprocess, i);
  389. ps->inst.mem = mem + NCDInterpProcess_StatementPreallocOffset(iprocess, i);
  390. }
  391. // init timer
  392. BSmallTimer_Init(&p->wait_timer, process_wait_timer_handler);
  393. // init work job
  394. BSmallPending_Init(&p->work_job, BReactor_PendingGroup(p->reactor), NULL, NULL);
  395. allocated:
  396. ASSERT(p->interp == interp)
  397. ASSERT(p->reactor == interp->params.reactor)
  398. ASSERT(p->iprocess == iprocess)
  399. ASSERT(p->ap == 0)
  400. ASSERT(p->fp == 0)
  401. ASSERT(p->num_statements == NCDInterpProcess_NumStatements(iprocess))
  402. ASSERT(p->error == 0)
  403. process_assert_statements_cleared(p);
  404. ASSERT(!BSmallPending_IsSet(&p->work_job))
  405. ASSERT(!BSmallTimer_IsRunning(&p->wait_timer))
  406. return p;
  407. fail0:
  408. BLog(BLOG_ERROR, "failed to allocate memory for process %s", NCDInterpProcess_Name(iprocess));
  409. return NULL;
  410. }
  411. void process_release (struct process *p, int no_push)
  412. {
  413. ASSERT(p->ap == 0)
  414. ASSERT(p->fp == 0)
  415. ASSERT(p->error == 0)
  416. process_assert_statements_cleared(p);
  417. ASSERT(!BSmallPending_IsSet(&p->work_job))
  418. ASSERT(!BSmallTimer_IsRunning(&p->wait_timer))
  419. // try to push to cache
  420. if (!no_push && !p->have_alloc) {
  421. if (NCDInterpProcess_CachePush(p->iprocess, p)) {
  422. return;
  423. }
  424. }
  425. // free work job
  426. BSmallPending_Free(&p->work_job, BReactor_PendingGroup(p->reactor));
  427. // free statement memory
  428. if (p->have_alloc) {
  429. for (int i = 0; i < p->num_statements; i++) {
  430. struct statement *ps = &p->statements[i];
  431. if (statement_mem_is_allocated(ps)) {
  432. free(ps->inst.mem);
  433. }
  434. }
  435. }
  436. // free strucure
  437. BFree(p);
  438. }
  439. void process_assert_statements_cleared (struct process *p)
  440. {
  441. #ifndef NDEBUG
  442. for (int i = 0; i < p->num_statements; i++) {
  443. ASSERT(p->statements[i].i == i)
  444. ASSERT(p->statements[i].inst.istate == SSTATE_FORGOTTEN)
  445. }
  446. #endif
  447. }
  448. int process_new (NCDInterpreter *interp, NCDInterpProcess *iprocess, NCDModuleProcess *module_process)
  449. {
  450. ASSERT(iprocess)
  451. // allocate prepared process struct
  452. struct process *p = process_allocate(interp, iprocess);
  453. if (!p) {
  454. return 0;
  455. }
  456. // set module process pointer
  457. p->module_process = module_process;
  458. // set module process handlers
  459. if (p->module_process) {
  460. NCDModuleProcess_Interp_SetHandlers(p->module_process, p,
  461. (NCDModuleProcess_interp_func_event)process_moduleprocess_func_event,
  462. (NCDModuleProcess_interp_func_getobj)process_moduleprocess_func_getobj);
  463. }
  464. // set state
  465. process_set_state(p, PSTATE_WORKING);
  466. BSmallPending_SetHandler(&p->work_job, (BSmallPending_handler)process_work_job_handler_working, p);
  467. // insert to processes list
  468. LinkedList1_Append(&interp->processes, &p->list_node);
  469. // schedule work
  470. BSmallPending_Set(&p->work_job, BReactor_PendingGroup(p->reactor));
  471. return 1;
  472. }
  473. void process_set_state (struct process *p, int state)
  474. {
  475. #ifndef NDEBUG
  476. p->state = state;
  477. #endif
  478. }
  479. void process_free (struct process *p, NCDModuleProcess **out_mp)
  480. {
  481. ASSERT(p->ap == 0)
  482. ASSERT(p->fp == 0)
  483. ASSERT(out_mp)
  484. ASSERT(!BSmallPending_IsSet(&p->work_job))
  485. // give module process to caller so it can inform the process creator that the process has terminated
  486. *out_mp = p->module_process;
  487. // remove from processes list
  488. LinkedList1_Remove(&p->interp->processes, &p->list_node);
  489. // free timer
  490. BReactor_RemoveSmallTimer(p->reactor, &p->wait_timer);
  491. // clear error
  492. p->error = 0;
  493. process_release(p, 0);
  494. }
  495. void process_start_terminating (struct process *p)
  496. {
  497. // set state terminating
  498. process_set_state(p, PSTATE_TERMINATING);
  499. BSmallPending_SetHandler(&p->work_job, (BSmallPending_handler)process_work_job_handler_terminating, p);
  500. // schedule work
  501. BSmallPending_Set(&p->work_job, BReactor_PendingGroup(p->reactor));
  502. }
  503. int process_have_child (struct process *p)
  504. {
  505. return (p->ap > 0 && p->statements[p->ap - 1].inst.istate == SSTATE_CHILD);
  506. }
  507. void process_assert_pointers (struct process *p)
  508. {
  509. ASSERT(p->ap <= p->num_statements)
  510. ASSERT(p->fp >= p->ap)
  511. ASSERT(p->fp <= p->num_statements)
  512. #ifndef NDEBUG
  513. // check AP
  514. for (int i = 0; i < p->ap; i++) {
  515. if (i == p->ap - 1) {
  516. ASSERT(p->statements[i].inst.istate == SSTATE_ADULT || p->statements[i].inst.istate == SSTATE_CHILD)
  517. } else {
  518. ASSERT(p->statements[i].inst.istate == SSTATE_ADULT)
  519. }
  520. }
  521. // check FP
  522. int fp = p->num_statements;
  523. while (fp > 0 && p->statements[fp - 1].inst.istate == SSTATE_FORGOTTEN) {
  524. fp--;
  525. }
  526. ASSERT(p->fp == fp)
  527. #endif
  528. }
  529. void process_logfunc (struct process *p)
  530. {
  531. BLog_Append("process %s: ", NCDInterpProcess_Name(p->iprocess));
  532. }
  533. void process_log (struct process *p, int level, const char *fmt, ...)
  534. {
  535. va_list vl;
  536. va_start(vl, fmt);
  537. BLog_LogViaFuncVarArg((BLog_logfunc)process_logfunc, p, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  538. va_end(vl);
  539. }
  540. void process_work_job_handler_working (struct process *p)
  541. {
  542. process_assert_pointers(p);
  543. ASSERT(p->state == PSTATE_WORKING)
  544. // cleaning up?
  545. if (p->ap < p->fp) {
  546. // order the last living statement to die, if needed
  547. struct statement *ps = &p->statements[p->fp - 1];
  548. if (ps->inst.istate == SSTATE_DYING) {
  549. return;
  550. }
  551. STATEMENT_LOG(ps, BLOG_INFO, "killing");
  552. // set statement state DYING
  553. ps->inst.istate = SSTATE_DYING;
  554. // order it to die
  555. NCDModuleInst_Die(&ps->inst);
  556. return;
  557. }
  558. // clean?
  559. if (process_have_child(p)) {
  560. ASSERT(p->ap > 0)
  561. ASSERT(p->ap <= p->num_statements)
  562. struct statement *ps = &p->statements[p->ap - 1];
  563. ASSERT(ps->inst.istate == SSTATE_CHILD)
  564. STATEMENT_LOG(ps, BLOG_INFO, "clean");
  565. // report clean
  566. NCDModuleInst_Clean(&ps->inst);
  567. return;
  568. }
  569. // finished?
  570. if (p->ap == p->num_statements) {
  571. process_log(p, BLOG_INFO, "victory");
  572. // set state up
  573. process_set_state(p, PSTATE_UP);
  574. BSmallPending_SetHandler(&p->work_job, (BSmallPending_handler)process_work_job_handler_up, p);
  575. // set module process up
  576. if (p->module_process) {
  577. NCDModuleProcess_Interp_Up(p->module_process);
  578. }
  579. return;
  580. }
  581. // advancing?
  582. struct statement *ps = &p->statements[p->ap];
  583. ASSERT(ps->inst.istate == SSTATE_FORGOTTEN)
  584. if (p->error) {
  585. STATEMENT_LOG(ps, BLOG_INFO, "waiting after error");
  586. // clear error
  587. p->error = 0;
  588. // set wait timer
  589. BReactor_SetSmallTimer(p->reactor, &p->wait_timer, BTIMER_SET_RELATIVE, p->interp->params.retry_time);
  590. } else {
  591. // advance
  592. process_advance(p);
  593. }
  594. }
  595. void process_work_job_handler_up (struct process *p)
  596. {
  597. process_assert_pointers(p);
  598. ASSERT(p->state == PSTATE_UP)
  599. ASSERT(p->ap < p->num_statements || process_have_child(p))
  600. // if we have module process, wait for its permission to continue
  601. if (p->module_process) {
  602. // set state waiting
  603. process_set_state(p, PSTATE_WAITING);
  604. BSmallPending_SetHandler(&p->work_job, (BSmallPending_handler)process_work_job_handler_waiting, p);
  605. // set module process down
  606. NCDModuleProcess_Interp_Down(p->module_process);
  607. return;
  608. }
  609. // set state working
  610. process_set_state(p, PSTATE_WORKING);
  611. BSmallPending_SetHandler(&p->work_job, (BSmallPending_handler)process_work_job_handler_working, p);
  612. // delegate the rest to the working handler
  613. process_work_job_handler_working(p);
  614. }
  615. void process_work_job_handler_waiting (struct process *p)
  616. {
  617. process_assert_pointers(p);
  618. ASSERT(p->state == PSTATE_WAITING)
  619. // do absolutely nothing. Having this no-op handler avoids a branch
  620. // in statement_instance_func_event().
  621. }
  622. void process_work_job_handler_terminating (struct process *p)
  623. {
  624. process_assert_pointers(p);
  625. ASSERT(p->state == PSTATE_TERMINATING)
  626. again:
  627. if (p->fp == 0) {
  628. NCDInterpreter *interp = p->interp;
  629. // free process
  630. NCDModuleProcess *mp;
  631. process_free(p, &mp);
  632. // if program is terminating amd there are no more processes, exit program
  633. if (interp->terminating && LinkedList1_IsEmpty(&interp->processes)) {
  634. ASSERT(!mp)
  635. interp->params.handler_finished(interp->params.user, interp->main_exit_code);
  636. return;
  637. }
  638. // inform the process creator that the process has terminated
  639. if (mp) {
  640. NCDModuleProcess_Interp_Terminated(mp);
  641. return;
  642. }
  643. return;
  644. }
  645. // order the last living statement to die, if needed
  646. struct statement *ps = &p->statements[p->fp - 1];
  647. ASSERT(ps->inst.istate != SSTATE_FORGOTTEN)
  648. if (ps->inst.istate == SSTATE_DYING) {
  649. return;
  650. }
  651. STATEMENT_LOG(ps, BLOG_INFO, "killing");
  652. // update AP
  653. if (p->ap > ps->i) {
  654. p->ap = ps->i;
  655. }
  656. // optimize for statements which can be destroyed immediately
  657. if (NCDModuleInst_TryFree(&ps->inst)) {
  658. STATEMENT_LOG(ps, BLOG_INFO, "died");
  659. // free arguments memory
  660. NCDValMem_Free(&ps->args_mem);
  661. // set statement state FORGOTTEN
  662. ps->inst.istate = SSTATE_FORGOTTEN;
  663. // update FP
  664. while (p->fp > 0 && p->statements[p->fp - 1].inst.istate == SSTATE_FORGOTTEN) {
  665. p->fp--;
  666. }
  667. goto again;
  668. }
  669. // set statement state DYING
  670. ps->inst.istate = SSTATE_DYING;
  671. // order it to die
  672. NCDModuleInst_Die(&ps->inst);
  673. return;
  674. }
  675. int replace_placeholders_callback (void *arg, int plid, NCDValMem *mem, NCDValRef *out)
  676. {
  677. struct process *p = arg;
  678. ASSERT(plid >= 0)
  679. ASSERT(mem)
  680. ASSERT(out)
  681. const NCD_string_id_t *varnames;
  682. size_t num_names;
  683. NCDPlaceholderDb_GetVariable(&p->interp->placeholder_db, plid, &varnames, &num_names);
  684. return process_resolve_variable_expr(p, p->ap, varnames, num_names, mem, out);
  685. }
  686. void process_advance (struct process *p)
  687. {
  688. process_assert_pointers(p);
  689. ASSERT(p->ap == p->fp)
  690. ASSERT(!process_have_child(p))
  691. ASSERT(p->ap < p->num_statements)
  692. ASSERT(!p->error)
  693. ASSERT(!BSmallPending_IsSet(&p->work_job))
  694. ASSERT(p->state == PSTATE_WORKING)
  695. struct statement *ps = &p->statements[p->ap];
  696. ASSERT(ps->inst.istate == SSTATE_FORGOTTEN)
  697. STATEMENT_LOG(ps, BLOG_INFO, "initializing");
  698. // need to determine the module and object to use it on (if it's a method)
  699. const struct NCDInterpModule *module;
  700. void *method_context = NULL;
  701. // get object names, e.g. "my.cat" in "my.cat->meow();"
  702. // (or NULL if this is not a method statement)
  703. const NCD_string_id_t *objnames;
  704. size_t num_objnames;
  705. NCDInterpProcess_StatementObjNames(p->iprocess, p->ap, &objnames, &num_objnames);
  706. if (!objnames) {
  707. // not a method; module is already known by NCDInterpProcess
  708. module = NCDInterpProcess_StatementGetSimpleModule(p->iprocess, p->ap);
  709. if (!module) {
  710. const char *cmdname_str = NCDInterpProcess_StatementCmdName(p->iprocess, p->ap, &p->interp->string_index);
  711. STATEMENT_LOG(ps, BLOG_ERROR, "unknown simple statement: %s", cmdname_str);
  712. goto fail0;
  713. }
  714. } else {
  715. // get object
  716. NCDObject object;
  717. if (!process_resolve_object_expr(p, p->ap, objnames, num_objnames, &object)) {
  718. goto fail0;
  719. }
  720. // get object type
  721. NCD_string_id_t object_type = NCDObject_Type(&object);
  722. if (object_type < 0) {
  723. STATEMENT_LOG(ps, BLOG_ERROR, "cannot call method on object with no type");
  724. goto fail0;
  725. }
  726. // get method context
  727. method_context = NCDObject_MethodUser(&object);
  728. // find module based on type of object
  729. module = NCDInterpProcess_StatementGetMethodModule(p->iprocess, p->ap, object_type, &p->interp->mindex);
  730. if (!module) {
  731. const char *type_str = NCDStringIndex_Value(&p->interp->string_index, object_type);
  732. const char *cmdname_str = NCDInterpProcess_StatementCmdName(p->iprocess, p->ap, &p->interp->string_index);
  733. STATEMENT_LOG(ps, BLOG_ERROR, "unknown method statement: %s::%s", type_str, cmdname_str);
  734. goto fail0;
  735. }
  736. }
  737. // copy arguments
  738. NCDValRef args;
  739. NCDValReplaceProg prog;
  740. if (!NCDInterpProcess_CopyStatementArgs(p->iprocess, ps->i, &ps->args_mem, &args, &prog)) {
  741. STATEMENT_LOG(ps, BLOG_ERROR, "NCDInterpProcess_CopyStatementArgs failed");
  742. goto fail0;
  743. }
  744. // replace placeholders with values of variables
  745. if (!NCDValReplaceProg_Execute(prog, &ps->args_mem, replace_placeholders_callback, p)) {
  746. STATEMENT_LOG(ps, BLOG_ERROR, "failed to replace variables in arguments with values");
  747. goto fail1;
  748. }
  749. // convert non-continuous strings unless the module can handle them
  750. if (!(module->module.flags & NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS)) {
  751. if (!NCDValMem_ConvertNonContinuousStrings(&ps->args_mem, &args)) {
  752. STATEMENT_LOG(ps, BLOG_ERROR, "NCDValMem_ConvertNonContinuousStrings failed");
  753. goto fail1;
  754. }
  755. }
  756. // allocate memory
  757. if (!statement_allocate_memory(ps, module->module.alloc_size)) {
  758. STATEMENT_LOG(ps, BLOG_ERROR, "failed to allocate memory");
  759. goto fail1;
  760. }
  761. // set statement state CHILD
  762. ps->inst.istate = SSTATE_CHILD;
  763. // increment AP
  764. p->ap++;
  765. // increment FP
  766. p->fp++;
  767. process_assert_pointers(p);
  768. // initialize module instance
  769. NCDModuleInst_Init(&ps->inst, module, method_context, args, &p->interp->module_params);
  770. return;
  771. fail1:
  772. NCDValMem_Free(&ps->args_mem);
  773. fail0:
  774. // set error
  775. p->error = 1;
  776. // schedule work to start the timer
  777. BSmallPending_Set(&p->work_job, BReactor_PendingGroup(p->reactor));
  778. }
  779. void process_wait_timer_handler (BSmallTimer *timer)
  780. {
  781. struct process *p = UPPER_OBJECT(timer, struct process, wait_timer);
  782. process_assert_pointers(p);
  783. ASSERT(!BSmallPending_IsSet(&p->work_job))
  784. // check if something happened that means we no longer need to retry
  785. if (p->ap != p->fp || process_have_child(p) || p->ap == p->num_statements) {
  786. return;
  787. }
  788. process_log(p, BLOG_INFO, "retrying");
  789. // advance. Note: the asserts for this are indeed satisfied, though this
  790. // it not trivial to prove.
  791. process_advance(p);
  792. }
  793. int process_find_object (struct process *p, int pos, NCD_string_id_t name, NCDObject *out_object)
  794. {
  795. ASSERT(pos >= 0)
  796. ASSERT(pos <= p->num_statements)
  797. ASSERT(out_object)
  798. int i = NCDInterpProcess_FindStatement(p->iprocess, pos, name);
  799. if (i >= 0) {
  800. struct statement *ps = &p->statements[i];
  801. ASSERT(i < p->num_statements)
  802. if (ps->inst.istate == SSTATE_FORGOTTEN) {
  803. process_log(p, BLOG_ERROR, "statement (%d) is uninitialized", i);
  804. return 0;
  805. }
  806. *out_object = NCDModuleInst_Object(&ps->inst);
  807. return 1;
  808. }
  809. if (p->module_process && NCDModuleProcess_Interp_GetSpecialObj(p->module_process, name, out_object)) {
  810. return 1;
  811. }
  812. return 0;
  813. }
  814. int process_resolve_object_expr (struct process *p, int pos, const NCD_string_id_t *names, size_t num_names, NCDObject *out_object)
  815. {
  816. ASSERT(pos >= 0)
  817. ASSERT(pos <= p->num_statements)
  818. ASSERT(names)
  819. ASSERT(num_names > 0)
  820. ASSERT(out_object)
  821. NCDObject object;
  822. if (!process_find_object(p, pos, names[0], &object)) {
  823. goto fail;
  824. }
  825. if (!NCDObject_ResolveObjExprCompact(&object, names + 1, num_names - 1, out_object)) {
  826. goto fail;
  827. }
  828. return 1;
  829. fail:;
  830. char *name = implode_id_strings(p->interp, names, num_names, '.');
  831. process_log(p, BLOG_ERROR, "failed to resolve object (%s) from position %zu", (name ? name : ""), pos);
  832. free(name);
  833. return 0;
  834. }
  835. int process_resolve_variable_expr (struct process *p, int pos, const NCD_string_id_t *names, size_t num_names, NCDValMem *mem, NCDValRef *out_value)
  836. {
  837. ASSERT(pos >= 0)
  838. ASSERT(pos <= p->num_statements)
  839. ASSERT(names)
  840. ASSERT(num_names > 0)
  841. ASSERT(mem)
  842. ASSERT(out_value)
  843. NCDObject object;
  844. if (!process_find_object(p, pos, names[0], &object)) {
  845. goto fail;
  846. }
  847. if (!NCDObject_ResolveVarExprCompact(&object, names + 1, num_names - 1, mem, out_value)) {
  848. goto fail;
  849. }
  850. return 1;
  851. fail:;
  852. char *name = implode_id_strings(p->interp, names, num_names, '.');
  853. process_log(p, BLOG_ERROR, "failed to resolve variable (%s) from position %zu", (name ? name : ""), pos);
  854. free(name);
  855. return 0;
  856. }
  857. void statement_logfunc (struct statement *ps)
  858. {
  859. process_logfunc(statement_process(ps));
  860. BLog_Append("statement %zu: ", ps->i);
  861. }
  862. void statement_log (struct statement *ps, int level, const char *fmt, ...)
  863. {
  864. va_list vl;
  865. va_start(vl, fmt);
  866. BLog_LogViaFuncVarArg((BLog_logfunc)statement_logfunc, ps, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  867. va_end(vl);
  868. }
  869. struct process * statement_process (struct statement *ps)
  870. {
  871. return UPPER_OBJECT(ps - ps->i, struct process, statements);
  872. }
  873. int statement_mem_is_allocated (struct statement *ps)
  874. {
  875. return (ps->mem_size < 0);
  876. }
  877. int statement_mem_size (struct statement *ps)
  878. {
  879. return (ps->mem_size >= 0 ? ps->mem_size : -ps->mem_size);
  880. }
  881. int statement_allocate_memory (struct statement *ps, int alloc_size)
  882. {
  883. ASSERT(alloc_size >= 0)
  884. if (alloc_size > statement_mem_size(ps)) {
  885. // allocate new memory
  886. char *new_mem = malloc(alloc_size);
  887. if (!new_mem) {
  888. STATEMENT_LOG(ps, BLOG_ERROR, "malloc failed");
  889. return 0;
  890. }
  891. // release old memory unless it was preallocated
  892. if (statement_mem_is_allocated(ps)) {
  893. free(ps->inst.mem);
  894. }
  895. struct process *p = statement_process(ps);
  896. // register memory in statement
  897. ps->inst.mem = new_mem;
  898. ps->mem_size = -alloc_size;
  899. // set the alloc flag in the process to make sure process_free()
  900. // releases the allocated memory
  901. p->have_alloc = 1;
  902. // register alloc size for future preallocations
  903. NCDInterpProcess_StatementBumpAllocSize(p->iprocess, ps->i, alloc_size);
  904. }
  905. return 1;
  906. }
  907. void statement_instance_func_event (NCDModuleInst *inst, int event)
  908. {
  909. struct statement *ps = UPPER_OBJECT(inst, struct statement, inst);
  910. ASSERT(ps->inst.istate == SSTATE_CHILD || ps->inst.istate == SSTATE_ADULT || ps->inst.istate == SSTATE_DYING)
  911. struct process *p = statement_process(ps);
  912. process_assert_pointers(p);
  913. // schedule work
  914. BSmallPending_Set(&p->work_job, BReactor_PendingGroup(p->reactor));
  915. switch (event) {
  916. case NCDMODULE_EVENT_UP: {
  917. ASSERT(ps->inst.istate == SSTATE_CHILD)
  918. STATEMENT_LOG(ps, BLOG_INFO, "up");
  919. // set state ADULT
  920. ps->inst.istate = SSTATE_ADULT;
  921. } break;
  922. case NCDMODULE_EVENT_DOWN: {
  923. ASSERT(ps->inst.istate == SSTATE_ADULT)
  924. STATEMENT_LOG(ps, BLOG_INFO, "down");
  925. // set state CHILD
  926. ps->inst.istate = SSTATE_CHILD;
  927. // clear error
  928. if (ps->i < p->ap) {
  929. p->error = 0;
  930. }
  931. // update AP
  932. if (p->ap > ps->i + 1) {
  933. p->ap = ps->i + 1;
  934. }
  935. } break;
  936. case NCDMODULE_EVENT_DOWNUP: {
  937. ASSERT(ps->inst.istate == SSTATE_ADULT)
  938. STATEMENT_LOG(ps, BLOG_INFO, "down");
  939. STATEMENT_LOG(ps, BLOG_INFO, "up");
  940. // clear error
  941. if (ps->i < p->ap) {
  942. p->error = 0;
  943. }
  944. // update AP
  945. if (p->ap > ps->i + 1) {
  946. p->ap = ps->i + 1;
  947. }
  948. } break;
  949. case NCDMODULE_EVENT_DEAD: {
  950. STATEMENT_LOG(ps, BLOG_INFO, "died");
  951. // free instance
  952. NCDModuleInst_Free(&ps->inst);
  953. // free arguments memory
  954. NCDValMem_Free(&ps->args_mem);
  955. // set state FORGOTTEN
  956. ps->inst.istate = SSTATE_FORGOTTEN;
  957. // update AP
  958. if (p->ap > ps->i) {
  959. p->ap = ps->i;
  960. }
  961. // update FP
  962. while (p->fp > 0 && p->statements[p->fp - 1].inst.istate == SSTATE_FORGOTTEN) {
  963. p->fp--;
  964. }
  965. } break;
  966. case NCDMODULE_EVENT_DEADERROR: {
  967. STATEMENT_LOG(ps, BLOG_ERROR, "died with error");
  968. // free instance
  969. NCDModuleInst_Free(&ps->inst);
  970. // free arguments memory
  971. NCDValMem_Free(&ps->args_mem);
  972. // set state FORGOTTEN
  973. ps->inst.istate = SSTATE_FORGOTTEN;
  974. // set error
  975. if (ps->i < p->ap) {
  976. p->error = 1;
  977. }
  978. // update AP
  979. if (p->ap > ps->i) {
  980. p->ap = ps->i;
  981. }
  982. // update FP
  983. while (p->fp > 0 && p->statements[p->fp - 1].inst.istate == SSTATE_FORGOTTEN) {
  984. p->fp--;
  985. }
  986. } break;
  987. }
  988. }
  989. int statement_instance_func_getobj (NCDModuleInst *inst, NCD_string_id_t objname, NCDObject *out_object)
  990. {
  991. struct statement *ps = UPPER_OBJECT(inst, struct statement, inst);
  992. ASSERT(ps->inst.istate != SSTATE_FORGOTTEN)
  993. return process_find_object(statement_process(ps), ps->i, objname, out_object);
  994. }
  995. int statement_instance_func_initprocess (void *vinterp, NCDModuleProcess* mp, NCD_string_id_t template_name)
  996. {
  997. NCDInterpreter *interp = vinterp;
  998. // find process
  999. NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&interp->iprogram, template_name);
  1000. if (!iprocess) {
  1001. const char *str = NCDStringIndex_Value(&interp->string_index, template_name);
  1002. BLog(BLOG_ERROR, "no template named %s", str);
  1003. return 0;
  1004. }
  1005. // make sure it's a template
  1006. if (!NCDInterpProcess_IsTemplate(iprocess)) {
  1007. const char *str = NCDStringIndex_Value(&interp->string_index, template_name);
  1008. BLog(BLOG_ERROR, "need template to create a process, but %s is a process", str);
  1009. return 0;
  1010. }
  1011. // create process
  1012. if (!process_new(interp, iprocess, mp)) {
  1013. const char *str = NCDStringIndex_Value(&interp->string_index, template_name);
  1014. BLog(BLOG_ERROR, "failed to create process from template %s", str);
  1015. return 0;
  1016. }
  1017. if (BLog_WouldLog(BLOG_INFO, BLOG_CURRENT_CHANNEL)) {
  1018. const char *str = NCDStringIndex_Value(&interp->string_index, template_name);
  1019. BLog(BLOG_INFO, "created process from template %s", str);
  1020. }
  1021. return 1;
  1022. }
  1023. void statement_instance_logfunc (NCDModuleInst *inst)
  1024. {
  1025. struct statement *ps = UPPER_OBJECT(inst, struct statement, inst);
  1026. ASSERT(ps->inst.istate != SSTATE_FORGOTTEN)
  1027. statement_logfunc(ps);
  1028. BLog_Append("module: ");
  1029. }
  1030. void statement_instance_func_interp_exit (void *vinterp, int exit_code)
  1031. {
  1032. NCDInterpreter *interp = vinterp;
  1033. start_terminate(interp, exit_code);
  1034. }
  1035. int statement_instance_func_interp_getargs (void *vinterp, NCDValMem *mem, NCDValRef *out_value)
  1036. {
  1037. NCDInterpreter *interp = vinterp;
  1038. *out_value = NCDVal_NewList(mem, interp->params.num_extra_args);
  1039. if (NCDVal_IsInvalid(*out_value)) {
  1040. BLog(BLOG_ERROR, "NCDVal_NewList failed");
  1041. goto fail;
  1042. }
  1043. for (int i = 0; i < interp->params.num_extra_args; i++) {
  1044. NCDValRef arg = NCDVal_NewString(mem, interp->params.extra_args[i]);
  1045. if (NCDVal_IsInvalid(arg)) {
  1046. BLog(BLOG_ERROR, "NCDVal_NewString failed");
  1047. goto fail;
  1048. }
  1049. if (!NCDVal_ListAppend(*out_value, arg)) {
  1050. BLog(BLOG_ERROR, "depth limit exceeded");
  1051. goto fail;
  1052. }
  1053. }
  1054. return 1;
  1055. fail:
  1056. *out_value = NCDVal_NewInvalid();
  1057. return 1;
  1058. }
  1059. btime_t statement_instance_func_interp_getretrytime (void *vinterp)
  1060. {
  1061. NCDInterpreter *interp = vinterp;
  1062. return interp->params.retry_time;
  1063. }
  1064. void process_moduleprocess_func_event (struct process *p, int event)
  1065. {
  1066. ASSERT(p->module_process)
  1067. switch (event) {
  1068. case NCDMODULEPROCESS_INTERP_EVENT_CONTINUE: {
  1069. ASSERT(p->state == PSTATE_WAITING)
  1070. // set state working
  1071. process_set_state(p, PSTATE_WORKING);
  1072. BSmallPending_SetHandler(&p->work_job, (BSmallPending_handler)process_work_job_handler_working, p);
  1073. // schedule work
  1074. BSmallPending_Set(&p->work_job, BReactor_PendingGroup(p->reactor));
  1075. } break;
  1076. case NCDMODULEPROCESS_INTERP_EVENT_TERMINATE: {
  1077. ASSERT(p->state != PSTATE_TERMINATING)
  1078. process_log(p, BLOG_INFO, "process termination requested");
  1079. // start terminating
  1080. process_start_terminating(p);
  1081. } break;
  1082. default: ASSERT(0);
  1083. }
  1084. }
  1085. int process_moduleprocess_func_getobj (struct process *p, NCD_string_id_t name, NCDObject *out_object)
  1086. {
  1087. ASSERT(p->module_process)
  1088. return process_find_object(p, p->num_statements, name, out_object);
  1089. }