ncd.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. /**
  2. * @file ncd.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdint.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <misc/version.h>
  34. #include <misc/loglevel.h>
  35. #include <misc/offset.h>
  36. #include <misc/read_file.h>
  37. #include <misc/balloc.h>
  38. #include <misc/concat_strings.h>
  39. #include <misc/open_standard_streams.h>
  40. #include <misc/expstring.h>
  41. #include <misc/split_string.h>
  42. #include <structure/LinkedList1.h>
  43. #include <base/BLog.h>
  44. #include <base/BLog_syslog.h>
  45. #include <system/BReactor.h>
  46. #include <system/BSignal.h>
  47. #include <system/BProcess.h>
  48. #include <udevmonitor/NCDUdevManager.h>
  49. #include <ncd/NCDConfigParser.h>
  50. #include <ncd/NCDModule.h>
  51. #include <ncd/NCDModuleIndex.h>
  52. #include <ncd/NCDSugar.h>
  53. #include <ncd/NCDInterpProg.h>
  54. #include <ncd/modules/modules.h>
  55. #include "ncd.h"
  56. #include <generated/blog_channel_ncd.h>
  57. #define LOGGER_STDOUT 1
  58. #define LOGGER_STDERR 2
  59. #define LOGGER_SYSLOG 3
  60. #define SSTATE_CHILD 1
  61. #define SSTATE_ADULT 2
  62. #define SSTATE_DYING 3
  63. #define SSTATE_FORGOTTEN 4
  64. #define PSTATE_WORKING 1
  65. #define PSTATE_UP 2
  66. #define PSTATE_WAITING 3
  67. #define PSTATE_TERMINATING 4
  68. struct process;
  69. struct statement {
  70. struct process *p;
  71. NCDModuleInst inst;
  72. NCDValMem args_mem;
  73. char *mem;
  74. int mem_size;
  75. int i;
  76. int state;
  77. };
  78. struct process {
  79. NCDProcess *proc_ast;
  80. NCDInterpBlock *iblock;
  81. NCDModuleProcess *module_process;
  82. BTimer wait_timer;
  83. BPending work_job;
  84. LinkedList1Node list_node; // node in processes
  85. char *mem;
  86. int mem_size;
  87. int state;
  88. int ap;
  89. int fp;
  90. int have_error;
  91. int num_statements;
  92. struct statement statements[];
  93. };
  94. // command-line options
  95. struct {
  96. int help;
  97. int version;
  98. int logger;
  99. char *logger_syslog_facility;
  100. char *logger_syslog_ident;
  101. int loglevel;
  102. int loglevels[BLOG_NUM_CHANNELS];
  103. char *config_file;
  104. int retry_time;
  105. int no_udev;
  106. char **extra_args;
  107. int num_extra_args;
  108. } options;
  109. // reactor
  110. BReactor reactor;
  111. // are we terminating
  112. int terminating;
  113. int main_exit_code;
  114. // process manager
  115. BProcessManager manager;
  116. // udev manager
  117. NCDUdevManager umanager;
  118. // module index
  119. NCDModuleIndex mindex;
  120. // program AST
  121. NCDProgram program;
  122. // structure for efficient interpretation
  123. NCDInterpProg iprogram;
  124. // common module parameters
  125. struct NCDModuleInst_params module_params;
  126. struct NCDModuleInst_iparams module_iparams;
  127. // processes
  128. LinkedList1 processes;
  129. // buffer for concatenating base_type::method_name
  130. char method_concat_buf[200];
  131. static void print_help (const char *name);
  132. static void print_version (void);
  133. static int parse_arguments (int argc, char *argv[]);
  134. static void signal_handler (void *unused);
  135. static void start_terminate (int exit_code);
  136. static int process_new (NCDProcess *proc_ast, NCDInterpBlock *iblock, NCDModuleProcess *module_process);
  137. static void process_free (struct process *p, NCDModuleProcess **out_mp);
  138. static int process_mem_is_preallocated (struct process *p, char *mem);
  139. static void process_start_terminating (struct process *p);
  140. static int process_rap (struct process *p);
  141. static void process_assert_pointers (struct process *p);
  142. static void process_logfunc (struct process *p);
  143. static void process_log (struct process *p, int level, const char *fmt, ...);
  144. static void process_schedule_work (struct process *p);
  145. static void process_work_job_handler (struct process *p);
  146. static void process_advance (struct process *p);
  147. static void process_wait_timer_handler (struct process *p);
  148. static int process_find_object (struct process *p, int pos, const char *name, NCDObject *out_object);
  149. static int process_resolve_object_expr (struct process *p, int pos, char **names, NCDObject *out_object);
  150. static int process_resolve_variable_expr (struct process *p, int pos, char **names, NCDValMem *mem, NCDValRef *out_value);
  151. static void statement_logfunc (struct statement *ps);
  152. static void statement_log (struct statement *ps, int level, const char *fmt, ...);
  153. static int statement_allocate_memory (struct statement *ps, int alloc_size, void **out_mem);
  154. static int statement_resolve_argument (struct statement *ps, NCDInterpValue *arg, NCDValMem *mem, NCDValRef *out);
  155. static void statement_instance_func_event (struct statement *ps, int event);
  156. static int statement_instance_func_getobj (struct statement *ps, const char *objname, NCDObject *out_object);
  157. static int statement_instance_func_initprocess (struct statement *ps, NCDModuleProcess *mp, const char *template_name);
  158. static void statement_instance_logfunc (struct statement *ps);
  159. static void statement_instance_func_interp_exit (struct statement *ps, int exit_code);
  160. static int statement_instance_func_interp_getargs (struct statement *ps, NCDValMem *mem, NCDValRef *out_value);
  161. static btime_t statement_instance_func_interp_getretrytime (struct statement *ps);
  162. static void process_moduleprocess_func_event (struct process *p, int event);
  163. static int process_moduleprocess_func_getobj (struct process *p, const char *name, NCDObject *out_object);
  164. int main (int argc, char **argv)
  165. {
  166. if (argc <= 0) {
  167. return 1;
  168. }
  169. // set exit code
  170. main_exit_code = 1;
  171. // open standard streams
  172. open_standard_streams();
  173. // parse command-line arguments
  174. if (!parse_arguments(argc, argv)) {
  175. fprintf(stderr, "Failed to parse arguments\n");
  176. print_help(argv[0]);
  177. goto fail0;
  178. }
  179. // handle --help and --version
  180. if (options.help) {
  181. print_version();
  182. print_help(argv[0]);
  183. return 0;
  184. }
  185. if (options.version) {
  186. print_version();
  187. return 0;
  188. }
  189. // initialize logger
  190. switch (options.logger) {
  191. case LOGGER_STDOUT:
  192. BLog_InitStdout();
  193. break;
  194. case LOGGER_STDERR:
  195. BLog_InitStderr();
  196. break;
  197. case LOGGER_SYSLOG:
  198. if (!BLog_InitSyslog(options.logger_syslog_ident, options.logger_syslog_facility)) {
  199. fprintf(stderr, "Failed to initialize syslog logger\n");
  200. goto fail0;
  201. }
  202. break;
  203. default:
  204. ASSERT(0);
  205. }
  206. // configure logger channels
  207. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  208. if (options.loglevels[i] >= 0) {
  209. BLog_SetChannelLoglevel(i, options.loglevels[i]);
  210. }
  211. else if (options.loglevel >= 0) {
  212. BLog_SetChannelLoglevel(i, options.loglevel);
  213. }
  214. }
  215. BLog(BLOG_NOTICE, "initializing "GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION);
  216. // initialize network
  217. if (!BNetwork_GlobalInit()) {
  218. BLog(BLOG_ERROR, "BNetwork_GlobalInit failed");
  219. goto fail1;
  220. }
  221. // init time
  222. BTime_Init();
  223. // init reactor
  224. if (!BReactor_Init(&reactor)) {
  225. BLog(BLOG_ERROR, "BReactor_Init failed");
  226. goto fail1;
  227. }
  228. // set not terminating
  229. terminating = 0;
  230. // init process manager
  231. if (!BProcessManager_Init(&manager, &reactor)) {
  232. BLog(BLOG_ERROR, "BProcessManager_Init failed");
  233. goto fail1a;
  234. }
  235. // init udev manager
  236. NCDUdevManager_Init(&umanager, options.no_udev, &reactor, &manager);
  237. // init module index
  238. if (!NCDModuleIndex_Init(&mindex)) {
  239. BLog(BLOG_ERROR, "NCDModuleIndex_Init failed");
  240. goto fail1b;
  241. }
  242. // add module groups to index
  243. for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
  244. if (!NCDModuleIndex_AddGroup(&mindex, *g)) {
  245. BLog(BLOG_ERROR, "NCDModuleIndex_AddGroup failed");
  246. goto fail2;
  247. }
  248. }
  249. // setup signal handler
  250. if (!BSignal_Init(&reactor, signal_handler, NULL)) {
  251. BLog(BLOG_ERROR, "BSignal_Init failed");
  252. goto fail2;
  253. }
  254. // read config file
  255. uint8_t *file;
  256. size_t file_len;
  257. if (!read_file(options.config_file, &file, &file_len)) {
  258. BLog(BLOG_ERROR, "failed to read config file");
  259. goto fail3;
  260. }
  261. // parse config file
  262. if (!NCDConfigParser_Parse((char *)file, file_len, &program)) {
  263. BLog(BLOG_ERROR, "NCDConfigParser_Parse failed");
  264. free(file);
  265. goto fail3;
  266. }
  267. // fee config file memory
  268. free(file);
  269. // desugar
  270. if (!NCDSugar_Desugar(&program)) {
  271. BLog(BLOG_ERROR, "NCDSugar_Desugar failed");
  272. goto fail4;
  273. }
  274. // init interp program
  275. if (!NCDInterpProg_Init(&iprogram, &program)) {
  276. BLog(BLOG_ERROR, "NCDInterpProg_Init failed");
  277. goto fail4;
  278. }
  279. // init module params
  280. struct NCDModuleInitParams params;
  281. params.reactor = &reactor;
  282. params.manager = &manager;
  283. params.umanager = &umanager;
  284. // init modules
  285. size_t num_inited_modules = 0;
  286. for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
  287. if ((*g)->func_globalinit && !(*g)->func_globalinit(params)) {
  288. BLog(BLOG_ERROR, "globalinit failed for some module");
  289. goto fail5;
  290. }
  291. num_inited_modules++;
  292. }
  293. // init common module params
  294. module_params.func_event = (NCDModuleInst_func_event)statement_instance_func_event;
  295. module_params.func_getobj = (NCDModuleInst_func_getobj)statement_instance_func_getobj;
  296. module_params.logfunc = (BLog_logfunc)statement_instance_logfunc;
  297. module_iparams.reactor = &reactor;
  298. module_iparams.manager = &manager;
  299. module_iparams.umanager = &umanager;
  300. module_iparams.func_initprocess = (NCDModuleInst_func_initprocess)statement_instance_func_initprocess;
  301. module_iparams.func_interp_exit = (NCDModuleInst_func_interp_exit)statement_instance_func_interp_exit;
  302. module_iparams.func_interp_getargs = (NCDModuleInst_func_interp_getargs)statement_instance_func_interp_getargs;
  303. module_iparams.func_interp_getretrytime = (NCDModuleInst_func_interp_getretrytime)statement_instance_func_interp_getretrytime;
  304. // init processes list
  305. LinkedList1_Init(&processes);
  306. // init processes
  307. for (NCDProcess *p = NCDProgram_FirstProcess(&program); p; p = NCDProgram_NextProcess(&program, p)) {
  308. if (NCDProcess_IsTemplate(p)) {
  309. continue;
  310. }
  311. // find iblock
  312. NCDProcess *f_proc;
  313. NCDInterpBlock *iblock;
  314. int res = NCDInterpProg_FindProcess(&iprogram, NCDProcess_Name(p), &f_proc, &iblock);
  315. ASSERT(res)
  316. ASSERT(f_proc == p)
  317. if (!process_new(p, iblock, NULL)) {
  318. BLog(BLOG_ERROR, "failed to initialize process, exiting");
  319. goto fail6;
  320. }
  321. }
  322. // enter event loop
  323. BLog(BLOG_NOTICE, "entering event loop");
  324. BReactor_Exec(&reactor);
  325. ASSERT(LinkedList1_IsEmpty(&processes))
  326. fail6:;
  327. LinkedList1Node *ln;
  328. while (ln = LinkedList1_GetFirst(&processes)) {
  329. struct process *p = UPPER_OBJECT(ln, struct process, list_node);
  330. NCDModuleProcess *mp;
  331. process_free(p, &mp);
  332. ASSERT(!mp)
  333. }
  334. fail5:
  335. // free modules
  336. while (num_inited_modules > 0) {
  337. const struct NCDModuleGroup **g = &ncd_modules[num_inited_modules - 1];
  338. if ((*g)->func_globalfree) {
  339. (*g)->func_globalfree();
  340. }
  341. num_inited_modules--;
  342. }
  343. // free interp program
  344. NCDInterpProg_Free(&iprogram);
  345. fail4:
  346. // free program AST
  347. NCDProgram_Free(&program);
  348. fail3:
  349. // remove signal handler
  350. BSignal_Finish();
  351. fail2:
  352. // free module index
  353. NCDModuleIndex_Free(&mindex);
  354. fail1b:
  355. // free udev manager
  356. NCDUdevManager_Free(&umanager);
  357. // free process manager
  358. BProcessManager_Free(&manager);
  359. fail1a:
  360. // free reactor
  361. BReactor_Free(&reactor);
  362. fail1:
  363. // free logger
  364. BLog(BLOG_NOTICE, "exiting");
  365. BLog_Free();
  366. fail0:
  367. // finish objects
  368. DebugObjectGlobal_Finish();
  369. return main_exit_code;
  370. }
  371. void print_help (const char *name)
  372. {
  373. printf(
  374. "Usage:\n"
  375. " %s\n"
  376. " [--help]\n"
  377. " [--version]\n"
  378. " [--logger <stdout/stderr/syslog>]\n"
  379. " (logger=syslog?\n"
  380. " [--syslog-facility <string>]\n"
  381. " [--syslog-ident <string>]\n"
  382. " )\n"
  383. " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
  384. " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
  385. " --config-file <file>\n"
  386. " [--retry-time <ms>]\n"
  387. " [--no-udev]\n"
  388. " [-- [<extra_arg>] ...]\n",
  389. name
  390. );
  391. }
  392. void print_version (void)
  393. {
  394. printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
  395. }
  396. int parse_arguments (int argc, char *argv[])
  397. {
  398. if (argc <= 0) {
  399. return 0;
  400. }
  401. options.help = 0;
  402. options.version = 0;
  403. options.logger = LOGGER_STDERR;
  404. options.logger_syslog_facility = "daemon";
  405. options.logger_syslog_ident = argv[0];
  406. options.loglevel = -1;
  407. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  408. options.loglevels[i] = -1;
  409. }
  410. options.config_file = NULL;
  411. options.retry_time = DEFAULT_RETRY_TIME;
  412. options.no_udev = 0;
  413. options.extra_args = NULL;
  414. options.num_extra_args = 0;
  415. for (int i = 1; i < argc; i++) {
  416. char *arg = argv[i];
  417. if (!strcmp(arg, "--help")) {
  418. options.help = 1;
  419. }
  420. else if (!strcmp(arg, "--version")) {
  421. options.version = 1;
  422. }
  423. else if (!strcmp(arg, "--logger")) {
  424. if (1 >= argc - i) {
  425. fprintf(stderr, "%s: requires an argument\n", arg);
  426. return 0;
  427. }
  428. char *arg2 = argv[i + 1];
  429. if (!strcmp(arg2, "stdout")) {
  430. options.logger = LOGGER_STDOUT;
  431. }
  432. else if (!strcmp(arg2, "stderr")) {
  433. options.logger = LOGGER_STDERR;
  434. }
  435. else if (!strcmp(arg2, "syslog")) {
  436. options.logger = LOGGER_SYSLOG;
  437. }
  438. else {
  439. fprintf(stderr, "%s: wrong argument\n", arg);
  440. return 0;
  441. }
  442. i++;
  443. }
  444. else if (!strcmp(arg, "--syslog-facility")) {
  445. if (1 >= argc - i) {
  446. fprintf(stderr, "%s: requires an argument\n", arg);
  447. return 0;
  448. }
  449. options.logger_syslog_facility = argv[i + 1];
  450. i++;
  451. }
  452. else if (!strcmp(arg, "--syslog-ident")) {
  453. if (1 >= argc - i) {
  454. fprintf(stderr, "%s: requires an argument\n", arg);
  455. return 0;
  456. }
  457. options.logger_syslog_ident = argv[i + 1];
  458. i++;
  459. }
  460. else if (!strcmp(arg, "--loglevel")) {
  461. if (1 >= argc - i) {
  462. fprintf(stderr, "%s: requires an argument\n", arg);
  463. return 0;
  464. }
  465. if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
  466. fprintf(stderr, "%s: wrong argument\n", arg);
  467. return 0;
  468. }
  469. i++;
  470. }
  471. else if (!strcmp(arg, "--channel-loglevel")) {
  472. if (2 >= argc - i) {
  473. fprintf(stderr, "%s: requires two arguments\n", arg);
  474. return 0;
  475. }
  476. int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
  477. if (channel < 0) {
  478. fprintf(stderr, "%s: wrong channel argument\n", arg);
  479. return 0;
  480. }
  481. int loglevel = parse_loglevel(argv[i + 2]);
  482. if (loglevel < 0) {
  483. fprintf(stderr, "%s: wrong loglevel argument\n", arg);
  484. return 0;
  485. }
  486. options.loglevels[channel] = loglevel;
  487. i += 2;
  488. }
  489. else if (!strcmp(arg, "--config-file")) {
  490. if (1 >= argc - i) {
  491. fprintf(stderr, "%s: requires an argument\n", arg);
  492. return 0;
  493. }
  494. options.config_file = argv[i + 1];
  495. i++;
  496. }
  497. else if (!strcmp(arg, "--retry-time")) {
  498. if (1 >= argc - i) {
  499. fprintf(stderr, "%s: requires an argument\n", arg);
  500. return 0;
  501. }
  502. if ((options.retry_time = atoi(argv[i + 1])) < 0) {
  503. fprintf(stderr, "%s: wrong argument\n", arg);
  504. return 0;
  505. }
  506. i++;
  507. }
  508. else if (!strcmp(arg, "--no-udev")) {
  509. options.no_udev = 1;
  510. }
  511. else if (!strcmp(arg, "--")) {
  512. options.extra_args = &argv[i + 1];
  513. options.num_extra_args = argc - i - 1;
  514. i += options.num_extra_args;
  515. }
  516. else {
  517. fprintf(stderr, "unknown option: %s\n", arg);
  518. return 0;
  519. }
  520. }
  521. if (options.help || options.version) {
  522. return 1;
  523. }
  524. if (!options.config_file) {
  525. fprintf(stderr, "--config-file is required\n");
  526. return 0;
  527. }
  528. return 1;
  529. }
  530. void signal_handler (void *unused)
  531. {
  532. BLog(BLOG_NOTICE, "termination requested");
  533. start_terminate(1);
  534. }
  535. void start_terminate (int exit_code)
  536. {
  537. main_exit_code = exit_code;
  538. if (terminating) {
  539. return;
  540. }
  541. terminating = 1;
  542. if (LinkedList1_IsEmpty(&processes)) {
  543. BReactor_Quit(&reactor, 0);
  544. return;
  545. }
  546. // start terminating non-template processes
  547. for (LinkedList1Node *ln = LinkedList1_GetFirst(&processes); ln; ln = LinkedList1Node_Next(ln)) {
  548. struct process *p = UPPER_OBJECT(ln, struct process, list_node);
  549. if (p->module_process) {
  550. continue;
  551. }
  552. if (p->state != PSTATE_TERMINATING) {
  553. process_start_terminating(p);
  554. }
  555. }
  556. }
  557. static int process_new (NCDProcess *proc_ast, NCDInterpBlock *iblock, NCDModuleProcess *module_process)
  558. {
  559. // get block
  560. NCDBlock *block = NCDProcess_Block(proc_ast);
  561. // get num statements
  562. int num_statements = NCDBlock_NumStatements(block);
  563. // calculate allocation size
  564. bsize_t alloc_size = bsize_add(bsize_fromsize(sizeof(struct process)), bsize_mul(bsize_fromsize(num_statements), bsize_fromsize(sizeof(struct statement))));
  565. // allocate strucure
  566. struct process *p = BAllocSize(alloc_size);
  567. if (!p) {
  568. BLog(BLOG_ERROR, "BAllocSize failed");
  569. goto fail0;
  570. }
  571. // set some stuff
  572. p->proc_ast = proc_ast;
  573. p->iblock = iblock;
  574. p->module_process = module_process;
  575. p->num_statements = num_statements;
  576. // set module process handlers
  577. if (p->module_process) {
  578. NCDModuleProcess_Interp_SetHandlers(p->module_process, p,
  579. (NCDModuleProcess_interp_func_event)process_moduleprocess_func_event,
  580. (NCDModuleProcess_interp_func_getobj)process_moduleprocess_func_getobj);
  581. }
  582. // preallocate statement memory
  583. if ((p->mem_size = NCDInterpBlock_PreallocSize(iblock)) < 0) {
  584. BLog(BLOG_ERROR, "NCDInterpBlock_PreallocSize");
  585. goto fail1;
  586. }
  587. if (!(p->mem = BAllocSize(bsize_fromint(p->mem_size)))) {
  588. BLog(BLOG_ERROR, "BAllocSize failed");
  589. goto fail1;
  590. }
  591. // init statements
  592. for (int i = 0; i < num_statements; i++) {
  593. struct statement *ps = &p->statements[i];
  594. ps->p = p;
  595. ps->i = i;
  596. ps->state = SSTATE_FORGOTTEN;
  597. ps->mem_size = NCDInterpBlock_StatementPreallocSize(iblock, i);
  598. ps->mem = (ps->mem_size == 0 ? NULL : p->mem + NCDInterpBlock_StatementPreallocOffset(iblock, i));
  599. }
  600. // set state working
  601. p->state = PSTATE_WORKING;
  602. // set AP=0
  603. p->ap = 0;
  604. // set FP=0
  605. p->fp = 0;
  606. // set no error
  607. p->have_error = 0;
  608. // init timer
  609. BTimer_Init(&p->wait_timer, options.retry_time, (BTimer_handler)process_wait_timer_handler, p);
  610. // init work job
  611. BPending_Init(&p->work_job, BReactor_PendingGroup(&reactor), (BPending_handler)process_work_job_handler, p);
  612. // insert to processes list
  613. LinkedList1_Append(&processes, &p->list_node);
  614. // schedule work
  615. BPending_Set(&p->work_job);
  616. return 1;
  617. fail1:
  618. BFree(p);
  619. fail0:
  620. BLog(BLOG_ERROR, "failed to initialize process %s", NCDProcess_Name(proc_ast));
  621. return 0;
  622. }
  623. void process_free (struct process *p, NCDModuleProcess **out_mp)
  624. {
  625. ASSERT(p->ap == 0)
  626. ASSERT(p->fp == 0)
  627. ASSERT(out_mp)
  628. // give module process to caller so it can inform the process creator that the process has terminated
  629. *out_mp = p->module_process;
  630. // free statement memory
  631. for (int i = 0; i < p->num_statements; i++) {
  632. struct statement *ps = &p->statements[i];
  633. if (ps->mem && !process_mem_is_preallocated(p, ps->mem)) {
  634. free(ps->mem);
  635. }
  636. }
  637. // remove from processes list
  638. LinkedList1_Remove(&processes, &p->list_node);
  639. // free work job
  640. BPending_Free(&p->work_job);
  641. // free timer
  642. BReactor_RemoveTimer(&reactor, &p->wait_timer);
  643. // free preallocated memory
  644. BFree(p->mem);
  645. // free strucure
  646. BFree(p);
  647. }
  648. int process_mem_is_preallocated (struct process *p, char *mem)
  649. {
  650. ASSERT(mem)
  651. return (mem >= p->mem && mem < p->mem + p->mem_size);
  652. }
  653. void process_start_terminating (struct process *p)
  654. {
  655. ASSERT(p->state != PSTATE_TERMINATING)
  656. // set terminating
  657. p->state = PSTATE_TERMINATING;
  658. // schedule work
  659. process_schedule_work(p);
  660. }
  661. int process_rap (struct process *p)
  662. {
  663. if (p->ap > 0 && p->statements[p->ap - 1].state == SSTATE_CHILD) {
  664. return (p->ap - 1);
  665. } else {
  666. return p->ap;
  667. }
  668. }
  669. void process_assert_pointers (struct process *p)
  670. {
  671. ASSERT(p->ap <= p->num_statements)
  672. ASSERT(p->fp >= p->ap)
  673. ASSERT(p->fp <= p->num_statements)
  674. #ifndef NDEBUG
  675. // check AP
  676. for (int i = 0; i < p->ap; i++) {
  677. if (i == p->ap - 1) {
  678. ASSERT(p->statements[i].state == SSTATE_ADULT || p->statements[i].state == SSTATE_CHILD)
  679. } else {
  680. ASSERT(p->statements[i].state == SSTATE_ADULT)
  681. }
  682. }
  683. // check FP
  684. int fp = p->num_statements;
  685. while (fp > 0 && p->statements[fp - 1].state == SSTATE_FORGOTTEN) {
  686. fp--;
  687. }
  688. ASSERT(p->fp == fp)
  689. #endif
  690. }
  691. void process_logfunc (struct process *p)
  692. {
  693. BLog_Append("process %s: ", NCDProcess_Name(p->proc_ast));
  694. }
  695. void process_log (struct process *p, int level, const char *fmt, ...)
  696. {
  697. va_list vl;
  698. va_start(vl, fmt);
  699. BLog_LogViaFuncVarArg((BLog_logfunc)process_logfunc, p, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  700. va_end(vl);
  701. }
  702. void process_schedule_work (struct process *p)
  703. {
  704. process_assert_pointers(p);
  705. // stop timer
  706. BReactor_RemoveTimer(&reactor, &p->wait_timer);
  707. // schedule work
  708. BPending_Set(&p->work_job);
  709. }
  710. void process_work_job_handler (struct process *p)
  711. {
  712. process_assert_pointers(p);
  713. ASSERT(!BTimer_IsRunning(&p->wait_timer))
  714. if (p->state == PSTATE_WAITING) {
  715. return;
  716. }
  717. if (p->state == PSTATE_TERMINATING) {
  718. if (p->fp == 0) {
  719. // free process
  720. NCDModuleProcess *mp;
  721. process_free(p, &mp);
  722. // if program is terminating amd there are no more processes, exit program
  723. if (terminating && LinkedList1_IsEmpty(&processes)) {
  724. ASSERT(!mp)
  725. BReactor_Quit(&reactor, 0);
  726. return;
  727. }
  728. // inform the process creator that the process has terminated
  729. if (mp) {
  730. NCDModuleProcess_Interp_Terminated(mp);
  731. return;
  732. }
  733. return;
  734. }
  735. // order the last living statement to die, if needed
  736. struct statement *ps = &p->statements[p->fp - 1];
  737. ASSERT(ps->state != SSTATE_FORGOTTEN)
  738. if (ps->state != SSTATE_DYING) {
  739. statement_log(ps, BLOG_INFO, "killing");
  740. // set statement state DYING
  741. ps->state = SSTATE_DYING;
  742. // update AP
  743. if (p->ap > ps->i) {
  744. p->ap = ps->i;
  745. }
  746. // order it to die
  747. NCDModuleInst_Die(&ps->inst);
  748. return;
  749. }
  750. return;
  751. }
  752. // process was up but is no longer?
  753. if (p->state == PSTATE_UP && !(p->ap == process_rap(p) && p->ap == p->num_statements)) {
  754. // if we have module process, wait for its permission to continue
  755. if (p->module_process) {
  756. // set state waiting
  757. p->state = PSTATE_WAITING;
  758. // set module process down
  759. NCDModuleProcess_Interp_Down(p->module_process);
  760. return;
  761. }
  762. // set state working
  763. p->state = PSTATE_WORKING;
  764. }
  765. // cleaning up?
  766. if (p->ap < p->fp) {
  767. // order the last living statement to die, if needed
  768. struct statement *ps = &p->statements[p->fp - 1];
  769. if (ps->state != SSTATE_DYING) {
  770. statement_log(ps, BLOG_INFO, "killing");
  771. // set statement state DYING
  772. ps->state = SSTATE_DYING;
  773. // order it to die
  774. NCDModuleInst_Die(&ps->inst);
  775. return;
  776. }
  777. return;
  778. }
  779. // clean?
  780. if (p->ap > process_rap(p)) {
  781. ASSERT(p->ap > 0)
  782. ASSERT(p->ap <= p->num_statements)
  783. struct statement *ps = &p->statements[p->ap - 1];
  784. ASSERT(ps->state == SSTATE_CHILD)
  785. statement_log(ps, BLOG_INFO, "clean");
  786. // report clean
  787. NCDModuleInst_Clean(&ps->inst);
  788. return;
  789. }
  790. // advancing?
  791. if (p->ap < p->num_statements) {
  792. ASSERT(p->state == PSTATE_WORKING)
  793. struct statement *ps = &p->statements[p->ap];
  794. ASSERT(ps->state == SSTATE_FORGOTTEN)
  795. if (p->have_error) {
  796. statement_log(ps, BLOG_INFO, "waiting after error");
  797. // clear error
  798. p->have_error = 0;
  799. // set wait timer
  800. BReactor_SetTimer(&reactor, &p->wait_timer);
  801. } else {
  802. // advance
  803. process_advance(p);
  804. }
  805. return;
  806. }
  807. // have we just finished?
  808. if (p->state == PSTATE_WORKING) {
  809. process_log(p, BLOG_INFO, "victory");
  810. // set state up
  811. p->state = PSTATE_UP;
  812. // set module process up
  813. if (p->module_process) {
  814. NCDModuleProcess_Interp_Up(p->module_process);
  815. return;
  816. }
  817. }
  818. }
  819. void process_advance (struct process *p)
  820. {
  821. process_assert_pointers(p);
  822. ASSERT(p->ap == p->fp)
  823. ASSERT(p->ap == process_rap(p))
  824. ASSERT(p->ap < p->num_statements)
  825. ASSERT(!p->have_error)
  826. ASSERT(!BPending_IsSet(&p->work_job))
  827. ASSERT(!BTimer_IsRunning(&p->wait_timer))
  828. ASSERT(p->state == PSTATE_WORKING)
  829. struct statement *ps = &p->statements[p->ap];
  830. ASSERT(ps->state == SSTATE_FORGOTTEN)
  831. statement_log(ps, BLOG_INFO, "initializing");
  832. NCDObject object;
  833. NCDObject *object_ptr = NULL;
  834. char **object_names = NCDInterpBlock_StatementObjNames(p->iblock, p->ap);
  835. const char *type = NCDInterpBlock_StatementCmdName(p->iblock, p->ap);
  836. // if this is a method-like statement, type is really "base_type(object)::method_name"
  837. if (object_names) {
  838. // get object
  839. if (!process_resolve_object_expr(p, p->ap, object_names, &object)) {
  840. goto fail0;
  841. }
  842. object_ptr = &object;
  843. // get object type
  844. const char *object_type = NCDObject_Type(&object);
  845. if (!object_type) {
  846. statement_log(ps, BLOG_ERROR, "cannot call method on object with no type");
  847. goto fail0;
  848. }
  849. // build type string
  850. int res = snprintf(method_concat_buf, sizeof(method_concat_buf), "%s::%s", object_type, type);
  851. if (res >= sizeof(method_concat_buf) || res < 0) {
  852. statement_log(ps, BLOG_ERROR, "type/method name too long");
  853. goto fail0;
  854. }
  855. type = method_concat_buf;
  856. }
  857. // find module to instantiate
  858. const struct NCDModule *module = NCDModuleIndex_FindModule(&mindex, type);
  859. if (!module) {
  860. statement_log(ps, BLOG_ERROR, "failed to find module: %s", type);
  861. goto fail0;
  862. }
  863. // register alloc size for future preallocations
  864. NCDInterpBlock_StatementBumpAllocSize(p->iblock, p->ap, module->alloc_size);
  865. // init args mem
  866. NCDValMem_Init(&ps->args_mem);
  867. // resolve arguments
  868. NCDValRef args;
  869. NCDInterpValue *iargs = NCDInterpBlock_StatementInterpValue(p->iblock, ps->i);
  870. if (!statement_resolve_argument(ps, iargs, &ps->args_mem, &args)) {
  871. statement_log(ps, BLOG_ERROR, "failed to resolve arguments");
  872. goto fail1;
  873. }
  874. // allocate memory
  875. void *mem;
  876. if (!statement_allocate_memory(ps, module->alloc_size, &mem)) {
  877. statement_log(ps, BLOG_ERROR, "failed to allocate memory");
  878. goto fail1;
  879. }
  880. // set statement state CHILD
  881. ps->state = SSTATE_CHILD;
  882. // increment AP
  883. p->ap++;
  884. // increment FP
  885. p->fp++;
  886. process_assert_pointers(p);
  887. // initialize module instance
  888. NCDModuleInst_Init(&ps->inst, module, mem, object_ptr, args, ps, &module_params, &module_iparams);
  889. return;
  890. fail1:
  891. NCDValMem_Free(&ps->args_mem);
  892. fail0:
  893. // set error
  894. p->have_error = 1;
  895. // schedule work to start the timer
  896. process_schedule_work(p);
  897. }
  898. void process_wait_timer_handler (struct process *p)
  899. {
  900. process_assert_pointers(p);
  901. ASSERT(p->ap == p->fp)
  902. ASSERT(p->ap == process_rap(p))
  903. ASSERT(p->ap < p->num_statements)
  904. ASSERT(!p->have_error)
  905. ASSERT(!BPending_IsSet(&p->work_job))
  906. ASSERT(p->state == PSTATE_WORKING)
  907. process_log(p, BLOG_INFO, "retrying");
  908. // advance
  909. process_advance(p);
  910. }
  911. int process_find_object (struct process *p, int pos, const char *name, NCDObject *out_object)
  912. {
  913. ASSERT(pos >= 0)
  914. ASSERT(pos <= p->num_statements)
  915. ASSERT(name)
  916. ASSERT(out_object)
  917. int i = NCDInterpBlock_FindStatement(p->iblock, pos, name);
  918. if (i >= 0) {
  919. struct statement *ps = &p->statements[i];
  920. ASSERT(i < p->num_statements)
  921. if (ps->state == SSTATE_FORGOTTEN) {
  922. process_log(p, BLOG_ERROR, "statement (%d) is uninitialized", i);
  923. return 0;
  924. }
  925. *out_object = NCDModuleInst_Object(&ps->inst);
  926. return 1;
  927. }
  928. if (p->module_process && NCDModuleProcess_Interp_GetSpecialObj(p->module_process, name, out_object)) {
  929. return 1;
  930. }
  931. return 0;
  932. }
  933. int process_resolve_object_expr (struct process *p, int pos, char **names, NCDObject *out_object)
  934. {
  935. ASSERT(pos >= 0)
  936. ASSERT(pos <= p->num_statements)
  937. ASSERT(names)
  938. ASSERT(count_strings(names) > 0)
  939. ASSERT(out_object)
  940. NCDObject object;
  941. if (!process_find_object(p, pos, names[0], &object)) {
  942. goto fail;
  943. }
  944. if (!NCDObject_ResolveObjExpr(&object, names + 1, out_object)) {
  945. goto fail;
  946. }
  947. return 1;
  948. fail:;
  949. char *name = implode_strings(names, '.');
  950. process_log(p, BLOG_ERROR, "failed to resolve object (%s) from position %zu", (name ? name : ""), pos);
  951. free(name);
  952. return 0;
  953. }
  954. int process_resolve_variable_expr (struct process *p, int pos, char **names, NCDValMem *mem, NCDValRef *out_value)
  955. {
  956. ASSERT(pos >= 0)
  957. ASSERT(pos <= p->num_statements)
  958. ASSERT(names)
  959. ASSERT(count_strings(names) > 0)
  960. ASSERT(mem)
  961. ASSERT(out_value)
  962. NCDObject object;
  963. if (!process_find_object(p, pos, names[0], &object)) {
  964. goto fail;
  965. }
  966. if (!NCDObject_ResolveVarExpr(&object, names + 1, mem, out_value)) {
  967. goto fail;
  968. }
  969. return 1;
  970. fail:;
  971. char *name = implode_strings(names, '.');
  972. process_log(p, BLOG_ERROR, "failed to resolve variable (%s) from position %zu", (name ? name : ""), pos);
  973. free(name);
  974. return 0;
  975. }
  976. void statement_logfunc (struct statement *ps)
  977. {
  978. process_logfunc(ps->p);
  979. BLog_Append("statement %zu: ", ps->i);
  980. }
  981. void statement_log (struct statement *ps, int level, const char *fmt, ...)
  982. {
  983. if (!BLog_WouldLog(BLOG_CURRENT_CHANNEL, level)) {
  984. return;
  985. }
  986. va_list vl;
  987. va_start(vl, fmt);
  988. BLog_LogViaFuncVarArg((BLog_logfunc)statement_logfunc, ps, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  989. va_end(vl);
  990. }
  991. int statement_allocate_memory (struct statement *ps, int alloc_size, void **out_mem)
  992. {
  993. ASSERT(alloc_size >= 0)
  994. ASSERT(out_mem)
  995. if (alloc_size == 0) {
  996. *out_mem = NULL;
  997. return 1;
  998. }
  999. if (alloc_size > ps->mem_size) {
  1000. if (ps->mem && !process_mem_is_preallocated(ps->p, ps->mem)) {
  1001. free(ps->mem);
  1002. }
  1003. if (!(ps->mem = malloc(alloc_size))) {
  1004. statement_log(ps, BLOG_ERROR, "malloc failed");
  1005. ps->mem_size = 0;
  1006. return 0;
  1007. }
  1008. ps->mem_size = alloc_size;
  1009. }
  1010. *out_mem = ps->mem;
  1011. return 1;
  1012. }
  1013. int statement_resolve_argument (struct statement *ps, NCDInterpValue *arg, NCDValMem *mem, NCDValRef *out)
  1014. {
  1015. ASSERT(ps->i <= process_rap(ps->p))
  1016. ASSERT(arg)
  1017. ASSERT(mem)
  1018. ASSERT(out)
  1019. switch (arg->type) {
  1020. case NCDVALUE_STRING: {
  1021. *out = NCDVal_NewStringBin(mem, (uint8_t *)arg->string, arg->string_len);
  1022. if (NCDVal_IsInvalid(*out)) {
  1023. statement_log(ps, BLOG_ERROR, "NCDVal_NewStringBin failed");
  1024. return 0;
  1025. }
  1026. } break;
  1027. case NCDVALUE_VAR: {
  1028. if (!process_resolve_variable_expr(ps->p, ps->i, arg->variable_names, mem, out)) {
  1029. return 0;
  1030. }
  1031. if (NCDVal_IsInvalid(*out)) {
  1032. return 0;
  1033. }
  1034. } break;
  1035. case NCDVALUE_LIST: {
  1036. *out = NCDVal_NewList(mem, arg->list_count);
  1037. if (NCDVal_IsInvalid(*out)) {
  1038. statement_log(ps, BLOG_ERROR, "NCDVal_NewList failed");
  1039. return 0;
  1040. }
  1041. for (LinkedList1Node *n = LinkedList1_GetFirst(&arg->list); n; n = LinkedList1Node_Next(n)) {
  1042. struct NCDInterpValueListElem *elem = UPPER_OBJECT(n, struct NCDInterpValueListElem, list_node);
  1043. NCDValRef new_elem;
  1044. if (!statement_resolve_argument(ps, &elem->value, mem, &new_elem)) {
  1045. return 0;
  1046. }
  1047. NCDVal_ListAppend(*out, new_elem);
  1048. }
  1049. } break;
  1050. case NCDVALUE_MAP: {
  1051. *out = NCDVal_NewMap(mem, arg->map_count);
  1052. if (NCDVal_IsInvalid(*out)) {
  1053. statement_log(ps, BLOG_ERROR, "NCDVal_NewMap failed");
  1054. return 0;
  1055. }
  1056. for (LinkedList1Node *n = LinkedList1_GetFirst(&arg->maplist); n; n = LinkedList1Node_Next(n)) {
  1057. struct NCDInterpValueMapElem *elem = UPPER_OBJECT(n, struct NCDInterpValueMapElem, maplist_node);
  1058. NCDValRef new_key;
  1059. if (!statement_resolve_argument(ps, &elem->key, mem, &new_key)) {
  1060. return 0;
  1061. }
  1062. NCDValRef new_val;
  1063. if (!statement_resolve_argument(ps, &elem->val, mem, &new_val)) {
  1064. return 0;
  1065. }
  1066. int res = NCDVal_MapInsert(*out, new_key, new_val);
  1067. if (!res) {
  1068. statement_log(ps, BLOG_ERROR, "duplicate map keys");
  1069. return 0;
  1070. }
  1071. }
  1072. } break;
  1073. default: ASSERT(0);
  1074. }
  1075. return 1;
  1076. }
  1077. void statement_instance_func_event (struct statement *ps, int event)
  1078. {
  1079. ASSERT(ps->state == SSTATE_CHILD || ps->state == SSTATE_ADULT || ps->state == SSTATE_DYING)
  1080. struct process *p = ps->p;
  1081. process_assert_pointers(p);
  1082. // schedule work
  1083. process_schedule_work(p);
  1084. switch (event) {
  1085. case NCDMODULE_EVENT_UP: {
  1086. ASSERT(ps->state == SSTATE_CHILD)
  1087. statement_log(ps, BLOG_INFO, "up");
  1088. // set state ADULT
  1089. ps->state = SSTATE_ADULT;
  1090. } break;
  1091. case NCDMODULE_EVENT_DOWN: {
  1092. ASSERT(ps->state == SSTATE_ADULT)
  1093. statement_log(ps, BLOG_INFO, "down");
  1094. // set state CHILD
  1095. ps->state = SSTATE_CHILD;
  1096. // clear error
  1097. if (ps->i < p->ap) {
  1098. p->have_error = 0;
  1099. }
  1100. // update AP
  1101. if (p->ap > ps->i + 1) {
  1102. p->ap = ps->i + 1;
  1103. }
  1104. } break;
  1105. case NCDMODULE_EVENT_DEAD: {
  1106. int is_error = NCDModuleInst_HaveError(&ps->inst);
  1107. if (is_error) {
  1108. statement_log(ps, BLOG_ERROR, "died with error");
  1109. } else {
  1110. statement_log(ps, BLOG_INFO, "died");
  1111. }
  1112. // free instance
  1113. NCDModuleInst_Free(&ps->inst);
  1114. // free arguments memory
  1115. NCDValMem_Free(&ps->args_mem);
  1116. // set state FORGOTTEN
  1117. ps->state = SSTATE_FORGOTTEN;
  1118. // set error
  1119. if (is_error && ps->i < p->ap) {
  1120. p->have_error = 1;
  1121. }
  1122. // update AP
  1123. if (p->ap > ps->i) {
  1124. p->ap = ps->i;
  1125. }
  1126. // update FP
  1127. while (p->fp > 0 && p->statements[p->fp - 1].state == SSTATE_FORGOTTEN) {
  1128. p->fp--;
  1129. }
  1130. } break;
  1131. }
  1132. }
  1133. int statement_instance_func_getobj (struct statement *ps, const char *objname, NCDObject *out_object)
  1134. {
  1135. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1136. return process_find_object(ps->p, ps->i, objname, out_object);
  1137. }
  1138. int statement_instance_func_initprocess (struct statement *ps, NCDModuleProcess *mp, const char *template_name)
  1139. {
  1140. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1141. // find template
  1142. NCDProcess *p_ast;
  1143. NCDInterpBlock *iblock;
  1144. if (!NCDInterpProg_FindProcess(&iprogram, template_name, &p_ast, &iblock) || !NCDProcess_IsTemplate(p_ast)) {
  1145. statement_log(ps, BLOG_ERROR, "no template named %s", template_name);
  1146. return 0;
  1147. }
  1148. // create process
  1149. if (!process_new(p_ast, iblock, mp)) {
  1150. statement_log(ps, BLOG_ERROR, "failed to create process from template %s", template_name);
  1151. return 0;
  1152. }
  1153. statement_log(ps, BLOG_INFO, "created process from template %s", template_name);
  1154. return 1;
  1155. }
  1156. void statement_instance_logfunc (struct statement *ps)
  1157. {
  1158. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1159. statement_logfunc(ps);
  1160. BLog_Append("module: ");
  1161. }
  1162. void statement_instance_func_interp_exit (struct statement *ps, int exit_code)
  1163. {
  1164. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1165. start_terminate(exit_code);
  1166. }
  1167. int statement_instance_func_interp_getargs (struct statement *ps, NCDValMem *mem, NCDValRef *out_value)
  1168. {
  1169. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1170. *out_value = NCDVal_NewList(mem, options.num_extra_args);
  1171. if (NCDVal_IsInvalid(*out_value)) {
  1172. statement_log(ps, BLOG_ERROR, "NCDVal_NewList failed");
  1173. goto fail;
  1174. }
  1175. for (int i = 0; i < options.num_extra_args; i++) {
  1176. NCDValRef arg = NCDVal_NewString(mem, options.extra_args[i]);
  1177. if (NCDVal_IsInvalid(arg)) {
  1178. statement_log(ps, BLOG_ERROR, "NCDVal_NewString failed");
  1179. goto fail;
  1180. }
  1181. NCDVal_ListAppend(*out_value, arg);
  1182. }
  1183. return 1;
  1184. fail:
  1185. *out_value = NCDVal_NewInvalid();
  1186. return 1;
  1187. }
  1188. btime_t statement_instance_func_interp_getretrytime (struct statement *ps)
  1189. {
  1190. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1191. return options.retry_time;
  1192. }
  1193. void process_moduleprocess_func_event (struct process *p, int event)
  1194. {
  1195. ASSERT(p->module_process)
  1196. switch (event) {
  1197. case NCDMODULEPROCESS_INTERP_EVENT_CONTINUE: {
  1198. ASSERT(p->state == PSTATE_WAITING)
  1199. // set state working
  1200. p->state = PSTATE_WORKING;
  1201. // schedule work
  1202. process_schedule_work(p);
  1203. } break;
  1204. case NCDMODULEPROCESS_INTERP_EVENT_TERMINATE: {
  1205. ASSERT(p->state != PSTATE_TERMINATING)
  1206. process_log(p, BLOG_INFO, "process termination requested");
  1207. // start terminating
  1208. process_start_terminating(p);
  1209. } break;
  1210. default: ASSERT(0);
  1211. }
  1212. }
  1213. int process_moduleprocess_func_getobj (struct process *p, const char *name, NCDObject *out_object)
  1214. {
  1215. ASSERT(p->module_process)
  1216. return process_find_object(p, p->num_statements, name, out_object);
  1217. }