ps.c 5.5 KB

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