0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
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
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
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
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
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
0124 DL_FOREACH(pslist, proc)
0125 {
0126 if (EXSUCCEED==ndrx_proc_pid_get_from_ps(proc->qname, &they))
0127 {
0128
0129
0130 if (EXFAIL!=pid_x && (int)pid_x==they)
0131 {
0132
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
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
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