Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Master Configuration for ndrx command line utility!
0003  *
0004  * @file config.c
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 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <memory.h>
0038 #include <sys_mqueue.h>
0039 #include <errno.h>
0040 #include <fcntl.h>           /* For O_* constants */
0041 #include <sys/stat.h>
0042 #include <unistd.h>
0043 
0044 #include <ndrstandard.h>
0045 #include <ndebug.h>
0046 #include <ndrx.h>
0047 #include <atmi.h>
0048 #include <userlog.h>
0049 #include <atmi_int.h>
0050 /*---------------------------Externs------------------------------------*/
0051 /*---------------------------Macros-------------------------------------*/
0052 /*---------------------------Enums--------------------------------------*/
0053 /*---------------------------Typedefs-----------------------------------*/
0054 /*---------------------------Globals------------------------------------*/
0055 ndrx_config_t G_config;
0056 exprivate int M_is_reply_q_open = EXFALSE;
0057 /*---------------------------Statics------------------------------------*/
0058 /*---------------------------Prototypes---------------------------------*/
0059 
0060 /**
0061  * Close shared resources
0062  */
0063 expublic int ndrx_xadmin_shm_close(void)
0064 {
0065     int ret = EXSUCCEED;
0066     
0067     if (M_is_reply_q_open)
0068     {
0069         ndrx_epoll_shmdetach();
0070 
0071         /* mark the library that needs to reconnect to shm... */
0072         ndrx_libatmi_deinit();
0073     }
0074     
0075     M_is_reply_q_open = EXFALSE;
0076     
0077 out:
0078     return ret;
0079 }
0080 
0081 /**
0082  * Open reply queue if needed
0083  * @return EXFAIL/EXSUCCEED
0084  */
0085 expublic int ndrx_xadmin_open_rply_q(void)
0086 {
0087     int ret = EXSUCCEED;
0088     int err;
0089     int erri;   /**< error index */
0090 #define NDRX_EIDX_EINVAL            0
0091 #define NDRX_EIDX_ENOSPC            1
0092 #define NDRX_EIDX_SYSERR            2
0093     
0094     char *reason[]={NDRX_QERR_MSG_EINVAL,
0095                 NDRX_QERR_MSG_ENOSPC,
0096                 NDRX_QERR_MSG_SYSERR};
0097     
0098     NDRX_LOG(log_debug, "About to open xadmin's reply queue");
0099     /* Open new queue... */
0100     if (!M_is_reply_q_open)
0101     {
0102         if ((mqd_t)EXFAIL==(G_config.reply_queue = ndrx_mq_open_at(G_config.reply_queue_str,
0103                                             O_RDWR | O_CREAT,
0104                                             S_IWUSR | S_IRUSR, NULL)))
0105         {
0106             err = errno;
0107             switch (err)
0108             {
0109                 case EINVAL:
0110                     erri = NDRX_EIDX_EINVAL;
0111                     break;
0112                 case ENOSPC:
0113                     erri = NDRX_EIDX_ENOSPC;
0114                     break;
0115                 default:
0116                     erri = NDRX_EIDX_SYSERR;
0117                     break;
0118             }
0119 
0120             NDRX_LOG(log_error, "Failed to open queue: [%s] - %s err: %s",
0121                                             G_config.reply_queue_str, 
0122                                             reason[erri], strerror(errno));
0123             userlog("Failed to open queue: [%s] - %s err: %s",
0124                                             G_config.reply_queue_str, 
0125                                             reason[erri], strerror(errno));
0126             
0127             fprintf(stderr, "%s\n", reason[erri]);
0128 
0129             ret=EXFAIL;
0130             goto out;
0131         }
0132 
0133         NDRX_LOG(log_error, "Reply queue [%s] opened!", G_config.reply_queue_str);
0134         /* Just give some warning for System */
0135         fprintf(stderr, "* Shared resources opened...\n");
0136 
0137         M_is_reply_q_open=EXTRUE;
0138         
0139     }
0140     
0141 out:
0142     return ret;
0143 }
0144 
0145 /**
0146  * Load environment configuration
0147  * @return SUCCEED/FAIL
0148  */
0149 expublic int load_env_config(void)
0150 {
0151     char *p;
0152     int ret=EXSUCCEED;
0153 
0154     memset(&G_config, 0, sizeof(G_config));
0155     G_config.ndrxd_q = (mqd_t)EXFAIL;
0156     G_config.reply_queue = (mqd_t)EXFAIL;
0157     
0158     /* Common configuration loading... */   
0159     if (EXSUCCEED!=ndrx_load_common_env())
0160     {
0161         NDRX_LOG(log_error, "Failed to load common env");
0162         ret=EXFAIL;
0163         goto out;
0164     }
0165     
0166     /* set custom timeout if available */
0167     if (NULL!=(p = getenv(CONF_NDRX_XADMINTOUT)))
0168     {
0169         int tout = atoi(p);
0170         if (EXSUCCEED!=tptoutset(tout))
0171         {
0172             NDRX_LOG(log_error, "Failed to set `xadmin' Q timeout to: %d: %s",
0173                     tout, tpstrerror(tperrno));
0174             EXFAIL_OUT(ret);
0175         }
0176     }
0177     else
0178     {
0179         /* set the list call flags 
0180          * default to no timeout use
0181          */
0182         G_config.listcall_flags|=TPNOTIME;
0183     }
0184     
0185     if (NULL==(p=getenv(CONF_NDRX_DPID)))
0186     {
0187         NDRX_LOG(log_error, "Missing %s environment variable!", CONF_NDRX_DPID);
0188     }
0189     else
0190     {
0191         NDRX_STRCPY_SAFE(G_config.pid_file, p);
0192         NDRX_LOG(log_debug, "ndrxd pid file: %s", G_config.pid_file);
0193     }
0194     
0195     G_config.qprefix = getenv(CONF_NDRX_QPREFIX);
0196     if (NULL==G_config.qprefix)
0197     {
0198         NDRX_LOG(log_error, "Missing config key %s - FAIL", CONF_NDRX_QPREFIX);
0199         ret=EXFAIL;
0200         goto out;
0201     }
0202     
0203     G_config.qpath = getenv(CONF_NDRX_QPATH);
0204     if (NULL==G_config.qpath)
0205     {
0206         NDRX_LOG(log_error, "Missing config key %s - FAIL", CONF_NDRX_QPATH);
0207         ret=EXFAIL;
0208         goto out;
0209     }
0210 
0211     /* format the admin queue */
0212     snprintf(G_config.ndrxd_q_str, sizeof(G_config.ndrxd_q_str), 
0213             NDRX_NDRXD, G_config.qprefix);
0214 
0215     /* Open client queue to wait replies on */
0216     snprintf(G_config.reply_queue_str, sizeof(G_config.reply_queue_str),
0217             NDRX_NDRXCLT, G_config.qprefix, getpid());
0218 
0219     /* Unlink previous admin queue (if have such) - ignore any error 
0220     ndrx_mq_unlink(G_config.reply_queue_str);*/
0221     NDRX_LOG(log_debug, "Reply queue: [%s]",
0222                                         G_config.reply_queue_str);
0223 
0224     /* Get config key */
0225     G_config.ndrxd_logfile = getenv(CONF_NDRX_DMNLOG);
0226     if (NULL==G_config.ndrxd_logfile)
0227     {
0228         NDRX_LOG(log_error, "Missing config key %s - FAIL", CONF_NDRX_DMNLOG);
0229         ret=EXFAIL;
0230         goto out;
0231     }
0232     
0233     p = getenv(CONF_NDRX_IPCKEY);
0234     if (NULL==p)
0235     {
0236         /* Write to ULOG? */
0237         NDRX_LOG(log_error, "Missing config key %s - FAIL", CONF_NDRX_IPCKEY);
0238         userlog("Missing config key %s - FAIL", CONF_NDRX_IPCKEY);
0239         ret=EXFAIL;
0240         goto out;
0241     }
0242     else
0243     {
0244         int tmpkey;
0245 
0246         /* bsd warning of long: */
0247         sscanf(p, "%x", &tmpkey);
0248         G_config.ipckey = tmpkey;
0249 
0250         NDRX_LOG(log_debug, "SystemV SEM IPC Key set to: [%x]",
0251                             G_config.ipckey);
0252     }
0253     
0254 out:
0255     return ret;
0256 }
0257 
0258 /* vim: set ts=4 sw=4 et smartindent: */