| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626 |
- /**
- * @file ncd.c
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * This file is part of BadVPN.
- *
- * BadVPN is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2
- * as published by the Free Software Foundation.
- *
- * BadVPN is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
- #include <stdint.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <misc/version.h>
- #include <misc/loggers_string.h>
- #include <misc/loglevel.h>
- #include <misc/offset.h>
- #include <misc/read_file.h>
- #include <misc/balloc.h>
- #include <misc/concat_strings.h>
- #include <misc/string_begins_with.h>
- #include <misc/parse_number.h>
- #include <misc/open_standard_streams.h>
- #include <structure/LinkedList1.h>
- #include <structure/LinkedList2.h>
- #include <base/BLog.h>
- #include <system/BReactor.h>
- #include <system/BSignal.h>
- #include <system/BConnection.h>
- #include <system/BProcess.h>
- #include <udevmonitor/NCDUdevManager.h>
- #include <ncd/NCDConfigParser.h>
- #include <ncd/NCDModule.h>
- #include <ncd/NCDModuleIndex.h>
- #include <ncd/modules/modules.h>
- #ifndef BADVPN_USE_WINAPI
- #include <base/BLog_syslog.h>
- #endif
- #include <ncd/ncd.h>
- #include <generated/blog_channel_ncd.h>
- #define LOGGER_STDOUT 1
- #define LOGGER_SYSLOG 2
- #define ARG_VALUE_TYPE_STRING 1
- #define ARG_VALUE_TYPE_VARIABLE 2
- #define ARG_VALUE_TYPE_LIST 3
- #define SSTATE_CHILD 1
- #define SSTATE_ADULT 2
- #define SSTATE_DYING 3
- #define SSTATE_FORGOTTEN 4
- #define PSTATE_WORKING 1
- #define PSTATE_UP 2
- #define PSTATE_WAITING 3
- #define PSTATE_TERMINATING 4
- struct arg_value {
- int type;
- union {
- char *string;
- char *variable;
- LinkedList1 list;
- };
- };
- struct arg_list_elem {
- LinkedList1Node list_node;
- struct arg_value value;
- };
- struct statement {
- char *object_name;
- char *method_name;
- struct arg_value args;
- char *name;
- };
- struct process {
- NCDModuleProcess *module_process;
- NCDValue args;
- char *name;
- size_t num_statements;
- struct process_statement *statements;
- int state;
- size_t ap;
- size_t fp;
- BTimer wait_timer;
- BPending advance_job;
- BPending work_job;
- LinkedList2Node list_node; // node in processes
- };
- struct process_statement {
- struct process *p;
- size_t i;
- struct statement s;
- int state;
- const struct NCDModule *module;
- int have_error;
- btime_t error_until;
- NCDModuleInst inst;
- NCDValue inst_args;
- };
- // command-line options
- struct {
- int help;
- int version;
- int logger;
- #ifndef BADVPN_USE_WINAPI
- char *logger_syslog_facility;
- char *logger_syslog_ident;
- #endif
- int loglevel;
- int loglevels[BLOG_NUM_CHANNELS];
- char *config_file;
- int retry_time;
- } options;
- // reactor
- BReactor ss;
- // are we terminating
- int terminating;
- // process manager
- BProcessManager manager;
- // udev manager
- NCDUdevManager umanager;
- // module index
- NCDModuleIndex mindex;
- // config AST
- struct NCDConfig_processes *config_ast;
- // processes
- LinkedList2 processes;
- static void print_help (const char *name);
- static void print_version (void);
- static int parse_arguments (int argc, char *argv[]);
- static void signal_handler (void *unused);
- static int arg_value_init_string (struct arg_value *o, const char *string);
- static int arg_value_init_variable (struct arg_value *o, const char *variable);
- static void arg_value_init_list (struct arg_value *o);
- static int arg_value_list_append (struct arg_value *o, struct arg_value v);
- static void arg_value_free (struct arg_value *o);
- static int build_arg_list_from_ast_list (struct arg_value *o, struct NCDConfig_list *list);
- static int statement_init (struct statement *s, struct NCDConfig_statements *conf);
- static void statement_free (struct statement *s);
- static int process_new (struct NCDConfig_processes *conf, NCDModuleProcess *module_process, NCDValue args);
- static void process_free (struct process *p);
- static void process_start_terminating (struct process *p);
- static void process_free_statements (struct process *p);
- static size_t process_rap (struct process *p);
- static void process_assert_pointers (struct process *p);
- static void process_logfunc (struct process *p);
- static void process_log (struct process *p, int level, const char *fmt, ...);
- static void process_schedule_work (struct process *p);
- static void process_work_job_handler (struct process *p);
- static void process_advance_job_handler (struct process *p);
- static void process_wait_timer_handler (struct process *p);
- static struct process_statement * process_find_statement (struct process *p, size_t pos, const char *name);
- static int process_resolve_name (struct process *p, size_t pos, const char *name, struct process_statement **first_ps, const char **rest);
- static int process_resolve_variable (struct process *p, size_t pos, const char *varname, NCDValue *out);
- static struct process_statement * process_resolve_object (struct process *p, size_t pos, const char *objname);
- static void process_statement_logfunc (struct process_statement *ps);
- static void process_statement_log (struct process_statement *ps, int level, const char *fmt, ...);
- static void process_statement_set_error (struct process_statement *ps);
- static int process_statement_resolve_argument (struct process_statement *ps, struct arg_value *arg, NCDValue *out);
- static void process_statement_instance_func_event (struct process_statement *ps, int event);
- static int process_statement_instance_func_getvar (struct process_statement *ps, const char *varname, NCDValue *out);
- static NCDModuleInst * process_statement_instance_func_getobj (struct process_statement *ps, const char *objname);
- static int process_statement_instance_func_initprocess (struct process_statement *ps, NCDModuleProcess *mp, const char *template_name, NCDValue args);
- static void process_statement_instance_logfunc (struct process_statement *ps);
- static void process_moduleprocess_func_event (struct process *p, int event);
- static int process_moduleprocess_func_getvar (struct process *p, const char *name, NCDValue *out);
- static NCDModuleInst * process_moduleprocess_func_getobj (struct process *p, const char *name);
- int main (int argc, char **argv)
- {
- if (argc <= 0) {
- return 1;
- }
-
- // open standard streams
- open_standard_streams();
-
- // parse command-line arguments
- if (!parse_arguments(argc, argv)) {
- fprintf(stderr, "Failed to parse arguments\n");
- print_help(argv[0]);
- goto fail0;
- }
-
- // handle --help and --version
- if (options.help) {
- print_version();
- print_help(argv[0]);
- return 0;
- }
- if (options.version) {
- print_version();
- return 0;
- }
-
- // initialize logger
- switch (options.logger) {
- case LOGGER_STDOUT:
- BLog_InitStdout();
- break;
- #ifndef BADVPN_USE_WINAPI
- case LOGGER_SYSLOG:
- if (!BLog_InitSyslog(options.logger_syslog_ident, options.logger_syslog_facility)) {
- fprintf(stderr, "Failed to initialize syslog logger\n");
- goto fail0;
- }
- break;
- #endif
- default:
- ASSERT(0);
- }
-
- // configure logger channels
- for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
- if (options.loglevels[i] >= 0) {
- BLog_SetChannelLoglevel(i, options.loglevels[i]);
- }
- else if (options.loglevel >= 0) {
- BLog_SetChannelLoglevel(i, options.loglevel);
- }
- }
-
- BLog(BLOG_NOTICE, "initializing "GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION);
-
- // initialize network
- if (!BNetwork_GlobalInit()) {
- BLog(BLOG_ERROR, "BNetwork_GlobalInit failed");
- goto fail1;
- }
-
- // init time
- BTime_Init();
-
- // init reactor
- if (!BReactor_Init(&ss)) {
- BLog(BLOG_ERROR, "BReactor_Init failed");
- goto fail1;
- }
-
- // set not terminating
- terminating = 0;
-
- // init process manager
- if (!BProcessManager_Init(&manager, &ss)) {
- BLog(BLOG_ERROR, "BProcessManager_Init failed");
- goto fail1a;
- }
-
- // init udev manager
- NCDUdevManager_Init(&umanager, &ss, &manager);
-
- // init module index
- NCDModuleIndex_Init(&mindex);
-
- // add module groups to index
- for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
- if (!NCDModuleIndex_AddGroup(&mindex, *g)) {
- BLog(BLOG_ERROR, "NCDModuleIndex_AddGroup failed");
- goto fail2;
- }
- }
-
- // setup signal handler
- if (!BSignal_Init(&ss, signal_handler, NULL)) {
- BLog(BLOG_ERROR, "BSignal_Init failed");
- goto fail2;
- }
-
- // read config file
- uint8_t *file;
- size_t file_len;
- if (!read_file(options.config_file, &file, &file_len)) {
- BLog(BLOG_ERROR, "failed to read config file");
- goto fail3;
- }
-
- // parse config file
- if (!NCDConfigParser_Parse((char *)file, file_len, &config_ast)) {
- BLog(BLOG_ERROR, "NCDConfigParser_Parse failed");
- free(file);
- goto fail3;
- }
-
- // fee config file memory
- free(file);
-
- // init module params
- struct NCDModuleInitParams params;
- params.reactor = &ss;
- params.manager = &manager;
- params.umanager = &umanager;
-
- // init modules
- size_t num_inited_modules = 0;
- for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
- if ((*g)->func_globalinit && !(*g)->func_globalinit(params)) {
- BLog(BLOG_ERROR, "globalinit failed for some module");
- goto fail5;
- }
- num_inited_modules++;
- }
-
- // init processes list
- LinkedList2_Init(&processes);
-
- // init processes
- struct NCDConfig_processes *conf = config_ast;
- while (conf) {
- if (!conf->is_template) {
- NCDValue args;
- NCDValue_InitList(&args);
- if (!process_new(conf, NULL, args)) {
- NCDValue_Free(&args);
- }
- }
- conf = conf->next;
- }
-
- // enter event loop
- BLog(BLOG_NOTICE, "entering event loop");
- BReactor_Exec(&ss);
-
- ASSERT(LinkedList2_IsEmpty(&processes))
-
- fail5:
- // free modules
- while (num_inited_modules > 0) {
- const struct NCDModuleGroup **g = &ncd_modules[num_inited_modules - 1];
- if ((*g)->func_globalfree) {
- (*g)->func_globalfree();
- }
- num_inited_modules--;
- }
- // free configuration
- NCDConfig_free_processes(config_ast);
- fail3:
- // remove signal handler
- BSignal_Finish();
- fail2:
- // free module index
- NCDModuleIndex_Free(&mindex);
-
- // free udev manager
- NCDUdevManager_Free(&umanager);
-
- // free process manager
- BProcessManager_Free(&manager);
- fail1a:
- // free reactor
- BReactor_Free(&ss);
- fail1:
- // free logger
- BLog(BLOG_NOTICE, "exiting");
- BLog_Free();
- fail0:
- // finish objects
- DebugObjectGlobal_Finish();
-
- return 1;
- }
- void print_help (const char *name)
- {
- printf(
- "Usage:\n"
- " %s\n"
- " [--help]\n"
- " [--version]\n"
- " [--logger <"LOGGERS_STRING">]\n"
- #ifndef BADVPN_USE_WINAPI
- " (logger=syslog?\n"
- " [--syslog-facility <string>]\n"
- " [--syslog-ident <string>]\n"
- " )\n"
- #endif
- " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
- " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
- " --config-file <file>\n"
- " [--retry-time <ms>]\n",
- name
- );
- }
- void print_version (void)
- {
- printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
- }
- int parse_arguments (int argc, char *argv[])
- {
- if (argc <= 0) {
- return 0;
- }
-
- options.help = 0;
- options.version = 0;
- options.logger = LOGGER_STDOUT;
- #ifndef BADVPN_USE_WINAPI
- options.logger_syslog_facility = "daemon";
- options.logger_syslog_ident = argv[0];
- #endif
- options.loglevel = -1;
- for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
- options.loglevels[i] = -1;
- }
- options.config_file = NULL;
- options.retry_time = DEFAULT_RETRY_TIME;
-
- for (int i = 1; i < argc; i++) {
- char *arg = argv[i];
- if (!strcmp(arg, "--help")) {
- options.help = 1;
- }
- else if (!strcmp(arg, "--version")) {
- options.version = 1;
- }
- else if (!strcmp(arg, "--logger")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- char *arg2 = argv[i + 1];
- if (!strcmp(arg2, "stdout")) {
- options.logger = LOGGER_STDOUT;
- }
- #ifndef BADVPN_USE_WINAPI
- else if (!strcmp(arg2, "syslog")) {
- options.logger = LOGGER_SYSLOG;
- }
- #endif
- else {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- #ifndef BADVPN_USE_WINAPI
- else if (!strcmp(arg, "--syslog-facility")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.logger_syslog_facility = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--syslog-ident")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.logger_syslog_ident = argv[i + 1];
- i++;
- }
- #endif
- else if (!strcmp(arg, "--loglevel")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--channel-loglevel")) {
- if (2 >= argc - i) {
- fprintf(stderr, "%s: requires two arguments\n", arg);
- return 0;
- }
- int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
- if (channel < 0) {
- fprintf(stderr, "%s: wrong channel argument\n", arg);
- return 0;
- }
- int loglevel = parse_loglevel(argv[i + 2]);
- if (loglevel < 0) {
- fprintf(stderr, "%s: wrong loglevel argument\n", arg);
- return 0;
- }
- options.loglevels[channel] = loglevel;
- i += 2;
- }
- else if (!strcmp(arg, "--config-file")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.config_file = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--retry-time")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.retry_time = atoi(argv[i + 1])) < 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else {
- fprintf(stderr, "unknown option: %s\n", arg);
- return 0;
- }
- }
-
- if (options.help || options.version) {
- return 1;
- }
-
- if (!options.config_file) {
- fprintf(stderr, "--config-file is required\n");
- return 0;
- }
-
- return 1;
- }
- void signal_handler (void *unused)
- {
- BLog(BLOG_NOTICE, "termination requested");
-
- if (terminating) {
- return;
- }
-
- terminating = 1;
-
- if (LinkedList2_IsEmpty(&processes)) {
- BReactor_Quit(&ss, 1);
- return;
- }
-
- // start terminating non-template processes
- LinkedList2Iterator it;
- LinkedList2Iterator_InitForward(&it, &processes);
- LinkedList2Node *n;
- while (n = LinkedList2Iterator_Next(&it)) {
- struct process *p = UPPER_OBJECT(n, struct process, list_node);
- if (p->module_process) {
- continue;
- }
- if (p->state != PSTATE_TERMINATING) {
- process_start_terminating(p);
- }
- }
- }
- int arg_value_init_string (struct arg_value *o, const char *string)
- {
- o->type = ARG_VALUE_TYPE_STRING;
- if (!(o->string = strdup(string))) {
- BLog(BLOG_ERROR, "strdup failed");
- return 0;
- }
-
- return 1;
- }
- int arg_value_init_variable (struct arg_value *o, const char *variable)
- {
- o->type = ARG_VALUE_TYPE_VARIABLE;
- if (!(o->variable = strdup(variable))) {
- BLog(BLOG_ERROR, "strdup failed");
- return 0;
- }
-
- return 1;
- }
- void arg_value_init_list (struct arg_value *o)
- {
- o->type = ARG_VALUE_TYPE_LIST;
- LinkedList1_Init(&o->list);
- }
- int arg_value_list_append (struct arg_value *o, struct arg_value v)
- {
- ASSERT(o->type == ARG_VALUE_TYPE_LIST)
-
- struct arg_list_elem *elem = malloc(sizeof(*elem));
- if (!elem) {
- BLog(BLOG_ERROR, "malloc failed");
- return 0;
- }
- LinkedList1_Append(&o->list, &elem->list_node);
- elem->value = v;
-
- return 1;
- }
- void arg_value_free (struct arg_value *o)
- {
- switch (o->type) {
- case ARG_VALUE_TYPE_STRING: {
- free(o->string);
- } break;
-
- case ARG_VALUE_TYPE_VARIABLE: {
- free(o->variable);
- } break;
-
- case ARG_VALUE_TYPE_LIST: {
- while (!LinkedList1_IsEmpty(&o->list)) {
- struct arg_list_elem *elem = UPPER_OBJECT(LinkedList1_GetFirst(&o->list), struct arg_list_elem, list_node);
- arg_value_free(&elem->value);
- LinkedList1_Remove(&o->list, &elem->list_node);
- free(elem);
- }
- } break;
-
- default: ASSERT(0);
- }
- }
- int build_arg_list_from_ast_list (struct arg_value *o, struct NCDConfig_list *list)
- {
- arg_value_init_list(o);
-
- for (struct NCDConfig_list *c = list; c; c = c->next) {
- struct arg_value e;
-
- switch (c->type) {
- case NCDCONFIG_ARG_STRING: {
- if (!arg_value_init_string(&e, c->string)) {
- goto fail;
- }
- } break;
-
- case NCDCONFIG_ARG_VAR: {
- char *variable = NCDConfig_concat_strings(c->var);
- if (!variable) {
- BLog(BLOG_ERROR, "NCDConfig_concat_strings failed");
- goto fail;
- }
-
- if (!arg_value_init_variable(&e, variable)) {
- free(variable);
- goto fail;
- }
-
- free(variable);
- } break;
-
- case NCDCONFIG_ARG_LIST: {
- if (!build_arg_list_from_ast_list(&e, c->list)) {
- goto fail;
- }
- } break;
-
- default: ASSERT(0);
- }
-
- if (!arg_value_list_append(o, e)) {
- arg_value_free(&e);
- goto fail;
- }
- }
-
- return 1;
-
- fail:
- arg_value_free(o);
- return 0;
- }
- int statement_init (struct statement *s, struct NCDConfig_statements *conf)
- {
- s->object_name = NULL;
- s->method_name = NULL;
- s->name = NULL;
-
- // set object name
- if (conf->objname) {
- if (!(s->object_name = NCDConfig_concat_strings(conf->objname))) {
- BLog(BLOG_ERROR, "NCDConfig_concat_strings failed");
- goto fail1;
- }
- }
-
- // set method name
- if (!(s->method_name = NCDConfig_concat_strings(conf->names))) {
- BLog(BLOG_ERROR, "NCDConfig_concat_strings failed");
- goto fail1;
- }
-
- // init name
- if (conf->name) {
- if (!(s->name = strdup(conf->name))) {
- BLog(BLOG_ERROR, "strdup failed");
- goto fail1;
- }
- }
-
- // init arguments
- if (!build_arg_list_from_ast_list(&s->args, conf->args)) {
- BLog(BLOG_ERROR, "build_arg_list_from_ast_list failed");
- goto fail1;
- }
-
- return 1;
-
- fail1:
- free(s->name);
- free(s->method_name);
- free(s->object_name);
- return 0;
- }
- void statement_free (struct statement *s)
- {
- // free arguments
- arg_value_free(&s->args);
-
- // free names
- free(s->name);
- free(s->method_name);
- free(s->object_name);
- }
- int process_new (struct NCDConfig_processes *conf, NCDModuleProcess *module_process, NCDValue args)
- {
- ASSERT(NCDValue_Type(&args) == NCDVALUE_LIST)
-
- // allocate strucure
- struct process *p = malloc(sizeof(*p));
- if (!p) {
- BLog(BLOG_ERROR, "malloc failed");
- goto fail0;
- }
-
- // set module process
- p->module_process = module_process;
-
- // set module process handlers
- if (p->module_process) {
- NCDModuleProcess_Interp_SetHandlers(p->module_process, p,
- (NCDModuleProcess_interp_func_event)process_moduleprocess_func_event,
- (NCDModuleProcess_interp_func_getvar)process_moduleprocess_func_getvar,
- (NCDModuleProcess_interp_func_getobj)process_moduleprocess_func_getobj);
- }
-
- // set arguments
- p->args = args;
-
- // init name
- if (!(p->name = strdup(conf->name))) {
- BLog(BLOG_ERROR, "strdup failed");
- goto fail1;
- }
-
- // count statements
- size_t num_st = 0;
- struct NCDConfig_statements *st = conf->statements;
- while (st) {
- if (num_st == SIZE_MAX) {
- BLog(BLOG_ERROR, "too many statements");
- goto fail2;
- }
- num_st++;
- st = st->next;
- }
-
- // allocate statements array
- if (!(p->statements = BAllocArray(num_st, sizeof(p->statements[0])))) {
- goto fail2;
- }
- p->num_statements = 0;
-
- // init statements
- st = conf->statements;
- while (st) {
- struct process_statement *ps = &p->statements[p->num_statements];
-
- ps->p = p;
- ps->i = p->num_statements;
-
- if (!statement_init(&ps->s, st)) {
- goto fail3;
- }
-
- ps->state = SSTATE_FORGOTTEN;
-
- ps->have_error = 0;
-
- p->num_statements++;
-
- st = st->next;
- }
-
- // set state working
- p->state = PSTATE_WORKING;
-
- // set AP=0
- p->ap = 0;
-
- // set FP=0
- p->fp = 0;
-
- // init timer
- BTimer_Init(&p->wait_timer, 0, (BTimer_handler)process_wait_timer_handler, p);
-
- // init advance job
- BPending_Init(&p->advance_job, BReactor_PendingGroup(&ss), (BPending_handler)process_advance_job_handler, p);
-
- // init work job
- BPending_Init(&p->work_job, BReactor_PendingGroup(&ss), (BPending_handler)process_work_job_handler, p);
-
- // insert to processes list
- LinkedList2_Append(&processes, &p->list_node);
-
- // schedule work
- BPending_Set(&p->work_job);
-
- return 1;
-
- fail3:
- process_free_statements(p);
- fail2:
- free(p->name);
- fail1:
- free(p);
- fail0:
- BLog(BLOG_ERROR, "failed to initialize process %s", conf->name);
- return 0;
- }
- void process_free (struct process *p)
- {
- ASSERT(p->ap == 0)
- ASSERT(p->fp == 0)
- ASSERT(p->state == PSTATE_TERMINATING)
-
- // inform module process that the process is terminated
- if (p->module_process) {
- NCDModuleProcess_Interp_Terminated(p->module_process);
- }
-
- // remove from processes list
- LinkedList2_Remove(&processes, &p->list_node);
-
- // free work job
- BPending_Free(&p->work_job);
-
- // free advance job
- BPending_Free(&p->advance_job);
-
- // free timer
- BReactor_RemoveTimer(&ss, &p->wait_timer);
-
- // free statements
- process_free_statements(p);
-
- // free name
- free(p->name);
-
- // free arguments
- NCDValue_Free(&p->args);
-
- // free strucure
- free(p);
- }
- void process_start_terminating (struct process *p)
- {
- ASSERT(p->state != PSTATE_TERMINATING)
-
- // set terminating
- p->state = PSTATE_TERMINATING;
-
- // schedule work
- process_schedule_work(p);
- }
- size_t process_rap (struct process *p)
- {
- if (p->ap > 0 && p->statements[p->ap - 1].state == SSTATE_CHILD) {
- return (p->ap - 1);
- } else {
- return p->ap;
- }
- }
- void process_free_statements (struct process *p)
- {
- // free statments
- while (p->num_statements > 0) {
- statement_free(&p->statements[p->num_statements - 1].s);
- p->num_statements--;
- }
-
- // free stataments array
- free(p->statements);
- }
- void process_assert_pointers (struct process *p)
- {
- ASSERT(p->ap <= p->num_statements)
- ASSERT(p->fp >= p->ap)
- ASSERT(p->fp <= p->num_statements)
-
- #ifndef NDEBUG
- // check AP
- for (size_t i = 0; i < p->ap; i++) {
- if (i == p->ap - 1) {
- ASSERT(p->statements[i].state == SSTATE_ADULT || p->statements[i].state == SSTATE_CHILD)
- } else {
- ASSERT(p->statements[i].state == SSTATE_ADULT)
- }
- }
-
- // check FP
- size_t fp = p->num_statements;
- while (fp > 0 && p->statements[fp - 1].state == SSTATE_FORGOTTEN) {
- fp--;
- }
- ASSERT(p->fp == fp)
- #endif
- }
- void process_logfunc (struct process *p)
- {
- BLog_Append("process %s: ", p->name);
- }
- void process_log (struct process *p, int level, const char *fmt, ...)
- {
- va_list vl;
- va_start(vl, fmt);
- BLog_LogViaFuncVarArg((BLog_logfunc)process_logfunc, p, BLOG_CURRENT_CHANNEL, level, fmt, vl);
- va_end(vl);
- }
- void process_schedule_work (struct process *p)
- {
- process_assert_pointers(p);
-
- // stop timer
- BReactor_RemoveTimer(&ss, &p->wait_timer);
-
- // stop advance job
- BPending_Unset(&p->advance_job);
-
- // schedule work
- BPending_Set(&p->work_job);
- }
- void process_work_job_handler (struct process *p)
- {
- process_assert_pointers(p);
- ASSERT(!BTimer_IsRunning(&p->wait_timer))
- ASSERT(!BPending_IsSet(&p->advance_job))
-
- if (p->state == PSTATE_WAITING) {
- return;
- }
-
- if (p->state == PSTATE_TERMINATING) {
- if (p->fp == 0) {
- // finished retreating
- process_free(p);
-
- // if program is terminating amd there are no more processes, exit program
- if (terminating && LinkedList2_IsEmpty(&processes)) {
- BReactor_Quit(&ss, 1);
- }
- return;
- }
-
- // order the last living statement to die, if needed
- struct process_statement *ps = &p->statements[p->fp - 1];
- ASSERT(ps->state != SSTATE_FORGOTTEN)
- if (ps->state != SSTATE_DYING) {
- process_statement_log(ps, BLOG_INFO, "killing");
-
- // order it to die
- NCDModuleInst_Die(&ps->inst);
-
- // set statement state DYING
- ps->state = SSTATE_DYING;
-
- // update AP
- if (p->ap > ps->i) {
- p->ap = ps->i;
- }
- }
- return;
- }
-
- // process was up but is no longer?
- if (p->state == PSTATE_UP && !(p->ap == process_rap(p) && p->ap == p->num_statements)) {
- // if we have module process, wait for its permission to continue
- if (p->module_process) {
- // set module process down
- NCDModuleProcess_Interp_Down(p->module_process);
-
- // set state waiting
- p->state = PSTATE_WAITING;
- return;
- }
-
- // set state working
- p->state = PSTATE_WORKING;
- }
-
- // cleaning up?
- if (p->ap < p->fp) {
- // order the last living statement to die, if needed
- struct process_statement *ps = &p->statements[p->fp - 1];
- if (ps->state != SSTATE_DYING) {
- process_statement_log(ps, BLOG_INFO, "killing");
-
- // order it to die
- NCDModuleInst_Die(&ps->inst);
-
- // set statement state DYING
- ps->state = SSTATE_DYING;
- }
- return;
- }
-
- // clean?
- if (p->ap > process_rap(p)) {
- ASSERT(p->ap > 0)
- ASSERT(p->ap <= p->num_statements)
-
- struct process_statement *ps = &p->statements[p->ap - 1];
- ASSERT(ps->state == SSTATE_CHILD)
-
- process_statement_log(ps, BLOG_INFO, "clean");
-
- // report clean
- NCDModuleInst_Clean(&ps->inst);
- return;
- }
-
- // advancing?
- if (p->ap < p->num_statements) {
- ASSERT(p->state == PSTATE_WORKING)
- struct process_statement *ps = &p->statements[p->ap];
- ASSERT(ps->state == SSTATE_FORGOTTEN)
-
- // clear expired error
- if (ps->have_error && ps->error_until <= btime_gettime()) {
- ps->have_error = 0;
- }
-
- if (ps->have_error) {
- process_statement_log(ps, BLOG_INFO, "waiting after error");
-
- // set wait timer
- BReactor_SetTimerAbsolute(&ss, &p->wait_timer, ps->error_until);
- } else {
- // schedule advance
- BPending_Set(&p->advance_job);
- }
- return;
- }
-
- // have we just finished?
- if (p->state == PSTATE_WORKING) {
- process_log(p, BLOG_INFO, "victory");
-
- // set module process up
- if (p->module_process) {
- NCDModuleProcess_Interp_Up(p->module_process);
- }
-
- // set state up
- p->state = PSTATE_UP;
- }
- }
- void process_advance_job_handler (struct process *p)
- {
- process_assert_pointers(p);
- ASSERT(p->ap == p->fp)
- ASSERT(p->ap == process_rap(p))
- ASSERT(p->ap < p->num_statements)
- ASSERT(!p->statements[p->ap].have_error)
- ASSERT(!BPending_IsSet(&p->work_job))
- ASSERT(!BTimer_IsRunning(&p->wait_timer))
- ASSERT(p->state == PSTATE_WORKING)
-
- struct process_statement *ps = &p->statements[p->ap];
- ASSERT(ps->state == SSTATE_FORGOTTEN)
-
- process_statement_log(ps, BLOG_INFO, "initializing");
-
- NCDModuleInst *method_object = NULL;
- char *type;
-
- // construct type
- if (!ps->s.object_name) {
- // this is a function_call(); type is "function_call"
- if (!(type = strdup(ps->s.method_name))) {
- process_statement_log(ps, BLOG_ERROR, "strdup failed");
- goto fail0;
- }
- } else {
- // this is a some.object.somewhere->method_call(); type is "base_type(some.object.somewhere)::method_call"
-
- // resolve object
- struct process_statement *obj_ps = process_resolve_object(p, p->ap, ps->s.object_name);
- if (!obj_ps) {
- process_statement_log(ps, BLOG_ERROR, "failed to resolve object %s for method call", ps->s.object_name);
- goto fail0;
- }
- ASSERT(obj_ps->state == SSTATE_ADULT)
-
- // base type defaults to type
- const char *base_type = (obj_ps->module->base_type ? obj_ps->module->base_type : obj_ps->module->type);
-
- // build type string
- if (!(type = concat_strings(3, base_type, "::", ps->s.method_name))) {
- process_statement_log(ps, BLOG_ERROR, "concat_strings failed");
- goto fail0;
- }
-
- method_object = &obj_ps->inst;
- }
-
- // find module to instantiate
- if (!(ps->module = NCDModuleIndex_FindModule(&mindex, type))) {
- process_statement_log(ps, BLOG_ERROR, "failed to find module: %s", type);
- goto fail1;
- }
-
- // resolve arguments
- if (!process_statement_resolve_argument(ps, &ps->s.args, &ps->inst_args)) {
- process_statement_log(ps, BLOG_ERROR, "failed to resolve arguments");
- goto fail1;
- }
-
- // initialize module instance
- NCDModuleInst_Init(
- &ps->inst, ps->module, method_object, &ps->inst_args, &ss, &manager, &umanager, ps,
- (NCDModuleInst_func_event)process_statement_instance_func_event,
- (NCDModuleInst_func_getvar)process_statement_instance_func_getvar,
- (NCDModuleInst_func_getobj)process_statement_instance_func_getobj,
- (NCDModuleInst_func_initprocess)process_statement_instance_func_initprocess,
- (BLog_logfunc)process_statement_instance_logfunc
- );
-
- // set statement state CHILD
- ps->state = SSTATE_CHILD;
-
- // increment AP
- p->ap++;
-
- // increment FP
- p->fp++;
-
- free(type);
-
- process_assert_pointers(p);
- return;
-
- fail1:
- free(type);
- fail0:
- // mark error
- process_statement_set_error(ps);
-
- // schedule work to start the timer
- process_schedule_work(p);
- }
- void process_wait_timer_handler (struct process *p)
- {
- process_assert_pointers(p);
- ASSERT(p->ap == p->fp)
- ASSERT(p->ap == process_rap(p))
- ASSERT(p->ap < p->num_statements)
- ASSERT(p->statements[p->ap].have_error)
- ASSERT(!BPending_IsSet(&p->work_job))
- ASSERT(!BPending_IsSet(&p->advance_job))
- ASSERT(p->state == PSTATE_WORKING)
-
- process_log(p, BLOG_INFO, "retrying");
-
- // clear error
- p->statements[p->ap].have_error = 0;
-
- // schedule work
- BPending_Set(&p->work_job);
- }
- struct process_statement * process_find_statement (struct process *p, size_t pos, const char *name)
- {
- process_assert_pointers(p);
- ASSERT(pos <= p->num_statements)
-
- for (size_t i = pos; i > 0; i--) {
- struct process_statement *ps = &p->statements[i - 1];
- if (ps->s.name && !strcmp(ps->s.name, name)) {
- return ps;
- }
- }
-
- return NULL;
- }
- int process_resolve_name (struct process *p, size_t pos, const char *name, struct process_statement **first_ps, const char **rest)
- {
- process_assert_pointers(p);
- ASSERT(pos <= p->num_statements)
- ASSERT(name)
-
- char *dot = strstr(name, ".");
- if (!dot) {
- *first_ps = process_find_statement(p, pos, name);
- *rest = NULL;
- } else {
- // copy modname and terminate
- char *modname = malloc((dot - name) + 1);
- if (!modname) {
- process_log(p, BLOG_ERROR, "malloc failed");
- return 0;
- }
- memcpy(modname, name, dot - name);
- modname[dot - name] = '\0';
-
- *first_ps = process_find_statement(p, pos, modname);
- *rest = dot + 1;
-
- free(modname);
- }
-
- return 1;
- }
- int process_resolve_variable (struct process *p, size_t pos, const char *varname, NCDValue *out)
- {
- process_assert_pointers(p);
- ASSERT(pos <= p->num_statements)
- ASSERT(varname)
-
- // find referred statement and remaining name
- struct process_statement *rps;
- const char *rest;
- if (!process_resolve_name(p, pos, varname, &rps, &rest)) {
- return 0;
- }
-
- if (!rps) {
- // handle _args
- if (!strcmp(varname, "_args")) {
- if (!NCDValue_InitCopy(out, &p->args)) {
- process_log(p, BLOG_ERROR, "NCDValue_InitCopy failed");
- return 0;
- }
-
- return 1;
- }
-
- // handle _argN
- size_t len;
- uintmax_t n;
- if ((len = string_begins_with(varname, "_arg")) && parse_unsigned_integer(varname + len, &n) && n < NCDValue_ListCount(&p->args)) {
- if (!NCDValue_InitCopy(out, NCDValue_ListGet(&p->args, n))) {
- process_log(p, BLOG_ERROR, "NCDValue_InitCopy failed");
- return 0;
- }
-
- return 1;
- }
-
- // handle special variables
- if (p->module_process) {
- if (NCDModuleProcess_Interp_GetSpecialVar(p->module_process, varname, out)) {
- return 1;
- }
- }
-
- process_log(p, BLOG_ERROR, "unknown statement name in variable: %s", varname);
- return 0;
- }
-
- // must not be forgotten
- if (rps->state == SSTATE_FORGOTTEN) {
- process_log(p, BLOG_ERROR, "referred module is uninitialized and cannot resolve variable: %s", varname);
- return 0;
- }
-
- // resolve variable in referred statement
- if (!NCDModuleInst_GetVar(&rps->inst, (rest ? rest : ""), out)) {
- process_log(p, BLOG_ERROR, "referred module failed to resolve variable: %s", varname);
- return 0;
- }
-
- return 1;
- }
- struct process_statement * process_resolve_object (struct process *p, size_t pos, const char *objname)
- {
- process_assert_pointers(p);
- ASSERT(pos <= p->num_statements)
- ASSERT(objname)
-
- // find referred statement and remaining name
- struct process_statement *rps;
- const char *rest;
- if (!process_resolve_name(p, pos, objname, &rps, &rest)) {
- return NULL;
- }
-
- if (!rps) {
- // handle special objects
- if (p->module_process) {
- NCDModuleInst *inst = NCDModuleProcess_Interp_GetSpecialObj(p->module_process, objname);
- if (inst) {
- struct process_statement *res_ps = UPPER_OBJECT(inst, struct process_statement, inst);
- ASSERT(res_ps->state == SSTATE_ADULT)
- return res_ps;
- }
- }
-
- process_log(p, BLOG_ERROR, "unknown statement name in object: %s", objname);
- return NULL;
- }
-
- // must not be forgotten
- if (rps->state == SSTATE_FORGOTTEN) {
- process_log(p, BLOG_ERROR, "referred module is uninitialized and cannot resolve object: %s", objname);
- return NULL;
- }
-
- // Resolve object in referred statement. If there is no rest, resolve empty string
- // instead, or use this statement if it fails. This allows a statement to forward method
- // calls elsewhere.
- NCDModuleInst *inst = NCDModuleInst_GetObj(&rps->inst, (rest ? rest : ""));
- if (!inst) {
- if (!rest) {
- return rps;
- }
-
- process_log(p, BLOG_ERROR, "referred module failed to resolve object: %s", objname);
- return NULL;
- }
-
- struct process_statement *res_ps = UPPER_OBJECT(inst, struct process_statement, inst);
- ASSERT(res_ps->state == SSTATE_ADULT)
-
- return res_ps;
- }
- void process_statement_logfunc (struct process_statement *ps)
- {
- process_logfunc(ps->p);
- BLog_Append("statement %zu: ", ps->i);
- }
- void process_statement_log (struct process_statement *ps, int level, const char *fmt, ...)
- {
- va_list vl;
- va_start(vl, fmt);
- BLog_LogViaFuncVarArg((BLog_logfunc)process_statement_logfunc, ps, BLOG_CURRENT_CHANNEL, level, fmt, vl);
- va_end(vl);
- }
- void process_statement_set_error (struct process_statement *ps)
- {
- ASSERT(ps->state == SSTATE_FORGOTTEN)
-
- ps->have_error = 1;
- ps->error_until = btime_add(btime_gettime(), options.retry_time);
- }
- int process_statement_resolve_argument (struct process_statement *ps, struct arg_value *arg, NCDValue *out)
- {
- ASSERT(ps->i <= process_rap(ps->p))
-
- switch (arg->type) {
- case ARG_VALUE_TYPE_STRING: {
- if (!NCDValue_InitString(out, arg->string)) {
- process_statement_log(ps, BLOG_ERROR, "NCDValue_InitString failed");
- return 0;
- }
- } break;
-
- case ARG_VALUE_TYPE_VARIABLE: {
- if (!process_resolve_variable(ps->p, ps->i, arg->variable, out)) {
- process_statement_log(ps, BLOG_ERROR, "failed to resolve variable");
- return 0;
- }
- } break;
-
- case ARG_VALUE_TYPE_LIST: do {
- NCDValue_InitList(out);
-
- for (LinkedList1Node *n = LinkedList1_GetFirst(&arg->list); n; n = LinkedList1Node_Next(n)) {
- struct arg_list_elem *elem = UPPER_OBJECT(n, struct arg_list_elem, list_node);
-
- NCDValue v;
- if (!process_statement_resolve_argument(ps, &elem->value, &v)) {
- goto list_fail1;
- }
-
- if (!NCDValue_ListAppend(out, v)) {
- process_statement_log(ps, BLOG_ERROR, "NCDValue_ListAppend failed");
- NCDValue_Free(&v);
- goto list_fail1;
- }
- }
-
- break;
-
- list_fail1:
- NCDValue_Free(out);
- return 0;
- } while (0); break;
-
- default: ASSERT(0);
- }
-
- return 1;
- }
- void process_statement_instance_func_event (struct process_statement *ps, int event)
- {
- ASSERT(ps->state == SSTATE_CHILD || ps->state == SSTATE_ADULT || ps->state == SSTATE_DYING)
-
- struct process *p = ps->p;
- process_assert_pointers(p);
-
- // schedule work
- process_schedule_work(p);
-
- switch (event) {
- case NCDMODULE_EVENT_UP: {
- ASSERT(ps->state == SSTATE_CHILD)
-
- process_statement_log(ps, BLOG_INFO, "up");
-
- // set state ADULT
- ps->state = SSTATE_ADULT;
- } break;
-
- case NCDMODULE_EVENT_DOWN: {
- ASSERT(ps->state == SSTATE_ADULT)
-
- process_statement_log(ps, BLOG_INFO, "down");
-
- // set state CHILD
- ps->state = SSTATE_CHILD;
-
- // update AP
- if (p->ap > ps->i + 1) {
- p->ap = ps->i + 1;
- }
- } break;
-
- case NCDMODULE_EVENT_DEAD: {
- int is_error = NCDModuleInst_HaveError(&ps->inst);
-
- if (is_error) {
- process_statement_log(ps, BLOG_ERROR, "died with error");
- } else {
- process_statement_log(ps, BLOG_INFO, "died");
- }
-
- // free instance
- NCDModuleInst_Free(&ps->inst);
-
- // free instance arguments
- NCDValue_Free(&ps->inst_args);
-
- // set state FORGOTTEN
- ps->state = SSTATE_FORGOTTEN;
-
- // set error
- if (is_error) {
- process_statement_set_error(ps);
- }
-
- // update AP
- if (p->ap > ps->i) {
- p->ap = ps->i;
- }
-
- // update FP
- while (p->fp > 0 && p->statements[p->fp - 1].state == SSTATE_FORGOTTEN) {
- p->fp--;
- }
- } break;
- }
- }
- int process_statement_instance_func_getvar (struct process_statement *ps, const char *varname, NCDValue *out)
- {
- ASSERT(ps->state != SSTATE_FORGOTTEN)
-
- return process_resolve_variable(ps->p, ps->i, varname, out);
- }
- NCDModuleInst * process_statement_instance_func_getobj (struct process_statement *ps, const char *objname)
- {
- ASSERT(ps->state != SSTATE_FORGOTTEN)
-
- struct process_statement *rps = process_resolve_object(ps->p, ps->i, objname);
- if (!rps) {
- return NULL;
- }
-
- return &rps->inst;
- }
- int process_statement_instance_func_initprocess (struct process_statement *ps, NCDModuleProcess *mp, const char *template_name, NCDValue args)
- {
- ASSERT(ps->state != SSTATE_FORGOTTEN)
- ASSERT(NCDValue_Type(&args) == NCDVALUE_LIST)
-
- // find template
- struct NCDConfig_processes *conf = config_ast;
- while (conf) {
- if (conf->is_template && !strcmp(conf->name, template_name)) {
- break;
- }
- conf = conf->next;
- }
-
- if (!conf) {
- process_statement_log(ps, BLOG_ERROR, "no template named %s", template_name);
- return 0;
- }
-
- // create process
- if (!process_new(conf, mp, args)) {
- process_statement_log(ps, BLOG_ERROR, "failed to create process from template %s", template_name);
- return 0;
- }
-
- process_statement_log(ps, BLOG_INFO, "created process from template %s", template_name);
-
- return 1;
- }
- void process_statement_instance_logfunc (struct process_statement *ps)
- {
- ASSERT(ps->state != SSTATE_FORGOTTEN)
-
- process_statement_logfunc(ps);
- BLog_Append("module: ");
- }
- void process_moduleprocess_func_event (struct process *p, int event)
- {
- ASSERT(p->module_process)
-
- switch (event) {
- case NCDMODULEPROCESS_INTERP_EVENT_CONTINUE: {
- ASSERT(p->state == PSTATE_WAITING)
-
- // set state working
- p->state = PSTATE_WORKING;
-
- // schedule work
- process_schedule_work(p);
- } break;
-
- case NCDMODULEPROCESS_INTERP_EVENT_TERMINATE: {
- ASSERT(p->state != PSTATE_TERMINATING)
-
- process_log(p, BLOG_INFO, "process termination requested");
-
- // start terminating
- process_start_terminating(p);
- } break;
-
- default: ASSERT(0);
- }
- }
- int process_moduleprocess_func_getvar (struct process *p, const char *name, NCDValue *out)
- {
- ASSERT(p->module_process)
-
- return process_resolve_variable(p, p->num_statements, name, out);
- }
- NCDModuleInst * process_moduleprocess_func_getobj (struct process *p, const char *name)
- {
- ASSERT(p->module_process)
-
- struct process_statement *rps = process_resolve_object(p, p->num_statements, name);
- if (!rps) {
- return NULL;
- }
-
- return &rps->inst;
- }
|