ncd.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  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. // check AP
  795. for (size_t i = 0; i < p->ap; i++) {
  796. if (i == p->ap - 1) {
  797. ASSERT(p->statements[i].state == SSTATE_ADULT || p->statements[i].state == SSTATE_CHILD)
  798. } else {
  799. ASSERT(p->statements[i].state == SSTATE_ADULT)
  800. }
  801. }
  802. // check FP
  803. size_t fp = p->num_statements;
  804. while (fp > 0 && p->statements[fp - 1].state == SSTATE_FORGOTTEN) {
  805. fp--;
  806. }
  807. ASSERT(p->fp == fp)
  808. }
  809. void process_logfunc (struct process *p)
  810. {
  811. BLog_Append("process %s: ", p->name);
  812. }
  813. void process_log (struct process *p, int level, const char *fmt, ...)
  814. {
  815. va_list vl;
  816. va_start(vl, fmt);
  817. BLog_LogViaFuncVarArg((BLog_logfunc)process_logfunc, p, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  818. va_end(vl);
  819. }
  820. void process_schedule_work (struct process *p)
  821. {
  822. process_assert_pointers(p);
  823. // stop timer
  824. BReactor_RemoveTimer(&ss, &p->wait_timer);
  825. // stop advance job
  826. BPending_Unset(&p->advance_job);
  827. // schedule work
  828. BPending_Set(&p->work_job);
  829. }
  830. void process_work_job_handler (struct process *p)
  831. {
  832. process_assert_pointers(p);
  833. ASSERT(!BTimer_IsRunning(&p->wait_timer))
  834. ASSERT(!BPending_IsSet(&p->advance_job))
  835. if (p->state == PSTATE_WAITING) {
  836. return;
  837. }
  838. if (p->state == PSTATE_TERMINATING) {
  839. if (p->fp == 0) {
  840. // finished retreating
  841. process_free(p);
  842. // if program is terminating amd there are no more processes, exit program
  843. if (terminating && LinkedList2_IsEmpty(&processes)) {
  844. BReactor_Quit(&ss, 1);
  845. }
  846. return;
  847. }
  848. // order the last living statement to die, if needed
  849. struct process_statement *ps = &p->statements[p->fp - 1];
  850. ASSERT(ps->state != SSTATE_FORGOTTEN)
  851. if (ps->state != SSTATE_DYING) {
  852. process_statement_log(ps, BLOG_INFO, "killing");
  853. // order it to die
  854. NCDModuleInst_Die(&ps->inst);
  855. // set statement state DYING
  856. ps->state = SSTATE_DYING;
  857. // update AP
  858. if (p->ap > ps->i) {
  859. p->ap = ps->i;
  860. }
  861. }
  862. return;
  863. }
  864. // process was up but is no longer?
  865. if (p->state == PSTATE_UP && !(p->ap == process_rap(p) && p->ap == p->num_statements)) {
  866. // if we have module process, wait for its permission to continue
  867. if (p->module_process) {
  868. // set module process down
  869. NCDModuleProcess_Interp_Down(p->module_process);
  870. // set state waiting
  871. p->state = PSTATE_WAITING;
  872. return;
  873. }
  874. // set state working
  875. p->state = PSTATE_WORKING;
  876. }
  877. // cleaning up?
  878. if (p->ap < p->fp) {
  879. // order the last living statement to die, if needed
  880. struct process_statement *ps = &p->statements[p->fp - 1];
  881. if (ps->state != SSTATE_DYING) {
  882. process_statement_log(ps, BLOG_INFO, "killing");
  883. // order it to die
  884. NCDModuleInst_Die(&ps->inst);
  885. // set statement state DYING
  886. ps->state = SSTATE_DYING;
  887. }
  888. return;
  889. }
  890. // clean?
  891. if (p->ap > process_rap(p)) {
  892. ASSERT(p->ap > 0)
  893. ASSERT(p->ap <= p->num_statements)
  894. struct process_statement *ps = &p->statements[p->ap - 1];
  895. ASSERT(ps->state == SSTATE_CHILD)
  896. process_statement_log(ps, BLOG_INFO, "clean");
  897. // report clean
  898. NCDModuleInst_Clean(&ps->inst);
  899. return;
  900. }
  901. // advancing?
  902. if (p->ap < p->num_statements) {
  903. ASSERT(p->state == PSTATE_WORKING)
  904. struct process_statement *ps = &p->statements[p->ap];
  905. ASSERT(ps->state == SSTATE_FORGOTTEN)
  906. // clear expired error
  907. if (ps->have_error && ps->error_until <= btime_gettime()) {
  908. ps->have_error = 0;
  909. }
  910. if (ps->have_error) {
  911. process_statement_log(ps, BLOG_INFO, "waiting after error");
  912. // set wait timer
  913. BReactor_SetTimerAbsolute(&ss, &p->wait_timer, ps->error_until);
  914. } else {
  915. // schedule advance
  916. BPending_Set(&p->advance_job);
  917. }
  918. return;
  919. }
  920. // have we just finished?
  921. if (p->state == PSTATE_WORKING) {
  922. process_log(p, BLOG_INFO, "victory");
  923. // set module process up
  924. if (p->module_process) {
  925. NCDModuleProcess_Interp_Up(p->module_process);
  926. }
  927. // set state up
  928. p->state = PSTATE_UP;
  929. }
  930. }
  931. void process_advance_job_handler (struct process *p)
  932. {
  933. process_assert_pointers(p);
  934. ASSERT(p->ap == p->fp)
  935. ASSERT(p->ap == process_rap(p))
  936. ASSERT(p->ap < p->num_statements)
  937. ASSERT(!p->statements[p->ap].have_error)
  938. ASSERT(!BPending_IsSet(&p->work_job))
  939. ASSERT(!BTimer_IsRunning(&p->wait_timer))
  940. ASSERT(p->state == PSTATE_WORKING)
  941. struct process_statement *ps = &p->statements[p->ap];
  942. ASSERT(ps->state == SSTATE_FORGOTTEN)
  943. process_statement_log(ps, BLOG_INFO, "initializing");
  944. NCDModuleInst *method_object = NULL;
  945. char *type;
  946. // construct type
  947. if (!ps->s.object_name) {
  948. // this is a function_call(); type is "function_call"
  949. if (!(type = strdup(ps->s.method_name))) {
  950. process_statement_log(ps, BLOG_ERROR, "strdup failed");
  951. goto fail0;
  952. }
  953. } else {
  954. // this is a some.object.somewhere->method_call(); type is "base_type(some.object.somewhere)::method_call"
  955. // resolve object
  956. struct process_statement *obj_ps = process_resolve_object(p, p->ap, ps->s.object_name);
  957. if (!obj_ps) {
  958. process_statement_log(ps, BLOG_ERROR, "failed to resolve object %s for method call", ps->s.object_name);
  959. goto fail0;
  960. }
  961. ASSERT(obj_ps->state == SSTATE_ADULT)
  962. // base type defaults to type
  963. const char *base_type = (obj_ps->module->base_type ? obj_ps->module->base_type : obj_ps->module->type);
  964. // build type string
  965. if (!(type = concat_strings(3, base_type, "::", ps->s.method_name))) {
  966. process_statement_log(ps, BLOG_ERROR, "concat_strings failed");
  967. goto fail0;
  968. }
  969. method_object = &obj_ps->inst;
  970. }
  971. // find module to instantiate
  972. if (!(ps->module = find_module(type))) {
  973. process_statement_log(ps, BLOG_ERROR, "failed to find module: %s", type);
  974. goto fail1;
  975. }
  976. // resolve arguments
  977. if (!process_statement_resolve_argument(ps, &ps->s.args, &ps->inst_args)) {
  978. process_statement_log(ps, BLOG_ERROR, "failed to resolve arguments");
  979. goto fail1;
  980. }
  981. // initialize module instance
  982. NCDModuleInst_Init(
  983. &ps->inst, ps->module, method_object, &ps->inst_args, &ss, &manager, &umanager, ps,
  984. (NCDModuleInst_func_event)process_statement_instance_func_event,
  985. (NCDModuleInst_func_getvar)process_statement_instance_func_getvar,
  986. (NCDModuleInst_func_getobj)process_statement_instance_func_getobj,
  987. (NCDModuleInst_func_initprocess)process_statement_instance_func_initprocess,
  988. (BLog_logfunc)process_statement_instance_logfunc
  989. );
  990. // set statement state CHILD
  991. ps->state = SSTATE_CHILD;
  992. // increment AP
  993. p->ap++;
  994. // increment FP
  995. p->fp++;
  996. free(type);
  997. process_assert_pointers(p);
  998. return;
  999. fail1:
  1000. free(type);
  1001. fail0:
  1002. // mark error
  1003. process_statement_set_error(ps);
  1004. // schedule work to start the timer
  1005. process_schedule_work(p);
  1006. }
  1007. void process_wait_timer_handler (struct process *p)
  1008. {
  1009. process_assert_pointers(p);
  1010. ASSERT(p->ap == p->fp)
  1011. ASSERT(p->ap == process_rap(p))
  1012. ASSERT(p->ap < p->num_statements)
  1013. ASSERT(p->statements[p->ap].have_error)
  1014. ASSERT(!BPending_IsSet(&p->work_job))
  1015. ASSERT(!BPending_IsSet(&p->advance_job))
  1016. ASSERT(p->state == PSTATE_WORKING)
  1017. process_log(p, BLOG_INFO, "retrying");
  1018. // clear error
  1019. p->statements[p->ap].have_error = 0;
  1020. // schedule work
  1021. BPending_Set(&p->work_job);
  1022. }
  1023. struct process_statement * process_find_statement (struct process *p, size_t pos, const char *name)
  1024. {
  1025. process_assert_pointers(p);
  1026. ASSERT(pos >= 0)
  1027. ASSERT(pos <= process_rap(p))
  1028. for (size_t i = pos; i > 0; i--) {
  1029. struct process_statement *ps = &p->statements[i - 1];
  1030. if (ps->s.name && !strcmp(ps->s.name, name)) {
  1031. return ps;
  1032. }
  1033. }
  1034. return NULL;
  1035. }
  1036. int process_resolve_name (struct process *p, size_t pos, const char *name, struct process_statement **first_ps, const char **rest)
  1037. {
  1038. process_assert_pointers(p);
  1039. ASSERT(pos >= 0)
  1040. ASSERT(pos <= process_rap(p))
  1041. ASSERT(name)
  1042. char *dot = strstr(name, ".");
  1043. if (!dot) {
  1044. *first_ps = process_find_statement(p, pos, name);
  1045. *rest = NULL;
  1046. } else {
  1047. // copy modname and terminate
  1048. char *modname = malloc((dot - name) + 1);
  1049. if (!modname) {
  1050. process_log(p, BLOG_ERROR, "malloc failed");
  1051. return 0;
  1052. }
  1053. memcpy(modname, name, dot - name);
  1054. modname[dot - name] = '\0';
  1055. *first_ps = process_find_statement(p, pos, modname);
  1056. *rest = dot + 1;
  1057. free(modname);
  1058. }
  1059. return 1;
  1060. }
  1061. int process_resolve_variable (struct process *p, size_t pos, const char *varname, NCDValue *out)
  1062. {
  1063. process_assert_pointers(p);
  1064. ASSERT(pos >= 0)
  1065. ASSERT(pos <= process_rap(p))
  1066. ASSERT(varname)
  1067. // find referred statement and remaining name
  1068. struct process_statement *rps;
  1069. const char *rest;
  1070. if (!process_resolve_name(p, pos, varname, &rps, &rest)) {
  1071. return 0;
  1072. }
  1073. if (!rps) {
  1074. // handle _args
  1075. if (!strcmp(varname, "_args")) {
  1076. if (!NCDValue_InitCopy(out, &p->args)) {
  1077. process_log(p, BLOG_ERROR, "NCDValue_InitCopy failed");
  1078. return 0;
  1079. }
  1080. return 1;
  1081. }
  1082. // handle _argN
  1083. size_t len;
  1084. uintmax_t n;
  1085. if ((len = string_begins_with(varname, "_arg")) && parse_unsigned_integer(varname + len, &n) && n < NCDValue_ListCount(&p->args)) {
  1086. if (!NCDValue_InitCopy(out, NCDValue_ListGet(&p->args, n))) {
  1087. process_log(p, BLOG_ERROR, "NCDValue_InitCopy failed");
  1088. return 0;
  1089. }
  1090. return 1;
  1091. }
  1092. // handle special variables
  1093. if (p->module_process) {
  1094. if (NCDModuleProcess_Interp_GetSpecialVar(p->module_process, varname, out)) {
  1095. return 1;
  1096. }
  1097. }
  1098. process_log(p, BLOG_ERROR, "unknown statement name in variable: %s", varname);
  1099. return 0;
  1100. }
  1101. ASSERT(rps->state == SSTATE_ADULT)
  1102. // resolve variable in referred statement
  1103. if (!NCDModuleInst_GetVar(&rps->inst, (rest ? rest : ""), out)) {
  1104. process_log(p, BLOG_ERROR, "referred module failed to resolve variable: %s", varname);
  1105. return 0;
  1106. }
  1107. return 1;
  1108. }
  1109. struct process_statement * process_resolve_object (struct process *p, size_t pos, const char *objname)
  1110. {
  1111. process_assert_pointers(p);
  1112. ASSERT(pos >= 0)
  1113. ASSERT(pos <= process_rap(p))
  1114. ASSERT(objname)
  1115. // find referred statement and remaining name
  1116. struct process_statement *rps;
  1117. const char *rest;
  1118. if (!process_resolve_name(p, pos, objname, &rps, &rest)) {
  1119. return NULL;
  1120. }
  1121. if (!rps) {
  1122. // handle special objects
  1123. if (p->module_process) {
  1124. NCDModuleInst *inst = NCDModuleProcess_Interp_GetSpecialObj(p->module_process, objname);
  1125. if (inst) {
  1126. struct process_statement *res_ps = UPPER_OBJECT(inst, struct process_statement, inst);
  1127. ASSERT(res_ps->state == SSTATE_ADULT)
  1128. return res_ps;
  1129. }
  1130. }
  1131. process_log(p, BLOG_ERROR, "unknown statement name in object: %s", objname);
  1132. return NULL;
  1133. }
  1134. ASSERT(rps->state == SSTATE_ADULT)
  1135. // Resolve object in referred statement. If there is no rest, resolve empty string
  1136. // instead, or use this statement if it fails. This allows a statement to forward method
  1137. // calls elsewhere.
  1138. NCDModuleInst *inst = NCDModuleInst_GetObj(&rps->inst, (rest ? rest : ""));
  1139. if (!inst) {
  1140. if (!rest) {
  1141. return rps;
  1142. }
  1143. process_log(p, BLOG_ERROR, "referred module failed to resolve object: %s", objname);
  1144. return NULL;
  1145. }
  1146. struct process_statement *res_ps = UPPER_OBJECT(inst, struct process_statement, inst);
  1147. ASSERT(res_ps->state == SSTATE_ADULT)
  1148. return res_ps;
  1149. }
  1150. void process_statement_logfunc (struct process_statement *ps)
  1151. {
  1152. process_logfunc(ps->p);
  1153. BLog_Append("statement %zu: ", ps->i);
  1154. }
  1155. void process_statement_log (struct process_statement *ps, int level, const char *fmt, ...)
  1156. {
  1157. va_list vl;
  1158. va_start(vl, fmt);
  1159. BLog_LogViaFuncVarArg((BLog_logfunc)process_statement_logfunc, ps, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  1160. va_end(vl);
  1161. }
  1162. void process_statement_set_error (struct process_statement *ps)
  1163. {
  1164. ASSERT(ps->state == SSTATE_FORGOTTEN)
  1165. ps->have_error = 1;
  1166. ps->error_until = btime_add(btime_gettime(), options.retry_time);
  1167. }
  1168. int process_statement_resolve_argument (struct process_statement *ps, struct arg_value *arg, NCDValue *out)
  1169. {
  1170. ASSERT(ps->i <= process_rap(ps->p))
  1171. switch (arg->type) {
  1172. case ARG_VALUE_TYPE_STRING: {
  1173. if (!NCDValue_InitString(out, arg->string)) {
  1174. process_statement_log(ps, BLOG_ERROR, "NCDValue_InitString failed");
  1175. return 0;
  1176. }
  1177. } break;
  1178. case ARG_VALUE_TYPE_VARIABLE: {
  1179. if (!process_resolve_variable(ps->p, ps->i, arg->variable, out)) {
  1180. process_statement_log(ps, BLOG_ERROR, "failed to resolve variable");
  1181. return 0;
  1182. }
  1183. } break;
  1184. case ARG_VALUE_TYPE_LIST: do {
  1185. NCDValue_InitList(out);
  1186. for (LinkedList1Node *n = LinkedList1_GetFirst(&arg->list); n; n = LinkedList1Node_Next(n)) {
  1187. struct arg_list_elem *elem = UPPER_OBJECT(n, struct arg_list_elem, list_node);
  1188. NCDValue v;
  1189. if (!process_statement_resolve_argument(ps, &elem->value, &v)) {
  1190. goto list_fail1;
  1191. }
  1192. if (!NCDValue_ListAppend(out, v)) {
  1193. process_statement_log(ps, BLOG_ERROR, "NCDValue_ListAppend failed");
  1194. NCDValue_Free(&v);
  1195. goto list_fail1;
  1196. }
  1197. }
  1198. break;
  1199. list_fail1:
  1200. NCDValue_Free(out);
  1201. return 0;
  1202. } while (0); break;
  1203. default: ASSERT(0);
  1204. }
  1205. return 1;
  1206. }
  1207. void process_statement_instance_func_event (struct process_statement *ps, int event)
  1208. {
  1209. ASSERT(ps->state == SSTATE_CHILD || ps->state == SSTATE_ADULT || ps->state == SSTATE_DYING)
  1210. struct process *p = ps->p;
  1211. process_assert_pointers(p);
  1212. // schedule work
  1213. process_schedule_work(p);
  1214. switch (event) {
  1215. case NCDMODULE_EVENT_UP: {
  1216. ASSERT(ps->state == SSTATE_CHILD)
  1217. process_statement_log(ps, BLOG_INFO, "up");
  1218. // set state ADULT
  1219. ps->state = SSTATE_ADULT;
  1220. } break;
  1221. case NCDMODULE_EVENT_DOWN: {
  1222. ASSERT(ps->state == SSTATE_ADULT)
  1223. process_statement_log(ps, BLOG_INFO, "down");
  1224. // set state CHILD
  1225. ps->state = SSTATE_CHILD;
  1226. // update AP
  1227. if (p->ap > ps->i + 1) {
  1228. p->ap = ps->i + 1;
  1229. }
  1230. } break;
  1231. case NCDMODULE_EVENT_DEAD: {
  1232. int is_error = NCDModuleInst_HaveError(&ps->inst);
  1233. if (is_error) {
  1234. process_statement_log(ps, BLOG_ERROR, "died with error");
  1235. } else {
  1236. process_statement_log(ps, BLOG_INFO, "died");
  1237. }
  1238. // free instance
  1239. NCDModuleInst_Free(&ps->inst);
  1240. // free instance arguments
  1241. NCDValue_Free(&ps->inst_args);
  1242. // set state FORGOTTEN
  1243. ps->state = SSTATE_FORGOTTEN;
  1244. // set error
  1245. if (is_error) {
  1246. process_statement_set_error(ps);
  1247. }
  1248. // update AP
  1249. if (p->ap > ps->i) {
  1250. p->ap = ps->i;
  1251. }
  1252. // update FP
  1253. while (p->fp > 0 && p->statements[p->fp - 1].state == SSTATE_FORGOTTEN) {
  1254. p->fp--;
  1255. }
  1256. } break;
  1257. }
  1258. }
  1259. int process_statement_instance_func_getvar (struct process_statement *ps, const char *varname, NCDValue *out)
  1260. {
  1261. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1262. if (ps->i > process_rap(ps->p)) {
  1263. process_statement_log(ps, BLOG_ERROR, "tried to resolve variable %s but it's dirty", varname);
  1264. return 0;
  1265. }
  1266. return process_resolve_variable(ps->p, ps->i, varname, out);
  1267. }
  1268. NCDModuleInst * process_statement_instance_func_getobj (struct process_statement *ps, const char *objname)
  1269. {
  1270. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1271. if (ps->i > process_rap(ps->p)) {
  1272. process_statement_log(ps, BLOG_ERROR, "tried to resolve object %s but it's dirty", objname);
  1273. return 0;
  1274. }
  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. if (p->num_statements > process_rap(p)) {
  1335. process_log(p, BLOG_ERROR, "module process tried to resolve variable %s but it's dirty", name);
  1336. return 0;
  1337. }
  1338. return process_resolve_variable(p, p->num_statements, name, out);
  1339. }
  1340. NCDModuleInst * process_moduleprocess_func_getobj (struct process *p, const char *name)
  1341. {
  1342. ASSERT(p->module_process)
  1343. if (p->num_statements > process_rap(p)) {
  1344. process_log(p, BLOG_ERROR, "module process tried to resolve object %s but it's dirty", name);
  1345. return NULL;
  1346. }
  1347. struct process_statement *rps = process_resolve_object(p, p->num_statements, name);
  1348. if (!rps) {
  1349. return NULL;
  1350. }
  1351. return &rps->inst;
  1352. }