NCDInterpreter.c 39 KB

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