ncd.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. /**
  2. * @file ncd.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 <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <misc/version.h>
  34. #include <misc/loglevel.h>
  35. #include <misc/offset.h>
  36. #include <misc/read_file.h>
  37. #include <misc/balloc.h>
  38. #include <misc/concat_strings.h>
  39. #include <misc/open_standard_streams.h>
  40. #include <misc/expstring.h>
  41. #include <misc/split_string.h>
  42. #include <structure/LinkedList1.h>
  43. #include <base/BLog.h>
  44. #include <base/BLog_syslog.h>
  45. #include <system/BReactor.h>
  46. #include <system/BSignal.h>
  47. #include <system/BProcess.h>
  48. #include <udevmonitor/NCDUdevManager.h>
  49. #include <ncd/NCDConfigParser.h>
  50. #include <ncd/NCDModule.h>
  51. #include <ncd/NCDModuleIndex.h>
  52. #include <ncd/NCDSugar.h>
  53. #include <ncd/NCDInterpProg.h>
  54. #include <ncd/modules/modules.h>
  55. #include "ncd.h"
  56. #include <generated/blog_channel_ncd.h>
  57. #define LOGGER_STDOUT 1
  58. #define LOGGER_STDERR 2
  59. #define LOGGER_SYSLOG 3
  60. #define SSTATE_CHILD 1
  61. #define SSTATE_ADULT 2
  62. #define SSTATE_DYING 3
  63. #define SSTATE_FORGOTTEN 4
  64. #define PSTATE_WORKING 1
  65. #define PSTATE_UP 2
  66. #define PSTATE_WAITING 3
  67. #define PSTATE_TERMINATING 4
  68. struct process;
  69. struct statement {
  70. struct process *p;
  71. NCDModuleInst inst;
  72. NCDValMem args_mem;
  73. char *mem;
  74. int mem_size;
  75. int i;
  76. int state;
  77. };
  78. struct process {
  79. NCDProcess *proc_ast;
  80. NCDInterpBlock *iblock;
  81. NCDModuleProcess *module_process;
  82. BTimer wait_timer;
  83. BPending work_job;
  84. LinkedList1Node list_node; // node in processes
  85. char *mem;
  86. int mem_size;
  87. int state;
  88. int ap;
  89. int fp;
  90. int have_error;
  91. int num_statements;
  92. struct statement statements[];
  93. };
  94. // command-line options
  95. struct {
  96. int help;
  97. int version;
  98. int logger;
  99. char *logger_syslog_facility;
  100. char *logger_syslog_ident;
  101. int loglevel;
  102. int loglevels[BLOG_NUM_CHANNELS];
  103. char *config_file;
  104. int retry_time;
  105. int no_udev;
  106. char **extra_args;
  107. int num_extra_args;
  108. } options;
  109. // reactor
  110. BReactor reactor;
  111. // are we terminating
  112. int terminating;
  113. int main_exit_code;
  114. // process manager
  115. BProcessManager manager;
  116. // udev manager
  117. NCDUdevManager umanager;
  118. // module index
  119. NCDModuleIndex mindex;
  120. // program AST
  121. NCDProgram program;
  122. // structure for efficient interpretation
  123. NCDInterpProg iprogram;
  124. // common module parameters
  125. struct NCDModuleInst_params module_params;
  126. struct NCDModuleInst_iparams module_iparams;
  127. // processes
  128. LinkedList1 processes;
  129. // buffer for concatenating base_type::method_name
  130. char method_concat_buf[200];
  131. static void print_help (const char *name);
  132. static void print_version (void);
  133. static int parse_arguments (int argc, char *argv[]);
  134. static void signal_handler (void *unused);
  135. static void start_terminate (int exit_code);
  136. static int process_new (NCDProcess *proc_ast, NCDInterpBlock *iblock, NCDModuleProcess *module_process);
  137. static void process_free (struct process *p);
  138. static int process_mem_is_preallocated (struct process *p, char *mem);
  139. static void process_start_terminating (struct process *p);
  140. static int process_rap (struct process *p);
  141. static void process_assert_pointers (struct process *p);
  142. static void process_logfunc (struct process *p);
  143. static void process_log (struct process *p, int level, const char *fmt, ...);
  144. static void process_schedule_work (struct process *p);
  145. static void process_work_job_handler (struct process *p);
  146. static void process_advance (struct process *p);
  147. static void process_wait_timer_handler (struct process *p);
  148. static int process_find_object (struct process *p, int pos, const char *name, NCDObject *out_object);
  149. static int process_resolve_object_expr (struct process *p, int pos, char **names, NCDObject *out_object);
  150. static int process_resolve_variable_expr (struct process *p, int pos, char **names, NCDValMem *mem, NCDValRef *out_value);
  151. static void statement_logfunc (struct statement *ps);
  152. static void statement_log (struct statement *ps, int level, const char *fmt, ...);
  153. static int statement_allocate_memory (struct statement *ps, int alloc_size, void **out_mem);
  154. static int statement_resolve_argument (struct statement *ps, NCDInterpValue *arg, NCDValMem *mem, NCDValRef *out);
  155. static void statement_instance_func_event (struct statement *ps, int event);
  156. static int statement_instance_func_getobj (struct statement *ps, const char *objname, NCDObject *out_object);
  157. static int statement_instance_func_initprocess (struct statement *ps, NCDModuleProcess *mp, const char *template_name);
  158. static void statement_instance_logfunc (struct statement *ps);
  159. static void statement_instance_func_interp_exit (struct statement *ps, int exit_code);
  160. static int statement_instance_func_interp_getargs (struct statement *ps, NCDValMem *mem, NCDValRef *out_value);
  161. static btime_t statement_instance_func_interp_getretrytime (struct statement *ps);
  162. static void process_moduleprocess_func_event (struct process *p, int event);
  163. static int process_moduleprocess_func_getobj (struct process *p, const char *name, NCDObject *out_object);
  164. int main (int argc, char **argv)
  165. {
  166. if (argc <= 0) {
  167. return 1;
  168. }
  169. // set exit code
  170. main_exit_code = 1;
  171. // open standard streams
  172. open_standard_streams();
  173. // parse command-line arguments
  174. if (!parse_arguments(argc, argv)) {
  175. fprintf(stderr, "Failed to parse arguments\n");
  176. print_help(argv[0]);
  177. goto fail0;
  178. }
  179. // handle --help and --version
  180. if (options.help) {
  181. print_version();
  182. print_help(argv[0]);
  183. return 0;
  184. }
  185. if (options.version) {
  186. print_version();
  187. return 0;
  188. }
  189. // initialize logger
  190. switch (options.logger) {
  191. case LOGGER_STDOUT:
  192. BLog_InitStdout();
  193. break;
  194. case LOGGER_STDERR:
  195. BLog_InitStderr();
  196. break;
  197. case LOGGER_SYSLOG:
  198. if (!BLog_InitSyslog(options.logger_syslog_ident, options.logger_syslog_facility)) {
  199. fprintf(stderr, "Failed to initialize syslog logger\n");
  200. goto fail0;
  201. }
  202. break;
  203. default:
  204. ASSERT(0);
  205. }
  206. // configure logger channels
  207. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  208. if (options.loglevels[i] >= 0) {
  209. BLog_SetChannelLoglevel(i, options.loglevels[i]);
  210. }
  211. else if (options.loglevel >= 0) {
  212. BLog_SetChannelLoglevel(i, options.loglevel);
  213. }
  214. }
  215. BLog(BLOG_NOTICE, "initializing "GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION);
  216. // initialize network
  217. if (!BNetwork_GlobalInit()) {
  218. BLog(BLOG_ERROR, "BNetwork_GlobalInit failed");
  219. goto fail1;
  220. }
  221. // init time
  222. BTime_Init();
  223. // init reactor
  224. if (!BReactor_Init(&reactor)) {
  225. BLog(BLOG_ERROR, "BReactor_Init failed");
  226. goto fail1;
  227. }
  228. // set not terminating
  229. terminating = 0;
  230. // init process manager
  231. if (!BProcessManager_Init(&manager, &reactor)) {
  232. BLog(BLOG_ERROR, "BProcessManager_Init failed");
  233. goto fail1a;
  234. }
  235. // init udev manager
  236. NCDUdevManager_Init(&umanager, options.no_udev, &reactor, &manager);
  237. // init module index
  238. if (!NCDModuleIndex_Init(&mindex)) {
  239. BLog(BLOG_ERROR, "NCDModuleIndex_Init failed");
  240. goto fail1b;
  241. }
  242. // add module groups to index
  243. for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
  244. if (!NCDModuleIndex_AddGroup(&mindex, *g)) {
  245. BLog(BLOG_ERROR, "NCDModuleIndex_AddGroup failed");
  246. goto fail2;
  247. }
  248. }
  249. // setup signal handler
  250. if (!BSignal_Init(&reactor, signal_handler, NULL)) {
  251. BLog(BLOG_ERROR, "BSignal_Init failed");
  252. goto fail2;
  253. }
  254. // read config file
  255. uint8_t *file;
  256. size_t file_len;
  257. if (!read_file(options.config_file, &file, &file_len)) {
  258. BLog(BLOG_ERROR, "failed to read config file");
  259. goto fail3;
  260. }
  261. // parse config file
  262. if (!NCDConfigParser_Parse((char *)file, file_len, &program)) {
  263. BLog(BLOG_ERROR, "NCDConfigParser_Parse failed");
  264. free(file);
  265. goto fail3;
  266. }
  267. // fee config file memory
  268. free(file);
  269. // desugar
  270. if (!NCDSugar_Desugar(&program)) {
  271. BLog(BLOG_ERROR, "NCDSugar_Desugar failed");
  272. goto fail4;
  273. }
  274. // init interp program
  275. if (!NCDInterpProg_Init(&iprogram, &program)) {
  276. BLog(BLOG_ERROR, "NCDInterpProg_Init failed");
  277. goto fail4;
  278. }
  279. // init module params
  280. struct NCDModuleInitParams params;
  281. params.reactor = &reactor;
  282. params.manager = &manager;
  283. params.umanager = &umanager;
  284. // init modules
  285. size_t num_inited_modules = 0;
  286. for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
  287. if ((*g)->func_globalinit && !(*g)->func_globalinit(params)) {
  288. BLog(BLOG_ERROR, "globalinit failed for some module");
  289. goto fail5;
  290. }
  291. num_inited_modules++;
  292. }
  293. // init common module params
  294. module_params.func_event = (NCDModuleInst_func_event)statement_instance_func_event;
  295. module_params.func_getobj = (NCDModuleInst_func_getobj)statement_instance_func_getobj;
  296. module_params.logfunc = (BLog_logfunc)statement_instance_logfunc;
  297. module_iparams.reactor = &reactor;
  298. module_iparams.manager = &manager;
  299. module_iparams.umanager = &umanager;
  300. module_iparams.func_initprocess = (NCDModuleInst_func_initprocess)statement_instance_func_initprocess;
  301. module_iparams.func_interp_exit = (NCDModuleInst_func_interp_exit)statement_instance_func_interp_exit;
  302. module_iparams.func_interp_getargs = (NCDModuleInst_func_interp_getargs)statement_instance_func_interp_getargs;
  303. module_iparams.func_interp_getretrytime = (NCDModuleInst_func_interp_getretrytime)statement_instance_func_interp_getretrytime;
  304. // init processes list
  305. LinkedList1_Init(&processes);
  306. // init processes
  307. for (NCDProcess *p = NCDProgram_FirstProcess(&program); p; p = NCDProgram_NextProcess(&program, p)) {
  308. if (NCDProcess_IsTemplate(p)) {
  309. continue;
  310. }
  311. // find iblock
  312. NCDProcess *f_proc;
  313. NCDInterpBlock *iblock;
  314. int res = NCDInterpProg_FindProcess(&iprogram, NCDProcess_Name(p), &f_proc, &iblock);
  315. ASSERT(res)
  316. ASSERT(f_proc == p)
  317. if (!process_new(p, iblock, NULL)) {
  318. BLog(BLOG_ERROR, "failed to initialize process, exiting");
  319. goto fail6;
  320. }
  321. }
  322. // enter event loop
  323. BLog(BLOG_NOTICE, "entering event loop");
  324. BReactor_Exec(&reactor);
  325. ASSERT(LinkedList1_IsEmpty(&processes))
  326. fail6:;
  327. LinkedList1Node *ln;
  328. while (ln = LinkedList1_GetFirst(&processes)) {
  329. struct process *p = UPPER_OBJECT(ln, struct process, list_node);
  330. process_free(p);
  331. }
  332. fail5:
  333. // free modules
  334. while (num_inited_modules > 0) {
  335. const struct NCDModuleGroup **g = &ncd_modules[num_inited_modules - 1];
  336. if ((*g)->func_globalfree) {
  337. (*g)->func_globalfree();
  338. }
  339. num_inited_modules--;
  340. }
  341. // free interp program
  342. NCDInterpProg_Free(&iprogram);
  343. fail4:
  344. // free program AST
  345. NCDProgram_Free(&program);
  346. fail3:
  347. // remove signal handler
  348. BSignal_Finish();
  349. fail2:
  350. // free module index
  351. NCDModuleIndex_Free(&mindex);
  352. fail1b:
  353. // free udev manager
  354. NCDUdevManager_Free(&umanager);
  355. // free process manager
  356. BProcessManager_Free(&manager);
  357. fail1a:
  358. // free reactor
  359. BReactor_Free(&reactor);
  360. fail1:
  361. // free logger
  362. BLog(BLOG_NOTICE, "exiting");
  363. BLog_Free();
  364. fail0:
  365. // finish objects
  366. DebugObjectGlobal_Finish();
  367. return main_exit_code;
  368. }
  369. void print_help (const char *name)
  370. {
  371. printf(
  372. "Usage:\n"
  373. " %s\n"
  374. " [--help]\n"
  375. " [--version]\n"
  376. " [--logger <stdout/stderr/syslog>]\n"
  377. " (logger=syslog?\n"
  378. " [--syslog-facility <string>]\n"
  379. " [--syslog-ident <string>]\n"
  380. " )\n"
  381. " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
  382. " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
  383. " --config-file <file>\n"
  384. " [--retry-time <ms>]\n"
  385. " [--no-udev]\n"
  386. " [-- [<extra_arg>] ...]\n",
  387. name
  388. );
  389. }
  390. void print_version (void)
  391. {
  392. printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
  393. }
  394. int parse_arguments (int argc, char *argv[])
  395. {
  396. if (argc <= 0) {
  397. return 0;
  398. }
  399. options.help = 0;
  400. options.version = 0;
  401. options.logger = LOGGER_STDERR;
  402. options.logger_syslog_facility = "daemon";
  403. options.logger_syslog_ident = argv[0];
  404. options.loglevel = -1;
  405. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  406. options.loglevels[i] = -1;
  407. }
  408. options.config_file = NULL;
  409. options.retry_time = DEFAULT_RETRY_TIME;
  410. options.no_udev = 0;
  411. options.extra_args = NULL;
  412. options.num_extra_args = 0;
  413. for (int i = 1; i < argc; i++) {
  414. char *arg = argv[i];
  415. if (!strcmp(arg, "--help")) {
  416. options.help = 1;
  417. }
  418. else if (!strcmp(arg, "--version")) {
  419. options.version = 1;
  420. }
  421. else if (!strcmp(arg, "--logger")) {
  422. if (1 >= argc - i) {
  423. fprintf(stderr, "%s: requires an argument\n", arg);
  424. return 0;
  425. }
  426. char *arg2 = argv[i + 1];
  427. if (!strcmp(arg2, "stdout")) {
  428. options.logger = LOGGER_STDOUT;
  429. }
  430. else if (!strcmp(arg2, "stderr")) {
  431. options.logger = LOGGER_STDERR;
  432. }
  433. else if (!strcmp(arg2, "syslog")) {
  434. options.logger = LOGGER_SYSLOG;
  435. }
  436. else {
  437. fprintf(stderr, "%s: wrong argument\n", arg);
  438. return 0;
  439. }
  440. i++;
  441. }
  442. else if (!strcmp(arg, "--syslog-facility")) {
  443. if (1 >= argc - i) {
  444. fprintf(stderr, "%s: requires an argument\n", arg);
  445. return 0;
  446. }
  447. options.logger_syslog_facility = argv[i + 1];
  448. i++;
  449. }
  450. else if (!strcmp(arg, "--syslog-ident")) {
  451. if (1 >= argc - i) {
  452. fprintf(stderr, "%s: requires an argument\n", arg);
  453. return 0;
  454. }
  455. options.logger_syslog_ident = argv[i + 1];
  456. i++;
  457. }
  458. else if (!strcmp(arg, "--loglevel")) {
  459. if (1 >= argc - i) {
  460. fprintf(stderr, "%s: requires an argument\n", arg);
  461. return 0;
  462. }
  463. if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
  464. fprintf(stderr, "%s: wrong argument\n", arg);
  465. return 0;
  466. }
  467. i++;
  468. }
  469. else if (!strcmp(arg, "--channel-loglevel")) {
  470. if (2 >= argc - i) {
  471. fprintf(stderr, "%s: requires two arguments\n", arg);
  472. return 0;
  473. }
  474. int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
  475. if (channel < 0) {
  476. fprintf(stderr, "%s: wrong channel argument\n", arg);
  477. return 0;
  478. }
  479. int loglevel = parse_loglevel(argv[i + 2]);
  480. if (loglevel < 0) {
  481. fprintf(stderr, "%s: wrong loglevel argument\n", arg);
  482. return 0;
  483. }
  484. options.loglevels[channel] = loglevel;
  485. i += 2;
  486. }
  487. else if (!strcmp(arg, "--config-file")) {
  488. if (1 >= argc - i) {
  489. fprintf(stderr, "%s: requires an argument\n", arg);
  490. return 0;
  491. }
  492. options.config_file = argv[i + 1];
  493. i++;
  494. }
  495. else if (!strcmp(arg, "--retry-time")) {
  496. if (1 >= argc - i) {
  497. fprintf(stderr, "%s: requires an argument\n", arg);
  498. return 0;
  499. }
  500. if ((options.retry_time = atoi(argv[i + 1])) < 0) {
  501. fprintf(stderr, "%s: wrong argument\n", arg);
  502. return 0;
  503. }
  504. i++;
  505. }
  506. else if (!strcmp(arg, "--no-udev")) {
  507. options.no_udev = 1;
  508. }
  509. else if (!strcmp(arg, "--")) {
  510. options.extra_args = &argv[i + 1];
  511. options.num_extra_args = argc - i - 1;
  512. i += options.num_extra_args;
  513. }
  514. else {
  515. fprintf(stderr, "unknown option: %s\n", arg);
  516. return 0;
  517. }
  518. }
  519. if (options.help || options.version) {
  520. return 1;
  521. }
  522. if (!options.config_file) {
  523. fprintf(stderr, "--config-file is required\n");
  524. return 0;
  525. }
  526. return 1;
  527. }
  528. void signal_handler (void *unused)
  529. {
  530. BLog(BLOG_NOTICE, "termination requested");
  531. start_terminate(1);
  532. }
  533. void start_terminate (int exit_code)
  534. {
  535. main_exit_code = exit_code;
  536. if (terminating) {
  537. return;
  538. }
  539. terminating = 1;
  540. if (LinkedList1_IsEmpty(&processes)) {
  541. BReactor_Quit(&reactor, 0);
  542. return;
  543. }
  544. // start terminating non-template processes
  545. for (LinkedList1Node *ln = LinkedList1_GetFirst(&processes); ln; ln = LinkedList1Node_Next(ln)) {
  546. struct process *p = UPPER_OBJECT(ln, struct process, list_node);
  547. if (p->module_process) {
  548. continue;
  549. }
  550. if (p->state != PSTATE_TERMINATING) {
  551. process_start_terminating(p);
  552. }
  553. }
  554. }
  555. static int process_new (NCDProcess *proc_ast, NCDInterpBlock *iblock, NCDModuleProcess *module_process)
  556. {
  557. // get block
  558. NCDBlock *block = NCDProcess_Block(proc_ast);
  559. // get num statements
  560. int num_statements = NCDBlock_NumStatements(block);
  561. // calculate allocation size
  562. bsize_t alloc_size = bsize_add(bsize_fromsize(sizeof(struct process)), bsize_mul(bsize_fromsize(num_statements), bsize_fromsize(sizeof(struct statement))));
  563. // allocate strucure
  564. struct process *p = BAllocSize(alloc_size);
  565. if (!p) {
  566. BLog(BLOG_ERROR, "BAllocSize failed");
  567. goto fail0;
  568. }
  569. // set some stuff
  570. p->proc_ast = proc_ast;
  571. p->iblock = iblock;
  572. p->module_process = module_process;
  573. p->num_statements = num_statements;
  574. // set module process handlers
  575. if (p->module_process) {
  576. NCDModuleProcess_Interp_SetHandlers(p->module_process, p,
  577. (NCDModuleProcess_interp_func_event)process_moduleprocess_func_event,
  578. (NCDModuleProcess_interp_func_getobj)process_moduleprocess_func_getobj);
  579. }
  580. // preallocate statement memory
  581. if ((p->mem_size = NCDInterpBlock_PreallocSize(iblock)) < 0) {
  582. BLog(BLOG_ERROR, "NCDInterpBlock_PreallocSize");
  583. goto fail1;
  584. }
  585. if (!(p->mem = BAllocSize(bsize_fromint(p->mem_size)))) {
  586. BLog(BLOG_ERROR, "BAllocSize failed");
  587. goto fail1;
  588. }
  589. // init statements
  590. for (int i = 0; i < num_statements; i++) {
  591. struct statement *ps = &p->statements[i];
  592. ps->p = p;
  593. ps->i = i;
  594. ps->state = SSTATE_FORGOTTEN;
  595. ps->mem_size = NCDInterpBlock_StatementPreallocSize(iblock, i);
  596. ps->mem = (ps->mem_size == 0 ? NULL : p->mem + NCDInterpBlock_StatementPreallocOffset(iblock, i));
  597. }
  598. // set state working
  599. p->state = PSTATE_WORKING;
  600. // set AP=0
  601. p->ap = 0;
  602. // set FP=0
  603. p->fp = 0;
  604. // set no error
  605. p->have_error = 0;
  606. // init timer
  607. BTimer_Init(&p->wait_timer, options.retry_time, (BTimer_handler)process_wait_timer_handler, p);
  608. // init work job
  609. BPending_Init(&p->work_job, BReactor_PendingGroup(&reactor), (BPending_handler)process_work_job_handler, p);
  610. // insert to processes list
  611. LinkedList1_Append(&processes, &p->list_node);
  612. // schedule work
  613. BPending_Set(&p->work_job);
  614. return 1;
  615. fail1:
  616. BFree(p);
  617. fail0:
  618. BLog(BLOG_ERROR, "failed to initialize process %s", NCDProcess_Name(proc_ast));
  619. return 0;
  620. }
  621. void process_free (struct process *p)
  622. {
  623. ASSERT(p->ap == 0)
  624. ASSERT(p->fp == 0)
  625. // inform module process that the process is terminated
  626. if (p->module_process) {
  627. NCDModuleProcess_Interp_Terminated(p->module_process);
  628. }
  629. // free statement memory
  630. for (int i = 0; i < p->num_statements; i++) {
  631. struct statement *ps = &p->statements[i];
  632. if (ps->mem && !process_mem_is_preallocated(p, ps->mem)) {
  633. free(ps->mem);
  634. }
  635. }
  636. // remove from processes list
  637. LinkedList1_Remove(&processes, &p->list_node);
  638. // free work job
  639. BPending_Free(&p->work_job);
  640. // free timer
  641. BReactor_RemoveTimer(&reactor, &p->wait_timer);
  642. // free preallocated memory
  643. BFree(p->mem);
  644. // free strucure
  645. BFree(p);
  646. }
  647. int process_mem_is_preallocated (struct process *p, char *mem)
  648. {
  649. ASSERT(mem)
  650. return (mem >= p->mem && mem < p->mem + p->mem_size);
  651. }
  652. void process_start_terminating (struct process *p)
  653. {
  654. ASSERT(p->state != PSTATE_TERMINATING)
  655. // set terminating
  656. p->state = PSTATE_TERMINATING;
  657. // schedule work
  658. process_schedule_work(p);
  659. }
  660. int process_rap (struct process *p)
  661. {
  662. if (p->ap > 0 && p->statements[p->ap - 1].state == SSTATE_CHILD) {
  663. return (p->ap - 1);
  664. } else {
  665. return p->ap;
  666. }
  667. }
  668. void process_assert_pointers (struct process *p)
  669. {
  670. ASSERT(p->ap <= p->num_statements)
  671. ASSERT(p->fp >= p->ap)
  672. ASSERT(p->fp <= p->num_statements)
  673. #ifndef NDEBUG
  674. // check AP
  675. for (int i = 0; i < p->ap; i++) {
  676. if (i == p->ap - 1) {
  677. ASSERT(p->statements[i].state == SSTATE_ADULT || p->statements[i].state == SSTATE_CHILD)
  678. } else {
  679. ASSERT(p->statements[i].state == SSTATE_ADULT)
  680. }
  681. }
  682. // check FP
  683. int fp = p->num_statements;
  684. while (fp > 0 && p->statements[fp - 1].state == SSTATE_FORGOTTEN) {
  685. fp--;
  686. }
  687. ASSERT(p->fp == fp)
  688. #endif
  689. }
  690. void process_logfunc (struct process *p)
  691. {
  692. BLog_Append("process %s: ", NCDProcess_Name(p->proc_ast));
  693. }
  694. void process_log (struct process *p, int level, const char *fmt, ...)
  695. {
  696. va_list vl;
  697. va_start(vl, fmt);
  698. BLog_LogViaFuncVarArg((BLog_logfunc)process_logfunc, p, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  699. va_end(vl);
  700. }
  701. void process_schedule_work (struct process *p)
  702. {
  703. process_assert_pointers(p);
  704. // stop timer
  705. BReactor_RemoveTimer(&reactor, &p->wait_timer);
  706. // schedule work
  707. BPending_Set(&p->work_job);
  708. }
  709. void process_work_job_handler (struct process *p)
  710. {
  711. process_assert_pointers(p);
  712. ASSERT(!BTimer_IsRunning(&p->wait_timer))
  713. if (p->state == PSTATE_WAITING) {
  714. return;
  715. }
  716. if (p->state == PSTATE_TERMINATING) {
  717. if (p->fp == 0) {
  718. // finished retreating
  719. process_free(p);
  720. // if program is terminating amd there are no more processes, exit program
  721. if (terminating && LinkedList1_IsEmpty(&processes)) {
  722. BReactor_Quit(&reactor, 0);
  723. }
  724. return;
  725. }
  726. // order the last living statement to die, if needed
  727. struct statement *ps = &p->statements[p->fp - 1];
  728. ASSERT(ps->state != SSTATE_FORGOTTEN)
  729. if (ps->state != SSTATE_DYING) {
  730. statement_log(ps, BLOG_INFO, "killing");
  731. // order it to die
  732. NCDModuleInst_Die(&ps->inst);
  733. // set statement state DYING
  734. ps->state = SSTATE_DYING;
  735. // update AP
  736. if (p->ap > ps->i) {
  737. p->ap = ps->i;
  738. }
  739. }
  740. return;
  741. }
  742. // process was up but is no longer?
  743. if (p->state == PSTATE_UP && !(p->ap == process_rap(p) && p->ap == p->num_statements)) {
  744. // if we have module process, wait for its permission to continue
  745. if (p->module_process) {
  746. // set module process down
  747. NCDModuleProcess_Interp_Down(p->module_process);
  748. // set state waiting
  749. p->state = PSTATE_WAITING;
  750. return;
  751. }
  752. // set state working
  753. p->state = PSTATE_WORKING;
  754. }
  755. // cleaning up?
  756. if (p->ap < p->fp) {
  757. // order the last living statement to die, if needed
  758. struct statement *ps = &p->statements[p->fp - 1];
  759. if (ps->state != SSTATE_DYING) {
  760. statement_log(ps, BLOG_INFO, "killing");
  761. // order it to die
  762. NCDModuleInst_Die(&ps->inst);
  763. // set statement state DYING
  764. ps->state = SSTATE_DYING;
  765. }
  766. return;
  767. }
  768. // clean?
  769. if (p->ap > process_rap(p)) {
  770. ASSERT(p->ap > 0)
  771. ASSERT(p->ap <= p->num_statements)
  772. struct statement *ps = &p->statements[p->ap - 1];
  773. ASSERT(ps->state == SSTATE_CHILD)
  774. statement_log(ps, BLOG_INFO, "clean");
  775. // report clean
  776. NCDModuleInst_Clean(&ps->inst);
  777. return;
  778. }
  779. // advancing?
  780. if (p->ap < p->num_statements) {
  781. ASSERT(p->state == PSTATE_WORKING)
  782. struct statement *ps = &p->statements[p->ap];
  783. ASSERT(ps->state == SSTATE_FORGOTTEN)
  784. if (p->have_error) {
  785. statement_log(ps, BLOG_INFO, "waiting after error");
  786. // clear error
  787. p->have_error = 0;
  788. // set wait timer
  789. BReactor_SetTimer(&reactor, &p->wait_timer);
  790. } else {
  791. // advance
  792. process_advance(p);
  793. }
  794. return;
  795. }
  796. // have we just finished?
  797. if (p->state == PSTATE_WORKING) {
  798. process_log(p, BLOG_INFO, "victory");
  799. // set module process up
  800. if (p->module_process) {
  801. NCDModuleProcess_Interp_Up(p->module_process);
  802. }
  803. // set state up
  804. p->state = PSTATE_UP;
  805. }
  806. }
  807. void process_advance (struct process *p)
  808. {
  809. process_assert_pointers(p);
  810. ASSERT(p->ap == p->fp)
  811. ASSERT(p->ap == process_rap(p))
  812. ASSERT(p->ap < p->num_statements)
  813. ASSERT(!p->have_error)
  814. ASSERT(!BPending_IsSet(&p->work_job))
  815. ASSERT(!BTimer_IsRunning(&p->wait_timer))
  816. ASSERT(p->state == PSTATE_WORKING)
  817. struct statement *ps = &p->statements[p->ap];
  818. ASSERT(ps->state == SSTATE_FORGOTTEN)
  819. statement_log(ps, BLOG_INFO, "initializing");
  820. NCDObject object;
  821. NCDObject *object_ptr = NULL;
  822. char **object_names = NCDInterpBlock_StatementObjNames(p->iblock, p->ap);
  823. const char *type = NCDInterpBlock_StatementCmdName(p->iblock, p->ap);
  824. // if this is a method-like statement, type is really "base_type(object)::method_name"
  825. if (object_names) {
  826. // get object
  827. if (!process_resolve_object_expr(p, p->ap, object_names, &object)) {
  828. goto fail0;
  829. }
  830. object_ptr = &object;
  831. // get object type
  832. const char *object_type = NCDObject_Type(&object);
  833. if (!object_type) {
  834. statement_log(ps, BLOG_ERROR, "cannot call method on object with no type");
  835. goto fail0;
  836. }
  837. // build type string
  838. int res = snprintf(method_concat_buf, sizeof(method_concat_buf), "%s::%s", object_type, type);
  839. if (res >= sizeof(method_concat_buf) || res < 0) {
  840. statement_log(ps, BLOG_ERROR, "type/method name too long");
  841. goto fail0;
  842. }
  843. type = method_concat_buf;
  844. }
  845. // find module to instantiate
  846. const struct NCDModule *module = NCDModuleIndex_FindModule(&mindex, type);
  847. if (!module) {
  848. statement_log(ps, BLOG_ERROR, "failed to find module: %s", type);
  849. goto fail0;
  850. }
  851. // register alloc size for future preallocations
  852. NCDInterpBlock_StatementBumpAllocSize(p->iblock, p->ap, module->alloc_size);
  853. // init args mem
  854. NCDValMem_Init(&ps->args_mem);
  855. // resolve arguments
  856. NCDValRef args;
  857. NCDInterpValue *iargs = NCDInterpBlock_StatementInterpValue(p->iblock, ps->i);
  858. if (!statement_resolve_argument(ps, iargs, &ps->args_mem, &args)) {
  859. statement_log(ps, BLOG_ERROR, "failed to resolve arguments");
  860. goto fail1;
  861. }
  862. // allocate memory
  863. void *mem;
  864. if (!statement_allocate_memory(ps, module->alloc_size, &mem)) {
  865. statement_log(ps, BLOG_ERROR, "failed to allocate memory");
  866. goto fail1;
  867. }
  868. // initialize module instance
  869. NCDModuleInst_Init(&ps->inst, module, mem, object_ptr, args, ps, &module_params, &module_iparams);
  870. // set statement state CHILD
  871. ps->state = SSTATE_CHILD;
  872. // increment AP
  873. p->ap++;
  874. // increment FP
  875. p->fp++;
  876. process_assert_pointers(p);
  877. return;
  878. fail1:
  879. NCDValMem_Free(&ps->args_mem);
  880. fail0:
  881. // set error
  882. p->have_error = 1;
  883. // schedule work to start the timer
  884. process_schedule_work(p);
  885. }
  886. void process_wait_timer_handler (struct process *p)
  887. {
  888. process_assert_pointers(p);
  889. ASSERT(p->ap == p->fp)
  890. ASSERT(p->ap == process_rap(p))
  891. ASSERT(p->ap < p->num_statements)
  892. ASSERT(!p->have_error)
  893. ASSERT(!BPending_IsSet(&p->work_job))
  894. ASSERT(p->state == PSTATE_WORKING)
  895. process_log(p, BLOG_INFO, "retrying");
  896. // advance
  897. process_advance(p);
  898. }
  899. int process_find_object (struct process *p, int pos, const char *name, NCDObject *out_object)
  900. {
  901. ASSERT(pos >= 0)
  902. ASSERT(pos <= p->num_statements)
  903. ASSERT(name)
  904. ASSERT(out_object)
  905. int i = NCDInterpBlock_FindStatement(p->iblock, pos, name);
  906. if (i >= 0) {
  907. struct statement *ps = &p->statements[i];
  908. ASSERT(i < p->num_statements)
  909. if (ps->state == SSTATE_FORGOTTEN) {
  910. process_log(p, BLOG_ERROR, "statement (%d) is uninitialized", i);
  911. return 0;
  912. }
  913. *out_object = NCDModuleInst_Object(&ps->inst);
  914. return 1;
  915. }
  916. if (p->module_process && NCDModuleProcess_Interp_GetSpecialObj(p->module_process, name, out_object)) {
  917. return 1;
  918. }
  919. return 0;
  920. }
  921. int process_resolve_object_expr (struct process *p, int pos, char **names, NCDObject *out_object)
  922. {
  923. ASSERT(pos >= 0)
  924. ASSERT(pos <= p->num_statements)
  925. ASSERT(names)
  926. ASSERT(count_strings(names) > 0)
  927. ASSERT(out_object)
  928. NCDObject object;
  929. if (!process_find_object(p, pos, names[0], &object)) {
  930. goto fail;
  931. }
  932. if (!NCDObject_ResolveObjExpr(&object, names + 1, out_object)) {
  933. goto fail;
  934. }
  935. return 1;
  936. fail:;
  937. char *name = implode_strings(names, '.');
  938. process_log(p, BLOG_ERROR, "failed to resolve object (%s) from position %zu", (name ? name : ""), pos);
  939. free(name);
  940. return 0;
  941. }
  942. int process_resolve_variable_expr (struct process *p, int pos, char **names, NCDValMem *mem, NCDValRef *out_value)
  943. {
  944. ASSERT(pos >= 0)
  945. ASSERT(pos <= p->num_statements)
  946. ASSERT(names)
  947. ASSERT(count_strings(names) > 0)
  948. ASSERT(mem)
  949. ASSERT(out_value)
  950. NCDObject object;
  951. if (!process_find_object(p, pos, names[0], &object)) {
  952. goto fail;
  953. }
  954. if (!NCDObject_ResolveVarExpr(&object, names + 1, mem, out_value)) {
  955. goto fail;
  956. }
  957. return 1;
  958. fail:;
  959. char *name = implode_strings(names, '.');
  960. process_log(p, BLOG_ERROR, "failed to resolve variable (%s) from position %zu", (name ? name : ""), pos);
  961. free(name);
  962. return 0;
  963. }
  964. void statement_logfunc (struct statement *ps)
  965. {
  966. process_logfunc(ps->p);
  967. BLog_Append("statement %zu: ", ps->i);
  968. }
  969. void statement_log (struct statement *ps, int level, const char *fmt, ...)
  970. {
  971. if (!BLog_WouldLog(BLOG_CURRENT_CHANNEL, level)) {
  972. return;
  973. }
  974. va_list vl;
  975. va_start(vl, fmt);
  976. BLog_LogViaFuncVarArg((BLog_logfunc)statement_logfunc, ps, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  977. va_end(vl);
  978. }
  979. int statement_allocate_memory (struct statement *ps, int alloc_size, void **out_mem)
  980. {
  981. ASSERT(alloc_size >= 0)
  982. ASSERT(out_mem)
  983. if (alloc_size == 0) {
  984. *out_mem = NULL;
  985. return 1;
  986. }
  987. if (alloc_size > ps->mem_size) {
  988. if (ps->mem && !process_mem_is_preallocated(ps->p, ps->mem)) {
  989. free(ps->mem);
  990. }
  991. if (!(ps->mem = malloc(alloc_size))) {
  992. statement_log(ps, BLOG_ERROR, "malloc failed");
  993. ps->mem_size = 0;
  994. return 0;
  995. }
  996. ps->mem_size = alloc_size;
  997. }
  998. *out_mem = ps->mem;
  999. return 1;
  1000. }
  1001. int statement_resolve_argument (struct statement *ps, NCDInterpValue *arg, NCDValMem *mem, NCDValRef *out)
  1002. {
  1003. ASSERT(ps->i <= process_rap(ps->p))
  1004. ASSERT(arg)
  1005. ASSERT(mem)
  1006. ASSERT(out)
  1007. switch (arg->type) {
  1008. case NCDVALUE_STRING: {
  1009. *out = NCDVal_NewStringBin(mem, (uint8_t *)arg->string, arg->string_len);
  1010. if (NCDVal_IsInvalid(*out)) {
  1011. statement_log(ps, BLOG_ERROR, "NCDVal_NewStringBin failed");
  1012. return 0;
  1013. }
  1014. } break;
  1015. case NCDVALUE_VAR: {
  1016. if (!process_resolve_variable_expr(ps->p, ps->i, arg->variable_names, mem, out)) {
  1017. return 0;
  1018. }
  1019. if (NCDVal_IsInvalid(*out)) {
  1020. return 0;
  1021. }
  1022. } break;
  1023. case NCDVALUE_LIST: {
  1024. *out = NCDVal_NewList(mem, arg->list_count);
  1025. if (NCDVal_IsInvalid(*out)) {
  1026. statement_log(ps, BLOG_ERROR, "NCDVal_NewList failed");
  1027. return 0;
  1028. }
  1029. for (LinkedList1Node *n = LinkedList1_GetFirst(&arg->list); n; n = LinkedList1Node_Next(n)) {
  1030. struct NCDInterpValueListElem *elem = UPPER_OBJECT(n, struct NCDInterpValueListElem, list_node);
  1031. NCDValRef new_elem;
  1032. if (!statement_resolve_argument(ps, &elem->value, mem, &new_elem)) {
  1033. return 0;
  1034. }
  1035. NCDVal_ListAppend(*out, new_elem);
  1036. }
  1037. } break;
  1038. case NCDVALUE_MAP: {
  1039. *out = NCDVal_NewMap(mem, arg->map_count);
  1040. if (NCDVal_IsInvalid(*out)) {
  1041. statement_log(ps, BLOG_ERROR, "NCDVal_NewMap failed");
  1042. return 0;
  1043. }
  1044. for (LinkedList1Node *n = LinkedList1_GetFirst(&arg->maplist); n; n = LinkedList1Node_Next(n)) {
  1045. struct NCDInterpValueMapElem *elem = UPPER_OBJECT(n, struct NCDInterpValueMapElem, maplist_node);
  1046. NCDValRef new_key;
  1047. if (!statement_resolve_argument(ps, &elem->key, mem, &new_key)) {
  1048. return 0;
  1049. }
  1050. NCDValRef new_val;
  1051. if (!statement_resolve_argument(ps, &elem->val, mem, &new_val)) {
  1052. return 0;
  1053. }
  1054. int res = NCDVal_MapInsert(*out, new_key, new_val);
  1055. if (!res) {
  1056. statement_log(ps, BLOG_ERROR, "duplicate map keys");
  1057. return 0;
  1058. }
  1059. }
  1060. } break;
  1061. default: ASSERT(0);
  1062. }
  1063. return 1;
  1064. }
  1065. void statement_instance_func_event (struct statement *ps, int event)
  1066. {
  1067. ASSERT(ps->state == SSTATE_CHILD || ps->state == SSTATE_ADULT || ps->state == SSTATE_DYING)
  1068. struct process *p = ps->p;
  1069. process_assert_pointers(p);
  1070. // schedule work
  1071. process_schedule_work(p);
  1072. switch (event) {
  1073. case NCDMODULE_EVENT_UP: {
  1074. ASSERT(ps->state == SSTATE_CHILD)
  1075. statement_log(ps, BLOG_INFO, "up");
  1076. // set state ADULT
  1077. ps->state = SSTATE_ADULT;
  1078. } break;
  1079. case NCDMODULE_EVENT_DOWN: {
  1080. ASSERT(ps->state == SSTATE_ADULT)
  1081. statement_log(ps, BLOG_INFO, "down");
  1082. // set state CHILD
  1083. ps->state = SSTATE_CHILD;
  1084. // clear error
  1085. if (ps->i < p->ap) {
  1086. p->have_error = 0;
  1087. }
  1088. // update AP
  1089. if (p->ap > ps->i + 1) {
  1090. p->ap = ps->i + 1;
  1091. }
  1092. } break;
  1093. case NCDMODULE_EVENT_DEAD: {
  1094. int is_error = NCDModuleInst_HaveError(&ps->inst);
  1095. if (is_error) {
  1096. statement_log(ps, BLOG_ERROR, "died with error");
  1097. } else {
  1098. statement_log(ps, BLOG_INFO, "died");
  1099. }
  1100. // free instance
  1101. NCDModuleInst_Free(&ps->inst);
  1102. // free arguments memory
  1103. NCDValMem_Free(&ps->args_mem);
  1104. // set state FORGOTTEN
  1105. ps->state = SSTATE_FORGOTTEN;
  1106. // set error
  1107. if (is_error && ps->i < p->ap) {
  1108. p->have_error = 1;
  1109. }
  1110. // update AP
  1111. if (p->ap > ps->i) {
  1112. p->ap = ps->i;
  1113. }
  1114. // update FP
  1115. while (p->fp > 0 && p->statements[p->fp - 1].state == SSTATE_FORGOTTEN) {
  1116. p->fp--;
  1117. }
  1118. } break;
  1119. }
  1120. }
  1121. int statement_instance_func_getobj (struct statement *ps, const char *objname, NCDObject *out_object)
  1122. {
  1123. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1124. return process_find_object(ps->p, ps->i, objname, out_object);
  1125. }
  1126. int statement_instance_func_initprocess (struct statement *ps, NCDModuleProcess *mp, const char *template_name)
  1127. {
  1128. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1129. // find template
  1130. NCDProcess *p_ast;
  1131. NCDInterpBlock *iblock;
  1132. if (!NCDInterpProg_FindProcess(&iprogram, template_name, &p_ast, &iblock) || !NCDProcess_IsTemplate(p_ast)) {
  1133. statement_log(ps, BLOG_ERROR, "no template named %s", template_name);
  1134. return 0;
  1135. }
  1136. // create process
  1137. if (!process_new(p_ast, iblock, mp)) {
  1138. statement_log(ps, BLOG_ERROR, "failed to create process from template %s", template_name);
  1139. return 0;
  1140. }
  1141. statement_log(ps, BLOG_INFO, "created process from template %s", template_name);
  1142. return 1;
  1143. }
  1144. void statement_instance_logfunc (struct statement *ps)
  1145. {
  1146. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1147. statement_logfunc(ps);
  1148. BLog_Append("module: ");
  1149. }
  1150. void statement_instance_func_interp_exit (struct statement *ps, int exit_code)
  1151. {
  1152. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1153. start_terminate(exit_code);
  1154. }
  1155. int statement_instance_func_interp_getargs (struct statement *ps, NCDValMem *mem, NCDValRef *out_value)
  1156. {
  1157. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1158. *out_value = NCDVal_NewList(mem, options.num_extra_args);
  1159. if (NCDVal_IsInvalid(*out_value)) {
  1160. statement_log(ps, BLOG_ERROR, "NCDVal_NewList failed");
  1161. goto fail;
  1162. }
  1163. for (int i = 0; i < options.num_extra_args; i++) {
  1164. NCDValRef arg = NCDVal_NewString(mem, options.extra_args[i]);
  1165. if (NCDVal_IsInvalid(arg)) {
  1166. statement_log(ps, BLOG_ERROR, "NCDVal_NewString failed");
  1167. goto fail;
  1168. }
  1169. NCDVal_ListAppend(*out_value, arg);
  1170. }
  1171. return 1;
  1172. fail:
  1173. *out_value = NCDVal_NewInvalid();
  1174. return 1;
  1175. }
  1176. btime_t statement_instance_func_interp_getretrytime (struct statement *ps)
  1177. {
  1178. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1179. return options.retry_time;
  1180. }
  1181. void process_moduleprocess_func_event (struct process *p, int event)
  1182. {
  1183. ASSERT(p->module_process)
  1184. switch (event) {
  1185. case NCDMODULEPROCESS_INTERP_EVENT_CONTINUE: {
  1186. ASSERT(p->state == PSTATE_WAITING)
  1187. // set state working
  1188. p->state = PSTATE_WORKING;
  1189. // schedule work
  1190. process_schedule_work(p);
  1191. } break;
  1192. case NCDMODULEPROCESS_INTERP_EVENT_TERMINATE: {
  1193. ASSERT(p->state != PSTATE_TERMINATING)
  1194. process_log(p, BLOG_INFO, "process termination requested");
  1195. // start terminating
  1196. process_start_terminating(p);
  1197. } break;
  1198. default: ASSERT(0);
  1199. }
  1200. }
  1201. int process_moduleprocess_func_getobj (struct process *p, const char *name, NCDObject *out_object)
  1202. {
  1203. ASSERT(p->module_process)
  1204. return process_find_object(p, p->num_statements, name, out_object);
  1205. }