Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Enduro/X Deamon definitions
0003  *
0004  * @file ndrxd.h
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #ifndef NDRXD_H
0035 #define NDRXD_H
0036 
0037 #ifdef  __cplusplus
0038 extern "C" {
0039 #endif
0040 
0041 /*---------------------------Includes-----------------------------------*/
0042 #include <sys/param.h>
0043 #include <atmi.h>
0044 #include <atmi_int.h>
0045 #include <ndrxdcmn.h>
0046 #include <nstopwatch.h>
0047 #include <cconfig.h>
0048 #include <exenv.h>
0049 #include <libxml/xmlreader.h>
0050 #include <ndrx_ddr.h>
0051 #include <signal.h>
0052 #include <libndrxconf.h>
0053 /*---------------------------Externs------------------------------------*/
0054 extern sigset_t ndrx_G_org_mask;    /**< original process mask          */
0055 /*---------------------------Macros-------------------------------------*/
0056 #define PARSE_SECTION_FAIL         EXFAIL
0057 #define PARSE_SECTION_CONFIG      0
0058 #define PARSE_SECTION_SERVERS     1
0059 
0060 
0061 #define COMMAND_WAIT_DEFAULT        2 /**< seconds */
0062 
0063 #define RESPAWN_CNTR_MAX         1000
0064 #define DEF_SRV_STARTWAIT          30 /**< Default process startup time        */
0065 #define DEF_SRV_STOPWAIT           30 /**< Default process shutdown time       */
0066 #define DEF_RQADDRTTL              10 /**< Dflt request address time to live   */
0067     
0068 #define SANITY_CNT_IDLE            -1 /**< The PING is not issued, do not count*/
0069 #define SANITY_CNT_START           0  /**< Reset value of cycle counter        */
0070     
0071 #define MAX_SERVICE_LIST         1024 /**< MAX list of exportsvcs */
0072     
0073 #define NDRX_KILLSEQ_MAX            3   /**< Max number kill sequences          */
0074 /*---------------------------Enums--------------------------------------*/
0075 /*---------------------------Typedefs-----------------------------------*/
0076 
0077 /**
0078  * Type definition for *SERVER section entry
0079  */
0080 typedef struct conf_server_node conf_server_node_t;
0081 struct conf_server_node
0082 {
0083     char binary_name[MAXTIDENT+1]; /**< Name of the binary */
0084     /** Real binary name 
0085     char binary_name_real[MAXTIDENT+1];
0086      * */
0087     char fullpath[PATH_MAX+1]; /**< full path to executable, optional */
0088     /** Command line format (optional) */
0089     char cmdline[PATH_MAX+1];
0090     int srvid;
0091     int min;
0092     int max;
0093     int autokill;
0094     char env[PATH_MAX]; /**< Environment override file */
0095     
0096     int start_max;  /**< Max process startup time, if no server info in time
0097                      * process will be killed. */
0098     int pingtime;   /**< ping servers every X seconds (divided by sanity counter) */
0099     int ping_max;   /**< max time in which server must respond, otherwise it will be killed */
0100     int end_max;    /**< Max time in which process should exit */
0101     int killtime;   /**< periodical time for signal sending */
0102     int killseq[NDRX_KILLSEQ_MAX];  /**< Signals used for process killing      */
0103     /** list of services to export (for bridge, special). comma separated list */
0104     char exportsvcs[MAX_SERVICE_LIST]; 
0105     /** list of services that should not be exported over the bridge */
0106     char blacklistsvcs[MAX_SERVICE_LIST]; 
0107     int sleep_after;
0108     char SYSOPT[PATH_MAX/2 - 8 ]; /**< take off -i xxxxx PID */
0109     char APPOPT[PATH_MAX/2];
0110     char clopt[PATH_MAX];
0111     char rqaddr[MAXTIDENT+1];   /**< Request address */
0112     
0113     int srvstartwait; /**< Time to wait for server startup (after report in progress) */
0114     int srvstopwait; /**< Time to wait for server shutdown (after report in progress)*/
0115     
0116     /* common-config tag (loaded into NDRX_CCTAG before start) */
0117     char cctag[NDRX_CCTAG_MAX+1]; 
0118     int isprotected; /**< is binary protected... */
0119     
0120     int reloadonchange; /**< Reload binary on change - this max the monitoring of the bin */
0121     int respawn;    /**< Should we ndrxd respawn process automatically when dead */
0122     
0123     long rssmax;    /**< Default max resource memory size in bytes -1 not chk */
0124     long vszmax;    /**< Default max virtual memory size in bytes, -1 not chk */
0125     
0126     int mindispatchthreads; /**< minimum dispatch threads                     */
0127     int maxdispatchthreads; /**< maximum dispatch threads                     */
0128     int threadstacksize;    /**< thread stack size in KB, 0 - default         */
0129     int procgrp_no;          /**< process group number, 0 - not used          */
0130     int procgrp_lp_no;       /**< group number, for which process provides lock, 0 - not used */
0131     
0132     /* have entries for environment */
0133     
0134     /** linked environment groups */
0135     ndrx_env_grouplist_t *envgrouplist;
0136     
0137     /** individual environment variables */
0138     ndrx_env_list_t *envlist;
0139     
0140     /** Linked list, prev el*/
0141     conf_server_node_t *prev;
0142     /** Linked list, next el*/
0143     conf_server_node_t *next;
0144 };
0145 
0146 /**
0147  * Service node
0148  */
0149 typedef struct pm_node_svc pm_node_svc_t;
0150 struct pm_node_svc
0151 {
0152     svc_inf_t svc;
0153     pm_node_svc_t *prev;
0154     pm_node_svc_t *next;
0155 };
0156 /**
0157  * Process Model node.
0158  * Will be present in simple linked list.
0159  */
0160 typedef struct pm_node pm_node_t;
0161 struct pm_node
0162 {
0163     conf_server_node_t *conf; /* <<< This can be NULL?  */
0164     /** Name of the binary (logical name) */
0165     char binary_name[MAXTIDENT+1];
0166     /** Real binary name (if cmdline is used for startup config) */
0167     char binary_name_real[MAXTIDENT+1];
0168     /** request address, used by system V */
0169     char rqaddress[NDRX_MAX_Q_SIZE+1];
0170     int srvid;
0171     char clopt[PATH_MAX - 128]; /**< take off -i xxxxx PID and some key       */
0172     long state;             /**< process state code (current)                 */
0173     long reqstate;          /**< Requested state                              */
0174     short autostart;        /**< Start automatically after "start" cmd        */
0175     int exec_seq_try;       /**< Sequental start try                          */
0176     long rspstwatch;        /**< Sanity cycle counter for (start/ping/stop)   */
0177     long pingstwatch;       /**< Ping stopwatch  (measures stanity cycles)    */
0178     long pingtimer;    /**< Timer which counts ping intervals                 */
0179     ndrx_stopwatch_t pingroundtrip; /* Ping  roundtrip tipe                   */
0180     int pingseq;            /**< Last ping sequence sent                      */
0181             
0182     int num_term_sigs;      /**< Number of times to send term sig, before -9  */
0183     long last_sig;          /**< Time when signal was sent                    */
0184     int autokill;           /**< Kill the process if not started in time      */
0185     /**
0186      * process kill is requested (long startup, 
0187      * no ping, long shutdown)                      
0188      */
0189     int killreq;     
0190     /** process PID parent/root */
0191     pid_t pid;
0192     /** server process pid */
0193     pid_t svpid;
0194     long state_changed;     /**< Timer for state changed                      */
0195     pm_node_svc_t   *svcs;  /**< list of services                             */
0196     int flags;              /**< Flags sent by server info                    */
0197     short   nodeid;         /**< other node id, if this is bridge             */
0198     int reloadonchange_cksum; /**< Checksum code of the binary                */
0199     char binary_path[PATH_MAX+1]; /**< Path were binary lives...              */
0200     int resid;              /**< Res id to be installed in shm for poll & sysv*/
0201     int procgrp_lp_no;      /**< Group number for lock provider, actually reported */
0202     /* Linked list */
0203     pm_node_t *prev;
0204     pm_node_t *next;
0205 };
0206 
0207 /**
0208  * PID hash entry (we will have linked list for each entry.
0209  */
0210 typedef struct pm_pidhash pm_pidhash_t;
0211 struct pm_pidhash
0212 {
0213     pid_t pid;            /* process PID          */
0214     pm_node_t *p_pm;
0215     pm_pidhash_t *prev;
0216     pm_pidhash_t *next;
0217 };
0218 
0219 /**
0220  * Full configuration handler
0221  */
0222 typedef struct
0223 {
0224     /* Active monitor configuration */
0225     conf_server_node_t *monitor_config;
0226     int default_min;
0227     int default_max;
0228     int default_autokill; /**< Kill process which have not started in time */
0229     char default_env[PATH_MAX]; /**< Default env file (might be empty!)*/
0230     
0231     /* Reloadable system configuration */
0232     int sanity; /**< Sanity checking */
0233    
0234     int restart_min; /**< restart min wait sec */
0235     int restart_step; /**< restart stepping, sec */
0236     int restart_max; /**< restart max wait time, sec */
0237     
0238     int default_start_max;  /**< Max process startup time, if no server info in time
0239                              * process will be killed. */
0240     int default_pingtime;   /**< ping servers every X seconds (divided by sanity counter) */
0241     int default_ping_max;   /**< max time in which server must respond, 
0242                              * otherwise it will be killed */
0243     int default_end_max;    /**< Max time in which process should exit */
0244     int default_killtime;   /**< periodical time for signal sending */
0245     int default_killseq[NDRX_KILLSEQ_MAX];  /**< Signals used for process killing */
0246     /** Special config param for bridge services - which services to export */
0247     char default_exportsvcs[MAX_SERVICE_LIST];
0248     /** List of services that should not be exported over the bridge */
0249     char default_blacklistsvcs[MAX_SERVICE_LIST];
0250     /** common-config tag (loaded into NDRX_CCTAG before start) */
0251     char default_cctag[NDRX_CCTAG_MAX+1]; 
0252     
0253     int default_reloadonchange; /**< Reload binary on change */
0254     
0255     int qrmdelay;   /**< queue remove delay (i.e. remove only after this time + check shm on removal!) */
0256     
0257     int restart_to_check;    /**< Seconds after re-attach sanity & spawn checks will be done */  
0258     
0259     int checkpm;             /**< Time for sending info to self about process exit. */
0260     int brrefresh;           /**< Bridge refresh timer */
0261     
0262     int default_srvstartwait; /**< Time to wait for server startup (after report in progress) */
0263     int default_srvstopwait; /**< Time to wait for server shutdown (after report in progress)*/
0264     
0265     int gather_pq_stats;    /**< if set to 1, then queue stats will be gathered */
0266     int default_isprotected;    /**< if set to 1, then xadmin stop will not 
0267                                      * shutdown the process (only with -c) */
0268     int default_respawn;    /**< Set to 1 if auto respawn is required for process */
0269     
0270     /**
0271      * Request address time to live in seconds, 
0272      * used by System V to ulink queues for which no services
0273      * are present. This is case when service attaches or opens RQADDR
0274      * but process is not yet installed the information in service shared
0275      * memory. Thus ndrxd might want to unlink this, thus to stop doing that
0276      * we need a timer, for which ctime of system v queue goes older than 
0277      * ttl (in which time the xatmi server must be installed it into shm).
0278      */
0279     int rqaddrttl;
0280     char default_rqaddr[MAXTIDENT+1];   /**< Default request address */
0281     
0282     int ddrreload;     /**< rote reload setting schedule upload if previous was loaded */
0283     
0284     long default_rssmax; /**< Default max resource memory size in bytes, -1 nochk */
0285     long default_vszmax; /**< Default max virtual memory size in bytes, -1 nochk */
0286     
0287     int ctl_had_defaults; /**< Have defaults loaded atleast 1 time */
0288     
0289     int default_mindispatchthreads; /**< minimum dispatch threads             */
0290     int default_maxdispatchthreads; /**< maximum dispatch threads             */
0291     int default_threadstacksize;    /**< thread stack size in KB, 0 - default */
0292     int default_procgrp_no;         /**< Default process group number          */
0293     int default_procgrp_lp_no;      /**< Default process group number, used by lockprov */
0294     
0295     /** Environment group hash */
0296     ndrx_env_group_t *envgrouphash;
0297     
0298     /** have hash handle for services */
0299     ndrx_routcrit_hash_t *cirthash;
0300     
0301     /** have hash handle for services */
0302     ndrx_services_hash_t *services;
0303     
0304     /** malloc'd and compiled services memory block     */
0305     char *services_block;
0306     
0307     /** malloc'd an compiled routing blocks             */
0308     char *routing_block;
0309 
0310     /** process groups */
0311     ndrx_procgroups_t *procgroups;
0312     
0313 } config_t;
0314 
0315 /**
0316  * Standard command arguments
0317  */
0318 typedef struct
0319 {
0320     char    args[CMD_ARG_MAX+1];
0321     int     cmd;
0322 } cmd_arg_t;
0323 
0324 /**
0325  * Daemon configuration
0326  */
0327 typedef struct
0328 {
0329     char    *qprefix;
0330     char    *config_file;       /**< Pointer to configuration file path... */
0331     char    *config_file_short; /**< Pointer to configuration file path... */
0332     long    cmd_wait_time;      /**< Command wait time in nano-seconds (1/(10^9)) */
0333     char    *pidfile;           /**< PID file to open... */
0334     long    stat_flags;         /**< Program state flags */
0335     char    *qpath;             /**< Path to the queue directory */
0336     /* NDRXD restart: */
0337     short restarting;  /**< In restart mode, after restart_to_check expired, 
0338                         * process becomes in normal mode */
0339     ndrx_stopwatch_t time_from_restart; /**< Time counter, how long we are restarting/learning */
0340     int     fullstart;  /**< Are we in full start mode or not? */
0341     int     last_line;  /**< Last line parsed in config (for error reporting) */
0342 } sys_config_t;
0343 
0344 /**
0345  * State of the command processor
0346  */
0347 typedef struct
0348 {
0349     mqd_t listenq;                       /* The queue on which  */
0350     char  listenq_str[NDRX_MAX_Q_SIZE+1];
0351     int   context;                      /* Current context */
0352 } command_state_t;
0353 
0354 /**
0355  * Support structure for parse time
0356  */
0357 typedef struct
0358 {
0359     int     error;   /**< error is not set                */
0360     ndrx_growlist_t  stringbuffer;   /**< list used for string build */
0361     ndrx_routcrit_typehash_t *p_crit;/**< current criterion in parse, might be used for type checkings, etc. */
0362 } ndrx_ddr_parser_t;
0363 
0364 /*---------------------------Globals------------------------------------*/
0365 
0366 extern config_t     *G_app_config;          /**< Running application config   */
0367 extern sys_config_t  G_sys_config;          /**< Self daemon configuration    */
0368 extern ndrx_ddr_parser_t ndrx_G_ddrp;       /**< range Parsing time attributes */
0369 
0370 /* Active configuration:
0371  * externs from appconfig.c
0372  */
0373 extern char *G_config_file;
0374 extern pm_node_t *G_process_model;
0375 extern pm_node_t **G_process_model_hash;
0376 extern pm_pidhash_t **G_process_model_pid_hash;
0377 extern unsigned G_sanity_cycle; /* Sanity cycle */
0378 /*---------------------------Statics------------------------------------*/
0379 /*---------------------------Prototypes---------------------------------*/
0380 /* cmd processor: */
0381 extern int command_wait_and_run(int *finished, int *abort);
0382 extern int get_cmdq_attr(struct mq_attr *attr);
0383 
0384 /* pmodel */
0385 extern int remove_startfail_process(pm_node_t *p_pm, char *svcnm, pm_pidhash_t *pm_pid);
0386 extern pm_node_t * get_pm_from_srvid(int srvid);
0387 extern int remove_service_q(char *svc, short srvid, mqd_t in_qd, char *in_qstr);
0388 extern char * get_srv_admin_q(pm_node_t * p_pm);
0389 
0390 /* Configuration */
0391 extern int load_config(config_t *config, char *config_file);
0392 extern int load_active_config(config_t **app_config, pm_node_t **process_model,
0393             pm_node_t ***process_model_hash, pm_pidhash_t ***process_model_pid_hash);
0394 extern int load_active_config_live(void);
0395 /* test config or reload */
0396 extern int test_config(int reload, command_call_t * call, void (*p_reload_error)(command_call_t * call,
0397                     int srvid, char *old_bin, char *new_bin, int error, char *msg));
0398 extern int build_process_model(conf_server_node_t *p_server_conf,
0399                                 pm_node_t **p_pm_model, /* proces model linked list */
0400                                 pm_node_t **p_pm_hash/* Hash table models */);
0401 extern int add_to_pid_hash(pm_pidhash_t **pid_hash, pm_node_t *p_pm);
0402 extern int ndrxd_sigchld_init(void);
0403 extern void ndrxd_sigchld_uninit(void);
0404 extern pm_pidhash_t * pid_hash_get(pm_pidhash_t **pid_hash, pid_t pid);
0405 extern int delete_from_pid_hash(pm_pidhash_t **pid_hash, pm_pidhash_t *pm_pid);
0406 extern void sign_chld_handler(int sig);
0407 extern int cmd_close_queue(void);
0408 
0409 extern int ndrx_services_parse(config_t *config, xmlDocPtr doc, xmlNodePtr cur);
0410 extern void ndrx_services_free(config_t *config);
0411 
0412 extern int ndrx_routing_parse(config_t *config, xmlDocPtr doc, xmlNodePtr cur);
0413 extern void ddrerror(char *s, ...);
0414 
0415 extern void ndrx_ddr_apply_sanity(void);
0416 extern void ndrx_ddr_install(void);
0417 extern void ndrx_ddr_apply(void);
0418 
0419 /* Sanity & restart */
0420 extern int do_sanity_check(int isfinal);
0421 extern int self_notify(srv_status_t *status, int block);
0422 extern int remove_server_queues(char *process, pid_t pid, int srv_id, char *rplyq);
0423 extern ndrx_stopwatch_t * ndrx_get_santiy_stopwatch(void);
0424 
0425 /* Restart */
0426 extern int do_restart_actions(void);
0427 extern int do_restart_chk(void);
0428 
0429 /* Startup */
0430 extern int app_startup(command_startstop_t *call,
0431         void (*p_startup_progress)(command_startstop_t *call, pm_node_t *pm, int calltype),
0432         long *p_processes_started); /* have some progress feedback */
0433 extern int is_srvs_down(void);
0434 
0435 extern int ndrxd_unlink_pid_file(int second_call);
0436 extern void ndrx_mark_singlegrp_srv_booted(int nrgrps, int *sg_groups);
0437 
0438 /* Error handling API */
0439 extern void NDRXD_error (char *str);
0440 extern void NDRXD_set_error(int error_code);
0441 extern void NDRXD_set_error_msg(int error_code, char *msg);
0442 extern void NDRXD_set_error_fmt(int error_code, const char *fmt, ...);
0443 extern void NDRXD_unset_error(void);
0444 extern int NDRXD_is_error(void);
0445 extern void NDRXD_append_error_msg(char *msg);
0446 extern int * _ndrxd_getNDRXD_errno_addr (void);
0447 extern char * ndrxd_strerror (int err);
0448 extern char * NDRXD_error_res_get(int **error_code_ptr, int *errbufsz);
0449 
0450 #define ndrxd_errno (*_ndrxd_getNDRXD_errno_addr())
0451 
0452 /* Advertise & unadvertise */
0453 extern int cmd_xadunadv (command_call_t * call, char *data, size_t len, int context);
0454 extern int cmd_srvunadv (command_call_t * call, char *data, size_t len, int context);
0455 extern int cmd_srvadv (command_call_t * call, char *data, size_t len, int context);
0456 extern int cmd_xadreadv (command_call_t * call, char *data, size_t len, int context);
0457 
0458 extern int readv_request(int srvid, char *svc);
0459 extern char * get_srv_admin_q(pm_node_t * p_pm);
0460 extern int pq_run_santiy(int run_hist);
0461     
0462 /* reload on change: */
0463 extern int roc_is_reload_in_progress(unsigned sanity_cycle);
0464 extern int roc_check_binary(char *binary_path, unsigned sanity_cycle);
0465 extern void roc_mark_as_reloaded(char *binary_path, unsigned sanity_cycle);
0466 extern int self_sreload(pm_node_t *p_pm);
0467 
0468 extern int ndrxd_sanity_finally(void);
0469 
0470 
0471 extern void ndrx_ddr_delete_buffer(void *ptr);
0472 extern int ndrx_ddr_add_group(ndrx_routcritseq_dl_t * seq, char *grp, int is_mallocd);
0473 extern ndrx_routcritseq_dl_t * ndrx_ddr_new_rangeexpr(char *range_min, char *range_max);
0474 extern char *ndrx_ddr_new_rangeval(char *range, int is_negative, int dealloc);
0475 
0476 void ddrerror(char *s, ...);
0477 extern int ndrx_ddr_gen_blocks(config_t *config);
0478 extern void ndrx_ddr_free_all(config_t *config);
0479 
0480 #ifdef EX_USE_SYSVQ
0481 extern int do_sanity_check_sysv(int finalchk);
0482 extern int ndrxd_sysv_finally(void);
0483 #endif
0484 
0485 #ifdef  __cplusplus
0486 }
0487 #endif
0488 
0489 #endif  /* NDRXD_H */
0490 
0491 /* vim: set ts=4 sw=4 et smartindent: */