ncd.c 46 KB

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