ncd.c 41 KB

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