ncd.c 42 KB

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