ncd.c 45 KB

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