ncd.c 40 KB

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