ps.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* ps.c:
  2. *
  3. * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>,
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #include "sash.h"
  11. #include <fcntl.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <dirent.h>
  15. #include <pwd.h>
  16. #include <grp.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19. #include <linux/major.h>
  20. #include <linux/param.h>
  21. #include <sys/time.h>
  22. #include <sys/param.h>
  23. char psbuf[256];
  24. char name[40];
  25. int pid, state;
  26. char statec;
  27. int ppid, pgrp, session;
  28. dev_t tty;
  29. char tty_name[10];
  30. char master[] = "pqrstuvwxyzabcde";
  31. #define MAJOR(x) ((x) >> 8)
  32. #define MINOR(x) ((x) & 0xff)
  33. int port_xlate[16] = {1, 3, 5, 7,9 ,11,13,15,
  34. 2, 4, 6, 8,10,12,14,16};
  35. void dev_to_name(dev_t dev, char * ttyname)
  36. {
  37. strcpy(ttyname, "");
  38. if (MAJOR(dev) == 75)
  39. sprintf(ttyname,"X%d", MINOR(dev));
  40. else if (MAJOR(dev) == TTY_MAJOR)
  41. sprintf(ttyname,"S%d", MINOR(dev)-64);
  42. else if (MAJOR(dev) == PTY_SLAVE_MAJOR)
  43. sprintf(ttyname,"%c%x", master[MINOR(dev) / 16], MINOR(dev) & 0xf);
  44. }
  45. void
  46. do_ps(argc, argv)
  47. int argc;
  48. char **argv;
  49. {
  50. int i;
  51. int h;
  52. int max;
  53. FILE * f;
  54. DIR * d;
  55. unsigned long bytes, sbytes;
  56. struct dirent * de;
  57. char *ext;
  58. int l;
  59. time_t time_now;
  60. long uptime_secs;
  61. float idle_secs;
  62. float seconds, start, total_time;
  63. int utime, stime, start_time;
  64. int pcpu;
  65. /*extern int _vfprintf_fp_ref, _vfscanf_fp_ref;*/
  66. #if 0
  67. fclose(stdin);
  68. #endif
  69. printf(" PID PORT STAT SIZE SHARED %%CPU COMMAND\n"/*, _vfprintf_fp_ref, _vfscanf_fp_ref*/);
  70. h = open("/proc/uptime", O_RDONLY);
  71. if (h==-1) {
  72. perror("Unable to open /proc/uptime\n");
  73. return;
  74. }
  75. l = read(h, psbuf, 255);
  76. close(h);
  77. if (l<=0) {
  78. perror("Unable to read uptime");
  79. return;
  80. }
  81. psbuf[l] = '\0';
  82. psbuf[255] = '\0';
  83. ext = psbuf;
  84. uptime_secs = atol(ext);
  85. time_now = time(0);
  86. d = opendir("/proc");
  87. if (!d)
  88. return;
  89. while (de = readdir(d)) {
  90. for(i=0;i<strlen(de->d_name);i++)
  91. if (!isdigit(de->d_name[i]))
  92. goto next;
  93. sprintf(psbuf, "/proc/%s/stat", de->d_name);
  94. h = open(psbuf, O_RDONLY);
  95. if (h==-1)
  96. continue;
  97. l = read(h, psbuf, 255);
  98. if (l<=0) {
  99. perror("Unable to read status");
  100. close(h);
  101. continue;
  102. }
  103. psbuf[l] = '\0';
  104. psbuf[255] = '\0';
  105. ext = strrchr(psbuf, ')');
  106. ext[0] = '\0';
  107. statec = ext[2];
  108. ext += 4;
  109. ppid = atoi(ext);
  110. ext = strchr(ext, ' ')+1;
  111. pgrp = atoi(ext);
  112. ext = strchr(ext, ' ')+1;
  113. session = atoi(ext);
  114. ext = strchr(ext, ' ')+1;
  115. tty = atoi(ext);
  116. ext = strchr(ext, ' ')+1;
  117. //printf("1|%s\n", ext);
  118. //tpgid
  119. ext = strchr(ext, ' ')+1;
  120. //printf("2|%s\n", ext);
  121. //flags
  122. ext = strchr(ext, ' ')+1;
  123. //printf("3|%s\n", ext);
  124. //min_flt
  125. ext = strchr(ext, ' ')+1;
  126. //printf("4|%s\n", ext);
  127. //cmin_flt
  128. ext = strchr(ext, ' ')+1;
  129. //printf("5|%s\n", ext);
  130. //maj_flt
  131. ext = strchr(ext, ' ')+1;
  132. //printf("6|%s\n", ext);
  133. //cmaj_flt
  134. ext = strchr(ext, ' ')+1;
  135. //printf("7|%s\n", ext);
  136. utime = atoi(ext);
  137. ext = strchr(ext, ' ')+1;
  138. //printf("8|%s\n", ext);
  139. stime = atoi(ext);
  140. ext = strchr(ext, ' ')+1;
  141. //printf("9|%s\n", ext);
  142. //cutime
  143. ext = strchr(ext, ' ')+1;
  144. //printf("10|%s\n", ext);
  145. //cstime
  146. ext = strchr(ext, ' ')+1;
  147. //priority
  148. ext = strchr(ext, ' ')+1;
  149. //nice
  150. ext = strchr(ext, ' ')+1;
  151. //timeout
  152. ext = strchr(ext, ' ')+1;
  153. //it_real_value
  154. ext = strchr(ext, ' ')+1;
  155. start_time = atoi(ext);
  156. ext = strchr(psbuf, '(');
  157. ext++;
  158. strcpy(name, ext);
  159. pid = atoi(psbuf);
  160. state = statec;
  161. close(h);
  162. dev_to_name(tty, tty_name);
  163. bytes = 0;
  164. sbytes = 0;
  165. sprintf(psbuf, "/proc/%s/status", de->d_name);
  166. f = fopen(psbuf, "r");
  167. if (f) {
  168. while (fgets(psbuf, 250, f)) {
  169. if (strncmp(psbuf, "Mem:", 4) == 0) {
  170. bytes = atol(psbuf+5);
  171. bytes /= 1024;
  172. } else if (strncmp(psbuf, "Shared:", 7) == 0) {
  173. sbytes = atol(psbuf+8);
  174. sbytes /= 1024;
  175. } else if (strncmp(psbuf, "VmSize:", 7) == 0) {
  176. bytes = atol(psbuf+8);
  177. }
  178. }
  179. fclose(f);
  180. }
  181. seconds = ((uptime_secs * (long)HZ) - start_time) / HZ;
  182. /*printf("seconds=%s\n", gcvt(seconds, 15, psbuf));*/
  183. start = time_now - seconds;
  184. /*
  185. printf("1\n");
  186. gcvt(start, 15, psbuf);
  187. printf("2\n");
  188. printf("start=%s\n", psbuf);
  189. printf("utime=%d, stime=%d. start_time=%d\n", utime, stime, start_time);
  190. */
  191. total_time = (utime + stime);
  192. /*printf("total_time=%s\n", gcvt(total_time, 15, psbuf));*/
  193. pcpu = seconds ?
  194. (total_time * 10.0f * 100.0f / (float)HZ) / seconds :
  195. 0;
  196. if (pcpu > 999) pcpu = 999;
  197. sprintf(psbuf, "/proc/%s/cmdline", de->d_name);
  198. h = open(psbuf, O_RDONLY);
  199. if (h == -1) {
  200. perror("Unable to open cmdline");
  201. continue;
  202. }
  203. l = read(h, psbuf, 255);
  204. if (l < 0) {
  205. perror("Unable to read cmdline");
  206. close(h);
  207. continue;
  208. }
  209. close(h);
  210. /*
  211. * the args are NUL separated, substitute spaces instead
  212. */
  213. psbuf[l] = '\0';
  214. i=l;
  215. while(psbuf[i] == '\0')
  216. i--; /* Don't bother with trailing NULs */
  217. while(--i > 0)
  218. if (psbuf[i] == '\0')
  219. psbuf[i] = ' ';
  220. printf("%5d %4s %c %4ldK %3ldK %2u.%u %s\n", pid, tty_name, state,
  221. bytes, sbytes,
  222. pcpu / 10, pcpu % 10,
  223. /*(int)seconds / 60, (int)seconds % 60,*/
  224. l ? psbuf : name);
  225. next:
  226. ;
  227. }
  228. closedir(d);
  229. }