procps.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright 1998 by Albert Cahalan; all rights reserved.
  6. * Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
  7. * GNU Library General Public License Version 2, or any later version
  8. *
  9. */
  10. #include <dirent.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include <asm/page.h>
  15. #include "libbb.h"
  16. extern procps_status_t * procps_scan(int save_user_arg0)
  17. {
  18. static DIR *dir;
  19. struct dirent *entry;
  20. static procps_status_t ret_status;
  21. char *name;
  22. int n;
  23. char status[32];
  24. char buf[1024];
  25. FILE *fp;
  26. procps_status_t curstatus;
  27. int pid;
  28. long tasknice;
  29. struct stat sb;
  30. if (!dir) {
  31. dir = opendir("/proc");
  32. if(!dir)
  33. bb_error_msg_and_die("Can't open /proc");
  34. }
  35. for(;;) {
  36. if((entry = readdir(dir)) == NULL) {
  37. closedir(dir);
  38. dir = 0;
  39. return 0;
  40. }
  41. name = entry->d_name;
  42. if (!(*name >= '0' && *name <= '9'))
  43. continue;
  44. memset(&curstatus, 0, sizeof(procps_status_t));
  45. pid = atoi(name);
  46. curstatus.pid = pid;
  47. sprintf(status, "/proc/%d", pid);
  48. if(stat(status, &sb))
  49. continue;
  50. my_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
  51. sprintf(status, "/proc/%d/stat", pid);
  52. if((fp = fopen(status, "r")) == NULL)
  53. continue;
  54. name = fgets(buf, sizeof(buf), fp);
  55. fclose(fp);
  56. if(name == NULL)
  57. continue;
  58. name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
  59. if(name == 0 || name[1] != ' ')
  60. continue;
  61. *name = 0;
  62. sscanf(buf, "%*s (%15c", curstatus.short_cmd);
  63. n = sscanf(name+2,
  64. "%c %d "
  65. "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */
  66. "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
  67. #ifdef FEATURE_CPU_USAGE_PERCENTAGE
  68. "%lu %lu "
  69. #else
  70. "%*s %*s "
  71. #endif
  72. "%*s %*s %*s " /* cutime, cstime, priority */
  73. "%ld "
  74. "%*s %*s %*s " /* timeout, it_real_value, start_time */
  75. "%*s " /* vsize */
  76. "%ld",
  77. curstatus.state, &curstatus.ppid,
  78. #ifdef FEATURE_CPU_USAGE_PERCENTAGE
  79. &curstatus.utime, &curstatus.stime,
  80. #endif
  81. &tasknice,
  82. &curstatus.rss);
  83. #ifdef FEATURE_CPU_USAGE_PERCENTAGE
  84. if(n != 6)
  85. #else
  86. if(n != 4)
  87. #endif
  88. continue;
  89. if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
  90. curstatus.state[1] = 'W';
  91. else
  92. curstatus.state[1] = ' ';
  93. if (tasknice < 0)
  94. curstatus.state[2] = '<';
  95. else if (tasknice > 0)
  96. curstatus.state[2] = 'N';
  97. else
  98. curstatus.state[2] = ' ';
  99. #ifdef PAGE_SHIFT
  100. curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */
  101. #else
  102. curstatus.rss *= (getpagesize() >> 10); /* 2**10 = 1kb */
  103. #endif
  104. if(save_user_arg0) {
  105. sprintf(status, "/proc/%d/cmdline", pid);
  106. if((fp = fopen(status, "r")) == NULL)
  107. continue;
  108. if((n=fread(buf, 1, sizeof(buf)-1, fp)) > 0) {
  109. if(buf[n-1]=='\n')
  110. buf[--n] = 0;
  111. name = buf;
  112. while(n) {
  113. if(((unsigned char)*name) < ' ')
  114. *name = ' ';
  115. name++;
  116. n--;
  117. }
  118. *name = 0;
  119. if(buf[0])
  120. curstatus.cmd = strdup(buf);
  121. /* if NULL it work true also */
  122. }
  123. fclose(fp);
  124. }
  125. return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
  126. }
  127. }
  128. /* END CODE */
  129. /*
  130. Local Variables:
  131. c-file-style: "linux"
  132. c-basic-offset: 4
  133. tab-width: 4
  134. End:
  135. */