NCDInterpreter.c 42 KB

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