ncd.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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 <limits.h>
  34. #include <misc/version.h>
  35. #include <misc/loglevel.h>
  36. #include <misc/offset.h>
  37. #include <misc/read_file.h>
  38. #include <misc/balloc.h>
  39. #include <misc/open_standard_streams.h>
  40. #include <misc/split_string.h>
  41. #include <structure/LinkedList1.h>
  42. #include <base/BLog.h>
  43. #include <base/BLog_syslog.h>
  44. #include <system/BReactor.h>
  45. #include <system/BSignal.h>
  46. #include <system/BProcess.h>
  47. #include <udevmonitor/NCDUdevManager.h>
  48. #include <ncd/NCDConfigParser.h>
  49. #include <ncd/NCDModule.h>
  50. #include <ncd/NCDModuleIndex.h>
  51. #include <ncd/NCDSugar.h>
  52. #include <ncd/NCDInterpProg.h>
  53. #include <ncd/modules/modules.h>
  54. #include "ncd.h"
  55. #include <generated/blog_channel_ncd.h>
  56. #define LOGGER_STDOUT 1
  57. #define LOGGER_STDERR 2
  58. #define LOGGER_SYSLOG 3
  59. #define SSTATE_CHILD 1
  60. #define SSTATE_ADULT 2
  61. #define SSTATE_DYING 3
  62. #define SSTATE_FORGOTTEN 4
  63. #define PSTATE_WORKING 1
  64. #define PSTATE_UP 2
  65. #define PSTATE_WAITING 3
  66. #define PSTATE_TERMINATING 4
  67. struct process;
  68. struct statement {
  69. struct process *p;
  70. NCDModuleInst inst;
  71. NCDValMem args_mem;
  72. char *mem;
  73. int mem_size;
  74. int i;
  75. int state;
  76. };
  77. struct process {
  78. NCDInterpProcess *iblock;
  79. NCDModuleProcess *module_process;
  80. BTimer wait_timer;
  81. BPending work_job;
  82. LinkedList1Node list_node; // node in processes
  83. struct statement *statements;
  84. char *mem;
  85. int mem_size;
  86. int state;
  87. int ap;
  88. int fp;
  89. int have_error;
  90. int num_statements;
  91. };
  92. // command-line options
  93. struct {
  94. int help;
  95. int version;
  96. int logger;
  97. char *logger_syslog_facility;
  98. char *logger_syslog_ident;
  99. int loglevel;
  100. int loglevels[BLOG_NUM_CHANNELS];
  101. char *config_file;
  102. int retry_time;
  103. int no_udev;
  104. char **extra_args;
  105. int num_extra_args;
  106. } options;
  107. // reactor
  108. BReactor reactor;
  109. // are we terminating
  110. int terminating;
  111. int main_exit_code;
  112. // process manager
  113. BProcessManager manager;
  114. // udev manager
  115. NCDUdevManager umanager;
  116. // method index
  117. NCDMethodIndex method_index;
  118. // module index
  119. NCDModuleIndex mindex;
  120. // program AST
  121. NCDProgram program;
  122. // placeholder database
  123. NCDPlaceholderDb placeholder_db;
  124. // structure for efficient interpretation
  125. NCDInterpProg iprogram;
  126. // common module parameters
  127. struct NCDModuleInst_params module_params;
  128. struct NCDModuleInst_iparams module_iparams;
  129. // processes
  130. LinkedList1 processes;
  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 (NCDInterpProcess *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_have_child (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 int replace_placeholders_callback (void *arg, int plid, NCDValMem *mem, NCDValRef *out);
  147. static void process_advance (struct process *p);
  148. static void process_wait_timer_handler (struct process *p);
  149. static int process_find_object (struct process *p, int pos, const char *name, NCDObject *out_object);
  150. static int process_resolve_object_expr (struct process *p, int pos, const char *names, size_t num_names, NCDObject *out_object);
  151. static int process_resolve_variable_expr (struct process *p, int pos, const char *names, size_t num_names, NCDValMem *mem, NCDValRef *out_value);
  152. static void statement_logfunc (struct statement *ps);
  153. static void statement_log (struct statement *ps, int level, const char *fmt, ...);
  154. static int statement_allocate_memory (struct statement *ps, int alloc_size);
  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 method index
  238. if (!NCDMethodIndex_Init(&method_index)) {
  239. BLog(BLOG_ERROR, "NCDMethodIndex_Init failed");
  240. goto fail1b;
  241. }
  242. // init module index
  243. if (!NCDModuleIndex_Init(&mindex)) {
  244. BLog(BLOG_ERROR, "NCDModuleIndex_Init failed");
  245. goto fail1c;
  246. }
  247. // add module groups to index
  248. for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
  249. if (!NCDModuleIndex_AddGroup(&mindex, *g, &method_index)) {
  250. BLog(BLOG_ERROR, "NCDModuleIndex_AddGroup failed");
  251. goto fail2;
  252. }
  253. }
  254. // setup signal handler
  255. if (!BSignal_Init(&reactor, signal_handler, NULL)) {
  256. BLog(BLOG_ERROR, "BSignal_Init failed");
  257. goto fail2;
  258. }
  259. // read config file
  260. uint8_t *file;
  261. size_t file_len;
  262. if (!read_file(options.config_file, &file, &file_len)) {
  263. BLog(BLOG_ERROR, "failed to read config file");
  264. goto fail3;
  265. }
  266. // parse config file
  267. if (!NCDConfigParser_Parse((char *)file, file_len, &program)) {
  268. BLog(BLOG_ERROR, "NCDConfigParser_Parse failed");
  269. free(file);
  270. goto fail3;
  271. }
  272. // fee config file memory
  273. free(file);
  274. // desugar
  275. if (!NCDSugar_Desugar(&program)) {
  276. BLog(BLOG_ERROR, "NCDSugar_Desugar failed");
  277. goto fail4;
  278. }
  279. // init placeholder database
  280. if (!NCDPlaceholderDb_Init(&placeholder_db)) {
  281. BLog(BLOG_ERROR, "NCDPlaceholderDb_Init failed");
  282. goto fail4;
  283. }
  284. // init interp program
  285. if (!NCDInterpProg_Init(&iprogram, &program, &placeholder_db, &mindex, &method_index)) {
  286. BLog(BLOG_ERROR, "NCDInterpProg_Init failed");
  287. goto fail4a;
  288. }
  289. // init module params
  290. struct NCDModuleInitParams params;
  291. params.reactor = &reactor;
  292. params.manager = &manager;
  293. params.umanager = &umanager;
  294. // init modules
  295. size_t num_inited_modules = 0;
  296. for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
  297. if ((*g)->func_globalinit && !(*g)->func_globalinit(params)) {
  298. BLog(BLOG_ERROR, "globalinit failed for some module");
  299. goto fail5;
  300. }
  301. num_inited_modules++;
  302. }
  303. // init common module params
  304. module_params.func_event = (NCDModuleInst_func_event)statement_instance_func_event;
  305. module_params.func_getobj = (NCDModuleInst_func_getobj)statement_instance_func_getobj;
  306. module_params.logfunc = (BLog_logfunc)statement_instance_logfunc;
  307. module_iparams.reactor = &reactor;
  308. module_iparams.manager = &manager;
  309. module_iparams.umanager = &umanager;
  310. module_iparams.func_initprocess = (NCDModuleInst_func_initprocess)statement_instance_func_initprocess;
  311. module_iparams.func_interp_exit = (NCDModuleInst_func_interp_exit)statement_instance_func_interp_exit;
  312. module_iparams.func_interp_getargs = (NCDModuleInst_func_interp_getargs)statement_instance_func_interp_getargs;
  313. module_iparams.func_interp_getretrytime = (NCDModuleInst_func_interp_getretrytime)statement_instance_func_interp_getretrytime;
  314. // init processes list
  315. LinkedList1_Init(&processes);
  316. // init processes
  317. for (NCDProcess *p = NCDProgram_FirstProcess(&program); p; p = NCDProgram_NextProcess(&program, p)) {
  318. if (NCDProcess_IsTemplate(p)) {
  319. continue;
  320. }
  321. // find iblock
  322. NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&iprogram, NCDProcess_Name(p));
  323. ASSERT(iprocess)
  324. if (!process_new(iprocess, NULL)) {
  325. BLog(BLOG_ERROR, "failed to initialize process, exiting");
  326. goto fail6;
  327. }
  328. }
  329. // enter event loop
  330. BLog(BLOG_NOTICE, "entering event loop");
  331. BReactor_Exec(&reactor);
  332. ASSERT(LinkedList1_IsEmpty(&processes))
  333. fail6:;
  334. LinkedList1Node *ln;
  335. while (ln = LinkedList1_GetFirst(&processes)) {
  336. struct process *p = UPPER_OBJECT(ln, struct process, list_node);
  337. NCDModuleProcess *mp;
  338. process_free(p, &mp);
  339. ASSERT(!mp)
  340. }
  341. fail5:
  342. // free modules
  343. while (num_inited_modules > 0) {
  344. const struct NCDModuleGroup **g = &ncd_modules[num_inited_modules - 1];
  345. if ((*g)->func_globalfree) {
  346. (*g)->func_globalfree();
  347. }
  348. num_inited_modules--;
  349. }
  350. // free interp program
  351. NCDInterpProg_Free(&iprogram);
  352. fail4a:
  353. // free placeholder database
  354. NCDPlaceholderDb_Free(&placeholder_db);
  355. fail4:
  356. // free program AST
  357. NCDProgram_Free(&program);
  358. fail3:
  359. // remove signal handler
  360. BSignal_Finish();
  361. fail2:
  362. // free module index
  363. NCDModuleIndex_Free(&mindex);
  364. fail1c:
  365. // free method index
  366. NCDMethodIndex_Free(&method_index);
  367. fail1b:
  368. // free udev manager
  369. NCDUdevManager_Free(&umanager);
  370. // free process manager
  371. BProcessManager_Free(&manager);
  372. fail1a:
  373. // free reactor
  374. BReactor_Free(&reactor);
  375. fail1:
  376. // free logger
  377. BLog(BLOG_NOTICE, "exiting");
  378. BLog_Free();
  379. fail0:
  380. // finish objects
  381. DebugObjectGlobal_Finish();
  382. return main_exit_code;
  383. }
  384. void print_help (const char *name)
  385. {
  386. printf(
  387. "Usage:\n"
  388. " %s\n"
  389. " [--help]\n"
  390. " [--version]\n"
  391. " [--logger <stdout/stderr/syslog>]\n"
  392. " (logger=syslog?\n"
  393. " [--syslog-facility <string>]\n"
  394. " [--syslog-ident <string>]\n"
  395. " )\n"
  396. " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
  397. " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
  398. " --config-file <file>\n"
  399. " [--retry-time <ms>]\n"
  400. " [--no-udev]\n"
  401. " [-- [<extra_arg>] ...]\n",
  402. name
  403. );
  404. }
  405. void print_version (void)
  406. {
  407. printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
  408. }
  409. int parse_arguments (int argc, char *argv[])
  410. {
  411. if (argc <= 0) {
  412. return 0;
  413. }
  414. options.help = 0;
  415. options.version = 0;
  416. options.logger = LOGGER_STDERR;
  417. options.logger_syslog_facility = "daemon";
  418. options.logger_syslog_ident = argv[0];
  419. options.loglevel = -1;
  420. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  421. options.loglevels[i] = -1;
  422. }
  423. options.config_file = NULL;
  424. options.retry_time = DEFAULT_RETRY_TIME;
  425. options.no_udev = 0;
  426. options.extra_args = NULL;
  427. options.num_extra_args = 0;
  428. for (int i = 1; i < argc; i++) {
  429. char *arg = argv[i];
  430. if (!strcmp(arg, "--help")) {
  431. options.help = 1;
  432. }
  433. else if (!strcmp(arg, "--version")) {
  434. options.version = 1;
  435. }
  436. else if (!strcmp(arg, "--logger")) {
  437. if (1 >= argc - i) {
  438. fprintf(stderr, "%s: requires an argument\n", arg);
  439. return 0;
  440. }
  441. char *arg2 = argv[i + 1];
  442. if (!strcmp(arg2, "stdout")) {
  443. options.logger = LOGGER_STDOUT;
  444. }
  445. else if (!strcmp(arg2, "stderr")) {
  446. options.logger = LOGGER_STDERR;
  447. }
  448. else if (!strcmp(arg2, "syslog")) {
  449. options.logger = LOGGER_SYSLOG;
  450. }
  451. else {
  452. fprintf(stderr, "%s: wrong argument\n", arg);
  453. return 0;
  454. }
  455. i++;
  456. }
  457. else if (!strcmp(arg, "--syslog-facility")) {
  458. if (1 >= argc - i) {
  459. fprintf(stderr, "%s: requires an argument\n", arg);
  460. return 0;
  461. }
  462. options.logger_syslog_facility = argv[i + 1];
  463. i++;
  464. }
  465. else if (!strcmp(arg, "--syslog-ident")) {
  466. if (1 >= argc - i) {
  467. fprintf(stderr, "%s: requires an argument\n", arg);
  468. return 0;
  469. }
  470. options.logger_syslog_ident = argv[i + 1];
  471. i++;
  472. }
  473. else if (!strcmp(arg, "--loglevel")) {
  474. if (1 >= argc - i) {
  475. fprintf(stderr, "%s: requires an argument\n", arg);
  476. return 0;
  477. }
  478. if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
  479. fprintf(stderr, "%s: wrong argument\n", arg);
  480. return 0;
  481. }
  482. i++;
  483. }
  484. else if (!strcmp(arg, "--channel-loglevel")) {
  485. if (2 >= argc - i) {
  486. fprintf(stderr, "%s: requires two arguments\n", arg);
  487. return 0;
  488. }
  489. int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
  490. if (channel < 0) {
  491. fprintf(stderr, "%s: wrong channel argument\n", arg);
  492. return 0;
  493. }
  494. int loglevel = parse_loglevel(argv[i + 2]);
  495. if (loglevel < 0) {
  496. fprintf(stderr, "%s: wrong loglevel argument\n", arg);
  497. return 0;
  498. }
  499. options.loglevels[channel] = loglevel;
  500. i += 2;
  501. }
  502. else if (!strcmp(arg, "--config-file")) {
  503. if (1 >= argc - i) {
  504. fprintf(stderr, "%s: requires an argument\n", arg);
  505. return 0;
  506. }
  507. options.config_file = argv[i + 1];
  508. i++;
  509. }
  510. else if (!strcmp(arg, "--retry-time")) {
  511. if (1 >= argc - i) {
  512. fprintf(stderr, "%s: requires an argument\n", arg);
  513. return 0;
  514. }
  515. if ((options.retry_time = atoi(argv[i + 1])) < 0) {
  516. fprintf(stderr, "%s: wrong argument\n", arg);
  517. return 0;
  518. }
  519. i++;
  520. }
  521. else if (!strcmp(arg, "--no-udev")) {
  522. options.no_udev = 1;
  523. }
  524. else if (!strcmp(arg, "--")) {
  525. options.extra_args = &argv[i + 1];
  526. options.num_extra_args = argc - i - 1;
  527. i += options.num_extra_args;
  528. }
  529. else {
  530. fprintf(stderr, "unknown option: %s\n", arg);
  531. return 0;
  532. }
  533. }
  534. if (options.help || options.version) {
  535. return 1;
  536. }
  537. if (!options.config_file) {
  538. fprintf(stderr, "--config-file is required\n");
  539. return 0;
  540. }
  541. return 1;
  542. }
  543. void signal_handler (void *unused)
  544. {
  545. BLog(BLOG_NOTICE, "termination requested");
  546. start_terminate(1);
  547. }
  548. void start_terminate (int exit_code)
  549. {
  550. main_exit_code = exit_code;
  551. if (terminating) {
  552. return;
  553. }
  554. terminating = 1;
  555. if (LinkedList1_IsEmpty(&processes)) {
  556. BReactor_Quit(&reactor, 0);
  557. return;
  558. }
  559. // start terminating non-template processes
  560. for (LinkedList1Node *ln = LinkedList1_GetFirst(&processes); ln; ln = LinkedList1Node_Next(ln)) {
  561. struct process *p = UPPER_OBJECT(ln, struct process, list_node);
  562. if (p->module_process) {
  563. continue;
  564. }
  565. if (p->state != PSTATE_TERMINATING) {
  566. process_start_terminating(p);
  567. }
  568. }
  569. }
  570. int process_new (NCDInterpProcess *iblock, NCDModuleProcess *module_process)
  571. {
  572. ASSERT(iblock)
  573. // get num statements
  574. int num_statements = NCDInterpProcess_NumStatements(iblock);
  575. if (num_statements > SIZE_MAX) {
  576. BLog(BLOG_ERROR, "too many statements");
  577. goto fail0;
  578. }
  579. // calculate size of preallocated statement memory
  580. int mem_size = NCDInterpProcess_PreallocSize(iblock);
  581. if (mem_size < 0 || mem_size > SIZE_MAX) {
  582. BLog(BLOG_ERROR, "NCDInterpProcess_PreallocSize failed");
  583. goto fail0;
  584. }
  585. // allocate memory
  586. void *v_statements;
  587. void *v_mem;
  588. struct process *p = BAllocThreeArrays(1, sizeof(*p), num_statements, sizeof(p->statements[0]), mem_size, 1, &v_statements, &v_mem);
  589. if (!p) {
  590. BLog(BLOG_ERROR, "BAllocSize failed");
  591. goto fail0;
  592. }
  593. // set variables
  594. p->iblock = iblock;
  595. p->module_process = module_process;
  596. p->statements = v_statements;
  597. p->mem = v_mem;
  598. p->mem_size = mem_size;
  599. p->state = PSTATE_WORKING;
  600. p->ap = 0;
  601. p->fp = 0;
  602. p->have_error = 0;
  603. p->num_statements = num_statements;
  604. // set module process handlers
  605. if (p->module_process) {
  606. NCDModuleProcess_Interp_SetHandlers(p->module_process, p,
  607. (NCDModuleProcess_interp_func_event)process_moduleprocess_func_event,
  608. (NCDModuleProcess_interp_func_getobj)process_moduleprocess_func_getobj);
  609. }
  610. // init statements
  611. for (int i = 0; i < num_statements; i++) {
  612. struct statement *ps = &p->statements[i];
  613. ps->p = p;
  614. ps->i = i;
  615. ps->state = SSTATE_FORGOTTEN;
  616. ps->mem_size = NCDInterpProcess_StatementPreallocSize(iblock, i);
  617. ps->mem = (ps->mem_size == 0 ? NULL : p->mem + NCDInterpProcess_StatementPreallocOffset(iblock, i));
  618. }
  619. // init timer
  620. BTimer_Init(&p->wait_timer, options.retry_time, (BTimer_handler)process_wait_timer_handler, p);
  621. // init work job
  622. BPending_Init(&p->work_job, BReactor_PendingGroup(&reactor), (BPending_handler)process_work_job_handler, p);
  623. // insert to processes list
  624. LinkedList1_Append(&processes, &p->list_node);
  625. // schedule work
  626. BPending_Set(&p->work_job);
  627. return 1;
  628. fail0:
  629. BLog(BLOG_ERROR, "failed to initialize process %s", NCDInterpProcess_Name(iblock));
  630. return 0;
  631. }
  632. void process_free (struct process *p, NCDModuleProcess **out_mp)
  633. {
  634. ASSERT(p->ap == 0)
  635. ASSERT(p->fp == 0)
  636. ASSERT(out_mp)
  637. // give module process to caller so it can inform the process creator that the process has terminated
  638. *out_mp = p->module_process;
  639. // free statement memory
  640. for (int i = 0; i < p->num_statements; i++) {
  641. struct statement *ps = &p->statements[i];
  642. if (ps->mem && !process_mem_is_preallocated(p, ps->mem)) {
  643. free(ps->mem);
  644. }
  645. }
  646. // remove from processes list
  647. LinkedList1_Remove(&processes, &p->list_node);
  648. // free work job
  649. BPending_Free(&p->work_job);
  650. // free timer
  651. BReactor_RemoveTimer(&reactor, &p->wait_timer);
  652. // free strucure
  653. BFree(p);
  654. }
  655. int process_mem_is_preallocated (struct process *p, char *mem)
  656. {
  657. ASSERT(mem)
  658. return (mem >= p->mem && mem < p->mem + p->mem_size);
  659. }
  660. void process_start_terminating (struct process *p)
  661. {
  662. ASSERT(p->state != PSTATE_TERMINATING)
  663. // set terminating
  664. p->state = PSTATE_TERMINATING;
  665. // schedule work
  666. process_schedule_work(p);
  667. }
  668. int process_have_child (struct process *p)
  669. {
  670. return (p->ap > 0 && p->statements[p->ap - 1].state == SSTATE_CHILD);
  671. }
  672. void process_assert_pointers (struct process *p)
  673. {
  674. ASSERT(p->ap <= p->num_statements)
  675. ASSERT(p->fp >= p->ap)
  676. ASSERT(p->fp <= p->num_statements)
  677. #ifndef NDEBUG
  678. // check AP
  679. for (int i = 0; i < p->ap; i++) {
  680. if (i == p->ap - 1) {
  681. ASSERT(p->statements[i].state == SSTATE_ADULT || p->statements[i].state == SSTATE_CHILD)
  682. } else {
  683. ASSERT(p->statements[i].state == SSTATE_ADULT)
  684. }
  685. }
  686. // check FP
  687. int fp = p->num_statements;
  688. while (fp > 0 && p->statements[fp - 1].state == SSTATE_FORGOTTEN) {
  689. fp--;
  690. }
  691. ASSERT(p->fp == fp)
  692. #endif
  693. }
  694. void process_logfunc (struct process *p)
  695. {
  696. BLog_Append("process %s: ", NCDInterpProcess_Name(p->iblock));
  697. }
  698. void process_log (struct process *p, int level, const char *fmt, ...)
  699. {
  700. va_list vl;
  701. va_start(vl, fmt);
  702. BLog_LogViaFuncVarArg((BLog_logfunc)process_logfunc, p, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  703. va_end(vl);
  704. }
  705. void process_schedule_work (struct process *p)
  706. {
  707. process_assert_pointers(p);
  708. // stop timer
  709. BReactor_RemoveTimer(&reactor, &p->wait_timer);
  710. // schedule work
  711. BPending_Set(&p->work_job);
  712. }
  713. void process_work_job_handler (struct process *p)
  714. {
  715. process_assert_pointers(p);
  716. ASSERT(!BTimer_IsRunning(&p->wait_timer))
  717. if (p->state == PSTATE_WAITING) {
  718. return;
  719. }
  720. if (p->state == PSTATE_TERMINATING) {
  721. if (p->fp == 0) {
  722. // free process
  723. NCDModuleProcess *mp;
  724. process_free(p, &mp);
  725. // if program is terminating amd there are no more processes, exit program
  726. if (terminating && LinkedList1_IsEmpty(&processes)) {
  727. ASSERT(!mp)
  728. BReactor_Quit(&reactor, 0);
  729. return;
  730. }
  731. // inform the process creator that the process has terminated
  732. if (mp) {
  733. NCDModuleProcess_Interp_Terminated(mp);
  734. return;
  735. }
  736. return;
  737. }
  738. // order the last living statement to die, if needed
  739. struct statement *ps = &p->statements[p->fp - 1];
  740. ASSERT(ps->state != SSTATE_FORGOTTEN)
  741. if (ps->state != SSTATE_DYING) {
  742. statement_log(ps, BLOG_INFO, "killing");
  743. // set statement state DYING
  744. ps->state = SSTATE_DYING;
  745. // update AP
  746. if (p->ap > ps->i) {
  747. p->ap = ps->i;
  748. }
  749. // order it to die
  750. NCDModuleInst_Die(&ps->inst);
  751. return;
  752. }
  753. return;
  754. }
  755. // process was up but is no longer?
  756. if (p->state == PSTATE_UP && !(!process_have_child(p) && p->ap == p->num_statements)) {
  757. // if we have module process, wait for its permission to continue
  758. if (p->module_process) {
  759. // set state waiting
  760. p->state = PSTATE_WAITING;
  761. // set module process down
  762. NCDModuleProcess_Interp_Down(p->module_process);
  763. return;
  764. }
  765. // set state working
  766. p->state = PSTATE_WORKING;
  767. }
  768. // cleaning up?
  769. if (p->ap < p->fp) {
  770. // order the last living statement to die, if needed
  771. struct statement *ps = &p->statements[p->fp - 1];
  772. if (ps->state != SSTATE_DYING) {
  773. statement_log(ps, BLOG_INFO, "killing");
  774. // set statement state DYING
  775. ps->state = SSTATE_DYING;
  776. // order it to die
  777. NCDModuleInst_Die(&ps->inst);
  778. return;
  779. }
  780. return;
  781. }
  782. // clean?
  783. if (process_have_child(p)) {
  784. ASSERT(p->ap > 0)
  785. ASSERT(p->ap <= p->num_statements)
  786. struct statement *ps = &p->statements[p->ap - 1];
  787. ASSERT(ps->state == SSTATE_CHILD)
  788. statement_log(ps, BLOG_INFO, "clean");
  789. // report clean
  790. NCDModuleInst_Clean(&ps->inst);
  791. return;
  792. }
  793. // advancing?
  794. if (p->ap < p->num_statements) {
  795. ASSERT(p->state == PSTATE_WORKING)
  796. struct statement *ps = &p->statements[p->ap];
  797. ASSERT(ps->state == SSTATE_FORGOTTEN)
  798. if (p->have_error) {
  799. statement_log(ps, BLOG_INFO, "waiting after error");
  800. // clear error
  801. p->have_error = 0;
  802. // set wait timer
  803. BReactor_SetTimer(&reactor, &p->wait_timer);
  804. } else {
  805. // advance
  806. process_advance(p);
  807. }
  808. return;
  809. }
  810. // have we just finished?
  811. if (p->state == PSTATE_WORKING) {
  812. process_log(p, BLOG_INFO, "victory");
  813. // set state up
  814. p->state = PSTATE_UP;
  815. // set module process up
  816. if (p->module_process) {
  817. NCDModuleProcess_Interp_Up(p->module_process);
  818. return;
  819. }
  820. }
  821. }
  822. int replace_placeholders_callback (void *arg, int plid, NCDValMem *mem, NCDValRef *out)
  823. {
  824. struct statement *ps = arg;
  825. ASSERT(plid >= 0)
  826. ASSERT(mem)
  827. ASSERT(out)
  828. const char *varnames;
  829. size_t num_names;
  830. NCDPlaceholderDb_GetVariable(&placeholder_db, plid, &varnames, &num_names);
  831. return process_resolve_variable_expr(ps->p, ps->i, varnames, num_names, mem, out);
  832. }
  833. void process_advance (struct process *p)
  834. {
  835. process_assert_pointers(p);
  836. ASSERT(p->ap == p->fp)
  837. ASSERT(!process_have_child(p))
  838. ASSERT(p->ap < p->num_statements)
  839. ASSERT(!p->have_error)
  840. ASSERT(!BPending_IsSet(&p->work_job))
  841. ASSERT(!BTimer_IsRunning(&p->wait_timer))
  842. ASSERT(p->state == PSTATE_WORKING)
  843. struct statement *ps = &p->statements[p->ap];
  844. ASSERT(ps->state == SSTATE_FORGOTTEN)
  845. statement_log(ps, BLOG_INFO, "initializing");
  846. // need to determine the module and object to use it on (if it's a method)
  847. const struct NCDModule *module;
  848. NCDObject object;
  849. NCDObject *object_ptr = NULL;
  850. // get object names, e.g. "my.cat" in "my.cat->meow();"
  851. // (or NULL if this is not a method statement)
  852. const char *objnames;
  853. size_t num_objnames;
  854. NCDInterpProcess_StatementObjNames(p->iblock, p->ap, &objnames, &num_objnames);
  855. if (!objnames) {
  856. // not a method; module is already known by NCDInterpProcess
  857. module = NCDInterpProcess_StatementGetSimpleModule(p->iblock, p->ap);
  858. if (!module) {
  859. statement_log(ps, BLOG_ERROR, "unknown simple statement: %s", NCDInterpProcess_StatementCmdName(p->iblock, p->ap));
  860. goto fail0;
  861. }
  862. } else {
  863. // get object
  864. if (!process_resolve_object_expr(p, p->ap, objnames, num_objnames, &object)) {
  865. goto fail0;
  866. }
  867. object_ptr = &object;
  868. // get object type
  869. const char *object_type = NCDObject_Type(&object);
  870. if (!object_type) {
  871. statement_log(ps, BLOG_ERROR, "cannot call method on object with no type");
  872. goto fail0;
  873. }
  874. // find module based on type of object
  875. module = NCDInterpProcess_StatementGetMethodModule(p->iblock, p->ap, object_type, &method_index);
  876. if (!module) {
  877. statement_log(ps, BLOG_ERROR, "unknown method statement: %s::%s", object_type, NCDInterpProcess_StatementCmdName(p->iblock, p->ap));
  878. goto fail0;
  879. }
  880. }
  881. // register alloc size for future preallocations
  882. NCDInterpProcess_StatementBumpAllocSize(p->iblock, p->ap, module->alloc_size);
  883. // copy arguments
  884. NCDValRef args;
  885. NCDValReplaceProg prog;
  886. if (!NCDInterpProcess_CopyStatementArgs(p->iblock, ps->i, &ps->args_mem, &args, &prog)) {
  887. statement_log(ps, BLOG_ERROR, "NCDInterpProcess_CopyStatementArgs failed");
  888. goto fail0;
  889. }
  890. // replace placeholders with values of variables
  891. if (!NCDValReplaceProg_Execute(prog, &ps->args_mem, replace_placeholders_callback, ps)) {
  892. statement_log(ps, BLOG_ERROR, "failed to replace variables in arguments with values");
  893. goto fail1;
  894. }
  895. // allocate memory
  896. if (!statement_allocate_memory(ps, module->alloc_size)) {
  897. statement_log(ps, BLOG_ERROR, "failed to allocate memory");
  898. goto fail1;
  899. }
  900. char *mem = (module->alloc_size == 0 ? NULL : ps->mem);
  901. // set statement state CHILD
  902. ps->state = SSTATE_CHILD;
  903. // increment AP
  904. p->ap++;
  905. // increment FP
  906. p->fp++;
  907. process_assert_pointers(p);
  908. // initialize module instance
  909. NCDModuleInst_Init(&ps->inst, module, mem, object_ptr, args, ps, &module_params, &module_iparams);
  910. return;
  911. fail1:
  912. NCDValMem_Free(&ps->args_mem);
  913. fail0:
  914. // set error
  915. p->have_error = 1;
  916. // schedule work to start the timer
  917. process_schedule_work(p);
  918. }
  919. void process_wait_timer_handler (struct process *p)
  920. {
  921. process_assert_pointers(p);
  922. ASSERT(p->ap == p->fp)
  923. ASSERT(!process_have_child(p))
  924. ASSERT(p->ap < p->num_statements)
  925. ASSERT(!p->have_error)
  926. ASSERT(!BPending_IsSet(&p->work_job))
  927. ASSERT(p->state == PSTATE_WORKING)
  928. process_log(p, BLOG_INFO, "retrying");
  929. // advance
  930. process_advance(p);
  931. }
  932. int process_find_object (struct process *p, int pos, const char *name, NCDObject *out_object)
  933. {
  934. ASSERT(pos >= 0)
  935. ASSERT(pos <= p->num_statements)
  936. ASSERT(name)
  937. ASSERT(out_object)
  938. int i = NCDInterpProcess_FindStatement(p->iblock, pos, name);
  939. if (i >= 0) {
  940. struct statement *ps = &p->statements[i];
  941. ASSERT(i < p->num_statements)
  942. if (ps->state == SSTATE_FORGOTTEN) {
  943. process_log(p, BLOG_ERROR, "statement (%d) is uninitialized", i);
  944. return 0;
  945. }
  946. *out_object = NCDModuleInst_Object(&ps->inst);
  947. return 1;
  948. }
  949. if (p->module_process && NCDModuleProcess_Interp_GetSpecialObj(p->module_process, name, out_object)) {
  950. return 1;
  951. }
  952. return 0;
  953. }
  954. int process_resolve_object_expr (struct process *p, int pos, const char *names, size_t num_names, NCDObject *out_object)
  955. {
  956. ASSERT(pos >= 0)
  957. ASSERT(pos <= p->num_statements)
  958. ASSERT(names)
  959. ASSERT(num_names > 0)
  960. ASSERT(out_object)
  961. NCDObject object;
  962. if (!process_find_object(p, pos, names, &object)) {
  963. goto fail;
  964. }
  965. if (!NCDObject_ResolveObjExprCompact(&object, names + strlen(names) + 1, num_names - 1, out_object)) {
  966. goto fail;
  967. }
  968. return 1;
  969. fail:;
  970. char *name = implode_compact_strings(names, num_names, '.');
  971. process_log(p, BLOG_ERROR, "failed to resolve object (%s) from position %zu", (name ? name : ""), pos);
  972. free(name);
  973. return 0;
  974. }
  975. int process_resolve_variable_expr (struct process *p, int pos, const char *names, size_t num_names, NCDValMem *mem, NCDValRef *out_value)
  976. {
  977. ASSERT(pos >= 0)
  978. ASSERT(pos <= p->num_statements)
  979. ASSERT(names)
  980. ASSERT(num_names > 0)
  981. ASSERT(mem)
  982. ASSERT(out_value)
  983. NCDObject object;
  984. if (!process_find_object(p, pos, names, &object)) {
  985. goto fail;
  986. }
  987. if (!NCDObject_ResolveVarExprCompact(&object, names + strlen(names) + 1, num_names - 1, mem, out_value)) {
  988. goto fail;
  989. }
  990. return 1;
  991. fail:;
  992. char *name = implode_compact_strings(names, num_names, '.');
  993. process_log(p, BLOG_ERROR, "failed to resolve variable (%s) from position %zu", (name ? name : ""), pos);
  994. free(name);
  995. return 0;
  996. }
  997. void statement_logfunc (struct statement *ps)
  998. {
  999. process_logfunc(ps->p);
  1000. BLog_Append("statement %zu: ", ps->i);
  1001. }
  1002. void statement_log (struct statement *ps, int level, const char *fmt, ...)
  1003. {
  1004. if (!BLog_WouldLog(BLOG_CURRENT_CHANNEL, level)) {
  1005. return;
  1006. }
  1007. va_list vl;
  1008. va_start(vl, fmt);
  1009. BLog_LogViaFuncVarArg((BLog_logfunc)statement_logfunc, ps, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  1010. va_end(vl);
  1011. }
  1012. int statement_allocate_memory (struct statement *ps, int alloc_size)
  1013. {
  1014. ASSERT(alloc_size >= 0)
  1015. if (alloc_size > ps->mem_size) {
  1016. if (ps->mem && !process_mem_is_preallocated(ps->p, ps->mem)) {
  1017. free(ps->mem);
  1018. }
  1019. if (!(ps->mem = malloc(alloc_size))) {
  1020. statement_log(ps, BLOG_ERROR, "malloc failed");
  1021. ps->mem_size = 0;
  1022. return 0;
  1023. }
  1024. ps->mem_size = alloc_size;
  1025. }
  1026. return 1;
  1027. }
  1028. void statement_instance_func_event (struct statement *ps, int event)
  1029. {
  1030. ASSERT(ps->state == SSTATE_CHILD || ps->state == SSTATE_ADULT || ps->state == SSTATE_DYING)
  1031. struct process *p = ps->p;
  1032. process_assert_pointers(p);
  1033. // schedule work
  1034. process_schedule_work(p);
  1035. switch (event) {
  1036. case NCDMODULE_EVENT_UP: {
  1037. ASSERT(ps->state == SSTATE_CHILD)
  1038. statement_log(ps, BLOG_INFO, "up");
  1039. // set state ADULT
  1040. ps->state = SSTATE_ADULT;
  1041. } break;
  1042. case NCDMODULE_EVENT_DOWN: {
  1043. ASSERT(ps->state == SSTATE_ADULT)
  1044. statement_log(ps, BLOG_INFO, "down");
  1045. // set state CHILD
  1046. ps->state = SSTATE_CHILD;
  1047. // clear error
  1048. if (ps->i < p->ap) {
  1049. p->have_error = 0;
  1050. }
  1051. // update AP
  1052. if (p->ap > ps->i + 1) {
  1053. p->ap = ps->i + 1;
  1054. }
  1055. } break;
  1056. case NCDMODULE_EVENT_DEAD: {
  1057. int is_error = NCDModuleInst_HaveError(&ps->inst);
  1058. if (is_error) {
  1059. statement_log(ps, BLOG_ERROR, "died with error");
  1060. } else {
  1061. statement_log(ps, BLOG_INFO, "died");
  1062. }
  1063. // free instance
  1064. NCDModuleInst_Free(&ps->inst);
  1065. // free arguments memory
  1066. NCDValMem_Free(&ps->args_mem);
  1067. // set state FORGOTTEN
  1068. ps->state = SSTATE_FORGOTTEN;
  1069. // set error
  1070. if (is_error && ps->i < p->ap) {
  1071. p->have_error = 1;
  1072. }
  1073. // update AP
  1074. if (p->ap > ps->i) {
  1075. p->ap = ps->i;
  1076. }
  1077. // update FP
  1078. while (p->fp > 0 && p->statements[p->fp - 1].state == SSTATE_FORGOTTEN) {
  1079. p->fp--;
  1080. }
  1081. } break;
  1082. }
  1083. }
  1084. int statement_instance_func_getobj (struct statement *ps, const char *objname, NCDObject *out_object)
  1085. {
  1086. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1087. return process_find_object(ps->p, ps->i, objname, out_object);
  1088. }
  1089. int statement_instance_func_initprocess (struct statement *ps, NCDModuleProcess *mp, const char *template_name)
  1090. {
  1091. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1092. // find process
  1093. NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&iprogram, template_name);
  1094. if (!iprocess) {
  1095. statement_log(ps, BLOG_ERROR, "no template named %s", template_name);
  1096. return 0;
  1097. }
  1098. // make sure it's a template
  1099. if (!NCDInterpProcess_IsTemplate(iprocess)) {
  1100. statement_log(ps, BLOG_ERROR, "need template to create a process, but %s is a process", template_name);
  1101. return 0;
  1102. }
  1103. // create process
  1104. if (!process_new(iprocess, mp)) {
  1105. statement_log(ps, BLOG_ERROR, "failed to create process from template %s", template_name);
  1106. return 0;
  1107. }
  1108. statement_log(ps, BLOG_INFO, "created process from template %s", template_name);
  1109. return 1;
  1110. }
  1111. void statement_instance_logfunc (struct statement *ps)
  1112. {
  1113. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1114. statement_logfunc(ps);
  1115. BLog_Append("module: ");
  1116. }
  1117. void statement_instance_func_interp_exit (struct statement *ps, int exit_code)
  1118. {
  1119. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1120. start_terminate(exit_code);
  1121. }
  1122. int statement_instance_func_interp_getargs (struct statement *ps, NCDValMem *mem, NCDValRef *out_value)
  1123. {
  1124. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1125. *out_value = NCDVal_NewList(mem, options.num_extra_args);
  1126. if (NCDVal_IsInvalid(*out_value)) {
  1127. statement_log(ps, BLOG_ERROR, "NCDVal_NewList failed");
  1128. goto fail;
  1129. }
  1130. for (int i = 0; i < options.num_extra_args; i++) {
  1131. NCDValRef arg = NCDVal_NewString(mem, options.extra_args[i]);
  1132. if (NCDVal_IsInvalid(arg)) {
  1133. statement_log(ps, BLOG_ERROR, "NCDVal_NewString failed");
  1134. goto fail;
  1135. }
  1136. NCDVal_ListAppend(*out_value, arg);
  1137. }
  1138. return 1;
  1139. fail:
  1140. *out_value = NCDVal_NewInvalid();
  1141. return 1;
  1142. }
  1143. btime_t statement_instance_func_interp_getretrytime (struct statement *ps)
  1144. {
  1145. ASSERT(ps->state != SSTATE_FORGOTTEN)
  1146. return options.retry_time;
  1147. }
  1148. void process_moduleprocess_func_event (struct process *p, int event)
  1149. {
  1150. ASSERT(p->module_process)
  1151. switch (event) {
  1152. case NCDMODULEPROCESS_INTERP_EVENT_CONTINUE: {
  1153. ASSERT(p->state == PSTATE_WAITING)
  1154. // set state working
  1155. p->state = PSTATE_WORKING;
  1156. // schedule work
  1157. process_schedule_work(p);
  1158. } break;
  1159. case NCDMODULEPROCESS_INTERP_EVENT_TERMINATE: {
  1160. ASSERT(p->state != PSTATE_TERMINATING)
  1161. process_log(p, BLOG_INFO, "process termination requested");
  1162. // start terminating
  1163. process_start_terminating(p);
  1164. } break;
  1165. default: ASSERT(0);
  1166. }
  1167. }
  1168. int process_moduleprocess_func_getobj (struct process *p, const char *name, NCDObject *out_object)
  1169. {
  1170. ASSERT(p->module_process)
  1171. return process_find_object(p, p->num_statements, name, out_object);
  1172. }