Whitespace changes only
[monitoring-plugins.git] / plugins-root / pst3.c
blob851365697e759d944d577d98ac6736960e11783e
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.
15 * This executable works by reading process address structures, so needs
16 * to be executed as root
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
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
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.
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.
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/>.
42 *****************************************************************************/
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <dirent.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <procfs.h>
50 #include <errno.h>
51 #include <sys/types32.h>
54 * Constants
57 #define PROC_DIR "/proc"
58 #define ARGS 30
61 * Globals
64 static char * szProg;
67 * Prototypes
69 void usage();
71 /*----------------------------------------------------------------------------*/
73 int main (int argc, char **argv)
75 DIR *procdir;
76 struct dirent *proc;
77 char ps_name[ARGS];
78 char as_name[ARGS];
79 psinfo_t psinfo;
81 /* Set our program name global */
82 if ((szProg = strrchr(argv[0], '/')) != NULL)
83 szProg++;
84 else
85 szProg = argv[0];
87 /* if given any parameters, print out help */
88 if(argc > 1) {
89 (void)usage();
90 exit(1);
93 /* Make sure that our euid is root */
94 if (geteuid() != 0)
96 fprintf(stderr, "%s: This program can only be run by the root user!\n", szProg);
97 exit(1);
100 if ((procdir = opendir(PROC_DIR)) == NULL) {
101 fprintf(stderr, "%s: cannot open PROC directory %s\n", szProg, PROC_DIR);
102 exit(1);
105 /* Display column headings */
106 printf("%c %5s %5s %5s %6s %6s %4s %s %s\n",
107 'S',
108 "UID",
109 "PID",
110 "PPID",
111 "VSZ",
112 "RSS",
113 "%CPU",
114 "COMMAND",
115 "ARGS"
118 /* Zip through all of the process entries */
119 while((proc = readdir(procdir))) {
120 int ps_fd;
121 int as_fd;
122 off_t argoff;
123 int i;
124 char *args;
125 char *procname;
126 char *ptr;
127 int argslen;
128 uintptr_t args_addr;;
129 uintptr_t *args_vecs;;
130 int args_count;
132 if(proc->d_name[0] == '.')
133 continue;
135 sprintf(ps_name,"%s/%s/%s",PROC_DIR,proc->d_name,"psinfo");
136 sprintf(as_name,"%s/%s/%s",PROC_DIR,proc->d_name,"as");
137 try_again:
138 if((ps_fd = open(ps_name, O_RDONLY)) == -1)
139 continue;
141 if((as_fd = open(as_name, O_RDONLY)) == -1)
142 continue;
144 if(read(ps_fd, &psinfo, sizeof(psinfo)) != sizeof(psinfo)) {
145 int err = errno;
146 close(ps_fd);
147 close(as_fd);
148 if(err == EAGAIN) goto try_again;
149 if(err != ENOENT)
150 fprintf(stderr, "%s: read() on %s: %s\n", szProg,
151 ps_name, strerror(err));
152 continue;
154 close(ps_fd);
156 /* system process, ignore since the previous version did */
158 psinfo.pr_nlwp == 0 ||
159 strcmp(psinfo.pr_lwp.pr_clname, "SYS") == 0
161 continue;
164 /* get the procname to match previous versions */
165 procname = strdup(psinfo.pr_psargs);
166 if((ptr = strchr(procname, ' ')) != NULL)
167 *ptr = '\0';
168 if((ptr = strrchr(procname, '/')) != NULL)
169 ptr++;
170 else
171 ptr = procname;
174 * print out what we currently know
176 printf("%c %5d %5d %5d %6lu %6lu %4.1f %s ",
177 psinfo.pr_lwp.pr_sname,
178 psinfo.pr_euid,
179 psinfo.pr_pid,
180 psinfo.pr_ppid,
181 psinfo.pr_size,
182 psinfo.pr_rssize,
183 ((float)(psinfo.pr_pctcpu) / 0x8000 * 100.0),
186 free(procname);
189 * and now for the command line stuff
192 args_addr = psinfo.pr_argv;
193 args_count = psinfo.pr_argc;
194 args_vecs = malloc(args_count * sizeof(uintptr_t));
196 if(psinfo.pr_dmodel == PR_MODEL_NATIVE) {
197 /* this process matches target process */
198 pread(as_fd,args_vecs, args_count * sizeof(uintptr_t),
199 args_addr);
200 } else {
201 /* this process is 64bit, target process is 32 bit*/
202 caddr32_t *args_vecs32 = (caddr32_t *)args_vecs;
203 pread(as_fd,args_vecs32,args_count * sizeof(caddr32_t),
204 args_addr);
205 for (i=args_count-1;i>=0;--i)
206 args_vecs[i]=args_vecs32[i];
210 * now read in the args - if what we read in fills buffer
211 * resize buffer and reread that bit again
213 argslen=ARGS;
214 args=malloc(argslen+1);
215 for(i=0;i<args_count;i++) {
216 memset(args,'\0',argslen+1);
217 if(pread(as_fd, args, argslen, args_vecs[i]) <= 0) {
218 break;
220 args[argslen]='\0';
221 if(strlen(args) == argslen){
222 argslen += ARGS;
223 args = realloc(args, argslen + 1);
224 i--;
225 continue;
227 printf(" %s", args);
229 free(args_vecs);
230 free(args);
231 close(as_fd);
232 printf("\n");
235 return (0);
238 /*----------------------------------------------------------------------------*/
240 void usage() {
241 printf("%s: Help output\n\n", szProg);
242 printf("If this program is given any arguments, this help is displayed.\n");
243 printf("This command is used to print out the full command line for all\n");
244 printf("running processes because /usr/bin/ps is limited to 80 chars and\n");
245 printf("/usr/ucb/ps can merge columns together.\n\n");
246 printf("Columns are:\n");
247 printf("\tS - State of process - see 'ps' man page\n");
248 printf("\tUID - UID of the process owner\n");
249 printf("\tPID - PID of the process\n");
250 printf("\tPPID - PID of the parent process\n");
251 printf("\tVSZ - Virtual memory usage (kilobytes)\n");
252 printf("\tRSS - Real memory usage (kilobytes)\n");
253 printf("\t%%CPU - CPU usage\n");
254 printf("\tCOMMAND - Command being run\n");
255 printf("\tARGS - Full command line with arguements\n");
256 return;