Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief `svmqps' print System V mapping tables
0003  *
0004  * @file cmd_svmaps.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 <errno.h>
0038 #include <memory.h>
0039 #include <sys/types.h>
0040 #include <sys/stat.h>
0041 #include <ndrstandard.h>
0042 #include <ndebug.h>
0043 
0044 #include <ndrx.h>
0045 #include <ndrxdcmn.h>
0046 #include <atmi_int.h>
0047 #include <gencall.h>
0048 
0049 #include <nstopwatch.h>
0050 #include <nclopt.h>
0051 #include <sys_unix.h>
0052 #include <utlist.h>
0053 /*---------------------------Externs------------------------------------*/
0054 /*---------------------------Macros-------------------------------------*/
0055 /*---------------------------Enums--------------------------------------*/
0056 /*---------------------------Typedefs-----------------------------------*/
0057 /*---------------------------Globals------------------------------------*/
0058 /*---------------------------Statics------------------------------------*/
0059 /*---------------------------Prototypes---------------------------------*/
0060 
0061 
0062 /**
0063  * Print header
0064  * @return
0065  */
0066 exprivate void print_hdr(void)
0067 {
0068     fprintf(stderr, "  SLOT      MSGID FLAGS                CTIME Q NAME\n");
0069     fprintf(stderr, "------ ---------- ----------------- -------- -----------------------------------\n");
0070 }
0071 
0072 /**
0073  * Print System V queue mapping tables
0074  * @param p_cmd_map
0075  * @param argc
0076  * @param argv
0077  * @return SUCCEED
0078  */
0079 expublic int cmd_svmaps(cmd_mapping_t *p_cmd_map, int argc, char **argv, int *p_have_next)
0080 {
0081     int ret=EXSUCCEED;
0082     short print_systemv = EXFALSE;
0083     short print_posix =  EXFALSE;
0084     short print_all =  EXFALSE;
0085     short print_isused =  EXFALSE;
0086     short print_wasused =  EXFALSE;
0087     int attach_status = EXFALSE;
0088     ndrx_shm_t *map_p2s;
0089     ndrx_shm_t *map_s2p;
0090     ndrx_sem_t *map_sem;
0091     int i, queuesmax;
0092     ndrx_svq_map_t *el;
0093     ndrx_shm_t *map;
0094     char flagsstr[128];
0095     int total = 0;
0096     ncloptmap_t clopt[] =
0097     {
0098         {'p', BFLD_SHORT, (void *)&print_posix, 0, 
0099                                 NCLOPT_OPT | NCLOPT_TRUEBOOL, 
0100                                 "Posix -> System V mapping table (default)"},
0101         {'s', BFLD_SHORT, (void *)&print_systemv, 0, 
0102                                 NCLOPT_OPT | NCLOPT_TRUEBOOL, 
0103                                 "Print System V -> Posix mapping table"},
0104         {'a', BFLD_SHORT, (void *)&print_all, 0, 
0105                                 NCLOPT_OPT | NCLOPT_TRUEBOOL, 
0106                                 "Print all entries"},
0107         {'i', BFLD_SHORT, (void *)&print_isused, 0, 
0108                                 NCLOPT_OPT | NCLOPT_TRUEBOOL, 
0109                                 "Print entries in use"},
0110         {'w', BFLD_SHORT, (void *)&print_wasused, 0, 
0111                                 NCLOPT_OPT | NCLOPT_TRUEBOOL, 
0112                                 "Print entries which was used"},
0113         {0}
0114     };
0115     
0116     /* parse command line */
0117     if (nstd_parse_clopt(clopt, EXTRUE,  argc, argv, EXFALSE))
0118     {
0119         fprintf(stderr, XADMIN_INVALID_OPTIONS_MSG);
0120         EXFAIL_OUT(ret);
0121     }
0122     
0123     if (!print_isused && !print_wasused && !print_all)
0124     {
0125         print_isused = EXTRUE;
0126     }
0127     
0128     if (EXFAIL==(attach_status = ndrx_svqshm_attach()))
0129     {
0130         fprintf(stderr, "* System V shared resources does not exists\n");
0131         NDRX_LOG(log_error, "System V shared resources does not exists");
0132         EXFAIL_OUT(ret);
0133     }
0134     
0135     if (!ndrx_svqshm_shmres_get(&map_p2s, &map_s2p, &map_sem, &queuesmax))
0136     {
0137         fprintf(stderr, "System malfunction, System V SHM must be open!\n");
0138         NDRX_LOG(log_error, "System malfunction, System V SHM must be open");
0139         userlog("System malfunction, System V SHM must be open");
0140         EXFAIL_OUT(ret);
0141     }
0142     
0143     /* Print header at first step! */
0144     print_hdr();
0145     
0146     /* select table & process that flags and print the entries... */
0147     
0148     /* Read lock resources  */
0149     map = map_p2s;
0150     
0151     if (print_systemv)
0152     {
0153         map = map_s2p;
0154     }
0155     
0156     /* lock the memory */
0157     /* ###################### CRITICAL SECTION ############################### */
0158     if (EXSUCCEED!=ndrx_sem_rwlock(map_sem, 0, NDRX_SEM_TYP_READ))
0159     {
0160         goto out;
0161     }
0162     
0163     for (i=0; i<queuesmax; i++)
0164     {
0165         el = NDRX_SVQ_INDEX(map->mem, i);
0166         
0167         if (print_all ||
0168                 (print_isused && (el->flags & NDRX_SVQ_MAP_ISUSED)) ||
0169                 
0170                 (print_wasused && (el->flags & NDRX_SVQ_MAP_WASUSED) && 
0171                     !(el->flags & NDRX_SVQ_MAP_ISUSED))
0172                 
0173                 )
0174         {
0175             flagsstr[0]=EXEOS;
0176             
0177             if (el->flags & NDRX_SVQ_MAP_ISUSED)
0178             {
0179                 if (EXEOS!=flagsstr[0])
0180                 {
0181                     strcat(flagsstr, ",");
0182                 }
0183                 strcat(flagsstr, "iuse");
0184             }
0185             else if (el->flags & NDRX_SVQ_MAP_WASUSED)
0186             {
0187                 if (EXEOS!=flagsstr[0])
0188                 {
0189                     strcat(flagsstr, ",");
0190                 }
0191                 strcat(flagsstr, "wuse");
0192             }
0193             
0194             if (el->flags & NDRX_SVQ_MAP_RQADDR)
0195             {
0196                 if (EXEOS!=flagsstr[0])
0197                 {
0198                     strcat(flagsstr, ",");
0199                 }
0200                 strcat(flagsstr, "rqaddr");
0201             }
0202             
0203             fprintf(stdout, "%6d %10d %-17.17s %8.8s %s\n",
0204                     i, el->qid, flagsstr, 
0205                     ndrx_decode_msec(ndrx_stopwatch_get_delta(&el->ctime), 0, 0, 2),
0206                     el->qstr);
0207             total++;
0208         }
0209     }
0210     
0211     /* un-lock the memory */
0212     ndrx_sem_rwunlock(map_sem, 0, NDRX_SEM_TYP_READ);
0213     /* ###################### CRITICAL SECTION, END ########################## */
0214     
0215     fprintf(stderr, "\nTOTAL: %d\n", total);
0216     
0217 
0218 out:
0219     
0220     /*
0221      * WARNING ! Do not detach if doing any background ops...  
0222      */
0223     if (EXTRUE==attach_status)
0224     {
0225         ndrx_svqshm_detach();
0226     }
0227 
0228     return ret;
0229 }
0230 /* vim: set ts=4 sw=4 et smartindent: */