Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Various utility commands
0003  *
0004  * @file cmd_util.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 
0043 #include <ndrx.h>
0044 #include <ndrxdcmn.h>
0045 #include <atmi_int.h>
0046 #include <gencall.h>
0047 #include <utlist.h>
0048 
0049 #include "nclopt.h"
0050 /*---------------------------Externs------------------------------------*/
0051 /*---------------------------Macros-------------------------------------*/
0052 /*---------------------------Enums--------------------------------------*/
0053 /*---------------------------Typedefs-----------------------------------*/
0054 /*---------------------------Globals------------------------------------*/
0055 /*---------------------------Statics------------------------------------*/
0056 /*---------------------------Prototypes---------------------------------*/
0057 
0058 /**
0059  * Print all processes on stdout
0060  * @param p_cmd_map
0061  * @param argc
0062  * @param argv
0063  * @return SUCCEED
0064  */
0065 expublic int cmd_ps(cmd_mapping_t *p_cmd_map, int argc, char **argv, int *p_have_next)
0066 {
0067     int ret=EXSUCCEED;
0068     /* Add filter options... */
0069     char flt_a[PATH_MAX/4]={EXEOS};
0070     char flt_b[PATH_MAX/41]={EXEOS};
0071     char flt_c[PATH_MAX/4]={EXEOS};
0072     char flt_d[PATH_MAX/4]={EXEOS};
0073     char flt_r[PATH_MAX/4]={EXEOS};
0074     long pid_x = EXFAIL;
0075     short pid_only = EXFALSE;
0076     short mems = EXFALSE;
0077     pid_t me = getpid();
0078     pid_t they;
0079     
0080     string_list_t * pslist = NULL;
0081     string_list_t * proc = NULL;
0082     
0083     ncloptmap_t clopt[] =
0084     {
0085         {'a', BFLD_STRING, (void *)flt_a, sizeof(flt_a), 
0086                                 NCLOPT_OPT|NCLOPT_HAVE_VALUE, "Filter A (grep style)"},
0087         {'b', BFLD_STRING, (void *)flt_b, sizeof(flt_b), 
0088                                 NCLOPT_OPT|NCLOPT_HAVE_VALUE, "Filter B (grep style)"},
0089         {'c', BFLD_STRING, (void *)flt_c, sizeof(flt_c), 
0090                                 NCLOPT_OPT|NCLOPT_HAVE_VALUE, "Filter C (grep style)"},
0091         {'d', BFLD_STRING, (void *)flt_d, sizeof(flt_d), 
0092                                 NCLOPT_OPT|NCLOPT_HAVE_VALUE, "Filter D (grep style)"},
0093         {'r', BFLD_STRING, (void *)flt_r, sizeof(flt_r), 
0094                                 NCLOPT_OPT|NCLOPT_HAVE_VALUE, "Posix regexp"},
0095         {'p', BFLD_SHORT,  (void *)&pid_only, sizeof(pid_only), 
0096                                 NCLOPT_OPT | NCLOPT_TRUEBOOL, "Print pid only"},
0097         {'m', BFLD_SHORT,  (void *)&mems, sizeof(mems), 
0098                                 NCLOPT_OPT | NCLOPT_TRUEBOOL,
0099                             "Print memory stats (pid), -p mode only (pid:rss:vsz in kb)"},
0100         {'x', BFLD_LONG,   (void *)&pid_x, sizeof(pid_x), 
0101                                 NCLOPT_OPT|NCLOPT_HAVE_VALUE, "Exclude pid"},
0102         {0}
0103     };
0104     
0105     /* parse command line */
0106     
0107     if (nstd_parse_clopt(clopt, EXTRUE,  argc, argv, EXFALSE))
0108     {
0109         fprintf(stderr, XADMIN_INVALID_OPTIONS_MSG);
0110         EXFAIL_OUT(ret);
0111     }
0112     
0113     if (mems && !pid_only)
0114     {
0115         fprintf(stderr, NDRX_XADMIN_ERR_FMT_PFX "-m can be used only with -p!\n");
0116         EXFAIL_OUT(ret);
0117     }
0118     
0119     pslist = ndrx_sys_ps_list(flt_a, flt_b, flt_c, flt_d, flt_r);
0120     
0121     if (NULL!=pslist)
0122     {
0123         /* print to stdout... */
0124         DL_FOREACH(pslist, proc)
0125         {
0126             if (EXSUCCEED==ndrx_proc_pid_get_from_ps(proc->qname, &they))
0127             {
0128                 /* will not print our selves... as usually not needed */
0129                 
0130                 if (EXFAIL!=pid_x && (int)pid_x==they)
0131                 {
0132                     /* Exclude pid... */
0133                     NDRX_LOG(log_debug, "Exclude %d", (int)they);
0134                 }
0135                 else if (me!=they)
0136                 {
0137                     if (pid_only)
0138                     {
0139                         ndrx_proc_info_t inf;
0140                         
0141                         if (mems)
0142                         {
0143                             /* read memory stats... */
0144                             memset(&inf, 0, sizeof(inf));
0145                             
0146                             if (EXSUCCEED!=ndrx_proc_get_infos(they, &inf))
0147                             {
0148                                 fprintf(stderr, "Failed to read %d stats\n", 
0149                                         (int)they);
0150                             }
0151                             printf("%d:%ld:%ld\n", (int)they, inf.rss, inf.vsz);
0152                         }
0153                         else
0154                         {
0155                             printf("%d\n", (int)they);
0156                         }
0157                     }
0158                     else
0159                     {
0160                         printf("%s\n", proc->qname);
0161                     }
0162                 }
0163             }
0164             else if (!pid_only)
0165             {
0166                 /* do not show our selves (our pid here) */
0167                 printf("%s\n", proc->qname);
0168             }
0169         }
0170     }
0171     
0172 out:
0173     
0174     if (NULL!=pslist)
0175     {
0176          ndrx_string_list_free(pslist);
0177     }
0178 
0179     return ret;
0180 }
0181 /* vim: set ts=4 sw=4 et smartindent: */