ncd.c 41 KB

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