ncd.c 41 KB

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