Code

7852aa1cbb011a7fe6a51d7c2b19012344eef281
[nagiosplug.git] / plugins-root / pst3.c
1 /*****************************************************************************
2
3 * pst3
4
5 * License: GPL
6 * Copyright (c) 2008 Nagios Plugin Development Team
7
8 * Description:
9
10 * This file contains the pst3 executable. This is a replacement ps command
11 * for Solaris to get output which provides a long argument listing, which
12 * is not possible with the standard ps command (due to truncation). /usr/ucb/ps
13 * also has issues where some fields run into each other.
14
15 * This executable works by reading process address structures, so needs
16 * to be executed as root
17 *
18 * Originally written by R.W.Ingraham
19 * Rewritten by Duncan Ferguson (Altinity Ltd, June 2008)
20 *   The rewrite was necessary as /dev/kmem is not available within
21 *   non-global zones on Solaris 10
22 *
23 *   Details for rewrite came from
24 *    source of /usr/ucb/ps on Solaris:
25 *     http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/ucbcmd/ps/ps.c#argvoff
26 *    usenet group posting
27 *     http://groups.google.com/group/comp.unix.solaris/tree/browse_frm/month/2001-09/bfa40c08bac819a2?rnum=141&_done=%2Fgroup%2Fcomp.unix.solaris%2Fbrowse_frm%2Fmonth%2F2001-09%3F
28
29 * This program is free software: you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, either version 3 of the License, or
32 * (at your option) any later version.
33
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37 * GNU General Public License for more details.
38
39 * You should have received a copy of the GNU General Public License
40 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
41
42 *****************************************************************************/
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <dirent.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <procfs.h>
52 #include <errno.h>
53 #include <sys/types32.h>
55 /*
56  *  Constants
57  */
59 #define PROC_DIR  "/proc"
60 #define ARGS            30
62 /*
63  *  Globals
64  */
66 static char *        szProg;
68 /*
69  *  Prototypes
70  */
71 void usage();
73 /*----------------------------------------------------------------------------*/
75 int main (int argc, char **argv)
76 {
77   DIR *procdir;
78   struct dirent *proc;
79   char ps_name[ARGS];
80   char as_name[ARGS];
81   psinfo_t psinfo;
83   /* Set our program name global */
84   if ((szProg = strrchr(argv[0], '/')) != NULL)
85     szProg++;
86   else
87     szProg = argv[0];
89   /* if given any parameters, print out help */
90   if(argc > 1) {
91     (void)usage();
92     exit(1);
93   }
95   /* Make sure that our euid is root */
96   if (geteuid() != 0)
97   {
98     fprintf(stderr, "%s: This program can only be run by the root user!\n", szProg);
99     exit(1);
100   }
102   if ((procdir = opendir(PROC_DIR)) == NULL) {
103     fprintf(stderr, "%s: cannot open PROC directory %s\n", szProg, PROC_DIR);
104     exit(1);
105   }
107   /* Display column headings */
108   printf("%c %5s %5s %5s %6s %6s %4s %s %s\n",
109     'S',
110     "UID",
111     "PID",
112     "PPID",
113     "VSZ",
114     "RSS",
115     "%CPU",
116     "COMMAND",
117     "ARGS"
118   );
120   /* Zip through all of the process entries */
121   while((proc = readdir(procdir))) {
122     int ps_fd;
123     int as_fd;
124     off_t argoff;
125     int i;
126     char *args;
127     char *procname;
128     char *ptr;
129     int argslen;
130     uintptr_t args_addr;;
131     uintptr_t *args_vecs;;
132     int args_count;
134     if(proc->d_name[0] == '.')
135       continue;
137     sprintf(ps_name,"%s/%s/%s",PROC_DIR,proc->d_name,"psinfo");
138     sprintf(as_name,"%s/%s/%s",PROC_DIR,proc->d_name,"as");
139 try_again:
140     if((ps_fd = open(ps_name, O_RDONLY)) == -1)
141       continue;
143     if((as_fd = open(as_name, O_RDONLY)) == -1)
144       continue;
146     if(read(ps_fd, &psinfo, sizeof(psinfo)) != sizeof(psinfo)) {
147       int err = errno;
148       close(ps_fd);
149       close(as_fd);
150       if(err == EAGAIN) goto try_again;
151       if(err != ENOENT)
152         fprintf(stderr, "%s: read() on %s: %s\n", szProg,
153           ps_name, strerror(err));
154       continue;
155     }
156     close(ps_fd);
158     /* system process, ignore since the previous version did */
159     if(
160       psinfo.pr_nlwp == 0 ||
161       strcmp(psinfo.pr_lwp.pr_clname, "SYS") == 0
162     ) {
163       continue;
164     }
166     /* get the procname to match previous versions */
167     procname = strdup(psinfo.pr_psargs);
168     if((ptr = strchr(procname, ' ')) != NULL)
169         *ptr = '\0';
170     if((ptr = strrchr(procname, '/')) != NULL)
171         ptr++;
172     else
173         ptr = procname;
175     /*
176      * print out what we currently know
177      */
178     printf("%c %5d %5d %5d %6lu %6lu %4.1f %s ",
179       psinfo.pr_lwp.pr_sname,
180       psinfo.pr_euid,
181       psinfo.pr_pid,
182       psinfo.pr_ppid,
183       psinfo.pr_size,
184       psinfo.pr_rssize,
185       ((float)(psinfo.pr_pctcpu) / 0x8000 * 100.0),
186       ptr
187     );
188     free(procname);
190     /*
191      * and now for the command line stuff
192      */
194     args_addr = psinfo.pr_argv;
195     args_count = psinfo.pr_argc;
196     args_vecs = malloc(args_count * sizeof(uintptr_t));
198     if(psinfo.pr_dmodel == PR_MODEL_NATIVE) {
199       /* this process matches target process */
200       pread(as_fd,args_vecs, args_count * sizeof(uintptr_t),
201         args_addr);
202     } else {
203       /* this process is 64bit, target process is 32 bit*/
204       caddr32_t *args_vecs32 = (caddr32_t *)args_vecs;
205       pread(as_fd,args_vecs32,args_count * sizeof(caddr32_t),
206         args_addr);
207       for (i=args_count-1;i>=0;--i)
208         args_vecs[i]=args_vecs32[i];
209     }
211     /*
212      * now read in the args - if what we read in fills buffer
213      * resize buffer and reread that bit again
214      */
215     argslen=ARGS;
216     args=malloc(argslen+1);
217     for(i=0;i<args_count;i++) {
218       memset(args,'\0',argslen+1);
219       if(pread(as_fd, args, argslen, args_vecs[i]) <= 0) {
220         break;
221       }
222       args[argslen]='\0';
223       if(strlen(args) == argslen){
224         argslen += ARGS;
225         args = realloc(args, argslen + 1);
226         i--;
227         continue;
228       }
229       printf(" %s", args);
230     }
231     free(args_vecs);
232     free(args);
233     close(as_fd);
234     printf("\n");
235   }
237   (void) closedir(procdir);
239   return (0);
242 /*----------------------------------------------------------------------------*/
244 void usage() {
245   printf("%s: Help output\n\n", szProg);
246   printf("If this program is given any arguments, this help is displayed.\n");
247   printf("This command is used to print out the full command line for all\n");
248   printf("running processes because /usr/bin/ps is limited to 80 chars and\n");
249   printf("/usr/ucb/ps can merge columns together.\n\n");
250   printf("Columns are:\n");
251   printf("\tS        - State of process - see 'ps' man page\n");
252   printf("\tUID      - UID of the process owner\n");
253   printf("\tPID      - PID of the process\n");
254   printf("\tPPID     - PID of the parent process\n");
255   printf("\tVSZ      - Virtual memory usage (kilobytes)\n");
256   printf("\tRSS      - Real memory usage (kilobytes)\n");
257   printf("\t%%CPU     - CPU usage\n");
258   printf("\tCOMMAND  - Command being run\n");
259   printf("\tARGS     - Full command line with arguements\n");
260   return;