pst3.c 6.9 KB

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