ncd.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  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/loggers_string.h>
  35. #include <misc/loglevel.h>
  36. #include <misc/offset.h>
  37. #include <misc/read_file.h>
  38. #include <misc/balloc.h>
  39. #include <misc/concat_strings.h>
  40. #include <misc/string_begins_with.h>
  41. #include <misc/parse_number.h>
  42. #include <misc/open_standard_streams.h>
  43. #include <misc/expstring.h>
  44. #include <structure/LinkedList1.h>
  45. #include <structure/LinkedList2.h>
  46. #include <base/BLog.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/modules/modules.h>
  56. #ifndef BADVPN_USE_WINAPI
  57. #include <base/BLog_syslog.h>
  58. #endif
  59. #include <ncd/ncd.h>
  60. #include <generated/blog_channel_ncd.h>
  61. #define LOGGER_STDOUT 1
  62. #define LOGGER_SYSLOG 2
  63. #define ARG_VALUE_TYPE_STRING 1
  64. #define ARG_VALUE_TYPE_VARIABLE 2
  65. #define ARG_VALUE_TYPE_LIST 3
  66. #define SSTATE_CHILD 1
  67. #define SSTATE_ADULT 2
  68. #define SSTATE_DYING 3
  69. #define SSTATE_FORGOTTEN 4
  70. #define PSTATE_WORKING 1
  71. #define PSTATE_UP 2
  72. #define PSTATE_WAITING 3
  73. #define PSTATE_TERMINATING 4
  74. struct arg_value {
  75. int type;
  76. union {
  77. char *string;
  78. char **variable_names;
  79. LinkedList1 list;
  80. };
  81. };
  82. struct arg_list_elem {
  83. LinkedList1Node list_node;
  84. struct arg_value value;
  85. };
  86. struct statement {
  87. char **object_names;
  88. char *method_name;
  89. struct arg_value args;
  90. char *name;
  91. };
  92. struct process {
  93. NCDModuleProcess *module_process;
  94. char *name;
  95. size_t num_statements;
  96. struct process_statement *statements;
  97. int state;
  98. size_t ap;
  99. size_t fp;
  100. BTimer wait_timer;
  101. BPending advance_job;
  102. BPending work_job;
  103. LinkedList2Node list_node; // node in processes
  104. };
  105. struct process_statement {
  106. struct process *p;
  107. size_t i;
  108. struct statement s;
  109. int state;
  110. const struct NCDModule *module;
  111. int have_error;
  112. btime_t error_until;
  113. NCDModuleInst inst;
  114. NCDValue inst_args;
  115. };
  116. // command-line options
  117. struct {
  118. int help;
  119. int version;
  120. int logger;
  121. #ifndef BADVPN_USE_WINAPI
  122. char *logger_syslog_facility;
  123. char *logger_syslog_ident;
  124. #endif
  125. int loglevel;
  126. int loglevels[BLOG_NUM_CHANNELS];
  127. char *config_file;
  128. int retry_time;
  129. int no_udev;
  130. } options;
  131. // reactor
  132. BReactor ss;
  133. // are we terminating
  134. int terminating;
  135. // process manager
  136. BProcessManager manager;
  137. // udev manager
  138. NCDUdevManager umanager;
  139. // module index
  140. NCDModuleIndex mindex;
  141. // config AST
  142. struct NCDConfig_processes *config_ast;
  143. // processes
  144. LinkedList2 processes;
  145. static void print_help (const char *name);
  146. static void print_version (void);
  147. static int parse_arguments (int argc, char *argv[]);
  148. static void signal_handler (void *unused);
  149. static int arg_value_init_string (struct arg_value *o, const char *string);
  150. static int arg_value_init_variable (struct arg_value *o, struct NCDConfig_strings *ast_names);
  151. static void arg_value_init_list (struct arg_value *o);
  152. static int arg_value_list_append (struct arg_value *o, struct arg_value v);
  153. static void arg_value_free (struct arg_value *o);
  154. static int build_arg_list_from_ast_list (struct arg_value *o, struct NCDConfig_list *list);
  155. static char ** names_new (struct NCDConfig_strings *ast_names);
  156. static size_t names_count (char **names);
  157. static char * names_tostring (char **names);
  158. static void names_free (char **names);
  159. static int statement_init (struct statement *s, struct NCDConfig_statements *conf);
  160. static void statement_free (struct statement *s);
  161. static int process_new (struct NCDConfig_processes *conf, NCDModuleProcess *module_process);
  162. static void process_free (struct process *p);
  163. static void process_start_terminating (struct process *p);
  164. static void process_free_statements (struct process *p);
  165. static size_t process_rap (struct process *p);
  166. static void process_assert_pointers (struct process *p);
  167. static void process_logfunc (struct process *p);
  168. static void process_log (struct process *p, int level, const char *fmt, ...);
  169. static void process_schedule_work (struct process *p);
  170. static void process_work_job_handler (struct process *p);
  171. static void process_advance_job_handler (struct process *p);
  172. static void process_wait_timer_handler (struct process *p);
  173. static int process_find_object (struct process *p, size_t pos, const char *name, NCDObject *out_object);
  174. static int process_resolve_object_expr (struct process *p, size_t pos, char **names, NCDObject *out_object);
  175. static int process_resolve_variable_expr (struct process *p, size_t pos, char **names, NCDValue *out_value);
  176. static void process_statement_logfunc (struct process_statement *ps);
  177. static void process_statement_log (struct process_statement *ps, int level, const char *fmt, ...);
  178. static void process_statement_set_error (struct process_statement *ps);
  179. static int process_statement_resolve_argument (struct process_statement *ps, struct arg_value *arg, NCDValue *out);
  180. static void process_statement_instance_func_event (struct process_statement *ps, int event);
  181. static int process_statement_instance_func_getobj (struct process_statement *ps, const char *objname, NCDObject *out_object);
  182. static int process_statement_instance_func_initprocess (struct process_statement *ps, NCDModuleProcess *mp, const char *template_name);
  183. static void process_statement_instance_logfunc (struct process_statement *ps);
  184. static void process_moduleprocess_func_event (struct process *p, int event);
  185. static int process_moduleprocess_func_getobj (struct process *p, const char *name, NCDObject *out_object);
  186. int main (int argc, char **argv)
  187. {
  188. if (argc <= 0) {
  189. return 1;
  190. }
  191. // open standard streams
  192. open_standard_streams();
  193. // parse command-line arguments
  194. if (!parse_arguments(argc, argv)) {
  195. fprintf(stderr, "Failed to parse arguments\n");
  196. print_help(argv[0]);
  197. goto fail0;
  198. }
  199. // handle --help and --version
  200. if (options.help) {
  201. print_version();
  202. print_help(argv[0]);
  203. return 0;
  204. }
  205. if (options.version) {
  206. print_version();
  207. return 0;
  208. }
  209. // initialize logger
  210. switch (options.logger) {
  211. case LOGGER_STDOUT:
  212. BLog_InitStdout();
  213. break;
  214. #ifndef BADVPN_USE_WINAPI
  215. case LOGGER_SYSLOG:
  216. if (!BLog_InitSyslog(options.logger_syslog_ident, options.logger_syslog_facility)) {
  217. fprintf(stderr, "Failed to initialize syslog logger\n");
  218. goto fail0;
  219. }
  220. break;
  221. #endif
  222. default:
  223. ASSERT(0);
  224. }
  225. // configure logger channels
  226. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  227. if (options.loglevels[i] >= 0) {
  228. BLog_SetChannelLoglevel(i, options.loglevels[i]);
  229. }
  230. else if (options.loglevel >= 0) {
  231. BLog_SetChannelLoglevel(i, options.loglevel);
  232. }
  233. }
  234. BLog(BLOG_NOTICE, "initializing "GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION);
  235. // initialize network
  236. if (!BNetwork_GlobalInit()) {
  237. BLog(BLOG_ERROR, "BNetwork_GlobalInit failed");
  238. goto fail1;
  239. }
  240. // init time
  241. BTime_Init();
  242. // init reactor
  243. if (!BReactor_Init(&ss)) {
  244. BLog(BLOG_ERROR, "BReactor_Init failed");
  245. goto fail1;
  246. }
  247. // set not terminating
  248. terminating = 0;
  249. // init process manager
  250. if (!BProcessManager_Init(&manager, &ss)) {
  251. BLog(BLOG_ERROR, "BProcessManager_Init failed");
  252. goto fail1a;
  253. }
  254. // init udev manager
  255. NCDUdevManager_Init(&umanager, options.no_udev, &ss, &manager);
  256. // init module index
  257. NCDModuleIndex_Init(&mindex);
  258. // add module groups to index
  259. for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
  260. if (!NCDModuleIndex_AddGroup(&mindex, *g)) {
  261. BLog(BLOG_ERROR, "NCDModuleIndex_AddGroup failed");
  262. goto fail2;
  263. }
  264. }
  265. // setup signal handler
  266. if (!BSignal_Init(&ss, signal_handler, NULL)) {
  267. BLog(BLOG_ERROR, "BSignal_Init failed");
  268. goto fail2;
  269. }
  270. // read config file
  271. uint8_t *file;
  272. size_t file_len;
  273. if (!read_file(options.config_file, &file, &file_len)) {
  274. BLog(BLOG_ERROR, "failed to read config file");
  275. goto fail3;
  276. }
  277. // parse config file
  278. if (!NCDConfigParser_Parse((char *)file, file_len, &config_ast)) {
  279. BLog(BLOG_ERROR, "NCDConfigParser_Parse failed");
  280. free(file);
  281. goto fail3;
  282. }
  283. // fee config file memory
  284. free(file);
  285. // init module params
  286. struct NCDModuleInitParams params;
  287. params.reactor = &ss;
  288. params.manager = &manager;
  289. params.umanager = &umanager;
  290. // init modules
  291. size_t num_inited_modules = 0;
  292. for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
  293. if ((*g)->func_globalinit && !(*g)->func_globalinit(params)) {
  294. BLog(BLOG_ERROR, "globalinit failed for some module");
  295. goto fail5;
  296. }
  297. num_inited_modules++;
  298. }
  299. // init processes list
  300. LinkedList2_Init(&processes);
  301. // init processes
  302. struct NCDConfig_processes *conf = config_ast;
  303. while (conf) {
  304. if (!conf->is_template) {
  305. process_new(conf, NULL);
  306. }
  307. conf = conf->next;
  308. }
  309. // enter event loop
  310. BLog(BLOG_NOTICE, "entering event loop");
  311. BReactor_Exec(&ss);
  312. ASSERT(LinkedList2_IsEmpty(&processes))
  313. fail5:
  314. // free modules
  315. while (num_inited_modules > 0) {
  316. const struct NCDModuleGroup **g = &ncd_modules[num_inited_modules - 1];
  317. if ((*g)->func_globalfree) {
  318. (*g)->func_globalfree();
  319. }
  320. num_inited_modules--;
  321. }
  322. // free configuration
  323. NCDConfig_free_processes(config_ast);
  324. fail3:
  325. // remove signal handler
  326. BSignal_Finish();
  327. fail2:
  328. // free module index
  329. NCDModuleIndex_Free(&mindex);
  330. // free udev manager
  331. NCDUdevManager_Free(&umanager);
  332. // free process manager
  333. BProcessManager_Free(&manager);
  334. fail1a:
  335. // free reactor
  336. BReactor_Free(&ss);
  337. fail1:
  338. // free logger
  339. BLog(BLOG_NOTICE, "exiting");
  340. BLog_Free();
  341. fail0:
  342. // finish objects
  343. DebugObjectGlobal_Finish();
  344. return 1;
  345. }
  346. void print_help (const char *name)
  347. {
  348. printf(
  349. "Usage:\n"
  350. " %s\n"
  351. " [--help]\n"
  352. " [--version]\n"
  353. " [--logger <"LOGGERS_STRING">]\n"
  354. #ifndef BADVPN_USE_WINAPI
  355. " (logger=syslog?\n"
  356. " [--syslog-facility <string>]\n"
  357. " [--syslog-ident <string>]\n"
  358. " )\n"
  359. #endif
  360. " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
  361. " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
  362. " --config-file <file>\n"
  363. " [--retry-time <ms>]\n"
  364. " [--no-udev]\n",
  365. name
  366. );
  367. }
  368. void print_version (void)
  369. {
  370. printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
  371. }
  372. int parse_arguments (int argc, char *argv[])
  373. {
  374. if (argc <= 0) {
  375. return 0;
  376. }
  377. options.help = 0;
  378. options.version = 0;
  379. options.logger = LOGGER_STDOUT;
  380. #ifndef BADVPN_USE_WINAPI
  381. options.logger_syslog_facility = "daemon";
  382. options.logger_syslog_ident = argv[0];
  383. #endif
  384. options.loglevel = -1;
  385. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  386. options.loglevels[i] = -1;
  387. }
  388. options.config_file = NULL;
  389. options.retry_time = DEFAULT_RETRY_TIME;
  390. options.no_udev = 0;
  391. for (int i = 1; i < argc; i++) {
  392. char *arg = argv[i];
  393. if (!strcmp(arg, "--help")) {
  394. options.help = 1;
  395. }
  396. else if (!strcmp(arg, "--version")) {
  397. options.version = 1;
  398. }
  399. else if (!strcmp(arg, "--logger")) {
  400. if (1 >= argc - i) {
  401. fprintf(stderr, "%s: requires an argument\n", arg);
  402. return 0;
  403. }
  404. char *arg2 = argv[i + 1];
  405. if (!strcmp(arg2, "stdout")) {
  406. options.logger = LOGGER_STDOUT;
  407. }
  408. #ifndef BADVPN_USE_WINAPI
  409. else if (!strcmp(arg2, "syslog")) {
  410. options.logger = LOGGER_SYSLOG;
  411. }
  412. #endif
  413. else {
  414. fprintf(stderr, "%s: wrong argument\n", arg);
  415. return 0;
  416. }
  417. i++;
  418. }
  419. #ifndef BADVPN_USE_WINAPI
  420. else if (!strcmp(arg, "--syslog-facility")) {
  421. if (1 >= argc - i) {
  422. fprintf(stderr, "%s: requires an argument\n", arg);
  423. return 0;
  424. }
  425. options.logger_syslog_facility = argv[i + 1];
  426. i++;
  427. }
  428. else if (!strcmp(arg, "--syslog-ident")) {
  429. if (1 >= argc - i) {
  430. fprintf(stderr, "%s: requires an argument\n", arg);
  431. return 0;
  432. }
  433. options.logger_syslog_ident = argv[i + 1];
  434. i++;
  435. }
  436. #endif
  437. else if (!strcmp(arg, "--loglevel")) {
  438. if (1 >= argc - i) {
  439. fprintf(stderr, "%s: requires an argument\n", arg);
  440. return 0;
  441. }
  442. if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
  443. fprintf(stderr, "%s: wrong argument\n", arg);
  444. return 0;
  445. }
  446. i++;
  447. }
  448. else if (!strcmp(arg, "--channel-loglevel")) {
  449. if (2 >= argc - i) {
  450. fprintf(stderr, "%s: requires two arguments\n", arg);
  451. return 0;
  452. }
  453. int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
  454. if (channel < 0) {
  455. fprintf(stderr, "%s: wrong channel argument\n", arg);
  456. return 0;
  457. }
  458. int loglevel = parse_loglevel(argv[i + 2]);
  459. if (loglevel < 0) {
  460. fprintf(stderr, "%s: wrong loglevel argument\n", arg);
  461. return 0;
  462. }
  463. options.loglevels[channel] = loglevel;
  464. i += 2;
  465. }
  466. else if (!strcmp(arg, "--config-file")) {
  467. if (1 >= argc - i) {
  468. fprintf(stderr, "%s: requires an argument\n", arg);
  469. return 0;
  470. }
  471. options.config_file = argv[i + 1];
  472. i++;
  473. }
  474. else if (!strcmp(arg, "--retry-time")) {
  475. if (1 >= argc - i) {
  476. fprintf(stderr, "%s: requires an argument\n", arg);
  477. return 0;
  478. }
  479. if ((options.retry_time = atoi(argv[i + 1])) < 0) {
  480. fprintf(stderr, "%s: wrong argument\n", arg);
  481. return 0;
  482. }
  483. i++;
  484. }
  485. else if (!strcmp(arg, "--no-udev")) {
  486. options.no_udev = 1;
  487. }
  488. else {
  489. fprintf(stderr, "unknown option: %s\n", arg);
  490. return 0;
  491. }
  492. }
  493. if (options.help || options.version) {
  494. return 1;
  495. }
  496. if (!options.config_file) {
  497. fprintf(stderr, "--config-file is required\n");
  498. return 0;
  499. }
  500. return 1;
  501. }
  502. void signal_handler (void *unused)
  503. {
  504. BLog(BLOG_NOTICE, "termination requested");
  505. if (terminating) {
  506. return;
  507. }
  508. terminating = 1;
  509. if (LinkedList2_IsEmpty(&processes)) {
  510. BReactor_Quit(&ss, 1);
  511. return;
  512. }
  513. // start terminating non-template processes
  514. LinkedList2Iterator it;
  515. LinkedList2Iterator_InitForward(&it, &processes);
  516. LinkedList2Node *n;
  517. while (n = LinkedList2Iterator_Next(&it)) {
  518. struct process *p = UPPER_OBJECT(n, struct process, list_node);
  519. if (p->module_process) {
  520. continue;
  521. }
  522. if (p->state != PSTATE_TERMINATING) {
  523. process_start_terminating(p);
  524. }
  525. }
  526. }
  527. int arg_value_init_string (struct arg_value *o, const char *string)
  528. {
  529. o->type = ARG_VALUE_TYPE_STRING;
  530. if (!(o->string = strdup(string))) {
  531. BLog(BLOG_ERROR, "strdup failed");
  532. return 0;
  533. }
  534. return 1;
  535. }
  536. int arg_value_init_variable (struct arg_value *o, struct NCDConfig_strings *ast_names)
  537. {
  538. ASSERT(ast_names)
  539. o->type = ARG_VALUE_TYPE_VARIABLE;
  540. if (!(o->variable_names = names_new(ast_names))) {
  541. return 0;
  542. }
  543. return 1;
  544. }
  545. void arg_value_init_list (struct arg_value *o)
  546. {
  547. o->type = ARG_VALUE_TYPE_LIST;
  548. LinkedList1_Init(&o->list);
  549. }
  550. int arg_value_list_append (struct arg_value *o, struct arg_value v)
  551. {
  552. ASSERT(o->type == ARG_VALUE_TYPE_LIST)
  553. struct arg_list_elem *elem = malloc(sizeof(*elem));
  554. if (!elem) {
  555. BLog(BLOG_ERROR, "malloc failed");
  556. return 0;
  557. }
  558. LinkedList1_Append(&o->list, &elem->list_node);
  559. elem->value = v;
  560. return 1;
  561. }
  562. void arg_value_free (struct arg_value *o)
  563. {
  564. switch (o->type) {
  565. case ARG_VALUE_TYPE_STRING: {
  566. free(o->string);
  567. } break;
  568. case ARG_VALUE_TYPE_VARIABLE: {
  569. names_free(o->variable_names);
  570. } break;
  571. case ARG_VALUE_TYPE_LIST: {
  572. while (!LinkedList1_IsEmpty(&o->list)) {
  573. struct arg_list_elem *elem = UPPER_OBJECT(LinkedList1_GetFirst(&o->list), struct arg_list_elem, list_node);
  574. arg_value_free(&elem->value);
  575. LinkedList1_Remove(&o->list, &elem->list_node);
  576. free(elem);
  577. }
  578. } break;
  579. default: ASSERT(0);
  580. }
  581. }
  582. int build_arg_list_from_ast_list (struct arg_value *o, struct NCDConfig_list *list)
  583. {
  584. arg_value_init_list(o);
  585. for (struct NCDConfig_list *c = list; c; c = c->next) {
  586. struct arg_value e;
  587. switch (c->type) {
  588. case NCDCONFIG_ARG_STRING: {
  589. if (!arg_value_init_string(&e, c->string)) {
  590. goto fail;
  591. }
  592. } break;
  593. case NCDCONFIG_ARG_VAR: {
  594. if (!arg_value_init_variable(&e, c->var)) {
  595. goto fail;
  596. }
  597. } break;
  598. case NCDCONFIG_ARG_LIST: {
  599. if (!build_arg_list_from_ast_list(&e, c->list)) {
  600. goto fail;
  601. }
  602. } break;
  603. default: ASSERT(0);
  604. }
  605. if (!arg_value_list_append(o, e)) {
  606. arg_value_free(&e);
  607. goto fail;
  608. }
  609. }
  610. return 1;
  611. fail:
  612. arg_value_free(o);
  613. return 0;
  614. }
  615. static char ** names_new (struct NCDConfig_strings *ast_names)
  616. {
  617. ASSERT(ast_names)
  618. bsize_t size = bsize_fromsize(1);
  619. for (struct NCDConfig_strings *n = ast_names; n; n = n->next) {
  620. size = bsize_add(size, bsize_fromsize(1));
  621. }
  622. char **names;
  623. if (size.is_overflow || !(names = BAllocArray(size.value, sizeof(names[0])))) {
  624. BLog(BLOG_ERROR, "BAllocArray failed");
  625. goto fail0;
  626. }
  627. size_t i = 0;
  628. for (struct NCDConfig_strings *n = ast_names; n; n = n->next) {
  629. if (!(names[i] = strdup(n->value))) {
  630. BLog(BLOG_ERROR, "strdup failed");
  631. goto fail1;
  632. }
  633. i++;
  634. }
  635. names[i] = NULL;
  636. return names;
  637. fail1:
  638. while (i-- > 0) {
  639. free(names[i]);
  640. }
  641. BFree(names);
  642. fail0:
  643. return NULL;
  644. }
  645. static size_t names_count (char **names)
  646. {
  647. ASSERT(names)
  648. size_t i;
  649. for (i = 0; names[i]; i++);
  650. return i;
  651. }
  652. static char * names_tostring (char **names)
  653. {
  654. ASSERT(names)
  655. ExpString str;
  656. if (!ExpString_Init(&str)) {
  657. goto fail0;
  658. }
  659. for (size_t i = 0; names[i]; i++) {
  660. if (i > 0 && !ExpString_AppendChar(&str, '.')) {
  661. goto fail1;
  662. }
  663. if (!ExpString_Append(&str, names[i])) {
  664. goto fail1;
  665. }
  666. }
  667. return ExpString_Get(&str);
  668. fail1:
  669. ExpString_Free(&str);
  670. fail0:
  671. return NULL;
  672. }
  673. static void names_free (char **names)
  674. {
  675. ASSERT(names)
  676. size_t i = names_count(names);
  677. while (i-- > 0) {
  678. free(names[i]);
  679. }
  680. BFree(names);
  681. }
  682. int statement_init (struct statement *s, struct NCDConfig_statements *conf)
  683. {
  684. // set object names
  685. if (conf->objname) {
  686. if (!(s->object_names = names_new(conf->objname))) {
  687. goto fail0;
  688. }
  689. } else {
  690. s->object_names = NULL;
  691. }
  692. // set method name
  693. if (!(s->method_name = NCDConfig_concat_strings(conf->names))) {
  694. BLog(BLOG_ERROR, "NCDConfig_concat_strings failed");
  695. goto fail1;
  696. }
  697. // init name
  698. if (conf->name) {
  699. if (!(s->name = strdup(conf->name))) {
  700. BLog(BLOG_ERROR, "strdup failed");
  701. goto fail2;
  702. }
  703. } else {
  704. s->name = NULL;
  705. }
  706. // init arguments
  707. if (!build_arg_list_from_ast_list(&s->args, conf->args)) {
  708. BLog(BLOG_ERROR, "build_arg_list_from_ast_list failed");
  709. goto fail3;
  710. }
  711. return 1;
  712. fail3:
  713. free(s->name);
  714. fail2:
  715. free(s->method_name);
  716. fail1:
  717. if (s->object_names) {
  718. names_free(s->object_names);
  719. }
  720. fail0:
  721. return 0;
  722. }
  723. void statement_free (struct statement *s)
  724. {
  725. // free arguments
  726. arg_value_free(&s->args);
  727. // free name
  728. free(s->name);
  729. // free method name
  730. free(s->method_name);
  731. // free object names
  732. if (s->object_names) {
  733. names_free(s->object_names);
  734. }
  735. }
  736. int process_new (struct NCDConfig_processes *conf, NCDModuleProcess *module_process)
  737. {
  738. // allocate strucure
  739. struct process *p = malloc(sizeof(*p));
  740. if (!p) {
  741. BLog(BLOG_ERROR, "malloc failed");
  742. goto fail0;
  743. }
  744. // set module process
  745. p->module_process = module_process;
  746. // set module process handlers
  747. if (p->module_process) {
  748. NCDModuleProcess_Interp_SetHandlers(p->module_process, p,
  749. (NCDModuleProcess_interp_func_event)process_moduleprocess_func_event,
  750. (NCDModuleProcess_interp_func_getobj)process_moduleprocess_func_getobj);
  751. }
  752. // init name
  753. if (!(p->name = strdup(conf->name))) {
  754. BLog(BLOG_ERROR, "strdup failed");
  755. goto fail1;
  756. }
  757. // count statements
  758. size_t num_st = 0;
  759. struct NCDConfig_statements *st = conf->statements;
  760. while (st) {
  761. if (num_st == SIZE_MAX) {
  762. BLog(BLOG_ERROR, "too many statements");
  763. goto fail2;
  764. }
  765. num_st++;
  766. st = st->next;
  767. }
  768. // allocate statements array
  769. if (!(p->statements = BAllocArray(num_st, sizeof(p->statements[0])))) {
  770. goto fail2;
  771. }
  772. p->num_statements = 0;
  773. // init statements
  774. st = conf->statements;
  775. while (st) {
  776. struct process_statement *ps = &p->statements[p->num_statements];
  777. ps->p = p;
  778. ps->i = p->num_statements;
  779. if (!statement_init(&ps->s, st)) {
  780. goto fail3;
  781. }
  782. ps->state = SSTATE_FORGOTTEN;
  783. ps->have_error = 0;
  784. p->num_statements++;
  785. st = st->next;
  786. }
  787. // set state working
  788. p->state = PSTATE_WORKING;
  789. // set AP=0
  790. p->ap = 0;
  791. // set FP=0
  792. p->fp = 0;
  793. // init timer
  794. BTimer_Init(&p->wait_timer, 0, (BTimer_handler)process_wait_timer_handler, p);
  795. // init advance job
  796. BPending_Init(&p->advance_job, BReactor_PendingGroup(&ss), (BPending_handler)process_advance_job_handler, p);
  797. // init work job
  798. BPending_Init(&p->work_job, BReactor_PendingGroup(&ss), (BPending_handler)process_work_job_handler, p);
  799. // insert to processes list
  800. LinkedList2_Append(&processes, &p->list_node);
  801. // schedule work
  802. BPending_Set(&p->work_job);
  803. return 1;
  804. fail3:
  805. process_free_statements(p);
  806. fail2:
  807. free(p->name);
  808. fail1:
  809. free(p);
  810. fail0:
  811. BLog(BLOG_ERROR, "failed to initialize process %s", conf->name);
  812. return 0;
  813. }
  814. void process_free (struct process *p)
  815. {
  816. ASSERT(p->ap == 0)
  817. ASSERT(p->fp == 0)
  818. ASSERT(p->state == PSTATE_TERMINATING)
  819. // inform module process that the process is terminated
  820. if (p->module_process) {
  821. NCDModuleProcess_Interp_Terminated(p->module_process);
  822. }
  823. // remove from processes list
  824. LinkedList2_Remove(&processes, &p->list_node);
  825. // free work job
  826. BPending_Free(&p->work_job);
  827. // free advance job
  828. BPending_Free(&p->advance_job);
  829. // free timer
  830. BReactor_RemoveTimer(&ss, &p->wait_timer);
  831. // free statements
  832. process_free_statements(p);
  833. // free name
  834. free(p->name);
  835. // free strucure
  836. free(p);
  837. }
  838. void process_start_terminating (struct process *p)
  839. {
  840. ASSERT(p->state != PSTATE_TERMINATING)
  841. // set terminating
  842. p->state = PSTATE_TERMINATING;
  843. // schedule work
  844. process_schedule_work(p);
  845. }
  846. size_t process_rap (struct process *p)
  847. {
  848. if (p->ap > 0 && p->statements[p->ap - 1].state == SSTATE_CHILD) {
  849. return (p->ap - 1);
  850. } else {
  851. return p->ap;
  852. }
  853. }
  854. void process_free_statements (struct process *p)
  855. {
  856. // free statments
  857. while (p->num_statements > 0) {
  858. statement_free(&p->statements[p->num_statements - 1].s);
  859. p->num_statements--;
  860. }
  861. // free stataments array
  862. free(p->statements);
  863. }
  864. void process_assert_pointers (struct process *p)
  865. {
  866. ASSERT(p->ap <= p->num_statements)
  867. ASSERT(p->fp >= p->ap)
  868. ASSERT(p->fp <= p->num_statements)
  869. #ifndef NDEBUG
  870. // check AP
  871. for (size_t i = 0; i < p->ap; i++) {
  872. if (i == p->ap - 1) {
  873. ASSERT(p->statements[i].state == SSTATE_ADULT || p->statements[i].state == SSTATE_CHILD)
  874. } else {
  875. ASSERT(p->statements[i].state == SSTATE_ADULT)
  876. }
  877. }
  878. // check FP
  879. size_t fp = p->num_statements;
  880. while (fp > 0 && p->statements[fp - 1].state == SSTATE_FORGOTTEN) {
  881. fp--;
  882. }
  883. ASSERT(p->fp == fp)
  884. #endif
  885. }
  886. void process_logfunc (struct process *p)
  887. {
  888. BLog_Append("process %s: ", p->name);
  889. }
  890. void process_log (struct process *p, int level, const char *fmt, ...)
  891. {
  892. va_list vl;
  893. va_start(vl, fmt);
  894. BLog_LogViaFuncVarArg((BLog_logfunc)process_logfunc, p, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  895. va_end(vl);
  896. }
  897. void process_schedule_work (struct process *p)
  898. {
  899. process_assert_pointers(p);
  900. // stop timer
  901. BReactor_RemoveTimer(&ss, &p->wait_timer);
  902. // stop advance job
  903. BPending_Unset(&p->advance_job);
  904. // schedule work
  905. BPending_Set(&p->work_job);
  906. }
  907. void process_work_job_handler (struct process *p)
  908. {
  909. process_assert_pointers(p);
  910. ASSERT(!BTimer_IsRunning(&p->wait_timer))
  911. ASSERT(!BPending_IsSet(&p->advance_job))
  912. if (p->state == PSTATE_WAITING) {
  913. return;
  914. }
  915. if (p->state == PSTATE_TERMINATING) {
  916. if (p->fp == 0) {
  917. // finished retreating
  918. process_free(p);
  919. // if program is terminating amd there are no more processes, exit program
  920. if (terminating && LinkedList2_IsEmpty(&processes)) {
  921. BReactor_Quit(&ss, 1);
  922. }
  923. return;
  924. }
  925. // order the last living statement to die, if needed
  926. struct process_statement *ps = &p->statements[p->fp - 1];
  927. ASSERT(ps->state != SSTATE_FORGOTTEN)
  928. if (ps->state != SSTATE_DYING) {
  929. process_statement_log(ps, BLOG_INFO, "killing");
  930. // order it to die
  931. NCDModuleInst_Die(&ps->inst);
  932. // set statement state DYING
  933. ps->state = SSTATE_DYING;
  934. // update AP
  935. if (p->ap > ps->i) {
  936. p->ap = ps->i;
  937. }
  938. }
  939. return;
  940. }
  941. // process was up but is no longer?
  942. if (p->state == PSTATE_UP && !(p->ap == process_rap(p) && p->ap == p->num_statements)) {
  943. // if we have module process, wait for its permission to continue
  944. if (p->module_process) {
  945. // set module process down
  946. NCDModuleProcess_Interp_Down(p->module_process);
  947. // set state waiting
  948. p->state = PSTATE_WAITING;
  949. return;
  950. }
  951. // set state working
  952. p->state = PSTATE_WORKING;
  953. }
  954. // cleaning up?
  955. if (p->ap < p->fp) {
  956. // order the last living statement to die, if needed
  957. struct process_statement *ps = &p->statements[p->fp - 1];
  958. if (ps->state != SSTATE_DYING) {
  959. process_statement_log(ps, BLOG_INFO, "killing");
  960. // order it to die
  961. NCDModuleInst_Die(&ps->inst);
  962. // set statement state DYING
  963. ps->state = SSTATE_DYING;
  964. }
  965. return;
  966. }
  967. // clean?
  968. if (p->ap > process_rap(p)) {
  969. ASSERT(p->ap > 0)
  970. ASSERT(p->ap <= p->num_statements)
  971. struct process_statement *ps = &p->statements[p->ap - 1];
  972. ASSERT(ps->state == SSTATE_CHILD)
  973. process_statement_log(ps, BLOG_INFO, "clean");
  974. // report clean
  975. NCDModuleInst_Clean(&ps->inst);
  976. return;
  977. }
  978. // advancing?
  979. if (p->ap < p->num_statements) {
  980. ASSERT(p->state == PSTATE_WORKING)
  981. struct process_statement *ps = &p->statements[p->ap];
  982. ASSERT(ps->state == SSTATE_FORGOTTEN)
  983. // clear expired error
  984. if (ps->have_error && ps->error_until <= btime_gettime()) {
  985. ps->have_error = 0;
  986. }
  987. if (ps->have_error) {
  988. process_statement_log(ps, BLOG_INFO, "waiting after error");
  989. // set wait timer
  990. BReactor_SetTimerAbsolute(&ss, &p->wait_timer, ps->error_until);
  991. } else {
  992. // schedule advance
  993. BPending_Set(&p->advance_job);
  994. }
  995. return;
  996. }
  997. // have we just finished?
  998. if (p->state == PSTATE_WORKING) {
  999. process_log(p, BLOG_INFO, "victory");
  1000. // set module process up
  1001. if (p->module_process) {
  1002. NCDModuleProcess_Interp_Up(p->module_process);
  1003. }
  1004. // set state up
  1005. p->state = PSTATE_UP;
  1006. }
  1007. }
  1008. void process_advance_job_handler (struct process *p)
  1009. {
  1010. process_assert_pointers(p);
  1011. ASSERT(p->ap == p->fp)
  1012. ASSERT(p->ap == process_rap(p))
  1013. ASSERT(p->ap < p->num_statements)
  1014. ASSERT(!p->statements[p->ap].have_error)
  1015. ASSERT(!BPending_IsSet(&p->work_job))
  1016. ASSERT(!BTimer_IsRunning(&p->wait_timer))
  1017. ASSERT(p->state == PSTATE_WORKING)
  1018. struct process_statement *ps = &p->statements[p->ap];
  1019. ASSERT(ps->state == SSTATE_FORGOTTEN)
  1020. process_statement_log(ps, BLOG_INFO, "initializing");
  1021. NCDObject object;
  1022. NCDObject *object_ptr = NULL;
  1023. char *type;
  1024. int free_type = 0;
  1025. if (!ps->s.object_names) {
  1026. // this is a function_call(); type is "function_call"
  1027. type = ps->s.method_name;
  1028. } else {
  1029. // this is a some.object.somewhere->method_call(); type is "base_type(some.object.somewhere)::method_call"
  1030. // get object
  1031. if (!process_resolve_object_expr(p, p->ap, ps->s.object_names, &object)) {
  1032. goto fail;
  1033. }
  1034. object_ptr = &object;
  1035. // get object type
  1036. const char *object_type = NCDObject_Type(&object);
  1037. if (!object_type) {
  1038. process_statement_log(ps, BLOG_ERROR, "cannot call method on object with no type");
  1039. goto fail;
  1040. }
  1041. // build type string
  1042. if (!(type = concat_strings(3, object_type, "::", ps->s.method_name))) {
  1043. process_statement_log(ps, BLOG_ERROR, "concat_strings failed");
  1044. goto fail;
  1045. }
  1046. free_type = 1;
  1047. }
  1048. // find module to instantiate
  1049. if (!(ps->module = NCDModuleIndex_FindModule(&mindex, type))) {
  1050. process_statement_log(ps, BLOG_ERROR, "failed to find module: %s", type);
  1051. goto fail;
  1052. }
  1053. // resolve arguments
  1054. if (!process_statement_resolve_argument(ps, &ps->s.args, &ps->inst_args)) {
  1055. process_statement_log(ps, BLOG_ERROR, "failed to resolve arguments");
  1056. goto fail;
  1057. }
  1058. // initialize module instance
  1059. NCDModuleInst_Init(
  1060. &ps->inst, ps->module, object_ptr, &ps->inst_args, &ss, &manager, &umanager, ps,
  1061. (NCDModuleInst_func_event)process_statement_instance_func_event,
  1062. (NCDModuleInst_func_getobj)process_statement_instance_func_getobj,
  1063. (NCDModuleInst_func_initprocess)process_statement_instance_func_initprocess,
  1064. (BLog_logfunc)process_statement_instance_logfunc
  1065. );
  1066. // set statement state CHILD
  1067. ps->state = SSTATE_CHILD;
  1068. // increment AP
  1069. p->ap++;
  1070. // increment FP
  1071. p->fp++;
  1072. if (free_type) {
  1073. free(type);
  1074. }
  1075. process_assert_pointers(p);
  1076. return;
  1077. fail:
  1078. if (free_type) {
  1079. free(type);
  1080. }
  1081. // mark error
  1082. process_statement_set_error(ps);
  1083. // schedule work to start the timer
  1084. process_schedule_work(p);
  1085. }
  1086. void process_wait_timer_handler (struct process *p)
  1087. {
  1088. process_assert_pointers(p);
  1089. ASSERT(p->ap == p->fp)
  1090. ASSERT(p->ap == process_rap(p))
  1091. ASSERT(p->ap < p->num_statements)
  1092. ASSERT(p->statements[p->ap].have_error)
  1093. ASSERT(!BPending_IsSet(&p->work_job))
  1094. ASSERT(!BPending_IsSet(&p->advance_job))
  1095. ASSERT(p->state == PSTATE_WORKING)
  1096. process_log(p, BLOG_INFO, "retrying");
  1097. // clear error
  1098. p->statements[p->ap].have_error = 0;
  1099. // schedule work
  1100. BPending_Set(&p->work_job);
  1101. }
  1102. int process_find_object (struct process *p, size_t pos, const char *name, NCDObject *out_object)
  1103. {
  1104. ASSERT(pos <= p->num_statements)
  1105. ASSERT(name)
  1106. ASSERT(out_object)
  1107. for (size_t i = pos; i > 0; i--) {
  1108. struct process_statement *ps = &p->statements[i - 1];
  1109. if (ps->s.name && !strcmp(ps->s.name, name)) {
  1110. if (ps->state == SSTATE_FORGOTTEN) {
  1111. process_log(p, BLOG_ERROR, "statement (%zu) is uninitialized", i - 1);
  1112. goto fail;
  1113. }
  1114. *out_object = NCDModuleInst_Object(&ps->inst);
  1115. return 1;
  1116. }
  1117. }
  1118. if (p->module_process && NCDModuleProcess_Interp_GetSpecialObj(p->module_process, name, out_object)) {
  1119. return 1;
  1120. }
  1121. fail:
  1122. return 0;
  1123. }
  1124. int process_resolve_object_expr (struct process *p, size_t pos, char **names, NCDObject *out_object)
  1125. {
  1126. ASSERT(pos <= p->num_statements)
  1127. ASSERT(names)
  1128. ASSERT(names_count(names) > 0)
  1129. ASSERT(out_object)
  1130. NCDObject object;
  1131. if (!process_find_object(p, pos, names[0], &object)) {
  1132. goto fail;
  1133. }
  1134. if (!NCDObject_ResolveObjExpr(&object, names + 1, out_object)) {
  1135. goto fail;
  1136. }
  1137. return 1;
  1138. fail:;
  1139. char *name = names_tostring(names);
  1140. process_log(p, BLOG_ERROR, "failed to resolve object (%s) from position %zu", (name ? name : ""), pos);
  1141. free(name);
  1142. return 0;
  1143. }
  1144. int process_resolve_variable_expr (struct process *p, size_t pos, char **names, NCDValue *out_value)
  1145. {
  1146. ASSERT(pos <= p->num_statements)
  1147. ASSERT(names)
  1148. ASSERT(names_count(names) > 0)
  1149. ASSERT(out_value)
  1150. NCDObject object;
  1151. if (!process_find_object(p, pos, names[0], &object)) {
  1152. goto fail;
  1153. }
  1154. if (!NCDObject_ResolveVarExpr(&object, names + 1, out_value)) {
  1155. goto fail;
  1156. }
  1157. return 1;
  1158. fail:;
  1159. char *name = names_tostring(names);
  1160. process_log(p, BLOG_ERROR, "failed to resolve variable (%s) from position %zu", (name ? name : ""), pos);
  1161. free(name);
  1162. return 0;
  1163. }
  1164. void process_statement_logfunc (struct process_statement *ps)
  1165. {
  1166. process_logfunc(ps->p);
  1167. BLog_Append("statement %zu: ", ps->i);
  1168. }
  1169. void process_statement_log (struct process_statement *ps, int level, const char *fmt, ...)
  1170. {
  1171. va_list vl;
  1172. va_start(vl, fmt);
  1173. BLog_LogViaFuncVarArg((BLog_logfunc)process_statement_logfunc, ps, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  1174. va_end(vl);
  1175. }
  1176. void process_statement_set_error (struct process_statement *ps)
  1177. {
  1178. ASSERT(ps->state == SSTATE_FORGOTTEN)
  1179. ps->have_error = 1;
  1180. ps->error_until = btime_add(btime_gettime(), options.retry_time);
  1181. }
  1182. int process_statement_resolve_argument (struct process_statement *ps, struct arg_value *arg, NCDValue *out)
  1183. {
  1184. ASSERT(ps->i <= process_rap(ps->p))
  1185. ASSERT(arg)
  1186. ASSERT(out)
  1187. switch (arg->type) {
  1188. case ARG_VALUE_TYPE_STRING: {
  1189. if (!NCDValue_InitString(out, arg->string)) {
  1190. process_statement_log(ps, BLOG_ERROR, "NCDValue_InitString failed");
  1191. return 0;
  1192. }
  1193. } break;
  1194. case ARG_VALUE_TYPE_VARIABLE: {
  1195. if (!process_resolve_variable_expr(ps->p, ps->i, arg->variable_names, out)) {
  1196. return 0;
  1197. }
  1198. } break;
  1199. case ARG_VALUE_TYPE_LIST: do {
  1200. NCDValue_InitList(out);
  1201. for (LinkedList1Node *n = LinkedList1_GetFirst(&arg->list); n; n = LinkedList1Node_Next(n)) {
  1202. struct arg_list_elem *elem = UPPER_OBJECT(n, struct arg_list_elem, list_node);
  1203. NCDValue v;
  1204. if (!process_statement_resolve_argument(ps, &elem->value, &v)) {
  1205. goto list_fail1;
  1206. }
  1207. if (!NCDValue_ListAppend(out, v)) {
  1208. process_statement_log(ps, BLOG_ERROR, "NCDValue_ListAppend failed");
  1209. NCDValue_Free(&v);
  1210. goto list_fail1;
  1211. }
  1212. }
  1213. break;
  1214. list_fail1:
  1215. NCDValue_Free(out);
  1216. return 0;
  1217. } while (0); break;
  1218. default: ASSERT(0);
  1219. }
  1220. return 1;
  1221. }
  1222. void process_statement_instance_func_event (struct process_statement *ps, int event)
  1223. {
  1224. ASSERT(ps->state == SSTATE_CHILD || ps->state == SSTATE_ADULT || ps->state == SSTATE_DYING)
  1225. struct process *p = ps->p;
  1226. process_assert_pointers(p);
  1227. // schedule work
  1228. process_schedule_work(p);
  1229. switch (event) {
  1230. case NCDMODULE_EVENT_UP: {
  1231. ASSERT(ps->state == SSTATE_CHILD)
  1232. process_statement_log(ps, BLOG_INFO, "up");
  1233. // set state ADULT
  1234. ps->state = SSTATE_ADULT;
  1235. } break;
  1236. case NCDMODULE_EVENT_DOWN: {
  1237. ASSERT(ps->state == SSTATE_ADULT)
  1238. process_statement_log(ps, BLOG_INFO, "down");
  1239. // set state CHILD
  1240. ps->state = SSTATE_CHILD;
  1241. // update AP
  1242. if (p->ap > ps->i + 1) {
  1243. p->ap = ps->i + 1;
  1244. }
  1245. } break;
  1246. case NCDMODULE_EVENT_DEAD: {
  1247. int is_error = NCDModuleInst_HaveError(&ps->inst);
  1248. if (is_error) {
  1249. process_statement_log(ps, BLOG_ERROR, "died with error");
  1250. } else {
  1251. process_statement_log(ps, BLOG_INFO, "died");
  1252. }
  1253. // free instance
  1254. NCDModuleInst_Free(&ps->inst);
  1255. // free instance arguments
  1256. NCDValue_Free(&ps->inst_args);
  1257. // set state FORGOTTEN
  1258. ps->state = SSTATE_FORGOTTEN;
  1259. // set error
  1260. if (is_error) {
  1261. process_statement_set_error(ps);
  1262. }
  1263. // update AP
  1264. if (p->ap > ps->i) {
  1265. p->ap = ps->i;
  1266. }
  1267. // update FP
  1268. while (p->fp > 0 && p->statements[p->fp - 1].state == SSTATE_FORGOTTEN) {
  1269. p->fp--;
  1270. }
  1271. } break;
  1272. }
  1273. }
  1274. int process_statement_instance_func_getobj (struct process_statement *ps, const char *objname, NCDObject *out_object)
  1275. {
  1276. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1277. return process_find_object(ps->p, ps->i, objname, out_object);
  1278. }
  1279. int process_statement_instance_func_initprocess (struct process_statement *ps, NCDModuleProcess *mp, const char *template_name)
  1280. {
  1281. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1282. // find template
  1283. struct NCDConfig_processes *conf = config_ast;
  1284. while (conf) {
  1285. if (conf->is_template && !strcmp(conf->name, template_name)) {
  1286. break;
  1287. }
  1288. conf = conf->next;
  1289. }
  1290. if (!conf) {
  1291. process_statement_log(ps, BLOG_ERROR, "no template named %s", template_name);
  1292. return 0;
  1293. }
  1294. // create process
  1295. if (!process_new(conf, mp)) {
  1296. process_statement_log(ps, BLOG_ERROR, "failed to create process from template %s", template_name);
  1297. return 0;
  1298. }
  1299. process_statement_log(ps, BLOG_INFO, "created process from template %s", template_name);
  1300. return 1;
  1301. }
  1302. void process_statement_instance_logfunc (struct process_statement *ps)
  1303. {
  1304. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1305. process_statement_logfunc(ps);
  1306. BLog_Append("module: ");
  1307. }
  1308. void process_moduleprocess_func_event (struct process *p, int event)
  1309. {
  1310. ASSERT(p->module_process)
  1311. switch (event) {
  1312. case NCDMODULEPROCESS_INTERP_EVENT_CONTINUE: {
  1313. ASSERT(p->state == PSTATE_WAITING)
  1314. // set state working
  1315. p->state = PSTATE_WORKING;
  1316. // schedule work
  1317. process_schedule_work(p);
  1318. } break;
  1319. case NCDMODULEPROCESS_INTERP_EVENT_TERMINATE: {
  1320. ASSERT(p->state != PSTATE_TERMINATING)
  1321. process_log(p, BLOG_INFO, "process termination requested");
  1322. // start terminating
  1323. process_start_terminating(p);
  1324. } break;
  1325. default: ASSERT(0);
  1326. }
  1327. }
  1328. int process_moduleprocess_func_getobj (struct process *p, const char *name, NCDObject *out_object)
  1329. {
  1330. ASSERT(p->module_process)
  1331. return process_find_object(p, p->num_statements, name, out_object);
  1332. }