ncd.c 42 KB

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