ncd.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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. // set statement state DYING
  732. ps->state = SSTATE_DYING;
  733. // update AP
  734. if (p->ap > ps->i) {
  735. p->ap = ps->i;
  736. }
  737. // order it to die
  738. NCDModuleInst_Die(&ps->inst);
  739. return;
  740. }
  741. return;
  742. }
  743. // process was up but is no longer?
  744. if (p->state == PSTATE_UP && !(p->ap == process_rap(p) && p->ap == p->num_statements)) {
  745. // if we have module process, wait for its permission to continue
  746. if (p->module_process) {
  747. // set module process down
  748. NCDModuleProcess_Interp_Down(p->module_process);
  749. // set state waiting
  750. p->state = PSTATE_WAITING;
  751. return;
  752. }
  753. // set state working
  754. p->state = PSTATE_WORKING;
  755. }
  756. // cleaning up?
  757. if (p->ap < p->fp) {
  758. // order the last living statement to die, if needed
  759. struct statement *ps = &p->statements[p->fp - 1];
  760. if (ps->state != SSTATE_DYING) {
  761. statement_log(ps, BLOG_INFO, "killing");
  762. // set statement state DYING
  763. ps->state = SSTATE_DYING;
  764. // order it to die
  765. NCDModuleInst_Die(&ps->inst);
  766. return;
  767. }
  768. return;
  769. }
  770. // clean?
  771. if (p->ap > process_rap(p)) {
  772. ASSERT(p->ap > 0)
  773. ASSERT(p->ap <= p->num_statements)
  774. struct statement *ps = &p->statements[p->ap - 1];
  775. ASSERT(ps->state == SSTATE_CHILD)
  776. statement_log(ps, BLOG_INFO, "clean");
  777. // report clean
  778. NCDModuleInst_Clean(&ps->inst);
  779. return;
  780. }
  781. // advancing?
  782. if (p->ap < p->num_statements) {
  783. ASSERT(p->state == PSTATE_WORKING)
  784. struct statement *ps = &p->statements[p->ap];
  785. ASSERT(ps->state == SSTATE_FORGOTTEN)
  786. if (p->have_error) {
  787. statement_log(ps, BLOG_INFO, "waiting after error");
  788. // clear error
  789. p->have_error = 0;
  790. // set wait timer
  791. BReactor_SetTimer(&reactor, &p->wait_timer);
  792. } else {
  793. // advance
  794. process_advance(p);
  795. }
  796. return;
  797. }
  798. // have we just finished?
  799. if (p->state == PSTATE_WORKING) {
  800. process_log(p, BLOG_INFO, "victory");
  801. // set module process up
  802. if (p->module_process) {
  803. NCDModuleProcess_Interp_Up(p->module_process);
  804. }
  805. // set state up
  806. p->state = PSTATE_UP;
  807. }
  808. }
  809. void process_advance (struct process *p)
  810. {
  811. process_assert_pointers(p);
  812. ASSERT(p->ap == p->fp)
  813. ASSERT(p->ap == process_rap(p))
  814. ASSERT(p->ap < p->num_statements)
  815. ASSERT(!p->have_error)
  816. ASSERT(!BPending_IsSet(&p->work_job))
  817. ASSERT(!BTimer_IsRunning(&p->wait_timer))
  818. ASSERT(p->state == PSTATE_WORKING)
  819. struct statement *ps = &p->statements[p->ap];
  820. ASSERT(ps->state == SSTATE_FORGOTTEN)
  821. statement_log(ps, BLOG_INFO, "initializing");
  822. NCDObject object;
  823. NCDObject *object_ptr = NULL;
  824. char **object_names = NCDInterpBlock_StatementObjNames(p->iblock, p->ap);
  825. const char *type = NCDInterpBlock_StatementCmdName(p->iblock, p->ap);
  826. // if this is a method-like statement, type is really "base_type(object)::method_name"
  827. if (object_names) {
  828. // get object
  829. if (!process_resolve_object_expr(p, p->ap, object_names, &object)) {
  830. goto fail0;
  831. }
  832. object_ptr = &object;
  833. // get object type
  834. const char *object_type = NCDObject_Type(&object);
  835. if (!object_type) {
  836. statement_log(ps, BLOG_ERROR, "cannot call method on object with no type");
  837. goto fail0;
  838. }
  839. // build type string
  840. int res = snprintf(method_concat_buf, sizeof(method_concat_buf), "%s::%s", object_type, type);
  841. if (res >= sizeof(method_concat_buf) || res < 0) {
  842. statement_log(ps, BLOG_ERROR, "type/method name too long");
  843. goto fail0;
  844. }
  845. type = method_concat_buf;
  846. }
  847. // find module to instantiate
  848. const struct NCDModule *module = NCDModuleIndex_FindModule(&mindex, type);
  849. if (!module) {
  850. statement_log(ps, BLOG_ERROR, "failed to find module: %s", type);
  851. goto fail0;
  852. }
  853. // register alloc size for future preallocations
  854. NCDInterpBlock_StatementBumpAllocSize(p->iblock, p->ap, module->alloc_size);
  855. // init args mem
  856. NCDValMem_Init(&ps->args_mem);
  857. // resolve arguments
  858. NCDValRef args;
  859. NCDInterpValue *iargs = NCDInterpBlock_StatementInterpValue(p->iblock, ps->i);
  860. if (!statement_resolve_argument(ps, iargs, &ps->args_mem, &args)) {
  861. statement_log(ps, BLOG_ERROR, "failed to resolve arguments");
  862. goto fail1;
  863. }
  864. // allocate memory
  865. void *mem;
  866. if (!statement_allocate_memory(ps, module->alloc_size, &mem)) {
  867. statement_log(ps, BLOG_ERROR, "failed to allocate memory");
  868. goto fail1;
  869. }
  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. // initialize module instance
  878. NCDModuleInst_Init(&ps->inst, module, mem, object_ptr, args, ps, &module_params, &module_iparams);
  879. return;
  880. fail1:
  881. NCDValMem_Free(&ps->args_mem);
  882. fail0:
  883. // set error
  884. p->have_error = 1;
  885. // schedule work to start the timer
  886. process_schedule_work(p);
  887. }
  888. void process_wait_timer_handler (struct process *p)
  889. {
  890. process_assert_pointers(p);
  891. ASSERT(p->ap == p->fp)
  892. ASSERT(p->ap == process_rap(p))
  893. ASSERT(p->ap < p->num_statements)
  894. ASSERT(!p->have_error)
  895. ASSERT(!BPending_IsSet(&p->work_job))
  896. ASSERT(p->state == PSTATE_WORKING)
  897. process_log(p, BLOG_INFO, "retrying");
  898. // advance
  899. process_advance(p);
  900. }
  901. int process_find_object (struct process *p, int pos, const char *name, NCDObject *out_object)
  902. {
  903. ASSERT(pos >= 0)
  904. ASSERT(pos <= p->num_statements)
  905. ASSERT(name)
  906. ASSERT(out_object)
  907. int i = NCDInterpBlock_FindStatement(p->iblock, pos, name);
  908. if (i >= 0) {
  909. struct statement *ps = &p->statements[i];
  910. ASSERT(i < p->num_statements)
  911. if (ps->state == SSTATE_FORGOTTEN) {
  912. process_log(p, BLOG_ERROR, "statement (%d) is uninitialized", i);
  913. return 0;
  914. }
  915. *out_object = NCDModuleInst_Object(&ps->inst);
  916. return 1;
  917. }
  918. if (p->module_process && NCDModuleProcess_Interp_GetSpecialObj(p->module_process, name, out_object)) {
  919. return 1;
  920. }
  921. return 0;
  922. }
  923. int process_resolve_object_expr (struct process *p, int pos, char **names, NCDObject *out_object)
  924. {
  925. ASSERT(pos >= 0)
  926. ASSERT(pos <= p->num_statements)
  927. ASSERT(names)
  928. ASSERT(count_strings(names) > 0)
  929. ASSERT(out_object)
  930. NCDObject object;
  931. if (!process_find_object(p, pos, names[0], &object)) {
  932. goto fail;
  933. }
  934. if (!NCDObject_ResolveObjExpr(&object, names + 1, out_object)) {
  935. goto fail;
  936. }
  937. return 1;
  938. fail:;
  939. char *name = implode_strings(names, '.');
  940. process_log(p, BLOG_ERROR, "failed to resolve object (%s) from position %zu", (name ? name : ""), pos);
  941. free(name);
  942. return 0;
  943. }
  944. int process_resolve_variable_expr (struct process *p, int pos, char **names, NCDValMem *mem, NCDValRef *out_value)
  945. {
  946. ASSERT(pos >= 0)
  947. ASSERT(pos <= p->num_statements)
  948. ASSERT(names)
  949. ASSERT(count_strings(names) > 0)
  950. ASSERT(mem)
  951. ASSERT(out_value)
  952. NCDObject object;
  953. if (!process_find_object(p, pos, names[0], &object)) {
  954. goto fail;
  955. }
  956. if (!NCDObject_ResolveVarExpr(&object, names + 1, mem, out_value)) {
  957. goto fail;
  958. }
  959. return 1;
  960. fail:;
  961. char *name = implode_strings(names, '.');
  962. process_log(p, BLOG_ERROR, "failed to resolve variable (%s) from position %zu", (name ? name : ""), pos);
  963. free(name);
  964. return 0;
  965. }
  966. void statement_logfunc (struct statement *ps)
  967. {
  968. process_logfunc(ps->p);
  969. BLog_Append("statement %zu: ", ps->i);
  970. }
  971. void statement_log (struct statement *ps, int level, const char *fmt, ...)
  972. {
  973. if (!BLog_WouldLog(BLOG_CURRENT_CHANNEL, level)) {
  974. return;
  975. }
  976. va_list vl;
  977. va_start(vl, fmt);
  978. BLog_LogViaFuncVarArg((BLog_logfunc)statement_logfunc, ps, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  979. va_end(vl);
  980. }
  981. int statement_allocate_memory (struct statement *ps, int alloc_size, void **out_mem)
  982. {
  983. ASSERT(alloc_size >= 0)
  984. ASSERT(out_mem)
  985. if (alloc_size == 0) {
  986. *out_mem = NULL;
  987. return 1;
  988. }
  989. if (alloc_size > ps->mem_size) {
  990. if (ps->mem && !process_mem_is_preallocated(ps->p, ps->mem)) {
  991. free(ps->mem);
  992. }
  993. if (!(ps->mem = malloc(alloc_size))) {
  994. statement_log(ps, BLOG_ERROR, "malloc failed");
  995. ps->mem_size = 0;
  996. return 0;
  997. }
  998. ps->mem_size = alloc_size;
  999. }
  1000. *out_mem = ps->mem;
  1001. return 1;
  1002. }
  1003. int statement_resolve_argument (struct statement *ps, NCDInterpValue *arg, NCDValMem *mem, NCDValRef *out)
  1004. {
  1005. ASSERT(ps->i <= process_rap(ps->p))
  1006. ASSERT(arg)
  1007. ASSERT(mem)
  1008. ASSERT(out)
  1009. switch (arg->type) {
  1010. case NCDVALUE_STRING: {
  1011. *out = NCDVal_NewStringBin(mem, (uint8_t *)arg->string, arg->string_len);
  1012. if (NCDVal_IsInvalid(*out)) {
  1013. statement_log(ps, BLOG_ERROR, "NCDVal_NewStringBin failed");
  1014. return 0;
  1015. }
  1016. } break;
  1017. case NCDVALUE_VAR: {
  1018. if (!process_resolve_variable_expr(ps->p, ps->i, arg->variable_names, mem, out)) {
  1019. return 0;
  1020. }
  1021. if (NCDVal_IsInvalid(*out)) {
  1022. return 0;
  1023. }
  1024. } break;
  1025. case NCDVALUE_LIST: {
  1026. *out = NCDVal_NewList(mem, arg->list_count);
  1027. if (NCDVal_IsInvalid(*out)) {
  1028. statement_log(ps, BLOG_ERROR, "NCDVal_NewList failed");
  1029. return 0;
  1030. }
  1031. for (LinkedList1Node *n = LinkedList1_GetFirst(&arg->list); n; n = LinkedList1Node_Next(n)) {
  1032. struct NCDInterpValueListElem *elem = UPPER_OBJECT(n, struct NCDInterpValueListElem, list_node);
  1033. NCDValRef new_elem;
  1034. if (!statement_resolve_argument(ps, &elem->value, mem, &new_elem)) {
  1035. return 0;
  1036. }
  1037. NCDVal_ListAppend(*out, new_elem);
  1038. }
  1039. } break;
  1040. case NCDVALUE_MAP: {
  1041. *out = NCDVal_NewMap(mem, arg->map_count);
  1042. if (NCDVal_IsInvalid(*out)) {
  1043. statement_log(ps, BLOG_ERROR, "NCDVal_NewMap failed");
  1044. return 0;
  1045. }
  1046. for (LinkedList1Node *n = LinkedList1_GetFirst(&arg->maplist); n; n = LinkedList1Node_Next(n)) {
  1047. struct NCDInterpValueMapElem *elem = UPPER_OBJECT(n, struct NCDInterpValueMapElem, maplist_node);
  1048. NCDValRef new_key;
  1049. if (!statement_resolve_argument(ps, &elem->key, mem, &new_key)) {
  1050. return 0;
  1051. }
  1052. NCDValRef new_val;
  1053. if (!statement_resolve_argument(ps, &elem->val, mem, &new_val)) {
  1054. return 0;
  1055. }
  1056. int res = NCDVal_MapInsert(*out, new_key, new_val);
  1057. if (!res) {
  1058. statement_log(ps, BLOG_ERROR, "duplicate map keys");
  1059. return 0;
  1060. }
  1061. }
  1062. } break;
  1063. default: ASSERT(0);
  1064. }
  1065. return 1;
  1066. }
  1067. void statement_instance_func_event (struct statement *ps, int event)
  1068. {
  1069. ASSERT(ps->state == SSTATE_CHILD || ps->state == SSTATE_ADULT || ps->state == SSTATE_DYING)
  1070. struct process *p = ps->p;
  1071. process_assert_pointers(p);
  1072. // schedule work
  1073. process_schedule_work(p);
  1074. switch (event) {
  1075. case NCDMODULE_EVENT_UP: {
  1076. ASSERT(ps->state == SSTATE_CHILD)
  1077. statement_log(ps, BLOG_INFO, "up");
  1078. // set state ADULT
  1079. ps->state = SSTATE_ADULT;
  1080. } break;
  1081. case NCDMODULE_EVENT_DOWN: {
  1082. ASSERT(ps->state == SSTATE_ADULT)
  1083. statement_log(ps, BLOG_INFO, "down");
  1084. // set state CHILD
  1085. ps->state = SSTATE_CHILD;
  1086. // clear error
  1087. if (ps->i < p->ap) {
  1088. p->have_error = 0;
  1089. }
  1090. // update AP
  1091. if (p->ap > ps->i + 1) {
  1092. p->ap = ps->i + 1;
  1093. }
  1094. } break;
  1095. case NCDMODULE_EVENT_DEAD: {
  1096. int is_error = NCDModuleInst_HaveError(&ps->inst);
  1097. if (is_error) {
  1098. statement_log(ps, BLOG_ERROR, "died with error");
  1099. } else {
  1100. statement_log(ps, BLOG_INFO, "died");
  1101. }
  1102. // free instance
  1103. NCDModuleInst_Free(&ps->inst);
  1104. // free arguments memory
  1105. NCDValMem_Free(&ps->args_mem);
  1106. // set state FORGOTTEN
  1107. ps->state = SSTATE_FORGOTTEN;
  1108. // set error
  1109. if (is_error && ps->i < p->ap) {
  1110. p->have_error = 1;
  1111. }
  1112. // update AP
  1113. if (p->ap > ps->i) {
  1114. p->ap = ps->i;
  1115. }
  1116. // update FP
  1117. while (p->fp > 0 && p->statements[p->fp - 1].state == SSTATE_FORGOTTEN) {
  1118. p->fp--;
  1119. }
  1120. } break;
  1121. }
  1122. }
  1123. int statement_instance_func_getobj (struct statement *ps, const char *objname, NCDObject *out_object)
  1124. {
  1125. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1126. return process_find_object(ps->p, ps->i, objname, out_object);
  1127. }
  1128. int statement_instance_func_initprocess (struct statement *ps, NCDModuleProcess *mp, const char *template_name)
  1129. {
  1130. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1131. // find template
  1132. NCDProcess *p_ast;
  1133. NCDInterpBlock *iblock;
  1134. if (!NCDInterpProg_FindProcess(&iprogram, template_name, &p_ast, &iblock) || !NCDProcess_IsTemplate(p_ast)) {
  1135. statement_log(ps, BLOG_ERROR, "no template named %s", template_name);
  1136. return 0;
  1137. }
  1138. // create process
  1139. if (!process_new(p_ast, iblock, mp)) {
  1140. statement_log(ps, BLOG_ERROR, "failed to create process from template %s", template_name);
  1141. return 0;
  1142. }
  1143. statement_log(ps, BLOG_INFO, "created process from template %s", template_name);
  1144. return 1;
  1145. }
  1146. void statement_instance_logfunc (struct statement *ps)
  1147. {
  1148. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1149. statement_logfunc(ps);
  1150. BLog_Append("module: ");
  1151. }
  1152. void statement_instance_func_interp_exit (struct statement *ps, int exit_code)
  1153. {
  1154. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1155. start_terminate(exit_code);
  1156. }
  1157. int statement_instance_func_interp_getargs (struct statement *ps, NCDValMem *mem, NCDValRef *out_value)
  1158. {
  1159. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1160. *out_value = NCDVal_NewList(mem, options.num_extra_args);
  1161. if (NCDVal_IsInvalid(*out_value)) {
  1162. statement_log(ps, BLOG_ERROR, "NCDVal_NewList failed");
  1163. goto fail;
  1164. }
  1165. for (int i = 0; i < options.num_extra_args; i++) {
  1166. NCDValRef arg = NCDVal_NewString(mem, options.extra_args[i]);
  1167. if (NCDVal_IsInvalid(arg)) {
  1168. statement_log(ps, BLOG_ERROR, "NCDVal_NewString failed");
  1169. goto fail;
  1170. }
  1171. NCDVal_ListAppend(*out_value, arg);
  1172. }
  1173. return 1;
  1174. fail:
  1175. *out_value = NCDVal_NewInvalid();
  1176. return 1;
  1177. }
  1178. btime_t statement_instance_func_interp_getretrytime (struct statement *ps)
  1179. {
  1180. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1181. return options.retry_time;
  1182. }
  1183. void process_moduleprocess_func_event (struct process *p, int event)
  1184. {
  1185. ASSERT(p->module_process)
  1186. switch (event) {
  1187. case NCDMODULEPROCESS_INTERP_EVENT_CONTINUE: {
  1188. ASSERT(p->state == PSTATE_WAITING)
  1189. // set state working
  1190. p->state = PSTATE_WORKING;
  1191. // schedule work
  1192. process_schedule_work(p);
  1193. } break;
  1194. case NCDMODULEPROCESS_INTERP_EVENT_TERMINATE: {
  1195. ASSERT(p->state != PSTATE_TERMINATING)
  1196. process_log(p, BLOG_INFO, "process termination requested");
  1197. // start terminating
  1198. process_start_terminating(p);
  1199. } break;
  1200. default: ASSERT(0);
  1201. }
  1202. }
  1203. int process_moduleprocess_func_getobj (struct process *p, const char *name, NCDObject *out_object)
  1204. {
  1205. ASSERT(p->module_process)
  1206. return process_find_object(p, p->num_statements, name, out_object);
  1207. }