Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Common routines of the admin server
0003  *
0004  * @file commons.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 <stdio.h>
0035 #include <stdlib.h>
0036 #include <string.h>
0037 #include <errno.h>
0038 #include <regex.h>
0039 #include <utlist.h>
0040 #include <unistd.h>
0041 #include <signal.h>
0042 #include <fcntl.h>
0043 
0044 #include <ndebug.h>
0045 #include <atmi.h>
0046 #include <atmi_int.h>
0047 #include <typed_buf.h>
0048 #include <ndrstandard.h>
0049 #include <ubf.h>
0050 #include <Exfields.h>
0051 #include <Excompat.h>
0052 #include <ubfutil.h>
0053 #include <sys_unix.h>
0054 #include <gencall.h>
0055 #include <tpadm.h>
0056 #include "tpadmsv.h"
0057 #include "expr.h"
0058 /*---------------------------Externs------------------------------------*/
0059 /*---------------------------Macros-------------------------------------*/
0060 /*---------------------------Enums--------------------------------------*/
0061 /*---------------------------Typedefs-----------------------------------*/
0062 /*---------------------------Globals------------------------------------*/
0063 /**
0064  * Mapping to from classes to their operations...
0065  */
0066 expublic ndrx_adm_class_map_t ndrx_G_class_map[] =
0067 {  
0068     /* Driving of the Preparing: */
0069     {NDRX_TA_CLASS_CLIENT,      "CL",       &ndrx_adm_client_get, ndrx_G_client_map}
0070     ,{NDRX_TA_CLASS_DOMAIN,     "DM",       &ndrx_adm_domain_get, ndrx_G_domain_map}
0071     ,{NDRX_TA_CLASS_MACHINE,    "MA",       &ndrx_adm_machine_get, ndrx_G_machine_map}
0072     ,{NDRX_TA_CLASS_QUEUE,      "QU",       &ndrx_adm_queue_get, ndrx_G_queue_map}
0073     ,{NDRX_TA_CLASS_SERVER,     "SR",       &ndrx_adm_server_get, ndrx_G_server_map}
0074     ,{NDRX_TA_CLASS_SERVICE,    "SC",       &ndrx_adm_service_get, ndrx_G_service_map}
0075     ,{NDRX_TA_CLASS_SVCGRP,     "SG",       &ndrx_adm_svcgrp_get, ndrx_G_svcgrp_map}
0076     ,{NDRX_TA_CLASS_BRCON,      "BC",       &ndrx_adm_brcon_get, ndrx_G_brcon_map}
0077     ,{NULL}
0078 };
0079 
0080 /*---------------------------Statics------------------------------------*/
0081 /*---------------------------Prototypes---------------------------------*/
0082 /**
0083  * Generic ppm call
0084  * @param p_rsp_process callback of ppm
0085  * @param req_cmd NDRXD_COM_*_RQ
0086  * @param rsp_cmd NDRXD_COM_*_RP
0087  * @param dst_qstr dest queue ndrxd, bridge, etc...
0088  * @return EXSUCCEED/EXFAIL
0089  */
0090 expublic int ndrx_adm_list_call(int (*p_rsp_process)(command_reply_t *reply, size_t reply_len), 
0091         int req_cmd, int resp_cmd, char *dst_qstr)
0092 {
0093     int ret = EXSUCCEED;
0094     command_call_t call;
0095     gencall_args_t call_args[NDRXD_COM_MAX+1];
0096     
0097     struct mq_attr new_attr, org_attr;
0098     
0099     memset(&call, 0, sizeof(call));
0100     
0101     call_args[req_cmd].ndrxd_cmd = req_cmd;
0102     call_args[req_cmd].p_rsp_process = p_rsp_process;
0103     call_args[req_cmd].p_put_output = NULL;
0104     call_args[req_cmd].need_reply = EXTRUE;
0105     
0106     call_args[resp_cmd].ndrxd_cmd = resp_cmd;
0107     call_args[resp_cmd].p_rsp_process = NULL;
0108     call_args[resp_cmd].p_put_output = NULL;
0109     call_args[resp_cmd].need_reply = EXFALSE;
0110 
0111     /* set queue to blocked */
0112     memset(&org_attr, 0, sizeof(org_attr));
0113     
0114     if (EXSUCCEED!=ndrx_mq_getattr(ndrx_get_G_atmi_conf()->reply_q, 
0115                 &org_attr))
0116     {
0117         NDRX_LOG(log_error, "Failed to get attr: %s", strerror(errno));
0118         EXFAIL_OUT(ret);
0119     }
0120     
0121     memcpy(&new_attr, &org_attr, sizeof(new_attr));
0122     new_attr.mq_flags &= ~O_NONBLOCK; /* remove non block flag */
0123     
0124     if (new_attr.mq_flags!=org_attr.mq_flags)
0125     {
0126         NDRX_LOG(log_debug, "change attr to blocked");
0127         if (EXSUCCEED!=ndrx_mq_setattr(ndrx_get_G_atmi_conf()->reply_q, 
0128                 &new_attr, NULL))
0129         {
0130             NDRX_LOG(log_error, "Failed to set new attr: %s", strerror(errno));
0131             EXFAIL_OUT(ret);
0132         }
0133     }
0134 
0135     /* This will scan the service list and return the machines connected */
0136         /* Then get listing... */
0137     ret = cmd_generic_listcall(req_cmd, NDRXD_SRC_SERVER,
0138                         NDRXD_CALL_TYPE_GENERIC,
0139                         &call, sizeof(call),
0140                         ndrx_get_G_atmi_conf()->reply_q_str,
0141                         ndrx_get_G_atmi_conf()->reply_q,
0142                         (mqd_t)EXFAIL,   /* do not keep open ndrxd q open */
0143                         dst_qstr /*ndrx_get_G_atmi_conf()->ndrxd_q_str*/,
0144                         0, NULL,
0145                         NULL,
0146                         call_args,
0147                         EXFALSE,
0148                         0);
0149     
0150     /* set queue back to unblocked. */
0151     if (new_attr.mq_flags!=org_attr.mq_flags)
0152     {
0153         NDRX_LOG(log_debug, "change attr to non blocked");
0154         if (EXSUCCEED!=ndrx_mq_setattr(ndrx_get_G_atmi_conf()->reply_q, 
0155                 &org_attr, NULL))
0156         {
0157             NDRX_LOG(log_error, "Failed to set old attr: %s", strerror(errno));
0158             EXFAIL_OUT(ret);
0159         }
0160     }
0161     
0162     if (EXSUCCEED!=ret)
0163     {
0164         NDRX_LOG(log_error, "Failed to call `%s' to collect infos", dst_qstr);
0165         EXFAIL_OUT(ret);
0166     }
0167     
0168 out:
0169     return ret;
0170 }
0171 
0172 /**
0173  * Return class map
0174  * @param clazz class name
0175  * @return ptr to class descr or NULL
0176  */
0177 expublic ndrx_adm_class_map_t *ndrx_adm_class_map_get(char *clazz)
0178 {
0179     int i;
0180     
0181     for (i=0; NULL!=ndrx_G_class_map[i].clazz; i++)
0182     {
0183         if (0==strcmp(ndrx_G_class_map[i].clazz, clazz))
0184         {
0185             return &ndrx_G_class_map[i];
0186         }
0187     }
0188     
0189     return NULL;
0190 }
0191 
0192 /* vim: set ts=4 sw=4 et smartindent: */