NCDInterpreter.c 40 KB

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