ncd.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. /**
  2. * @file ncd.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdint.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <misc/version.h>
  34. #include <misc/loglevel.h>
  35. #include <misc/offset.h>
  36. #include <misc/read_file.h>
  37. #include <misc/balloc.h>
  38. #include <misc/concat_strings.h>
  39. #include <misc/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. struct process_statement {
  73. struct process *p;
  74. btime_t error_until;
  75. NCDModuleInst inst;
  76. NCDValue inst_args;
  77. int i;
  78. int state;
  79. int have_error;
  80. };
  81. struct process {
  82. NCDProcess *proc_ast;
  83. NCDInterpBlock *iblock;
  84. NCDModuleProcess *module_process;
  85. BTimer wait_timer;
  86. BPending work_job;
  87. LinkedList1Node list_node; // node in processes
  88. int state;
  89. int ap;
  90. int fp;
  91. int num_statements;
  92. struct process_statement statements[];
  93. };
  94. // command-line options
  95. struct {
  96. int help;
  97. int version;
  98. int logger;
  99. char *logger_syslog_facility;
  100. char *logger_syslog_ident;
  101. int loglevel;
  102. int loglevels[BLOG_NUM_CHANNELS];
  103. char *config_file;
  104. int retry_time;
  105. int no_udev;
  106. char **extra_args;
  107. int num_extra_args;
  108. } options;
  109. // reactor
  110. BReactor 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. // buffer for concatenating base_type::method_name
  130. char method_concat_buf[200];
  131. static void print_help (const char *name);
  132. static void print_version (void);
  133. static int parse_arguments (int argc, char *argv[]);
  134. static void signal_handler (void *unused);
  135. static void start_terminate (int exit_code);
  136. static char * names_tostring (char **names);
  137. static int process_new (NCDProcess *proc_ast, NCDInterpBlock *iblock, NCDModuleProcess *module_process);
  138. static void process_free (struct process *p);
  139. static void process_start_terminating (struct process *p);
  140. static int process_rap (struct process *p);
  141. static void process_assert_pointers (struct process *p);
  142. static void process_logfunc (struct process *p);
  143. static void process_log (struct process *p, int level, const char *fmt, ...);
  144. static void process_schedule_work (struct process *p);
  145. static void process_work_job_handler (struct process *p);
  146. static void process_advance (struct process *p);
  147. static void process_wait_timer_handler (struct process *p);
  148. static int process_find_object (struct process *p, int pos, const char *name, NCDObject *out_object);
  149. static int process_resolve_object_expr (struct process *p, int pos, char **names, NCDObject *out_object);
  150. static int process_resolve_variable_expr (struct process *p, int pos, char **names, NCDValue *out_value);
  151. static void process_statement_logfunc (struct process_statement *ps);
  152. static void process_statement_log (struct process_statement *ps, int level, const char *fmt, ...);
  153. static void process_statement_set_error (struct process_statement *ps);
  154. static int process_statement_resolve_argument (struct process_statement *ps, NCDInterpValue *arg, NCDValue *out);
  155. static void process_statement_instance_func_event (struct process_statement *ps, int event);
  156. static int process_statement_instance_func_getobj (struct process_statement *ps, const char *objname, NCDObject *out_object);
  157. static int process_statement_instance_func_initprocess (struct process_statement *ps, NCDModuleProcess *mp, const char *template_name);
  158. static void process_statement_instance_logfunc (struct process_statement *ps);
  159. static void process_statement_instance_func_interp_exit (struct process_statement *ps, int exit_code);
  160. static int process_statement_instance_func_interp_getargs (struct process_statement *ps, NCDValue *out_value);
  161. static btime_t process_statement_instance_func_interp_getretrytime (struct process_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(&ss)) {
  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, &ss)) {
  232. BLog(BLOG_ERROR, "BProcessManager_Init failed");
  233. goto fail1a;
  234. }
  235. // init udev manager
  236. NCDUdevManager_Init(&umanager, options.no_udev, &ss, &manager);
  237. // init module index
  238. if (!NCDModuleIndex_Init(&mindex)) {
  239. BLog(BLOG_ERROR, "NCDModuleIndex_Init failed");
  240. goto fail1b;
  241. }
  242. // add module groups to index
  243. for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
  244. if (!NCDModuleIndex_AddGroup(&mindex, *g)) {
  245. BLog(BLOG_ERROR, "NCDModuleIndex_AddGroup failed");
  246. goto fail2;
  247. }
  248. }
  249. // setup signal handler
  250. if (!BSignal_Init(&ss, signal_handler, NULL)) {
  251. BLog(BLOG_ERROR, "BSignal_Init failed");
  252. goto fail2;
  253. }
  254. // read config file
  255. uint8_t *file;
  256. size_t file_len;
  257. if (!read_file(options.config_file, &file, &file_len)) {
  258. BLog(BLOG_ERROR, "failed to read config file");
  259. goto fail3;
  260. }
  261. // parse config file
  262. if (!NCDConfigParser_Parse((char *)file, file_len, &program)) {
  263. BLog(BLOG_ERROR, "NCDConfigParser_Parse failed");
  264. free(file);
  265. goto fail3;
  266. }
  267. // fee config file memory
  268. free(file);
  269. // desugar
  270. if (!NCDSugar_Desugar(&program)) {
  271. BLog(BLOG_ERROR, "NCDSugar_Desugar failed");
  272. goto fail4;
  273. }
  274. // init interp program
  275. if (!NCDInterpProg_Init(&iprogram, &program)) {
  276. BLog(BLOG_ERROR, "NCDInterpProg_Init failed");
  277. goto fail4;
  278. }
  279. // init module params
  280. struct NCDModuleInitParams params;
  281. params.reactor = &ss;
  282. params.manager = &manager;
  283. params.umanager = &umanager;
  284. // init modules
  285. size_t num_inited_modules = 0;
  286. for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
  287. if ((*g)->func_globalinit && !(*g)->func_globalinit(params)) {
  288. BLog(BLOG_ERROR, "globalinit failed for some module");
  289. goto fail5;
  290. }
  291. num_inited_modules++;
  292. }
  293. // init common module params
  294. module_params.func_event = (NCDModuleInst_func_event)process_statement_instance_func_event;
  295. module_params.func_getobj = (NCDModuleInst_func_getobj)process_statement_instance_func_getobj;
  296. module_params.logfunc = (BLog_logfunc)process_statement_instance_logfunc;
  297. module_iparams.reactor = &ss;
  298. module_iparams.manager = &manager;
  299. module_iparams.umanager = &umanager;
  300. module_iparams.func_initprocess = (NCDModuleInst_func_initprocess)process_statement_instance_func_initprocess;
  301. module_iparams.func_interp_exit = (NCDModuleInst_func_interp_exit)process_statement_instance_func_interp_exit;
  302. module_iparams.func_interp_getargs = (NCDModuleInst_func_interp_getargs)process_statement_instance_func_interp_getargs;
  303. module_iparams.func_interp_getretrytime = (NCDModuleInst_func_interp_getretrytime)process_statement_instance_func_interp_getretrytime;
  304. // init processes list
  305. LinkedList1_Init(&processes);
  306. // init processes
  307. for (NCDProcess *p = NCDProgram_FirstProcess(&program); p; p = NCDProgram_NextProcess(&program, p)) {
  308. if (NCDProcess_IsTemplate(p)) {
  309. continue;
  310. }
  311. // find iblock
  312. NCDProcess *f_proc;
  313. NCDInterpBlock *iblock;
  314. int res = NCDInterpProg_FindProcess(&iprogram, NCDProcess_Name(p), &f_proc, &iblock);
  315. ASSERT(res)
  316. ASSERT(f_proc == p)
  317. if (!process_new(p, iblock, NULL)) {
  318. BLog(BLOG_ERROR, "failed to initialize process, exiting");
  319. goto fail6;
  320. }
  321. }
  322. // enter event loop
  323. BLog(BLOG_NOTICE, "entering event loop");
  324. BReactor_Exec(&ss);
  325. ASSERT(LinkedList1_IsEmpty(&processes))
  326. fail6:;
  327. LinkedList1Node *ln;
  328. while (ln = LinkedList1_GetFirst(&processes)) {
  329. struct process *p = UPPER_OBJECT(ln, struct process, list_node);
  330. process_free(p);
  331. }
  332. fail5:
  333. // free modules
  334. while (num_inited_modules > 0) {
  335. const struct NCDModuleGroup **g = &ncd_modules[num_inited_modules - 1];
  336. if ((*g)->func_globalfree) {
  337. (*g)->func_globalfree();
  338. }
  339. num_inited_modules--;
  340. }
  341. // free interp program
  342. NCDInterpProg_Free(&iprogram);
  343. fail4:
  344. // free program AST
  345. NCDProgram_Free(&program);
  346. fail3:
  347. // remove signal handler
  348. BSignal_Finish();
  349. fail2:
  350. // free module index
  351. NCDModuleIndex_Free(&mindex);
  352. fail1b:
  353. // free udev manager
  354. NCDUdevManager_Free(&umanager);
  355. // free process manager
  356. BProcessManager_Free(&manager);
  357. fail1a:
  358. // free reactor
  359. BReactor_Free(&ss);
  360. fail1:
  361. // free logger
  362. BLog(BLOG_NOTICE, "exiting");
  363. BLog_Free();
  364. fail0:
  365. // finish objects
  366. DebugObjectGlobal_Finish();
  367. return main_exit_code;
  368. }
  369. void print_help (const char *name)
  370. {
  371. printf(
  372. "Usage:\n"
  373. " %s\n"
  374. " [--help]\n"
  375. " [--version]\n"
  376. " [--logger <stdout/stderr/syslog>]\n"
  377. " (logger=syslog?\n"
  378. " [--syslog-facility <string>]\n"
  379. " [--syslog-ident <string>]\n"
  380. " )\n"
  381. " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
  382. " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
  383. " --config-file <file>\n"
  384. " [--retry-time <ms>]\n"
  385. " [--no-udev]\n"
  386. " [-- [<extra_arg>] ...]\n",
  387. name
  388. );
  389. }
  390. void print_version (void)
  391. {
  392. printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
  393. }
  394. int parse_arguments (int argc, char *argv[])
  395. {
  396. if (argc <= 0) {
  397. return 0;
  398. }
  399. options.help = 0;
  400. options.version = 0;
  401. options.logger = LOGGER_STDERR;
  402. options.logger_syslog_facility = "daemon";
  403. options.logger_syslog_ident = argv[0];
  404. options.loglevel = -1;
  405. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  406. options.loglevels[i] = -1;
  407. }
  408. options.config_file = NULL;
  409. options.retry_time = DEFAULT_RETRY_TIME;
  410. options.no_udev = 0;
  411. options.extra_args = NULL;
  412. options.num_extra_args = 0;
  413. for (int i = 1; i < argc; i++) {
  414. char *arg = argv[i];
  415. if (!strcmp(arg, "--help")) {
  416. options.help = 1;
  417. }
  418. else if (!strcmp(arg, "--version")) {
  419. options.version = 1;
  420. }
  421. else if (!strcmp(arg, "--logger")) {
  422. if (1 >= argc - i) {
  423. fprintf(stderr, "%s: requires an argument\n", arg);
  424. return 0;
  425. }
  426. char *arg2 = argv[i + 1];
  427. if (!strcmp(arg2, "stdout")) {
  428. options.logger = LOGGER_STDOUT;
  429. }
  430. else if (!strcmp(arg2, "stderr")) {
  431. options.logger = LOGGER_STDERR;
  432. }
  433. else if (!strcmp(arg2, "syslog")) {
  434. options.logger = LOGGER_SYSLOG;
  435. }
  436. else {
  437. fprintf(stderr, "%s: wrong argument\n", arg);
  438. return 0;
  439. }
  440. i++;
  441. }
  442. else if (!strcmp(arg, "--syslog-facility")) {
  443. if (1 >= argc - i) {
  444. fprintf(stderr, "%s: requires an argument\n", arg);
  445. return 0;
  446. }
  447. options.logger_syslog_facility = argv[i + 1];
  448. i++;
  449. }
  450. else if (!strcmp(arg, "--syslog-ident")) {
  451. if (1 >= argc - i) {
  452. fprintf(stderr, "%s: requires an argument\n", arg);
  453. return 0;
  454. }
  455. options.logger_syslog_ident = argv[i + 1];
  456. i++;
  457. }
  458. else if (!strcmp(arg, "--loglevel")) {
  459. if (1 >= argc - i) {
  460. fprintf(stderr, "%s: requires an argument\n", arg);
  461. return 0;
  462. }
  463. if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
  464. fprintf(stderr, "%s: wrong argument\n", arg);
  465. return 0;
  466. }
  467. i++;
  468. }
  469. else if (!strcmp(arg, "--channel-loglevel")) {
  470. if (2 >= argc - i) {
  471. fprintf(stderr, "%s: requires two arguments\n", arg);
  472. return 0;
  473. }
  474. int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
  475. if (channel < 0) {
  476. fprintf(stderr, "%s: wrong channel argument\n", arg);
  477. return 0;
  478. }
  479. int loglevel = parse_loglevel(argv[i + 2]);
  480. if (loglevel < 0) {
  481. fprintf(stderr, "%s: wrong loglevel argument\n", arg);
  482. return 0;
  483. }
  484. options.loglevels[channel] = loglevel;
  485. i += 2;
  486. }
  487. else if (!strcmp(arg, "--config-file")) {
  488. if (1 >= argc - i) {
  489. fprintf(stderr, "%s: requires an argument\n", arg);
  490. return 0;
  491. }
  492. options.config_file = argv[i + 1];
  493. i++;
  494. }
  495. else if (!strcmp(arg, "--retry-time")) {
  496. if (1 >= argc - i) {
  497. fprintf(stderr, "%s: requires an argument\n", arg);
  498. return 0;
  499. }
  500. if ((options.retry_time = atoi(argv[i + 1])) < 0) {
  501. fprintf(stderr, "%s: wrong argument\n", arg);
  502. return 0;
  503. }
  504. i++;
  505. }
  506. else if (!strcmp(arg, "--no-udev")) {
  507. options.no_udev = 1;
  508. }
  509. else if (!strcmp(arg, "--")) {
  510. options.extra_args = &argv[i + 1];
  511. options.num_extra_args = argc - i - 1;
  512. i += options.num_extra_args;
  513. }
  514. else {
  515. fprintf(stderr, "unknown option: %s\n", arg);
  516. return 0;
  517. }
  518. }
  519. if (options.help || options.version) {
  520. return 1;
  521. }
  522. if (!options.config_file) {
  523. fprintf(stderr, "--config-file is required\n");
  524. return 0;
  525. }
  526. return 1;
  527. }
  528. void signal_handler (void *unused)
  529. {
  530. BLog(BLOG_NOTICE, "termination requested");
  531. start_terminate(1);
  532. }
  533. void start_terminate (int exit_code)
  534. {
  535. main_exit_code = exit_code;
  536. if (terminating) {
  537. return;
  538. }
  539. terminating = 1;
  540. if (LinkedList1_IsEmpty(&processes)) {
  541. BReactor_Quit(&ss, 0);
  542. return;
  543. }
  544. // start terminating non-template processes
  545. for (LinkedList1Node *ln = LinkedList1_GetFirst(&processes); ln; ln = LinkedList1Node_Next(ln)) {
  546. struct process *p = UPPER_OBJECT(ln, struct process, list_node);
  547. if (p->module_process) {
  548. continue;
  549. }
  550. if (p->state != PSTATE_TERMINATING) {
  551. process_start_terminating(p);
  552. }
  553. }
  554. }
  555. static char * names_tostring (char **names)
  556. {
  557. ASSERT(names)
  558. ExpString str;
  559. if (!ExpString_Init(&str)) {
  560. goto fail0;
  561. }
  562. for (size_t i = 0; names[i]; i++) {
  563. if (i > 0 && !ExpString_AppendChar(&str, '.')) {
  564. goto fail1;
  565. }
  566. if (!ExpString_Append(&str, names[i])) {
  567. goto fail1;
  568. }
  569. }
  570. return ExpString_Get(&str);
  571. fail1:
  572. ExpString_Free(&str);
  573. fail0:
  574. return NULL;
  575. }
  576. static int process_new (NCDProcess *proc_ast, NCDInterpBlock *iblock, NCDModuleProcess *module_process)
  577. {
  578. // get block
  579. NCDBlock *block = NCDProcess_Block(proc_ast);
  580. // get num statements
  581. int num_statements = NCDBlock_NumStatements(block);
  582. // calculate allocation size
  583. bsize_t alloc_size = bsize_add(bsize_fromsize(sizeof(struct process)), bsize_mul(bsize_fromsize(num_statements), bsize_fromsize(sizeof(struct process_statement))));
  584. // allocate strucure
  585. struct process *p = BAllocSize(alloc_size);
  586. if (!p) {
  587. BLog(BLOG_ERROR, "BAllocSize failed");
  588. goto fail0;
  589. }
  590. // set some stuff
  591. p->proc_ast = proc_ast;
  592. p->iblock = iblock;
  593. p->module_process = module_process;
  594. p->num_statements = num_statements;
  595. // set module process handlers
  596. if (p->module_process) {
  597. NCDModuleProcess_Interp_SetHandlers(p->module_process, p,
  598. (NCDModuleProcess_interp_func_event)process_moduleprocess_func_event,
  599. (NCDModuleProcess_interp_func_getobj)process_moduleprocess_func_getobj);
  600. }
  601. // init statements
  602. for (int i = 0; i < num_statements; i++) {
  603. struct process_statement *ps = &p->statements[i];
  604. ps->p = p;
  605. ps->i = i;
  606. ps->state = SSTATE_FORGOTTEN;
  607. ps->have_error = 0;
  608. }
  609. // set state working
  610. p->state = PSTATE_WORKING;
  611. // set AP=0
  612. p->ap = 0;
  613. // set FP=0
  614. p->fp = 0;
  615. // init timer
  616. BTimer_Init(&p->wait_timer, 0, (BTimer_handler)process_wait_timer_handler, p);
  617. // init work job
  618. BPending_Init(&p->work_job, BReactor_PendingGroup(&ss), (BPending_handler)process_work_job_handler, p);
  619. // insert to processes list
  620. LinkedList1_Append(&processes, &p->list_node);
  621. // schedule work
  622. BPending_Set(&p->work_job);
  623. return 1;
  624. fail0:
  625. BLog(BLOG_ERROR, "failed to initialize process %s", NCDProcess_Name(proc_ast));
  626. return 0;
  627. }
  628. void process_free (struct process *p)
  629. {
  630. ASSERT(p->ap == 0)
  631. ASSERT(p->fp == 0)
  632. // inform module process that the process is terminated
  633. if (p->module_process) {
  634. NCDModuleProcess_Interp_Terminated(p->module_process);
  635. }
  636. // remove from processes list
  637. LinkedList1_Remove(&processes, &p->list_node);
  638. // free work job
  639. BPending_Free(&p->work_job);
  640. // free timer
  641. BReactor_RemoveTimer(&ss, &p->wait_timer);
  642. // free strucure
  643. BFree(p);
  644. }
  645. void process_start_terminating (struct process *p)
  646. {
  647. ASSERT(p->state != PSTATE_TERMINATING)
  648. // set terminating
  649. p->state = PSTATE_TERMINATING;
  650. // schedule work
  651. process_schedule_work(p);
  652. }
  653. int process_rap (struct process *p)
  654. {
  655. if (p->ap > 0 && p->statements[p->ap - 1].state == SSTATE_CHILD) {
  656. return (p->ap - 1);
  657. } else {
  658. return p->ap;
  659. }
  660. }
  661. void process_assert_pointers (struct process *p)
  662. {
  663. ASSERT(p->ap <= p->num_statements)
  664. ASSERT(p->fp >= p->ap)
  665. ASSERT(p->fp <= p->num_statements)
  666. #ifndef NDEBUG
  667. // check AP
  668. for (int i = 0; i < p->ap; i++) {
  669. if (i == p->ap - 1) {
  670. ASSERT(p->statements[i].state == SSTATE_ADULT || p->statements[i].state == SSTATE_CHILD)
  671. } else {
  672. ASSERT(p->statements[i].state == SSTATE_ADULT)
  673. }
  674. }
  675. // check FP
  676. int fp = p->num_statements;
  677. while (fp > 0 && p->statements[fp - 1].state == SSTATE_FORGOTTEN) {
  678. fp--;
  679. }
  680. ASSERT(p->fp == fp)
  681. #endif
  682. }
  683. void process_logfunc (struct process *p)
  684. {
  685. BLog_Append("process %s: ", NCDProcess_Name(p->proc_ast));
  686. }
  687. void process_log (struct process *p, int level, const char *fmt, ...)
  688. {
  689. va_list vl;
  690. va_start(vl, fmt);
  691. BLog_LogViaFuncVarArg((BLog_logfunc)process_logfunc, p, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  692. va_end(vl);
  693. }
  694. void process_schedule_work (struct process *p)
  695. {
  696. process_assert_pointers(p);
  697. // stop timer
  698. BReactor_RemoveTimer(&ss, &p->wait_timer);
  699. // schedule work
  700. BPending_Set(&p->work_job);
  701. }
  702. void process_work_job_handler (struct process *p)
  703. {
  704. process_assert_pointers(p);
  705. ASSERT(!BTimer_IsRunning(&p->wait_timer))
  706. if (p->state == PSTATE_WAITING) {
  707. return;
  708. }
  709. if (p->state == PSTATE_TERMINATING) {
  710. if (p->fp == 0) {
  711. // finished retreating
  712. process_free(p);
  713. // if program is terminating amd there are no more processes, exit program
  714. if (terminating && LinkedList1_IsEmpty(&processes)) {
  715. BReactor_Quit(&ss, 0);
  716. }
  717. return;
  718. }
  719. // order the last living statement to die, if needed
  720. struct process_statement *ps = &p->statements[p->fp - 1];
  721. ASSERT(ps->state != SSTATE_FORGOTTEN)
  722. if (ps->state != SSTATE_DYING) {
  723. process_statement_log(ps, BLOG_INFO, "killing");
  724. // order it to die
  725. NCDModuleInst_Die(&ps->inst);
  726. // set statement state DYING
  727. ps->state = SSTATE_DYING;
  728. // update AP
  729. if (p->ap > ps->i) {
  730. p->ap = ps->i;
  731. }
  732. }
  733. return;
  734. }
  735. // process was up but is no longer?
  736. if (p->state == PSTATE_UP && !(p->ap == process_rap(p) && p->ap == p->num_statements)) {
  737. // if we have module process, wait for its permission to continue
  738. if (p->module_process) {
  739. // set module process down
  740. NCDModuleProcess_Interp_Down(p->module_process);
  741. // set state waiting
  742. p->state = PSTATE_WAITING;
  743. return;
  744. }
  745. // set state working
  746. p->state = PSTATE_WORKING;
  747. }
  748. // cleaning up?
  749. if (p->ap < p->fp) {
  750. // order the last living statement to die, if needed
  751. struct process_statement *ps = &p->statements[p->fp - 1];
  752. if (ps->state != SSTATE_DYING) {
  753. process_statement_log(ps, BLOG_INFO, "killing");
  754. // order it to die
  755. NCDModuleInst_Die(&ps->inst);
  756. // set statement state DYING
  757. ps->state = SSTATE_DYING;
  758. }
  759. return;
  760. }
  761. // clean?
  762. if (p->ap > process_rap(p)) {
  763. ASSERT(p->ap > 0)
  764. ASSERT(p->ap <= p->num_statements)
  765. struct process_statement *ps = &p->statements[p->ap - 1];
  766. ASSERT(ps->state == SSTATE_CHILD)
  767. process_statement_log(ps, BLOG_INFO, "clean");
  768. // report clean
  769. NCDModuleInst_Clean(&ps->inst);
  770. return;
  771. }
  772. // advancing?
  773. if (p->ap < p->num_statements) {
  774. ASSERT(p->state == PSTATE_WORKING)
  775. struct process_statement *ps = &p->statements[p->ap];
  776. ASSERT(ps->state == SSTATE_FORGOTTEN)
  777. // clear expired error
  778. if (ps->have_error && ps->error_until <= btime_gettime()) {
  779. ps->have_error = 0;
  780. }
  781. if (ps->have_error) {
  782. process_statement_log(ps, BLOG_INFO, "waiting after error");
  783. // set wait timer
  784. BReactor_SetTimerAbsolute(&ss, &p->wait_timer, ps->error_until);
  785. } else {
  786. // advance
  787. process_advance(p);
  788. }
  789. return;
  790. }
  791. // have we just finished?
  792. if (p->state == PSTATE_WORKING) {
  793. process_log(p, BLOG_INFO, "victory");
  794. // set module process up
  795. if (p->module_process) {
  796. NCDModuleProcess_Interp_Up(p->module_process);
  797. }
  798. // set state up
  799. p->state = PSTATE_UP;
  800. }
  801. }
  802. void process_advance (struct process *p)
  803. {
  804. process_assert_pointers(p);
  805. ASSERT(p->ap == p->fp)
  806. ASSERT(p->ap == process_rap(p))
  807. ASSERT(p->ap < p->num_statements)
  808. ASSERT(!p->statements[p->ap].have_error)
  809. ASSERT(!BPending_IsSet(&p->work_job))
  810. ASSERT(!BTimer_IsRunning(&p->wait_timer))
  811. ASSERT(p->state == PSTATE_WORKING)
  812. struct process_statement *ps = &p->statements[p->ap];
  813. ASSERT(ps->state == SSTATE_FORGOTTEN)
  814. process_statement_log(ps, BLOG_INFO, "initializing");
  815. NCDObject object;
  816. NCDObject *object_ptr = NULL;
  817. const char *type;
  818. char **object_names = NCDInterpBlock_StatementObjNames(p->iblock, p->ap);
  819. const char *method_name = NCDInterpBlock_StatementCmdName(p->iblock, p->ap);
  820. if (!object_names) {
  821. // this is a function_call(); type is "function_call"
  822. type = method_name;
  823. } else {
  824. // this is a some.object.somewhere->method_call(); type is "base_type(some.object.somewhere)::method_call"
  825. // get object
  826. if (!process_resolve_object_expr(p, p->ap, object_names, &object)) {
  827. goto fail;
  828. }
  829. object_ptr = &object;
  830. // get object type
  831. const char *object_type = NCDObject_Type(&object);
  832. if (!object_type) {
  833. process_statement_log(ps, BLOG_ERROR, "cannot call method on object with no type");
  834. goto fail;
  835. }
  836. // build type string
  837. int res = snprintf(method_concat_buf, sizeof(method_concat_buf), "%s::%s", object_type, method_name);
  838. if (res >= sizeof(method_concat_buf) || res < 0) {
  839. process_statement_log(ps, BLOG_ERROR, "type/method name too long");
  840. goto fail;
  841. }
  842. type = method_concat_buf;
  843. }
  844. // find module to instantiate
  845. const struct NCDModule *module = NCDModuleIndex_FindModule(&mindex, type);
  846. if (!module) {
  847. process_statement_log(ps, BLOG_ERROR, "failed to find module: %s", type);
  848. goto fail;
  849. }
  850. // resolve arguments
  851. if (!process_statement_resolve_argument(ps, NCDInterpBlock_StatementInterpValue(p->iblock, ps->i), &ps->inst_args)) {
  852. process_statement_log(ps, BLOG_ERROR, "failed to resolve arguments");
  853. goto fail;
  854. }
  855. // initialize module instance
  856. NCDModuleInst_Init(&ps->inst, module, object_ptr, &ps->inst_args, ps, &module_params, &module_iparams);
  857. // set statement state CHILD
  858. ps->state = SSTATE_CHILD;
  859. // increment AP
  860. p->ap++;
  861. // increment FP
  862. p->fp++;
  863. process_assert_pointers(p);
  864. return;
  865. fail:
  866. // mark error
  867. process_statement_set_error(ps);
  868. // schedule work to start the timer
  869. process_schedule_work(p);
  870. }
  871. void process_wait_timer_handler (struct process *p)
  872. {
  873. process_assert_pointers(p);
  874. ASSERT(p->ap == p->fp)
  875. ASSERT(p->ap == process_rap(p))
  876. ASSERT(p->ap < p->num_statements)
  877. ASSERT(p->statements[p->ap].have_error)
  878. ASSERT(!BPending_IsSet(&p->work_job))
  879. ASSERT(p->state == PSTATE_WORKING)
  880. process_log(p, BLOG_INFO, "retrying");
  881. // clear error
  882. p->statements[p->ap].have_error = 0;
  883. // schedule work
  884. BPending_Set(&p->work_job);
  885. }
  886. int process_find_object (struct process *p, int pos, const char *name, NCDObject *out_object)
  887. {
  888. ASSERT(pos >= 0)
  889. ASSERT(pos <= p->num_statements)
  890. ASSERT(name)
  891. ASSERT(out_object)
  892. int i = NCDInterpBlock_FindStatement(p->iblock, pos, name);
  893. if (i >= 0) {
  894. struct process_statement *ps = &p->statements[i];
  895. ASSERT(i < p->num_statements)
  896. if (ps->state == SSTATE_FORGOTTEN) {
  897. process_log(p, BLOG_ERROR, "statement (%d) is uninitialized", i);
  898. return 0;
  899. }
  900. *out_object = NCDModuleInst_Object(&ps->inst);
  901. return 1;
  902. }
  903. if (p->module_process && NCDModuleProcess_Interp_GetSpecialObj(p->module_process, name, out_object)) {
  904. return 1;
  905. }
  906. return 0;
  907. }
  908. int process_resolve_object_expr (struct process *p, int pos, char **names, NCDObject *out_object)
  909. {
  910. ASSERT(pos >= 0)
  911. ASSERT(pos <= p->num_statements)
  912. ASSERT(names)
  913. ASSERT(count_strings(names) > 0)
  914. ASSERT(out_object)
  915. NCDObject object;
  916. if (!process_find_object(p, pos, names[0], &object)) {
  917. goto fail;
  918. }
  919. if (!NCDObject_ResolveObjExpr(&object, names + 1, out_object)) {
  920. goto fail;
  921. }
  922. return 1;
  923. fail:;
  924. char *name = names_tostring(names);
  925. process_log(p, BLOG_ERROR, "failed to resolve object (%s) from position %zu", (name ? name : ""), pos);
  926. free(name);
  927. return 0;
  928. }
  929. int process_resolve_variable_expr (struct process *p, int pos, char **names, NCDValue *out_value)
  930. {
  931. ASSERT(pos >= 0)
  932. ASSERT(pos <= p->num_statements)
  933. ASSERT(names)
  934. ASSERT(count_strings(names) > 0)
  935. ASSERT(out_value)
  936. NCDObject object;
  937. if (!process_find_object(p, pos, names[0], &object)) {
  938. goto fail;
  939. }
  940. if (!NCDObject_ResolveVarExpr(&object, names + 1, out_value)) {
  941. goto fail;
  942. }
  943. return 1;
  944. fail:;
  945. char *name = names_tostring(names);
  946. process_log(p, BLOG_ERROR, "failed to resolve variable (%s) from position %zu", (name ? name : ""), pos);
  947. free(name);
  948. return 0;
  949. }
  950. void process_statement_logfunc (struct process_statement *ps)
  951. {
  952. process_logfunc(ps->p);
  953. BLog_Append("statement %zu: ", ps->i);
  954. }
  955. void process_statement_log (struct process_statement *ps, int level, const char *fmt, ...)
  956. {
  957. if (!BLog_WouldLog(BLOG_CURRENT_CHANNEL, level)) {
  958. return;
  959. }
  960. va_list vl;
  961. va_start(vl, fmt);
  962. BLog_LogViaFuncVarArg((BLog_logfunc)process_statement_logfunc, ps, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  963. va_end(vl);
  964. }
  965. void process_statement_set_error (struct process_statement *ps)
  966. {
  967. ASSERT(ps->state == SSTATE_FORGOTTEN)
  968. ps->have_error = 1;
  969. ps->error_until = btime_add(btime_gettime(), options.retry_time);
  970. }
  971. int process_statement_resolve_argument (struct process_statement *ps, NCDInterpValue *arg, NCDValue *out)
  972. {
  973. ASSERT(ps->i <= process_rap(ps->p))
  974. ASSERT(arg)
  975. ASSERT(out)
  976. switch (arg->type) {
  977. case NCDVALUE_STRING: {
  978. if (!NCDValue_InitStringBin(out, (uint8_t *)arg->string, arg->string_len)) {
  979. process_statement_log(ps, BLOG_ERROR, "NCDValue_InitStringBin failed");
  980. return 0;
  981. }
  982. } break;
  983. case NCDVALUE_VAR: {
  984. if (!process_resolve_variable_expr(ps->p, ps->i, arg->variable_names, out)) {
  985. return 0;
  986. }
  987. } break;
  988. case NCDVALUE_LIST: do {
  989. NCDValue_InitList(out);
  990. for (LinkedList1Node *n = LinkedList1_GetFirst(&arg->list); n; n = LinkedList1Node_Next(n)) {
  991. struct NCDInterpValueListElem *elem = UPPER_OBJECT(n, struct NCDInterpValueListElem, list_node);
  992. NCDValue v;
  993. if (!process_statement_resolve_argument(ps, &elem->value, &v)) {
  994. goto list_fail1;
  995. }
  996. if (!NCDValue_ListAppend(out, v)) {
  997. process_statement_log(ps, BLOG_ERROR, "NCDValue_ListAppend failed");
  998. NCDValue_Free(&v);
  999. goto list_fail1;
  1000. }
  1001. }
  1002. break;
  1003. list_fail1:
  1004. NCDValue_Free(out);
  1005. return 0;
  1006. } while (0); break;
  1007. case NCDVALUE_MAP: do {
  1008. NCDValue_InitMap(out);
  1009. for (LinkedList1Node *n = LinkedList1_GetFirst(&arg->maplist); n; n = LinkedList1Node_Next(n)) {
  1010. struct NCDInterpValueMapElem *elem = UPPER_OBJECT(n, struct NCDInterpValueMapElem, maplist_node);
  1011. NCDValue key;
  1012. NCDValue val;
  1013. if (!process_statement_resolve_argument(ps, &elem->key, &key)) {
  1014. goto map_fail;
  1015. }
  1016. if (!process_statement_resolve_argument(ps, &elem->val, &val)) {
  1017. NCDValue_Free(&key);
  1018. goto map_fail;
  1019. }
  1020. if (NCDValue_MapFindKey(out, &key)) {
  1021. process_statement_log(ps, BLOG_ERROR, "duplicate map keys");
  1022. NCDValue_Free(&key);
  1023. NCDValue_Free(&val);
  1024. goto map_fail;
  1025. }
  1026. if (!NCDValue_MapInsert(out, key, val)) {
  1027. process_statement_log(ps, BLOG_ERROR, "NCDValue_MapInsert failed");
  1028. NCDValue_Free(&key);
  1029. NCDValue_Free(&val);
  1030. goto map_fail;
  1031. }
  1032. }
  1033. break;
  1034. map_fail:
  1035. NCDValue_Free(out);
  1036. return 0;
  1037. } while (0); break;
  1038. default: ASSERT(0);
  1039. }
  1040. return 1;
  1041. }
  1042. void process_statement_instance_func_event (struct process_statement *ps, int event)
  1043. {
  1044. ASSERT(ps->state == SSTATE_CHILD || ps->state == SSTATE_ADULT || ps->state == SSTATE_DYING)
  1045. struct process *p = ps->p;
  1046. process_assert_pointers(p);
  1047. // schedule work
  1048. process_schedule_work(p);
  1049. switch (event) {
  1050. case NCDMODULE_EVENT_UP: {
  1051. ASSERT(ps->state == SSTATE_CHILD)
  1052. process_statement_log(ps, BLOG_INFO, "up");
  1053. // set state ADULT
  1054. ps->state = SSTATE_ADULT;
  1055. } break;
  1056. case NCDMODULE_EVENT_DOWN: {
  1057. ASSERT(ps->state == SSTATE_ADULT)
  1058. process_statement_log(ps, BLOG_INFO, "down");
  1059. // set state CHILD
  1060. ps->state = SSTATE_CHILD;
  1061. // update AP
  1062. if (p->ap > ps->i + 1) {
  1063. p->ap = ps->i + 1;
  1064. }
  1065. } break;
  1066. case NCDMODULE_EVENT_DEAD: {
  1067. int is_error = NCDModuleInst_HaveError(&ps->inst);
  1068. if (is_error) {
  1069. process_statement_log(ps, BLOG_ERROR, "died with error");
  1070. } else {
  1071. process_statement_log(ps, BLOG_INFO, "died");
  1072. }
  1073. // free instance
  1074. NCDModuleInst_Free(&ps->inst);
  1075. // free instance arguments
  1076. NCDValue_Free(&ps->inst_args);
  1077. // set state FORGOTTEN
  1078. ps->state = SSTATE_FORGOTTEN;
  1079. // set error
  1080. if (is_error) {
  1081. process_statement_set_error(ps);
  1082. }
  1083. // update AP
  1084. if (p->ap > ps->i) {
  1085. p->ap = ps->i;
  1086. }
  1087. // update FP
  1088. while (p->fp > 0 && p->statements[p->fp - 1].state == SSTATE_FORGOTTEN) {
  1089. p->fp--;
  1090. }
  1091. } break;
  1092. }
  1093. }
  1094. int process_statement_instance_func_getobj (struct process_statement *ps, const char *objname, NCDObject *out_object)
  1095. {
  1096. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1097. return process_find_object(ps->p, ps->i, objname, out_object);
  1098. }
  1099. int process_statement_instance_func_initprocess (struct process_statement *ps, NCDModuleProcess *mp, const char *template_name)
  1100. {
  1101. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1102. // find template
  1103. NCDProcess *p_ast;
  1104. NCDInterpBlock *iblock;
  1105. if (!NCDInterpProg_FindProcess(&iprogram, template_name, &p_ast, &iblock) || !NCDProcess_IsTemplate(p_ast)) {
  1106. process_statement_log(ps, BLOG_ERROR, "no template named %s", template_name);
  1107. return 0;
  1108. }
  1109. // create process
  1110. if (!process_new(p_ast, iblock, mp)) {
  1111. process_statement_log(ps, BLOG_ERROR, "failed to create process from template %s", template_name);
  1112. return 0;
  1113. }
  1114. process_statement_log(ps, BLOG_INFO, "created process from template %s", template_name);
  1115. return 1;
  1116. }
  1117. void process_statement_instance_logfunc (struct process_statement *ps)
  1118. {
  1119. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1120. process_statement_logfunc(ps);
  1121. BLog_Append("module: ");
  1122. }
  1123. void process_statement_instance_func_interp_exit (struct process_statement *ps, int exit_code)
  1124. {
  1125. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1126. start_terminate(exit_code);
  1127. }
  1128. int process_statement_instance_func_interp_getargs (struct process_statement *ps, NCDValue *out_value)
  1129. {
  1130. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1131. NCDValue_InitList(out_value);
  1132. for (int i = 0; i < options.num_extra_args; i++) {
  1133. NCDValue arg;
  1134. if (!NCDValue_InitString(&arg, options.extra_args[i])) {
  1135. process_statement_log(ps, BLOG_ERROR, "NCDValue_InitString failed");
  1136. goto fail1;
  1137. }
  1138. if (!NCDValue_ListAppend(out_value, arg)) {
  1139. process_statement_log(ps, BLOG_ERROR, "NCDValue_ListAppend failed");
  1140. NCDValue_Free(&arg);
  1141. goto fail1;
  1142. }
  1143. }
  1144. return 1;
  1145. fail1:
  1146. NCDValue_Free(out_value);
  1147. return 0;
  1148. }
  1149. btime_t process_statement_instance_func_interp_getretrytime (struct process_statement *ps)
  1150. {
  1151. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1152. return options.retry_time;
  1153. }
  1154. void process_moduleprocess_func_event (struct process *p, int event)
  1155. {
  1156. ASSERT(p->module_process)
  1157. switch (event) {
  1158. case NCDMODULEPROCESS_INTERP_EVENT_CONTINUE: {
  1159. ASSERT(p->state == PSTATE_WAITING)
  1160. // set state working
  1161. p->state = PSTATE_WORKING;
  1162. // schedule work
  1163. process_schedule_work(p);
  1164. } break;
  1165. case NCDMODULEPROCESS_INTERP_EVENT_TERMINATE: {
  1166. ASSERT(p->state != PSTATE_TERMINATING)
  1167. process_log(p, BLOG_INFO, "process termination requested");
  1168. // start terminating
  1169. process_start_terminating(p);
  1170. } break;
  1171. default: ASSERT(0);
  1172. }
  1173. }
  1174. int process_moduleprocess_func_getobj (struct process *p, const char *name, NCDObject *out_object)
  1175. {
  1176. ASSERT(p->module_process)
  1177. return process_find_object(p, p->num_statements, name, out_object);
  1178. }