NCDInterpreter.c 41 KB

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