procps.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. * SELinux support: (c) 2007 by Yuichi Nakamura <ynakam@hitachisoft.jp>
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  10. */
  11. #include "libbb.h"
  12. typedef struct unsigned_to_name_map_t {
  13. unsigned id;
  14. char name[USERNAME_MAX_SIZE];
  15. } unsigned_to_name_map_t;
  16. typedef struct cache_t {
  17. unsigned_to_name_map_t *cache;
  18. int size;
  19. } cache_t;
  20. static cache_t username, groupname;
  21. static void clear_cache(cache_t *cp)
  22. {
  23. free(cp->cache);
  24. cp->cache = NULL;
  25. cp->size = 0;
  26. }
  27. void clear_username_cache(void)
  28. {
  29. clear_cache(&username);
  30. clear_cache(&groupname);
  31. }
  32. #if 0 /* more generic, but we don't need that yet */
  33. /* Returns -N-1 if not found. */
  34. /* cp->cache[N] is allocated and must be filled in this case */
  35. static int get_cached(cache_t *cp, unsigned id)
  36. {
  37. int i;
  38. for (i = 0; i < cp->size; i++)
  39. if (cp->cache[i].id == id)
  40. return i;
  41. i = cp->size++;
  42. cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
  43. cp->cache[i++].id = id;
  44. return -i;
  45. }
  46. #endif
  47. typedef char* ug_func(char *name, int bufsize, long uid);
  48. static char* get_cached(cache_t *cp, unsigned id, ug_func* fp)
  49. {
  50. int i;
  51. for (i = 0; i < cp->size; i++)
  52. if (cp->cache[i].id == id)
  53. return cp->cache[i].name;
  54. i = cp->size++;
  55. cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
  56. cp->cache[i].id = id;
  57. /* Never fails. Generates numeric string if name isn't found */
  58. fp(cp->cache[i].name, sizeof(cp->cache[i].name), id);
  59. return cp->cache[i].name;
  60. }
  61. const char* get_cached_username(uid_t uid)
  62. {
  63. return get_cached(&username, uid, bb_getpwuid);
  64. }
  65. const char* get_cached_groupname(gid_t gid)
  66. {
  67. return get_cached(&groupname, gid, bb_getgrgid);
  68. }
  69. #define PROCPS_BUFSIZE 1024
  70. static int read_to_buf(const char *filename, void *buf)
  71. {
  72. int fd;
  73. /* open_read_close() would do two reads, checking for EOF.
  74. * When you have 10000 /proc/$NUM/stat to read, it isn't desirable */
  75. ssize_t ret = -1;
  76. fd = open(filename, O_RDONLY);
  77. if (fd >= 0) {
  78. ret = read(fd, buf, PROCPS_BUFSIZE-1);
  79. close(fd);
  80. }
  81. ((char *)buf)[ret > 0 ? ret : 0] = '\0';
  82. return ret;
  83. }
  84. procps_status_t *alloc_procps_scan(int flags)
  85. {
  86. procps_status_t* sp = xzalloc(sizeof(procps_status_t));
  87. sp->dir = xopendir("/proc");
  88. return sp;
  89. }
  90. void free_procps_scan(procps_status_t* sp)
  91. {
  92. closedir(sp->dir);
  93. free(sp->argv0);
  94. USE_SELINUX(free(sp->context);)
  95. free(sp);
  96. }
  97. #if ENABLE_FEATURE_FAST_TOP
  98. /* We cut a lot of corners here for speed */
  99. static unsigned long fast_strtoul_10(char **endptr)
  100. {
  101. char c;
  102. char *str = *endptr;
  103. unsigned long n = *str - '0';
  104. while ((c = *++str) != ' ')
  105. n = n*10 + (c - '0');
  106. *endptr = str + 1; /* We skip trailing space! */
  107. return n;
  108. }
  109. static char *skip_fields(char *str, int count)
  110. {
  111. do {
  112. while (*str++ != ' ')
  113. continue;
  114. /* we found a space char, str points after it */
  115. } while (--count);
  116. return str;
  117. }
  118. #endif
  119. void BUG_comm_size(void);
  120. procps_status_t *procps_scan(procps_status_t* sp, int flags)
  121. {
  122. struct dirent *entry;
  123. char buf[PROCPS_BUFSIZE];
  124. char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
  125. char *filename_tail;
  126. long tasknice;
  127. unsigned pid;
  128. int n;
  129. struct stat sb;
  130. if (!sp)
  131. sp = alloc_procps_scan(flags);
  132. for (;;) {
  133. entry = readdir(sp->dir);
  134. if (entry == NULL) {
  135. free_procps_scan(sp);
  136. return NULL;
  137. }
  138. pid = bb_strtou(entry->d_name, NULL, 10);
  139. if (errno)
  140. continue;
  141. /* After this point we have to break, not continue
  142. * ("continue" would mean that current /proc/NNN
  143. * is not a valid process info) */
  144. memset(&sp->vsz, 0, sizeof(*sp) - offsetof(procps_status_t, vsz));
  145. sp->pid = pid;
  146. if (!(flags & ~PSSCAN_PID)) break;
  147. #if ENABLE_SELINUX
  148. if (flags & PSSCAN_CONTEXT) {
  149. if (getpidcon(sp->pid, &sp->context) < 0)
  150. sp->context = NULL;
  151. }
  152. #endif
  153. filename_tail = filename + sprintf(filename, "/proc/%d", pid);
  154. if (flags & PSSCAN_UIDGID) {
  155. if (stat(filename, &sb))
  156. break;
  157. /* Need comment - is this effective or real UID/GID? */
  158. sp->uid = sb.st_uid;
  159. sp->gid = sb.st_gid;
  160. }
  161. if (flags & PSSCAN_STAT) {
  162. char *cp, *comm1;
  163. int tty;
  164. #if !ENABLE_FEATURE_FAST_TOP
  165. unsigned long vsz, rss;
  166. #endif
  167. /* see proc(5) for some details on this */
  168. strcpy(filename_tail, "/stat");
  169. n = read_to_buf(filename, buf);
  170. if (n < 0)
  171. break;
  172. cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
  173. /*if (!cp || cp[1] != ' ')
  174. break;*/
  175. cp[0] = '\0';
  176. if (sizeof(sp->comm) < 16)
  177. BUG_comm_size();
  178. comm1 = strchr(buf, '(');
  179. /*if (comm1)*/
  180. safe_strncpy(sp->comm, comm1 + 1, sizeof(sp->comm));
  181. #if !ENABLE_FEATURE_FAST_TOP
  182. n = sscanf(cp+2,
  183. "%c %u " /* state, ppid */
  184. "%u %u %d %*s " /* pgid, sid, tty, tpgid */
  185. "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
  186. "%lu %lu " /* utime, stime */
  187. "%*s %*s %*s " /* cutime, cstime, priority */
  188. "%ld " /* nice */
  189. "%*s %*s %*s " /* timeout, it_real_value, start_time */
  190. "%lu " /* vsize */
  191. "%lu " /* rss */
  192. /* "%lu %lu %lu %lu %lu %lu " rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */
  193. /* "%u %u %u %u " signal, blocked, sigignore, sigcatch */
  194. /* "%lu %lu %lu" wchan, nswap, cnswap */
  195. ,
  196. sp->state, &sp->ppid,
  197. &sp->pgid, &sp->sid, &tty,
  198. &sp->utime, &sp->stime,
  199. &tasknice,
  200. &vsz,
  201. &rss);
  202. if (n != 10)
  203. break;
  204. sp->vsz = vsz >> 10; /* vsize is in bytes and we want kb */
  205. sp->rss = rss >> 10;
  206. sp->tty_major = (tty >> 8) & 0xfff;
  207. sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00);
  208. #else
  209. /* This costs ~100 bytes more but makes top faster by 20%
  210. * If you run 10000 processes, this may be important for you */
  211. sp->state[0] = cp[2];
  212. cp += 4;
  213. sp->ppid = fast_strtoul_10(&cp);
  214. sp->pgid = fast_strtoul_10(&cp);
  215. sp->sid = fast_strtoul_10(&cp);
  216. tty = fast_strtoul_10(&cp);
  217. sp->tty_major = (tty >> 8) & 0xfff;
  218. sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00);
  219. cp = skip_fields(cp, 6); /* tpgid, flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
  220. sp->utime = fast_strtoul_10(&cp);
  221. sp->stime = fast_strtoul_10(&cp);
  222. cp = skip_fields(cp, 3); /* cutime, cstime, priority */
  223. tasknice = fast_strtoul_10(&cp);
  224. cp = skip_fields(cp, 3); /* timeout, it_real_value, start_time */
  225. sp->vsz = fast_strtoul_10(&cp) >> 10; /* vsize is in bytes and we want kb */
  226. sp->rss = fast_strtoul_10(&cp) >> 10;
  227. #endif
  228. if (sp->vsz == 0 && sp->state[0] != 'Z')
  229. sp->state[1] = 'W';
  230. else
  231. sp->state[1] = ' ';
  232. if (tasknice < 0)
  233. sp->state[2] = '<';
  234. else if (tasknice) /* > 0 */
  235. sp->state[2] = 'N';
  236. else
  237. sp->state[2] = ' ';
  238. }
  239. #if 0 /* PSSCAN_CMD is not used */
  240. if (flags & (PSSCAN_CMD|PSSCAN_ARGV0)) {
  241. if (sp->argv0) {
  242. free(sp->argv0);
  243. sp->argv0 = NULL;
  244. }
  245. if (sp->cmd) {
  246. free(sp->cmd);
  247. sp->cmd = NULL;
  248. }
  249. strcpy(filename_tail, "/cmdline");
  250. /* TODO: to get rid of size limits, read into malloc buf,
  251. * then realloc it down to real size. */
  252. n = read_to_buf(filename, buf);
  253. if (n <= 0)
  254. break;
  255. if (flags & PSSCAN_ARGV0)
  256. sp->argv0 = xstrdup(buf);
  257. if (flags & PSSCAN_CMD) {
  258. do {
  259. n--;
  260. if ((unsigned char)(buf[n]) < ' ')
  261. buf[n] = ' ';
  262. } while (n);
  263. sp->cmd = xstrdup(buf);
  264. }
  265. }
  266. #else
  267. if (flags & PSSCAN_ARGV0) {
  268. if (sp->argv0) {
  269. free(sp->argv0);
  270. sp->argv0 = NULL;
  271. }
  272. strcpy(filename_tail, "/cmdline");
  273. n = read_to_buf(filename, buf);
  274. if (n <= 0)
  275. break;
  276. if (flags & PSSCAN_ARGV0)
  277. sp->argv0 = xstrdup(buf);
  278. }
  279. #endif
  280. break;
  281. }
  282. return sp;
  283. }
  284. void read_cmdline(char *buf, int col, unsigned pid, const char *comm)
  285. {
  286. ssize_t sz;
  287. char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
  288. sprintf(filename, "/proc/%u/cmdline", pid);
  289. sz = open_read_close(filename, buf, col);
  290. if (sz > 0) {
  291. buf[sz] = '\0';
  292. while (--sz >= 0)
  293. if ((unsigned char)(buf[sz]) < ' ')
  294. buf[sz] = ' ';
  295. } else {
  296. snprintf(buf, col, "[%s]", comm);
  297. }
  298. }
  299. /* from kernel:
  300. // pid comm S ppid pgid sid tty_nr tty_pgrp flg
  301. sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
  302. %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
  303. %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
  304. task->pid,
  305. tcomm,
  306. state,
  307. ppid,
  308. pgid,
  309. sid,
  310. tty_nr,
  311. tty_pgrp,
  312. task->flags,
  313. min_flt,
  314. cmin_flt,
  315. maj_flt,
  316. cmaj_flt,
  317. cputime_to_clock_t(utime),
  318. cputime_to_clock_t(stime),
  319. cputime_to_clock_t(cutime),
  320. cputime_to_clock_t(cstime),
  321. priority,
  322. nice,
  323. num_threads,
  324. // 0,
  325. start_time,
  326. vsize,
  327. mm ? get_mm_rss(mm) : 0,
  328. rsslim,
  329. mm ? mm->start_code : 0,
  330. mm ? mm->end_code : 0,
  331. mm ? mm->start_stack : 0,
  332. esp,
  333. eip,
  334. the rest is some obsolete cruft
  335. */