ncd.c 42 KB

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