Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief TPLog routines at ATMI level
0003  *
0004  * @file atmi_tplog.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 <stdint.h>
0037 #include <stdlib.h>
0038 #include <memory.h>
0039 #include <errno.h>
0040 #include <dlfcn.h>
0041 
0042 #include <atmi.h>
0043 #include <atmi_shm.h>
0044 #include <ndrstandard.h>
0045 #include <ndebug.h>
0046 #include <ndrxdcmn.h>
0047 #include <userlog.h>
0048 #include <xa_cmn.h>
0049 #include <Exfields.h>
0050 #include <ubfutil.h>
0051 
0052 #include "tperror.h"
0053 /*---------------------------Externs------------------------------------*/
0054 /*---------------------------Macros-------------------------------------*/
0055 /*---------------------------Enums--------------------------------------*/
0056 /*---------------------------Typedefs-----------------------------------*/
0057 /*---------------------------Globals------------------------------------*/
0058 /*---------------------------Statics------------------------------------*/
0059 /*---------------------------Prototypes---------------------------------*/
0060 
0061 /**
0062  * Compare with current logger & setup it up if needed
0063  * @param new_file
0064  * @return 
0065  */
0066 exprivate int tplog_compare_set_file(char *new_file)
0067 {
0068     int changed = EXFALSE;
0069     int have_reqfile;
0070     char cur_filename[PATH_MAX];
0071     /* get the current file (if any we have) */
0072     have_reqfile = tploggetreqfile(cur_filename, sizeof(cur_filename));
0073     
0074     if (have_reqfile && 0==strcmp(new_file, cur_filename))
0075     {
0076         NDRX_LOG(log_warn, "Already logging to [%s] - not changing...", cur_filename);
0077         changed=EXFALSE;
0078     }
0079     else
0080     {
0081         /* just set it up - new file */
0082         tplogsetreqfile_direct(new_file);
0083         changed=EXTRUE;
0084     }
0085     
0086     return changed;
0087 }
0088 
0089 /**
0090  * Set the request file.
0091  * @param data optional, will search for filename here (XATMI buffer, UBF type
0092  * @param filename if file name not found in data, then use this one.
0093  *          and if type allows, then install this file name into buffer.
0094  * @param filesvc Service name to call for requesting the filename
0095  * @return SUCCEED/FAIL
0096  */
0097 expublic int ndrx_tplogsetreqfile(char **data, char *filename, char *filesvc)
0098 {
0099     int ret = EXSUCCEED;
0100     char btype[16] = {EXEOS};
0101     char stype[16] = {EXEOS};
0102     
0103     char ubf_filename[PATH_MAX] = {EXEOS};
0104     
0105     int buf_len;
0106     UBFH **p_ub = NULL;
0107     
0108     /* scenario 1 - have buffer:
0109      * Check for field existence, if exists, then get value
0110      * - compare with filename (maybe need switch) and do update UBF
0111      * - compare with current filename, if different then do switch the file
0112      */
0113     if (NULL!=data && NULL!=*data)
0114     {
0115         if (EXFAIL==ndrx_tptypes(*data, btype, stype))
0116         {
0117             EXFAIL_OUT(ret);
0118         }
0119         
0120         /* buffer is ok */
0121         if (0==strcmp(btype, "UBF") || 0==strcmp(btype, "FML") || 
0122                 0==strcmp(btype, "FML32"))
0123         {
0124             p_ub = (UBFH **)data;
0125             buf_len = sizeof(ubf_filename);
0126             
0127             if (Bpres(*p_ub, EX_NREQLOGFILE, 0))
0128             {
0129                 if (EXSUCCEED!=Bget(*p_ub, EX_NREQLOGFILE, 0, ubf_filename, &buf_len))
0130                 {
0131                     NDRX_LOG(log_error, "Failed to get EX_NREQLOGFILE: %s", 
0132                             Bstrerror(Berror));
0133                     ndrx_TPset_error_fmt(TPESYSTEM, "Failed to get EX_NREQLOGFILE: %s", 
0134                             Bstrerror(Berror));
0135                     EXFAIL_OUT(ret);
0136                 }
0137                 
0138                 /* Field exists, compare with current */
0139                 
0140                 if (NULL!=filename && EXEOS!=filename[0])
0141                 {
0142                     /* set new file */
0143                     tplog_compare_set_file(filename);
0144                     
0145                     if (0!=strcmp(ubf_filename, filename))
0146                     {
0147                         /* update UBF */
0148                         if (EXSUCCEED!=Bchg(*p_ub, EX_NREQLOGFILE, 0, filename, 0L))
0149                         {
0150                             NDRX_LOG(log_error, "Failed to set EX_NREQLOGFILE: %s", 
0151                                     Bstrerror(Berror));
0152                             
0153                             ndrx_TPset_error_fmt(TPESYSTEM, "Failed to set EX_NREQLOGFILE: %s", 
0154                                 Bstrerror(Berror));
0155                             
0156                             EXFAIL_OUT(ret);
0157                         }
0158                     }
0159                 }/* if have file name given in... */
0160                 else if (EXEOS!=ubf_filename[0])
0161                 {
0162                     /* Have file name in buffer */
0163                     tplog_compare_set_file(ubf_filename);
0164                 }
0165                 else
0166                 {
0167                     NDRX_LOG(log_warn, "Cannot set request log file: "
0168                             "no name in buffer, no name in 'filename'!");
0169                     ndrx_TPset_error_msg(TPEINVAL, "Cannot set request log file: "
0170                             "no name in buffer, no name in 'filename'!");
0171                     EXFAIL_OUT(ret);
0172                 }
0173             }
0174             else if (NULL!=filename && EXEOS!=filename[0])
0175             {
0176                 /* field does not exists, thus maybe need to install it */
0177                 tplog_compare_set_file(filename);
0178                 
0179                 /* set stuff in buffer */
0180                 if (EXSUCCEED!=Bchg(*p_ub, EX_NREQLOGFILE, 0, filename, 0L))
0181                 {
0182                     NDRX_LOG(log_error, "Failed to set EX_NREQLOGFILE: %s", 
0183                             Bstrerror(Berror));
0184 
0185                     ndrx_TPset_error_fmt(TPESYSTEM, "Failed to set EX_NREQLOGFILE: %s", 
0186                         Bstrerror(Berror));
0187 
0188                     EXFAIL_OUT(ret);
0189                 }
0190             }
0191             else if (NULL!=filesvc && EXEOS!=filesvc[0])
0192             {
0193                 long rsplen;
0194                 NDRX_LOG(log_debug, "About to call [%s] for new request "
0195                         "file log name", filesvc);
0196                 
0197                 if (EXFAIL == tpcall(filesvc, (char *)*data, 0L, (char **)data, &rsplen,TPNOTRAN))
0198                 {
0199                     NDRX_LOG(log_error, "%s failed: %s", filesvc, tpstrerror(tperrno));
0200                     /* tperror already set */
0201                     EXFAIL_OUT(ret);
0202                 }
0203                 else
0204                 {
0205                     /* call self again... 
0206                      * Only now with out request service
0207                      */
0208                     if (EXSUCCEED!=ndrx_tplogsetreqfile(data, filename, NULL))
0209                     {
0210                         EXFAIL_OUT(ret);
0211                     }
0212                 }
0213             }
0214             else
0215             {
0216                 NDRX_LOG(log_warn, "Cannot set request log file: "
0217                             "empty name in buffer, no name in 'filename'!");
0218                 ndrx_TPset_error_msg(TPEINVAL, "Cannot set request log file: "
0219                         "empty name in buffer, no name in 'filename'!");
0220                 EXFAIL_OUT(ret);
0221             }
0222         }
0223         else
0224         {
0225             NDRX_LOG(log_debug, "Buffer no UBF - cannot test request file");
0226             tplog_compare_set_file(filename);
0227         }
0228     }
0229     /*
0230      * Scenario 2: just file name
0231      */
0232     else if (NULL!=filename && EXEOS!=filename[0])
0233     {
0234         /* only have file name */
0235         tplog_compare_set_file(filename);
0236     }
0237     else
0238     {
0239         NDRX_LOG(log_warn, "Cannot set request log file: "
0240                             "no buffer and no name in 'filename'!");
0241         ndrx_TPset_error_msg(TPEINVAL, "Cannot set request log file: "
0242                 "no buffer and no name in 'filename'!");
0243         EXFAIL_OUT(ret);
0244     }
0245     
0246 out:
0247     return ret;
0248 }
0249 
0250 /**
0251  * Get the request file from buffer (if buffer is ok)
0252  * The error will be returned if there is not file or buffer invalid.
0253  * @param data
0254  * @param filename
0255  * @return 
0256  */
0257 expublic int ndrx_tploggetbufreqfile(char *data, char *filename, int bufsize)
0258 {
0259     int ret = EXSUCCEED;
0260     char btype[16] = {EXEOS};
0261     char stype[16] = {EXEOS};
0262     UBFH *p_ub;
0263     int buf_len;
0264     
0265     if (NULL!=data)
0266     {
0267         if (EXFAIL==ndrx_tptypes(data, btype, stype))
0268         {
0269             EXFAIL_OUT(ret);
0270         }
0271         
0272         /* buffer is ok */
0273         if (0==strcmp(btype, "UBF") || 0==strcmp(btype, "FML") || 
0274                 0==strcmp(btype, "FML32"))
0275         {
0276             p_ub = (UBFH *)data;
0277             buf_len = bufsize;
0278             
0279             if (Bpres(p_ub, EX_NREQLOGFILE, 0))
0280             {
0281                 if (EXSUCCEED!=Bget(p_ub, EX_NREQLOGFILE, 0, filename, &buf_len))
0282                 {
0283                     NDRX_LOG(log_error, "Failed to get EX_NREQLOGFILE: %s", 
0284                             Bstrerror(Berror));
0285                     ndrx_TPset_error_fmt(TPENOENT, "Failed to get EX_NREQLOGFILE: %s", 
0286                             Bstrerror(Berror));
0287                     EXFAIL_OUT(ret);
0288                 } 
0289             }
0290             else
0291             {
0292                 ndrx_TPset_error_fmt(TPENOENT, "No file exists: %s", 
0293                             Bstrerror(Berror));
0294                 EXFAIL_OUT(ret);
0295             }
0296         }
0297         else
0298         {
0299             ndrx_TPset_error_fmt(TPEINVAL, "Not UBF buffer: %s", 
0300                             Bstrerror(Berror));
0301             EXFAIL_OUT(ret);
0302         }
0303     }
0304     else
0305     {
0306         ndrx_TPset_error_fmt(TPEINVAL, "Null buffer: %s", 
0307                             Bstrerror(Berror));
0308         EXFAIL_OUT(ret);
0309     }
0310     
0311     
0312 out:
0313     return ret;
0314 }
0315 
0316 /**
0317  * Remove request file from buffer
0318  * @param data
0319  * @param filename
0320  * @return 
0321  */
0322 expublic int ndrx_tplogdelbufreqfile(char *data)
0323 {
0324     int ret = EXSUCCEED;
0325     char btype[16] = {EXEOS};
0326     char stype[16] = {EXEOS};
0327     UBFH *p_ub;
0328     
0329     if (NULL!=data)
0330     {
0331         if (EXFAIL==ndrx_tptypes(data, btype, stype))
0332         {
0333             EXFAIL_OUT(ret);
0334         }
0335         
0336         /* buffer is ok */
0337         if (0==strcmp(btype, "UBF") || 0==strcmp(btype, "FML") || 
0338                 0==strcmp(btype, "FML32"))
0339         {
0340             p_ub = (UBFH *)data;
0341             
0342             if (Bpres(p_ub, EX_NREQLOGFILE, 0))
0343             {
0344                 if (EXSUCCEED!=Bdel(p_ub, EX_NREQLOGFILE, 0))
0345                 {
0346                     NDRX_LOG(log_error, "Failed to get EX_NREQLOGFILE: %s", 
0347                             Bstrerror(Berror));
0348                     ndrx_TPset_error_fmt(TPENOENT, "Failed to get EX_NREQLOGFILE: %s", 
0349                             Bstrerror(Berror));
0350                     EXFAIL_OUT(ret);
0351                 } 
0352             }
0353             else
0354             {
0355                 ndrx_TPset_error_fmt(TPENOENT, "No file exists: %s", 
0356                             Bstrerror(Berror));
0357                 EXFAIL_OUT(ret);
0358             }
0359         }
0360         else
0361         {
0362             ndrx_TPset_error_fmt(TPEINVAL, "Not UBF buffer: %s", 
0363                             Bstrerror(Berror));
0364             EXFAIL_OUT(ret);
0365         }
0366     }
0367     else
0368     {
0369         ndrx_TPset_error_fmt(TPEINVAL, "Null buffer: %s", 
0370                             Bstrerror(Berror));
0371         EXFAIL_OUT(ret);
0372     }
0373     
0374 out:
0375     return ret;
0376 }
0377 /* vim: set ts=4 sw=4 et smartindent: */