ncd.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  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. const struct NCDModule *module;
  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_processes *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_processes *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_processes *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_processes(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_list *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_processes *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. char *type;
  885. // construct type
  886. if (!ps->s.object_name) {
  887. // this is a function_call(); type is "function_call"
  888. if (!(type = strdup(ps->s.method_name))) {
  889. process_statement_log(ps, BLOG_ERROR, "strdup failed");
  890. goto fail0;
  891. }
  892. } else {
  893. // this is a some.object.somewhere->method_call(); type is "base_type(some.object.somewhere)::method_call"
  894. // resolve object
  895. struct process_statement *obj_ps = process_resolve_object(p, p->ap, ps->s.object_name);
  896. if (!obj_ps) {
  897. process_statement_log(ps, BLOG_ERROR, "failed to resolve object %s for method call", ps->s.object_name);
  898. goto fail0;
  899. }
  900. ASSERT(obj_ps->state == SSTATE_ADULT)
  901. // base type defaults to type
  902. const char *base_type = (obj_ps->module->base_type ? obj_ps->module->base_type : obj_ps->module->type);
  903. // build type string
  904. if (!(type = concat_strings(3, base_type, "::", ps->s.method_name))) {
  905. process_statement_log(ps, BLOG_ERROR, "concat_strings failed");
  906. goto fail0;
  907. }
  908. method_object = &obj_ps->inst;
  909. }
  910. // find module to instantiate
  911. if (!(ps->module = find_module(type))) {
  912. process_statement_log(ps, BLOG_ERROR, "failed to find module: %s", type);
  913. goto fail1;
  914. }
  915. // init arguments list
  916. NCDValue_InitList(&ps->inst_args);
  917. // build arguments
  918. struct argument_elem *arg = ps->s.first_arg;
  919. while (arg) {
  920. // resolve argument
  921. NCDValue v;
  922. if (arg->is_var) {
  923. if (!process_resolve_variable(p, p->ap, arg->var, &v)) {
  924. process_statement_log(ps, BLOG_ERROR, "failed to resolve variable");
  925. goto fail2;
  926. }
  927. } else {
  928. if (!NCDValue_InitCopy(&v, &arg->val)) {
  929. process_statement_log(ps, BLOG_ERROR, "NCDValue_InitCopy failed");
  930. goto fail2;
  931. }
  932. }
  933. // move to list
  934. if (!NCDValue_ListAppend(&ps->inst_args, v)) {
  935. process_statement_log(ps, BLOG_ERROR, "NCDValue_ListAppend failed");
  936. NCDValue_Free(&v);
  937. goto fail2;
  938. }
  939. arg = arg->next_arg;
  940. }
  941. // initialize module instance
  942. NCDModuleInst_Init(
  943. &ps->inst, ps->module, method_object, &ps->inst_args, &ss, &manager, &umanager, ps,
  944. (NCDModuleInst_func_event)process_statement_instance_func_event,
  945. (NCDModuleInst_func_getvar)process_statement_instance_func_getvar,
  946. (NCDModuleInst_func_getobj)process_statement_instance_func_getobj,
  947. (NCDModuleInst_func_initprocess)process_statement_instance_func_initprocess,
  948. (BLog_logfunc)process_statement_instance_logfunc
  949. );
  950. // set statement state CHILD
  951. ps->state = SSTATE_CHILD;
  952. // increment AP
  953. p->ap++;
  954. // increment FP
  955. p->fp++;
  956. free(type);
  957. process_assert_pointers(p);
  958. return;
  959. fail2:
  960. NCDValue_Free(&ps->inst_args);
  961. fail1:
  962. free(type);
  963. fail0:
  964. // mark error
  965. process_statement_set_error(ps);
  966. // schedule work to start the timer
  967. process_schedule_work(p);
  968. }
  969. void process_wait_timer_handler (struct process *p)
  970. {
  971. process_assert_pointers(p);
  972. ASSERT(p->ap == p->fp)
  973. ASSERT(p->ap == process_rap(p))
  974. ASSERT(p->ap < p->num_statements)
  975. ASSERT(p->statements[p->ap].have_error)
  976. ASSERT(!BPending_IsSet(&p->work_job))
  977. ASSERT(!BPending_IsSet(&p->advance_job))
  978. ASSERT(p->state == PSTATE_WORKING)
  979. process_log(p, BLOG_INFO, "retrying");
  980. // clear error
  981. p->statements[p->ap].have_error = 0;
  982. // schedule work
  983. BPending_Set(&p->work_job);
  984. }
  985. struct process_statement * process_find_statement (struct process *p, size_t pos, const char *name)
  986. {
  987. process_assert_pointers(p);
  988. ASSERT(pos >= 0)
  989. ASSERT(pos <= process_rap(p))
  990. for (size_t i = pos; i > 0; i--) {
  991. struct process_statement *ps = &p->statements[i - 1];
  992. if (ps->s.name && !strcmp(ps->s.name, name)) {
  993. return ps;
  994. }
  995. }
  996. return NULL;
  997. }
  998. int process_resolve_name (struct process *p, size_t pos, const char *name, struct process_statement **first_ps, const char **rest)
  999. {
  1000. process_assert_pointers(p);
  1001. ASSERT(pos >= 0)
  1002. ASSERT(pos <= process_rap(p))
  1003. ASSERT(name)
  1004. char *dot = strstr(name, ".");
  1005. if (!dot) {
  1006. *first_ps = process_find_statement(p, pos, name);
  1007. *rest = NULL;
  1008. } else {
  1009. // copy modname and terminate
  1010. char *modname = malloc((dot - name) + 1);
  1011. if (!modname) {
  1012. process_log(p, BLOG_ERROR, "malloc failed");
  1013. return 0;
  1014. }
  1015. memcpy(modname, name, dot - name);
  1016. modname[dot - name] = '\0';
  1017. *first_ps = process_find_statement(p, pos, modname);
  1018. *rest = dot + 1;
  1019. free(modname);
  1020. }
  1021. return 1;
  1022. }
  1023. int process_resolve_variable (struct process *p, size_t pos, const char *varname, NCDValue *out)
  1024. {
  1025. process_assert_pointers(p);
  1026. ASSERT(pos >= 0)
  1027. ASSERT(pos <= process_rap(p))
  1028. ASSERT(varname)
  1029. // find referred statement and remaining name
  1030. struct process_statement *rps;
  1031. const char *rest;
  1032. if (!process_resolve_name(p, pos, varname, &rps, &rest)) {
  1033. return 0;
  1034. }
  1035. if (!rps) {
  1036. // handle _args
  1037. if (!strcmp(varname, "_args")) {
  1038. if (!NCDValue_InitCopy(out, &p->args)) {
  1039. process_log(p, BLOG_ERROR, "NCDValue_InitCopy failed");
  1040. return 0;
  1041. }
  1042. return 1;
  1043. }
  1044. // handle _argN
  1045. size_t len;
  1046. uintmax_t n;
  1047. if ((len = string_begins_with(varname, "_arg")) && parse_unsigned_integer(varname + len, &n) && n < NCDValue_ListCount(&p->args)) {
  1048. if (!NCDValue_InitCopy(out, NCDValue_ListGet(&p->args, n))) {
  1049. process_log(p, BLOG_ERROR, "NCDValue_InitCopy failed");
  1050. return 0;
  1051. }
  1052. return 1;
  1053. }
  1054. // handle special variables
  1055. if (p->module_process) {
  1056. if (NCDModuleProcess_Interp_GetSpecialVar(p->module_process, varname, out)) {
  1057. return 1;
  1058. }
  1059. }
  1060. process_log(p, BLOG_ERROR, "unknown statement name in variable: %s", varname);
  1061. return 0;
  1062. }
  1063. ASSERT(rps->state == SSTATE_ADULT)
  1064. // resolve variable in referred statement
  1065. if (!NCDModuleInst_GetVar(&rps->inst, (rest ? rest : ""), out)) {
  1066. process_log(p, BLOG_ERROR, "referred module failed to resolve variable: %s", varname);
  1067. return 0;
  1068. }
  1069. return 1;
  1070. }
  1071. struct process_statement * process_resolve_object (struct process *p, size_t pos, const char *objname)
  1072. {
  1073. process_assert_pointers(p);
  1074. ASSERT(pos >= 0)
  1075. ASSERT(pos <= process_rap(p))
  1076. ASSERT(objname)
  1077. // find referred statement and remaining name
  1078. struct process_statement *rps;
  1079. const char *rest;
  1080. if (!process_resolve_name(p, pos, objname, &rps, &rest)) {
  1081. return NULL;
  1082. }
  1083. if (!rps) {
  1084. // handle special objects
  1085. if (p->module_process) {
  1086. NCDModuleInst *inst = NCDModuleProcess_Interp_GetSpecialObj(p->module_process, objname);
  1087. if (inst) {
  1088. struct process_statement *res_ps = UPPER_OBJECT(inst, struct process_statement, inst);
  1089. ASSERT(res_ps->state == SSTATE_ADULT)
  1090. return res_ps;
  1091. }
  1092. }
  1093. process_log(p, BLOG_ERROR, "unknown statement name in object: %s", objname);
  1094. return NULL;
  1095. }
  1096. ASSERT(rps->state == SSTATE_ADULT)
  1097. // Resolve object in referred statement. If there is no rest, resolve empty string
  1098. // instead, or use this statement if it fails. This allows a statement to forward method
  1099. // calls elsewhere.
  1100. NCDModuleInst *inst = NCDModuleInst_GetObj(&rps->inst, (rest ? rest : ""));
  1101. if (!inst) {
  1102. if (!rest) {
  1103. return rps;
  1104. }
  1105. process_log(p, BLOG_ERROR, "referred module failed to resolve object: %s", objname);
  1106. return NULL;
  1107. }
  1108. struct process_statement *res_ps = UPPER_OBJECT(inst, struct process_statement, inst);
  1109. ASSERT(res_ps->state == SSTATE_ADULT)
  1110. return res_ps;
  1111. }
  1112. void process_statement_logfunc (struct process_statement *ps)
  1113. {
  1114. process_logfunc(ps->p);
  1115. BLog_Append("statement %zu: ", ps->i);
  1116. }
  1117. void process_statement_log (struct process_statement *ps, int level, const char *fmt, ...)
  1118. {
  1119. va_list vl;
  1120. va_start(vl, fmt);
  1121. BLog_LogViaFuncVarArg((BLog_logfunc)process_statement_logfunc, ps, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  1122. va_end(vl);
  1123. }
  1124. void process_statement_set_error (struct process_statement *ps)
  1125. {
  1126. ASSERT(ps->state == SSTATE_FORGOTTEN)
  1127. ps->have_error = 1;
  1128. ps->error_until = btime_add(btime_gettime(), options.retry_time);
  1129. }
  1130. void process_statement_instance_func_event (struct process_statement *ps, int event)
  1131. {
  1132. ASSERT(ps->state == SSTATE_CHILD || ps->state == SSTATE_ADULT || ps->state == SSTATE_DYING)
  1133. struct process *p = ps->p;
  1134. process_assert_pointers(p);
  1135. // schedule work
  1136. process_schedule_work(p);
  1137. switch (event) {
  1138. case NCDMODULE_EVENT_UP: {
  1139. ASSERT(ps->state == SSTATE_CHILD)
  1140. process_statement_log(ps, BLOG_INFO, "up");
  1141. // set state ADULT
  1142. ps->state = SSTATE_ADULT;
  1143. } break;
  1144. case NCDMODULE_EVENT_DOWN: {
  1145. ASSERT(ps->state == SSTATE_ADULT)
  1146. process_statement_log(ps, BLOG_INFO, "down");
  1147. // set state CHILD
  1148. ps->state = SSTATE_CHILD;
  1149. // update AP
  1150. if (p->ap > ps->i + 1) {
  1151. p->ap = ps->i + 1;
  1152. }
  1153. } break;
  1154. case NCDMODULE_EVENT_DEAD: {
  1155. int is_error = NCDModuleInst_HaveError(&ps->inst);
  1156. if (is_error) {
  1157. process_statement_log(ps, BLOG_ERROR, "died with error");
  1158. } else {
  1159. process_statement_log(ps, BLOG_INFO, "died");
  1160. }
  1161. // free instance
  1162. NCDModuleInst_Free(&ps->inst);
  1163. // free instance arguments
  1164. NCDValue_Free(&ps->inst_args);
  1165. // set state FORGOTTEN
  1166. ps->state = SSTATE_FORGOTTEN;
  1167. // set error
  1168. if (is_error) {
  1169. process_statement_set_error(ps);
  1170. }
  1171. // update AP
  1172. if (p->ap > ps->i) {
  1173. p->ap = ps->i;
  1174. }
  1175. // update FP
  1176. while (p->fp > 0 && p->statements[p->fp - 1].state == SSTATE_FORGOTTEN) {
  1177. p->fp--;
  1178. }
  1179. } break;
  1180. }
  1181. }
  1182. int process_statement_instance_func_getvar (struct process_statement *ps, const char *varname, NCDValue *out)
  1183. {
  1184. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1185. if (ps->i > process_rap(ps->p)) {
  1186. process_statement_log(ps, BLOG_ERROR, "tried to resolve variable %s but it's dirty", varname);
  1187. return 0;
  1188. }
  1189. return process_resolve_variable(ps->p, ps->i, varname, out);
  1190. }
  1191. NCDModuleInst * process_statement_instance_func_getobj (struct process_statement *ps, const char *objname)
  1192. {
  1193. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1194. if (ps->i > process_rap(ps->p)) {
  1195. process_statement_log(ps, BLOG_ERROR, "tried to resolve object %s but it's dirty", objname);
  1196. return 0;
  1197. }
  1198. struct process_statement *rps = process_resolve_object(ps->p, ps->i, objname);
  1199. if (!rps) {
  1200. return NULL;
  1201. }
  1202. return &rps->inst;
  1203. }
  1204. int process_statement_instance_func_initprocess (struct process_statement *ps, NCDModuleProcess *mp, const char *template_name, NCDValue args)
  1205. {
  1206. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1207. ASSERT(NCDValue_Type(&args) == NCDVALUE_LIST)
  1208. // find template
  1209. struct NCDConfig_processes *conf = config_ast;
  1210. while (conf) {
  1211. if (conf->is_template && !strcmp(conf->name, template_name)) {
  1212. break;
  1213. }
  1214. conf = conf->next;
  1215. }
  1216. if (!conf) {
  1217. process_statement_log(ps, BLOG_ERROR, "no template named %s", template_name);
  1218. return 0;
  1219. }
  1220. // create process
  1221. if (!process_new(conf, mp, args)) {
  1222. process_statement_log(ps, BLOG_ERROR, "failed to create process from template %s", template_name);
  1223. return 0;
  1224. }
  1225. process_statement_log(ps, BLOG_INFO, "created process from template %s", template_name);
  1226. return 1;
  1227. }
  1228. void process_statement_instance_logfunc (struct process_statement *ps)
  1229. {
  1230. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1231. process_statement_logfunc(ps);
  1232. BLog_Append("module: ");
  1233. }
  1234. void process_moduleprocess_func_event (struct process *p, int event)
  1235. {
  1236. ASSERT(p->module_process)
  1237. switch (event) {
  1238. case NCDMODULEPROCESS_INTERP_EVENT_CONTINUE: {
  1239. ASSERT(p->state == PSTATE_WAITING)
  1240. // set state working
  1241. p->state = PSTATE_WORKING;
  1242. // schedule work
  1243. process_schedule_work(p);
  1244. } break;
  1245. case NCDMODULEPROCESS_INTERP_EVENT_TERMINATE: {
  1246. ASSERT(p->state != PSTATE_TERMINATING)
  1247. process_log(p, BLOG_INFO, "process termination requested");
  1248. // start terminating
  1249. process_start_terminating(p);
  1250. } break;
  1251. default: ASSERT(0);
  1252. }
  1253. }
  1254. int process_moduleprocess_func_getvar (struct process *p, const char *name, NCDValue *out)
  1255. {
  1256. ASSERT(p->module_process)
  1257. if (p->num_statements > process_rap(p)) {
  1258. process_log(p, BLOG_ERROR, "module process tried to resolve variable %s but it's dirty", name);
  1259. return 0;
  1260. }
  1261. return process_resolve_variable(p, p->num_statements, name, out);
  1262. }
  1263. NCDModuleInst * process_moduleprocess_func_getobj (struct process *p, const char *name)
  1264. {
  1265. ASSERT(p->module_process)
  1266. if (p->num_statements > process_rap(p)) {
  1267. process_log(p, BLOG_ERROR, "module process tried to resolve object %s but it's dirty", name);
  1268. return NULL;
  1269. }
  1270. struct process_statement *rps = process_resolve_object(p, p->num_statements, name);
  1271. if (!rps) {
  1272. return NULL;
  1273. }
  1274. return &rps->inst;
  1275. }