ncd.c 42 KB

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