ncd.c 46 KB

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