ps.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. void ps(char*);
  13. void error(char*);
  14. int cmp(const void *c, const void*);
  15. Biobuf bout;
  16. int pflag;
  17. int aflag;
  18. int rflag;
  19. void
  20. main(int argc, char *argv[])
  21. {
  22. int fd, i, tot, none = 1;
  23. Dir *dir, **mem;
  24. ARGBEGIN {
  25. case 'a':
  26. aflag++;
  27. break;
  28. case 'p':
  29. pflag++;
  30. break;
  31. case 'r':
  32. rflag++;
  33. break;
  34. } ARGEND;
  35. Binit(&bout, 1, OWRITE);
  36. if(chdir("/proc")==-1)
  37. error("/proc");
  38. fd=open(".", OREAD);
  39. if(fd<0)
  40. error("/proc");
  41. tot = dirreadall(fd, &dir);
  42. if(tot <= 0){
  43. fprint(2, "ps: empty directory /proc\n");
  44. exits("empty");
  45. }
  46. mem = malloc(tot*sizeof(Dir*));
  47. for(i=0; i<tot; i++)
  48. mem[i] = dir++;
  49. qsort(mem, tot, sizeof(Dir*), cmp);
  50. for(i=0; i<tot; i++){
  51. ps(mem[i]->name);
  52. none = 0;
  53. }
  54. if(none)
  55. error("no processes; bad #p");
  56. exits(0);
  57. }
  58. void
  59. ps(char *s)
  60. {
  61. uint32_t utime, stime, rtime, size;
  62. int argc, basepri, fd, i, n, pri;
  63. char args[256], *argv[16], buf[64], pbuf[8], rbuf[20], rbuf1[20], status[4096];
  64. sprint(buf, "%s/status", s);
  65. fd = open(buf, OREAD);
  66. if(fd<0)
  67. return;
  68. n = read(fd, status, sizeof status-1);
  69. close(fd);
  70. if(n <= 0)
  71. return;
  72. status[n] = '\0';
  73. if((argc = tokenize(status, argv, nelem(argv)-1)) < 12)
  74. return;
  75. argv[argc] = 0;
  76. /*
  77. * 0 text
  78. * 1 user
  79. * 2 state
  80. * 3 cputime[6]
  81. * 9 memory
  82. * 10 basepri
  83. * 11 pri
  84. */
  85. utime = strtoul(argv[3], 0, 0)/1000;
  86. stime = strtoul(argv[4], 0, 0)/1000;
  87. rtime = strtoul(argv[5], 0, 0)/1000;
  88. size = strtoul(argv[9], 0, 0);
  89. if(pflag){
  90. basepri = strtoul(argv[10], 0, 0);
  91. pri = strtoul(argv[11], 0, 0);
  92. sprint(pbuf, " %2d %2d", basepri, pri);
  93. } else
  94. pbuf[0] = 0;
  95. if(rflag){
  96. if(rtime >= 86400)
  97. sprint(rbuf, " %lu:%02lu:%02lu:%02lu", rtime/86400, (rtime/3600)%24, (rtime/60)%60, rtime%60);
  98. else if(rtime >= 3600)
  99. sprint(rbuf, " %lu:%02lu:%02lu", rtime/3600, (rtime/60)%60, rtime%60);
  100. else
  101. sprint(rbuf, " %lu:%02lu", rtime/60, rtime%60);
  102. sprint(rbuf1, "%12s", rbuf);
  103. }else
  104. rbuf1[0] = 0;
  105. Bprint(&bout, "%-10s %8s%s %4lu:%.2lu %3lu:%.2lu %s %7luK %-8.8s ",
  106. argv[1],
  107. s,
  108. rbuf1,
  109. utime/60, utime%60,
  110. stime/60, stime%60,
  111. pbuf,
  112. size,
  113. argv[2]);
  114. if(aflag == 0){
  115. Noargs:
  116. Bprint(&bout, "%s\n", argv[0]);
  117. return;
  118. }
  119. sprint(buf, "%s/args", s);
  120. fd = open(buf, OREAD);
  121. if(fd < 0)
  122. goto Badargs;
  123. n = read(fd, args, sizeof args-1);
  124. close(fd);
  125. if(n < 0)
  126. goto Badargs;
  127. if(n == 0)
  128. goto Noargs;
  129. args[n] = '\0';
  130. for(i=0; i<n; i++)
  131. if(args[i] == '\n')
  132. args[i] = ' ';
  133. Bprint(&bout, "%s\n", args);
  134. return;
  135. Badargs:
  136. Bprint(&bout, "%s ?\n", argv[0]);
  137. }
  138. void
  139. error(char *s)
  140. {
  141. fprint(2, "ps: %s: ", s);
  142. perror("error");
  143. exits(s);
  144. }
  145. int
  146. cmp(const void *va, const void *vb)
  147. {
  148. Dir **a, **b;
  149. a = va;
  150. b = vb;
  151. return atoi((*a)->name) - atoi((*b)->name);
  152. }