NCDInterpreter.c 42 KB

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