ncd.c 44 KB

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