pst3.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. *****************************************************************************/
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <unistd.h>
  47. #include <dirent.h>
  48. #include <errno.h>
  49. #include <fcntl.h>
  50. #include <procfs.h>
  51. #include <sys/types32.h>
  52. /*
  53. * Constants
  54. */
  55. #define PROC_DIR "/proc"
  56. #define ARGS 30
  57. /*
  58. * Globals
  59. */
  60. static char * szProg;
  61. /*
  62. * Prototypes
  63. */
  64. void usage();
  65. /*----------------------------------------------------------------------------*/
  66. int main (int argc, char **argv)
  67. {
  68. DIR *procdir;
  69. struct dirent *proc;
  70. char ps_name[ARGS];
  71. char as_name[ARGS];
  72. psinfo_t psinfo;
  73. /* Set our program name global */
  74. if ((szProg = strrchr(argv[0], '/')) != NULL)
  75. szProg++;
  76. else
  77. szProg = argv[0];
  78. /* if given any parameters, print out help */
  79. if(argc > 1) {
  80. (void)usage();
  81. exit(1);
  82. }
  83. /* Make sure that our euid is root */
  84. if (geteuid() != 0)
  85. {
  86. fprintf(stderr, "%s: This program can only be run by the root user!\n", szProg);
  87. exit(1);
  88. }
  89. if ((procdir = opendir(PROC_DIR)) == NULL) {
  90. fprintf(stderr, "%s: cannot open PROC directory %s\n", szProg, PROC_DIR);
  91. exit(1);
  92. }
  93. /* Display column headings */
  94. printf("%c %5s %5s %5s %6s %6s %4s %s %s\n",
  95. 'S',
  96. "UID",
  97. "PID",
  98. "PPID",
  99. "VSZ",
  100. "RSS",
  101. "%CPU",
  102. "COMMAND",
  103. "ARGS"
  104. );
  105. /* Zip through all of the process entries */
  106. while((proc = readdir(procdir))) {
  107. int ps_fd;
  108. int as_fd;
  109. off_t argoff;
  110. int i, j;
  111. char *args;
  112. char *procname;
  113. char *ptr;
  114. int argslen;
  115. uintptr_t args_addr;;
  116. uintptr_t *args_vecs;;
  117. int args_count;
  118. if(proc->d_name[0] == '.')
  119. continue;
  120. sprintf(ps_name,"%s/%s/%s",PROC_DIR,proc->d_name,"psinfo");
  121. sprintf(as_name,"%s/%s/%s",PROC_DIR,proc->d_name,"as");
  122. try_again:
  123. if((ps_fd = open(ps_name, O_RDONLY)) == -1)
  124. continue;
  125. if((as_fd = open(as_name, O_RDONLY)) == -1) {
  126. close(ps_fd);
  127. continue;
  128. }
  129. if(read(ps_fd, &psinfo, sizeof(psinfo)) != sizeof(psinfo)) {
  130. int err = errno;
  131. close(ps_fd);
  132. close(as_fd);
  133. if(err == EAGAIN) goto try_again;
  134. if(err != ENOENT)
  135. fprintf(stderr, "%s: read() on %s: %s\n", szProg,
  136. ps_name, strerror(err));
  137. continue;
  138. }
  139. close(ps_fd);
  140. /* system process, ignore since the previous version did */
  141. if(
  142. psinfo.pr_nlwp == 0 ||
  143. strcmp(psinfo.pr_lwp.pr_clname, "SYS") == 0
  144. ) {
  145. continue;
  146. }
  147. /* get the procname to match previous versions */
  148. procname = strdup(psinfo.pr_psargs);
  149. if((ptr = strchr(procname, ' ')) != NULL)
  150. *ptr = '\0';
  151. if((ptr = strrchr(procname, '/')) != NULL)
  152. ptr++;
  153. else
  154. ptr = procname;
  155. /*
  156. * print out what we currently know
  157. */
  158. printf("%c %5d %5d %5d %6lu %6lu %4.1f %s ",
  159. psinfo.pr_lwp.pr_sname,
  160. psinfo.pr_euid,
  161. psinfo.pr_pid,
  162. psinfo.pr_ppid,
  163. psinfo.pr_size,
  164. psinfo.pr_rssize,
  165. ((float)(psinfo.pr_pctcpu) / 0x8000 * 100.0),
  166. ptr
  167. );
  168. free(procname);
  169. /*
  170. * and now for the command line stuff
  171. */
  172. args_addr = psinfo.pr_argv;
  173. args_count = psinfo.pr_argc;
  174. args_vecs = malloc(args_count * sizeof(uintptr_t));
  175. if(psinfo.pr_dmodel == PR_MODEL_NATIVE) {
  176. /* this process matches target process */
  177. pread(as_fd,args_vecs, args_count * sizeof(uintptr_t),
  178. args_addr);
  179. } else {
  180. /* this process is 64bit, target process is 32 bit*/
  181. caddr32_t *args_vecs32 = (caddr32_t *)args_vecs;
  182. pread(as_fd,args_vecs32,args_count * sizeof(caddr32_t),
  183. args_addr);
  184. for (i=args_count-1;i>=0;--i)
  185. args_vecs[i]=args_vecs32[i];
  186. }
  187. /*
  188. * now read in the args - if what we read in fills buffer
  189. * resize buffer and reread that bit again
  190. */
  191. argslen=ARGS;
  192. args=malloc(argslen+1);
  193. for(i=0;i<args_count;i++) {
  194. memset(args,'\0',argslen+1);
  195. if(pread(as_fd, args, argslen, args_vecs[i]) <= 0) {
  196. break;
  197. }
  198. args[argslen]='\0';
  199. if(strlen(args) == argslen){
  200. argslen += ARGS;
  201. args = realloc(args, argslen + 1);
  202. i--;
  203. continue;
  204. }
  205. /* Remove newlines from args output - consistent with "normal" ps */
  206. printf(" ");
  207. for (j=0;j<strlen(args);j++) {
  208. if (args[j] != '\n') {
  209. printf("%c", args[j]);
  210. }
  211. }
  212. }
  213. free(args_vecs);
  214. free(args);
  215. close(as_fd);
  216. printf("\n");
  217. }
  218. (void) closedir(procdir);
  219. return (0);
  220. }
  221. /*----------------------------------------------------------------------------*/
  222. void usage() {
  223. printf("%s: Help output\n\n", szProg);
  224. printf("If this program is given any arguments, this help is displayed.\n");
  225. printf("This command is used to print out the full command line for all\n");
  226. printf("running processes because /usr/bin/ps is limited to 80 chars and\n");
  227. printf("/usr/ucb/ps can merge columns together.\n\n");
  228. printf("Columns are:\n");
  229. printf("\tS - State of process - see 'ps' man page\n");
  230. printf("\tUID - UID of the process owner\n");
  231. printf("\tPID - PID of the process\n");
  232. printf("\tPPID - PID of the parent process\n");
  233. printf("\tVSZ - Virtual memory usage (kilobytes)\n");
  234. printf("\tRSS - Real memory usage (kilobytes)\n");
  235. printf("\t%%CPU - CPU usage\n");
  236. printf("\tCOMMAND - Command being run\n");
  237. printf("\tARGS - Full command line with arguments\n");
  238. return;
  239. }