NCDInterpreter.c 42 KB

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