ncd.c 46 KB

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