Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief `ppm' command implementation - Print process model
0003  *
0004  * @file cmd_ppm.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/param.h>
0039 
0040 #include <ndrstandard.h>
0041 #include <ndebug.h>
0042 #include <nstdutil.h>
0043 #include <nclopt.h>
0044 
0045 #include <ndrx.h>
0046 #include <ndrxdcmn.h>
0047 #include <atmi_int.h>
0048 #include <gencall.h>
0049 
0050 #include "nstopwatch.h"
0051 /*---------------------------Externs------------------------------------*/
0052 /*---------------------------Macros-------------------------------------*/
0053 #define FIX_NM(Xsrc, Xbuf, Xlen) \
0054             if (strlen(Xsrc) > Xlen)\
0055             {\
0056                 NDRX_STRNCPY(Xbuf, Xsrc, Xlen-1);\
0057                 Xbuf[Xlen-1] = '+';\
0058                 Xbuf[Xlen] = EXEOS;\
0059             }\
0060             else\
0061                 strcpy(Xbuf, Xsrc);
0062 /*---------------------------Enums--------------------------------------*/
0063 /*---------------------------Typedefs-----------------------------------*/
0064 /*---------------------------Globals------------------------------------*/
0065 /*---------------------------Statics------------------------------------*/
0066 struct 
0067 {
0068     long status;
0069     char *descr;
0070 } 
0071 M_descr [] =
0072 {
0073     {NDRXD_PM_NOT_STARTED, "nstrd"},
0074     {NDRXD_PM_DIED,        "died"},
0075     {NDRXD_PM_EXIT,        "exit"},
0076     {NDRXD_PM_ENOENT,      "nobin"},
0077     {NDRXD_PM_EACCESS,     "acces"},
0078     {NDRXD_PM_ELIMIT,      "limit"},
0079     {NDRXD_PM_EARGSLIM,    "eargs"},
0080     {NDRXD_PM_ESYSTEM,     "esys"},
0081     {NDRXD_PM_EENV,        "eenv"},
0082     {NDRXD_PM_EBADFILE,    "badfi"},
0083     {NDRXD_PM_STARTING,    "start"},
0084     {NDRXD_PM_RUNNING_OK,  "runok"},
0085     {NDRXD_PM_STOPPING,    "stop"},
0086     {NDRXD_PM_RESTART,     "rsta"},
0087     {NDRXD_PM_WAIT,        "wait"},
0088 };
0089 /*---------------------------Prototypes---------------------------------*/
0090 
0091 /**
0092  * Print header
0093  */
0094 exprivate void print_hdr(void)
0095 {
0096     fprintf(stderr, "BINARY   SRVID      PID    SVPID STATE REQST AS EXSQ RSP  NTRM LSIG K STCH FLAGS\n");
0097     fprintf(stderr, "-------- ----- -------- -------- ----- ----- -- ---- ---- ---- ---- - ---- -----\n");
0098 }
0099 
0100 /**
0101  * Print header, 2
0102  */
0103 exprivate void print_hdr2(void)
0104 {
0105     fprintf(stderr, "BINARY   SVBIN    SRVID      PID    SVPID RQADDR\n");
0106     fprintf(stderr, "-------- -------- ----- -------- -------- -----------------------\n");
0107 }
0108 
0109 /**
0110  * Print header, 3
0111  */
0112 exprivate void print_hdr3(void)
0113 {
0114     fprintf(stderr, "BINARY   SRVID      PID    SVPID PROCGRP   PGNO PROCGRPL PGLNO PGNLA\n");
0115     fprintf(stderr, "-------- ----- -------- -------- -------- ----- -------- ----- -----\n");
0116 }
0117 
0118 /**
0119  * Get 
0120  * @param reply
0121  * @param reply_len
0122  * @return 
0123  */
0124 exprivate char *get_status_descr(long status)
0125 {
0126     char *ret;
0127     static char buf[16];
0128     int i;
0129     
0130     for (i=0; i<N_DIM(M_descr); i++)
0131     {
0132         if (status==M_descr[i].status)
0133         {
0134             ret = M_descr[i].descr;
0135             goto out;
0136         }
0137     }
0138     snprintf(buf, sizeof(buf), "%ld", status);
0139     ret = buf;
0140     
0141 out:
0142     return ret;
0143 }
0144 /**
0145  * Decode binary flags
0146  */
0147 exprivate char *decode_flags(command_reply_ppm_t * ppm_info)
0148 {
0149     static char buf[10];
0150     
0151     buf[0] = EXEOS;
0152     
0153     if (ppm_info->flags & SRV_KEY_FLAGS_BRIDGE)
0154     {
0155         strcat(buf, "B");
0156     }
0157     
0158     if (ppm_info->flags & SRV_KEY_FLAGS_SENDREFERSH)
0159     {
0160         strcat(buf, "r");
0161     }
0162     
0163     if (ppm_info->flags & SRV_KEY_FLAGS_CONNECTED)
0164     {
0165         strcat(buf, "C");
0166     }
0167 
0168     /* This is lock provider... */
0169     if (ppm_info->flags & SRV_KEY_FLAGS_PROCGRPLP)
0170     {
0171         strcat(buf, "L");
0172     }
0173     
0174     return buf;
0175 }
0176 
0177 /**
0178  * Process response back, 2
0179  * @param reply
0180  * @param reply_len
0181  * @return
0182  */
0183 expublic int ppm_rsp_process2(command_reply_t *reply, size_t reply_len)
0184 {
0185     char binary[9+1];
0186 
0187     if (NDRXD_CALL_TYPE_PM_PPM==reply->msg_type)
0188     {
0189         command_reply_ppm_t * ppm_info = (command_reply_ppm_t*)reply;
0190         FIX_NM(ppm_info->binary_name, binary, (sizeof(binary)-1));
0191         fprintf(stdout, "%-8.8s %-8.8s %5d %8d %8d %s\n", 
0192                 ppm_info->binary_name,
0193                 ppm_info->binary_name_real,
0194                 ppm_info->srvid,
0195                 ppm_info->pid, 
0196                 ppm_info->svpid,
0197                 ppm_info->rqaddress
0198                 );
0199     }
0200     
0201     return EXSUCCEED;
0202 }
0203 
0204 /**
0205  * Process response back.
0206  * @param reply
0207  * @param reply_len
0208  * @return
0209  */
0210 expublic int ppm_rsp_process3(command_reply_t *reply, size_t reply_len)
0211 {
0212     char binary[9+1];
0213     char procgrp[8+1];
0214     char procgrp_lp[8+1];
0215     
0216     if (NDRXD_CALL_TYPE_PM_PPM==reply->msg_type)
0217     {
0218         command_reply_ppm_t * ppm_info = (command_reply_ppm_t*)reply;
0219         FIX_NM(ppm_info->binary_name, binary, (sizeof(binary)-1));
0220         FIX_NM(ppm_info->procgrp, procgrp, (sizeof(procgrp)-1));
0221 
0222         if (EXEOS==procgrp[0])
0223         {
0224             NDRX_STRCPY_SAFE(procgrp, "-");
0225         }
0226 
0227         FIX_NM(ppm_info->procgrp_lp, procgrp_lp, (sizeof(procgrp_lp)-1));
0228 
0229         if (EXEOS==procgrp_lp[0])
0230         {
0231             NDRX_STRCPY_SAFE(procgrp_lp, "-");
0232         }
0233 
0234         fprintf(stdout, "%-8.8s %5d %8d %8d %-8.8s %5d %-8.8s %5d %5d\n", 
0235                 ppm_info->binary_name,
0236                 ppm_info->srvid,
0237                 ppm_info->pid, 
0238                 ppm_info->svpid,
0239                 procgrp,
0240                 ppm_info->procgrp_no,
0241                 procgrp_lp,
0242                 ppm_info->procgrp_lp_no,
0243                 ppm_info->procgrp_lp_no_act
0244             );
0245     }
0246     
0247     return EXSUCCEED;
0248 }
0249 
0250 /**
0251  * Process response back.
0252  * @param reply
0253  * @param reply_len
0254  * @return
0255  */
0256 expublic int ppm_rsp_process(command_reply_t *reply, size_t reply_len)
0257 {
0258     char binary[9+1];
0259     
0260     if (reply->flags & NDRXD_CALL_FLAGS_PAGE2)
0261     {
0262         return ppm_rsp_process2(reply, reply_len);
0263     }
0264     else if (reply->flags & NDRXD_CALL_FLAGS_PAGE3)
0265     {
0266         return ppm_rsp_process3(reply, reply_len);
0267     }
0268 
0269     if (NDRXD_CALL_TYPE_PM_PPM==reply->msg_type)
0270     {
0271         command_reply_ppm_t * ppm_info = (command_reply_ppm_t*)reply;
0272         FIX_NM(ppm_info->binary_name, binary, (sizeof(binary)-1));
0273         fprintf(stdout, "%-8.8s %5d %8d %8d %-5.5s %-5.5s %2hd %4.4s "
0274                 "%4.4s %4.4s %4.4s %1d %4.4s %-5.5s\n", 
0275                 ppm_info->binary_name,
0276                 ppm_info->srvid,
0277                 ppm_info->pid, 
0278                 ppm_info->svpid,
0279                 get_status_descr(ppm_info->state),
0280                 get_status_descr(ppm_info->reqstate),
0281                 ppm_info->autostart,
0282                 ndrx_decode_num(ppm_info->exec_seq_try, 0, 0, 1), 
0283                 ndrx_decode_num(ppm_info->last_startup, 1, 0, 1), 
0284                 ndrx_decode_num(ppm_info->num_term_sigs, 2, 0, 1), 
0285                 ndrx_decode_num(ppm_info->last_sig, 3, 0, 1), 
0286                 ppm_info->autokill, 
0287                 ndrx_decode_num(ppm_info->state_changed, 4, 0, 1), 
0288                 decode_flags(ppm_info)
0289                 );
0290     }
0291     
0292     return EXSUCCEED;
0293 }
0294 
0295 /**
0296  * Get server listings
0297  * @param p_cmd_map
0298  * @param argc
0299  * @param argv
0300  * @return SUCCEED
0301  */
0302 expublic int cmd_ppm(cmd_mapping_t *p_cmd_map, int argc, char **argv, int *p_have_next)
0303 {
0304     int ret = EXSUCCEED;
0305     command_call_t call;
0306     short print_2nd = EXFALSE;
0307     short print_3rd = EXFALSE;
0308     
0309     ncloptmap_t clopt[] =
0310     {
0311         {'2', BFLD_SHORT, (void *)&print_2nd, 0, 
0312                                 NCLOPT_OPT | NCLOPT_TRUEBOOL, "Print page 2"},
0313         {'3', BFLD_SHORT, (void *)&print_3rd, 0, 
0314                                 NCLOPT_OPT | NCLOPT_TRUEBOOL, "Print page 3"},
0315         {0}
0316     };
0317     
0318     memset(&call, 0, sizeof(call));
0319 
0320     if (nstd_parse_clopt(clopt, EXTRUE,  argc, argv, EXFALSE))
0321     {
0322         fprintf(stderr, XADMIN_INVALID_OPTIONS_MSG);
0323         EXFAIL_OUT(ret);
0324     }
0325     
0326     if (print_2nd)
0327     {
0328         /* Print header at first step! */
0329         print_hdr2();
0330         call.flags|=NDRXD_CALL_FLAGS_PAGE2;
0331     }
0332     else if (print_3rd)
0333     {
0334         /* Print header at first step! */
0335         print_hdr3();
0336         call.flags|=NDRXD_CALL_FLAGS_PAGE3;
0337     }
0338     else
0339     {
0340         /* Print header at first step! */
0341         print_hdr();
0342     }
0343     
0344     /* Then get listing... */
0345     ret = cmd_generic_listcall(p_cmd_map->ndrxd_cmd, NDRXD_SRC_ADMIN,
0346                         NDRXD_CALL_TYPE_GENERIC,
0347                         &call, sizeof(call),
0348                         G_config.reply_queue_str,
0349                         G_config.reply_queue,
0350                         G_config.ndrxd_q,
0351                         G_config.ndrxd_q_str,
0352                         argc, argv,
0353                         p_have_next,
0354                         G_call_args,
0355                         EXFALSE,
0356                         G_config.listcall_flags);
0357 out:    
0358     return ret;
0359 }
0360 
0361 /* vim: set ts=4 sw=4 et smartindent: */