ncd.c 42 KB

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