ncd.c 41 KB

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