procps.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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 source tree.
  10. */
  11. #include "libbb.h"
  12. typedef struct id_to_name_map_t {
  13. uid_t id;
  14. char name[USERNAME_MAX_SIZE];
  15. } id_to_name_map_t;
  16. typedef struct cache_t {
  17. id_to_name_map_t *cache;
  18. int size;
  19. } cache_t;
  20. static cache_t *cache_user_group;
  21. void FAST_FUNC clear_username_cache(void)
  22. {
  23. if (cache_user_group) {
  24. free(cache_user_group[0].cache);
  25. free(cache_user_group[1].cache);
  26. free(cache_user_group);
  27. cache_user_group = NULL;
  28. }
  29. }
  30. static char* get_cached(int user_group, uid_t id,
  31. char* FAST_FUNC x2x_utoa(uid_t id))
  32. {
  33. cache_t *cp;
  34. int i;
  35. if (!cache_user_group)
  36. cache_user_group = xzalloc(sizeof(cache_user_group[0]) * 2);
  37. cp = &cache_user_group[user_group];
  38. for (i = 0; i < cp->size; i++)
  39. if (cp->cache[i].id == id)
  40. return cp->cache[i].name;
  41. i = cp->size++;
  42. cp->cache = xrealloc_vector(cp->cache, 2, i);
  43. cp->cache[i].id = id;
  44. /* Never fails. Generates numeric string if name isn't found */
  45. safe_strncpy(cp->cache[i].name, x2x_utoa(id), sizeof(cp->cache[i].name));
  46. return cp->cache[i].name;
  47. }
  48. const char* FAST_FUNC get_cached_username(uid_t uid)
  49. {
  50. return get_cached(0, uid, uid2uname_utoa);
  51. }
  52. const char* FAST_FUNC get_cached_groupname(gid_t gid)
  53. {
  54. return get_cached(1, gid, gid2group_utoa);
  55. }
  56. #define PROCPS_BUFSIZE 1024
  57. static int read_to_buf(const char *filename, void *buf)
  58. {
  59. int fd;
  60. /* open_read_close() would do two reads, checking for EOF.
  61. * When you have 10000 /proc/$NUM/stat to read, it isn't desirable */
  62. ssize_t ret = -1;
  63. fd = open(filename, O_RDONLY);
  64. if (fd >= 0) {
  65. ret = read(fd, buf, PROCPS_BUFSIZE-1);
  66. close(fd);
  67. }
  68. ((char *)buf)[ret > 0 ? ret : 0] = '\0';
  69. return ret;
  70. }
  71. static procps_status_t* FAST_FUNC alloc_procps_scan(void)
  72. {
  73. procps_status_t* sp = xzalloc(sizeof(procps_status_t));
  74. unsigned n = bb_getpagesize();
  75. while (1) {
  76. n >>= 1;
  77. if (!n) break;
  78. sp->shift_pages_to_bytes++;
  79. }
  80. sp->shift_pages_to_kb = sp->shift_pages_to_bytes - 10;
  81. sp->dir = xopendir("/proc");
  82. return sp;
  83. }
  84. void FAST_FUNC free_procps_scan(procps_status_t* sp)
  85. {
  86. closedir(sp->dir);
  87. #if ENABLE_FEATURE_SHOW_THREADS
  88. if (sp->task_dir)
  89. closedir(sp->task_dir);
  90. #endif
  91. free(sp->argv0);
  92. free(sp->exe);
  93. IF_SELINUX(free(sp->context);)
  94. free(sp);
  95. }
  96. #if ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
  97. static unsigned long long fast_strtoull_16(char **endptr)
  98. {
  99. unsigned char c;
  100. char *str = *endptr;
  101. unsigned long long n = 0;
  102. /* Need to stop on both ' ' and '\n' */
  103. while ((c = *str++) > ' ') {
  104. c = ((c|0x20) - '0');
  105. if (c > 9)
  106. /* c = c + '0' - 'a' + 10: */
  107. c = c - ('a' - '0' - 10);
  108. n = n*16 + c;
  109. }
  110. *endptr = str; /* We skip trailing space! */
  111. return n;
  112. }
  113. #endif
  114. #if ENABLE_FEATURE_FAST_TOP || ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
  115. /* We cut a lot of corners here for speed */
  116. static unsigned long fast_strtoul_10(char **endptr)
  117. {
  118. unsigned char c;
  119. char *str = *endptr;
  120. unsigned long n = *str - '0';
  121. /* Need to stop on both ' ' and '\n' */
  122. while ((c = *++str) > ' ')
  123. n = n*10 + (c - '0');
  124. *endptr = str + 1; /* We skip trailing space! */
  125. return n;
  126. }
  127. # if ENABLE_FEATURE_FAST_TOP
  128. static long fast_strtol_10(char **endptr)
  129. {
  130. if (**endptr != '-')
  131. return fast_strtoul_10(endptr);
  132. (*endptr)++;
  133. return - (long)fast_strtoul_10(endptr);
  134. }
  135. # endif
  136. static char *skip_fields(char *str, int count)
  137. {
  138. do {
  139. while (*str++ != ' ')
  140. continue;
  141. /* we found a space char, str points after it */
  142. } while (--count);
  143. return str;
  144. }
  145. #endif
  146. #if ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
  147. static char* skip_whitespace_if_prefixed_with(char *buf, const char *prefix)
  148. {
  149. char *tp = is_prefixed_with(buf, prefix);
  150. if (tp) {
  151. tp = skip_whitespace(tp);
  152. }
  153. return tp;
  154. }
  155. int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
  156. void (*cb)(struct smaprec *, void *), void *data)
  157. {
  158. FILE *file;
  159. struct smaprec currec;
  160. char filename[sizeof("/proc/%u/smaps") + sizeof(int)*3];
  161. char buf[PROCPS_BUFSIZE];
  162. #if !ENABLE_PMAP
  163. void (*cb)(struct smaprec *, void *) = NULL;
  164. void *data = NULL;
  165. #endif
  166. sprintf(filename, "/proc/%u/smaps", (int)pid);
  167. file = fopen_for_read(filename);
  168. if (!file)
  169. return 1;
  170. memset(&currec, 0, sizeof(currec));
  171. while (fgets(buf, PROCPS_BUFSIZE, file)) {
  172. // Each mapping datum has this form:
  173. // f7d29000-f7d39000 rw-s FILEOFS M:m INODE FILENAME
  174. // Size: nnn kB
  175. // Rss: nnn kB
  176. // .....
  177. char *tp, *p;
  178. #define SCAN(S, X) \
  179. if ((tp = skip_whitespace_if_prefixed_with(buf, S)) != NULL) { \
  180. total->X += currec.X = fast_strtoul_10(&tp); \
  181. continue; \
  182. }
  183. if (cb) {
  184. SCAN("Pss:" , smap_pss );
  185. SCAN("Swap:" , smap_swap );
  186. }
  187. SCAN("Private_Dirty:", private_dirty);
  188. SCAN("Private_Clean:", private_clean);
  189. SCAN("Shared_Dirty:" , shared_dirty );
  190. SCAN("Shared_Clean:" , shared_clean );
  191. #undef SCAN
  192. tp = strchr(buf, '-');
  193. if (tp) {
  194. // We reached next mapping - the line of this form:
  195. // f7d29000-f7d39000 rw-s FILEOFS M:m INODE FILENAME
  196. if (cb) {
  197. /* If we have a previous record, there's nothing more
  198. * for it, call the callback and clear currec
  199. */
  200. if (currec.smap_size)
  201. cb(&currec, data);
  202. free(currec.smap_name);
  203. }
  204. memset(&currec, 0, sizeof(currec));
  205. *tp = ' ';
  206. tp = buf;
  207. currec.smap_start = fast_strtoull_16(&tp);
  208. currec.smap_size = (fast_strtoull_16(&tp) - currec.smap_start) >> 10;
  209. strncpy(currec.smap_mode, tp, sizeof(currec.smap_mode)-1);
  210. // skipping "rw-s FILEOFS M:m INODE "
  211. tp = skip_whitespace(skip_fields(tp, 4));
  212. // filter out /dev/something (something != zero)
  213. if (!is_prefixed_with(tp, "/dev/") || strcmp(tp, "/dev/zero\n") == 0) {
  214. if (currec.smap_mode[1] == 'w') {
  215. currec.mapped_rw = currec.smap_size;
  216. total->mapped_rw += currec.smap_size;
  217. } else if (currec.smap_mode[1] == '-') {
  218. currec.mapped_ro = currec.smap_size;
  219. total->mapped_ro += currec.smap_size;
  220. }
  221. }
  222. if (strcmp(tp, "[stack]\n") == 0)
  223. total->stack += currec.smap_size;
  224. if (cb) {
  225. p = skip_non_whitespace(tp);
  226. if (p == tp) {
  227. currec.smap_name = xstrdup(" [ anon ]");
  228. } else {
  229. *p = '\0';
  230. currec.smap_name = xstrdup(tp);
  231. }
  232. }
  233. total->smap_size += currec.smap_size;
  234. }
  235. }
  236. fclose(file);
  237. if (cb) {
  238. if (currec.smap_size)
  239. cb(&currec, data);
  240. free(currec.smap_name);
  241. }
  242. return 0;
  243. }
  244. #endif
  245. procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
  246. {
  247. if (!sp)
  248. sp = alloc_procps_scan();
  249. for (;;) {
  250. struct dirent *entry;
  251. char buf[PROCPS_BUFSIZE];
  252. long tasknice;
  253. unsigned pid;
  254. int n;
  255. char filename[sizeof("/proc/%u/task/%u/cmdline") + sizeof(int)*3 * 2];
  256. char *filename_tail;
  257. #if ENABLE_FEATURE_SHOW_THREADS
  258. if (sp->task_dir) {
  259. entry = readdir(sp->task_dir);
  260. if (entry)
  261. goto got_entry;
  262. closedir(sp->task_dir);
  263. sp->task_dir = NULL;
  264. }
  265. #endif
  266. entry = readdir(sp->dir);
  267. if (entry == NULL) {
  268. free_procps_scan(sp);
  269. return NULL;
  270. }
  271. IF_FEATURE_SHOW_THREADS(got_entry:)
  272. pid = bb_strtou(entry->d_name, NULL, 10);
  273. if (errno)
  274. continue;
  275. #if ENABLE_FEATURE_SHOW_THREADS
  276. if ((flags & PSSCAN_TASKS) && !sp->task_dir) {
  277. /* We found another /proc/PID. Do not use it,
  278. * there will be /proc/PID/task/PID (same PID!),
  279. * so just go ahead and dive into /proc/PID/task. */
  280. sprintf(filename, "/proc/%u/task", pid);
  281. /* Note: if opendir fails, we just go to next /proc/XXX */
  282. sp->task_dir = opendir(filename);
  283. sp->main_thread_pid = pid;
  284. continue;
  285. }
  286. #endif
  287. /* After this point we can:
  288. * "break": stop parsing, return the data
  289. * "continue": try next /proc/XXX
  290. */
  291. memset(&sp->vsz, 0, sizeof(*sp) - offsetof(procps_status_t, vsz));
  292. sp->pid = pid;
  293. if (!(flags & ~PSSCAN_PID))
  294. break; /* we needed only pid, we got it */
  295. #if ENABLE_SELINUX
  296. if (flags & PSSCAN_CONTEXT) {
  297. if (getpidcon(sp->pid, &sp->context) < 0)
  298. sp->context = NULL;
  299. }
  300. #endif
  301. #if ENABLE_FEATURE_SHOW_THREADS
  302. if (sp->task_dir)
  303. filename_tail = filename + sprintf(filename, "/proc/%u/task/%u/", sp->main_thread_pid, pid);
  304. else
  305. #endif
  306. filename_tail = filename + sprintf(filename, "/proc/%u/", pid);
  307. if (flags & PSSCAN_UIDGID) {
  308. struct stat sb;
  309. if (stat(filename, &sb))
  310. continue; /* process probably exited */
  311. /* Effective UID/GID, not real */
  312. sp->uid = sb.st_uid;
  313. sp->gid = sb.st_gid;
  314. }
  315. /* These are all retrieved from proc/NN/stat in one go: */
  316. if (flags & (PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID
  317. | PSSCAN_COMM | PSSCAN_STATE
  318. | PSSCAN_VSZ | PSSCAN_RSS
  319. | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_START_TIME
  320. | PSSCAN_TTY | PSSCAN_NICE
  321. | PSSCAN_CPU)
  322. ) {
  323. int s_idx;
  324. char *cp, *comm1;
  325. int tty;
  326. #if !ENABLE_FEATURE_FAST_TOP
  327. unsigned long vsz, rss;
  328. #endif
  329. /* see proc(5) for some details on this */
  330. strcpy(filename_tail, "stat");
  331. n = read_to_buf(filename, buf);
  332. if (n < 0)
  333. continue; /* process probably exited */
  334. cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
  335. /*if (!cp || cp[1] != ' ')
  336. continue;*/
  337. cp[0] = '\0';
  338. BUILD_BUG_ON(sizeof(sp->comm) < 16);
  339. comm1 = strchr(buf, '(');
  340. /*if (comm1)*/
  341. safe_strncpy(sp->comm, comm1 + 1, sizeof(sp->comm));
  342. #if !ENABLE_FEATURE_FAST_TOP
  343. n = sscanf(cp+2,
  344. "%c %u " /* state, ppid */
  345. "%u %u %d %*s " /* pgid, sid, tty, tpgid */
  346. "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
  347. "%lu %lu " /* utime, stime */
  348. "%*s %*s %*s " /* cutime, cstime, priority */
  349. "%ld " /* nice */
  350. "%*s %*s " /* timeout, it_real_value */
  351. "%lu " /* start_time */
  352. "%lu " /* vsize */
  353. "%lu " /* rss */
  354. # if ENABLE_FEATURE_TOP_SMP_PROCESS
  355. "%*s %*s %*s %*s %*s %*s " /*rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */
  356. "%*s %*s %*s %*s " /*signal, blocked, sigignore, sigcatch */
  357. "%*s %*s %*s %*s " /*wchan, nswap, cnswap, exit_signal */
  358. "%d" /*cpu last seen on*/
  359. # endif
  360. ,
  361. sp->state, &sp->ppid,
  362. &sp->pgid, &sp->sid, &tty,
  363. &sp->utime, &sp->stime,
  364. &tasknice,
  365. &sp->start_time,
  366. &vsz,
  367. &rss
  368. # if ENABLE_FEATURE_TOP_SMP_PROCESS
  369. , &sp->last_seen_on_cpu
  370. # endif
  371. );
  372. if (n < 11)
  373. continue; /* bogus data, get next /proc/XXX */
  374. # if ENABLE_FEATURE_TOP_SMP_PROCESS
  375. if (n == 11)
  376. sp->last_seen_on_cpu = 0;
  377. # endif
  378. /* vsz is in bytes and we want kb */
  379. sp->vsz = vsz >> 10;
  380. /* vsz is in bytes but rss is in *PAGES*! Can you believe that? */
  381. sp->rss = rss << sp->shift_pages_to_kb;
  382. sp->tty_major = (tty >> 8) & 0xfff;
  383. sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00);
  384. #else
  385. /* This costs ~100 bytes more but makes top faster by 20%
  386. * If you run 10000 processes, this may be important for you */
  387. sp->state[0] = cp[2];
  388. cp += 4;
  389. sp->ppid = fast_strtoul_10(&cp);
  390. sp->pgid = fast_strtoul_10(&cp);
  391. sp->sid = fast_strtoul_10(&cp);
  392. tty = fast_strtoul_10(&cp);
  393. sp->tty_major = (tty >> 8) & 0xfff;
  394. sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00);
  395. cp = skip_fields(cp, 6); /* tpgid, flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
  396. sp->utime = fast_strtoul_10(&cp);
  397. sp->stime = fast_strtoul_10(&cp);
  398. cp = skip_fields(cp, 3); /* cutime, cstime, priority */
  399. tasknice = fast_strtol_10(&cp);
  400. cp = skip_fields(cp, 2); /* timeout, it_real_value */
  401. sp->start_time = fast_strtoul_10(&cp);
  402. /* vsz is in bytes and we want kb */
  403. sp->vsz = fast_strtoul_10(&cp) >> 10;
  404. /* vsz is in bytes but rss is in *PAGES*! Can you believe that? */
  405. sp->rss = fast_strtoul_10(&cp) << sp->shift_pages_to_kb;
  406. # if ENABLE_FEATURE_TOP_SMP_PROCESS
  407. /* (6): rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */
  408. /* (4): signal, blocked, sigignore, sigcatch */
  409. /* (4): wchan, nswap, cnswap, exit_signal */
  410. cp = skip_fields(cp, 14);
  411. //FIXME: is it safe to assume this field exists?
  412. sp->last_seen_on_cpu = fast_strtoul_10(&cp);
  413. # endif
  414. #endif /* FEATURE_FAST_TOP */
  415. #if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
  416. sp->niceness = tasknice;
  417. #endif
  418. sp->state[1] = ' ';
  419. sp->state[2] = ' ';
  420. s_idx = 1;
  421. if (sp->vsz == 0 && sp->state[0] != 'Z') {
  422. /* not sure what the purpose of this flag */
  423. sp->state[1] = 'W';
  424. s_idx = 2;
  425. }
  426. if (tasknice != 0) {
  427. if (tasknice < 0)
  428. sp->state[s_idx] = '<';
  429. else /* > 0 */
  430. sp->state[s_idx] = 'N';
  431. }
  432. }
  433. #if ENABLE_FEATURE_TOPMEM
  434. if (flags & PSSCAN_SMAPS)
  435. procps_read_smaps(pid, &sp->smaps, NULL, NULL);
  436. #endif /* TOPMEM */
  437. #if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
  438. if (flags & PSSCAN_RUIDGID) {
  439. FILE *file;
  440. strcpy(filename_tail, "status");
  441. file = fopen_for_read(filename);
  442. if (file) {
  443. while (fgets(buf, sizeof(buf), file)) {
  444. char *tp;
  445. #define SCAN_TWO(str, name, statement) \
  446. if ((tp = is_prefixed_with(buf, str)) != NULL) { \
  447. tp = skip_whitespace(tp); \
  448. sscanf(tp, "%u", &sp->name); \
  449. statement; \
  450. }
  451. SCAN_TWO("Uid:", ruid, continue);
  452. SCAN_TWO("Gid:", rgid, break);
  453. #undef SCAN_TWO
  454. }
  455. fclose(file);
  456. }
  457. }
  458. #endif /* PS_ADDITIONAL_COLUMNS */
  459. if (flags & PSSCAN_EXE) {
  460. strcpy(filename_tail, "exe");
  461. free(sp->exe);
  462. sp->exe = xmalloc_readlink(filename);
  463. }
  464. /* Note: if /proc/PID/cmdline is empty,
  465. * code below "breaks". Therefore it must be
  466. * the last code to parse /proc/PID/xxx data
  467. * (we used to have /proc/PID/exe parsing after it
  468. * and were getting stale sp->exe).
  469. */
  470. #if 0 /* PSSCAN_CMD is not used */
  471. if (flags & (PSSCAN_CMD|PSSCAN_ARGV0)) {
  472. free(sp->argv0);
  473. sp->argv0 = NULL;
  474. free(sp->cmd);
  475. sp->cmd = NULL;
  476. strcpy(filename_tail, "cmdline");
  477. /* TODO: to get rid of size limits, read into malloc buf,
  478. * then realloc it down to real size. */
  479. n = read_to_buf(filename, buf);
  480. if (n <= 0)
  481. break;
  482. if (flags & PSSCAN_ARGV0)
  483. sp->argv0 = xstrdup(buf);
  484. if (flags & PSSCAN_CMD) {
  485. do {
  486. n--;
  487. if ((unsigned char)(buf[n]) < ' ')
  488. buf[n] = ' ';
  489. } while (n);
  490. sp->cmd = xstrdup(buf);
  491. }
  492. }
  493. #else
  494. if (flags & (PSSCAN_ARGV0|PSSCAN_ARGVN)) {
  495. free(sp->argv0);
  496. sp->argv0 = NULL;
  497. strcpy(filename_tail, "cmdline");
  498. n = read_to_buf(filename, buf);
  499. if (n <= 0)
  500. break;
  501. if (flags & PSSCAN_ARGVN) {
  502. sp->argv_len = n;
  503. sp->argv0 = xmemdup(buf, n + 1);
  504. /* sp->argv0[n] = '\0'; - buf has it */
  505. } else {
  506. sp->argv_len = 0;
  507. sp->argv0 = xstrdup(buf);
  508. }
  509. }
  510. #endif
  511. break;
  512. } /* for (;;) */
  513. return sp;
  514. }
  515. void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm)
  516. {
  517. int sz;
  518. char filename[sizeof("/proc/%u/cmdline") + sizeof(int)*3];
  519. sprintf(filename, "/proc/%u/cmdline", pid);
  520. sz = open_read_close(filename, buf, col - 1);
  521. if (sz > 0) {
  522. const char *base;
  523. int comm_len;
  524. buf[sz] = '\0';
  525. while (--sz >= 0 && buf[sz] == '\0')
  526. continue;
  527. /* Prevent basename("process foo/bar") = "bar" */
  528. strchrnul(buf, ' ')[0] = '\0';
  529. base = bb_basename(buf); /* before we replace argv0's NUL with space */
  530. while (sz >= 0) {
  531. if ((unsigned char)(buf[sz]) < ' ')
  532. buf[sz] = ' ';
  533. sz--;
  534. }
  535. if (base[0] == '-') /* "-sh" (login shell)? */
  536. base++;
  537. /* If comm differs from argv0, prepend "{comm} ".
  538. * It allows to see thread names set by prctl(PR_SET_NAME).
  539. */
  540. if (!comm)
  541. return;
  542. comm_len = strlen(comm);
  543. /* Why compare up to comm_len, not COMM_LEN-1?
  544. * Well, some processes rewrite argv, and use _spaces_ there
  545. * while rewriting. (KDE is observed to do it).
  546. * I prefer to still treat argv0 "process foo bar"
  547. * as 'equal' to comm "process".
  548. */
  549. if (strncmp(base, comm, comm_len) != 0) {
  550. comm_len += 3;
  551. if (col > comm_len)
  552. memmove(buf + comm_len, buf, col - comm_len);
  553. snprintf(buf, col, "{%s}", comm);
  554. if (col <= comm_len)
  555. return;
  556. buf[comm_len - 1] = ' ';
  557. buf[col - 1] = '\0';
  558. }
  559. } else {
  560. snprintf(buf, col, "[%s]", comm ? comm : "?");
  561. }
  562. }
  563. /* from kernel:
  564. // pid comm S ppid pgid sid tty_nr tty_pgrp flg
  565. sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
  566. %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
  567. %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
  568. task->pid,
  569. tcomm,
  570. state,
  571. ppid,
  572. pgid,
  573. sid,
  574. tty_nr,
  575. tty_pgrp,
  576. task->flags,
  577. min_flt,
  578. cmin_flt,
  579. maj_flt,
  580. cmaj_flt,
  581. cputime_to_clock_t(utime),
  582. cputime_to_clock_t(stime),
  583. cputime_to_clock_t(cutime),
  584. cputime_to_clock_t(cstime),
  585. priority,
  586. nice,
  587. num_threads,
  588. // 0,
  589. start_time,
  590. vsize,
  591. mm ? get_mm_rss(mm) : 0,
  592. rsslim,
  593. mm ? mm->start_code : 0,
  594. mm ? mm->end_code : 0,
  595. mm ? mm->start_stack : 0,
  596. esp,
  597. eip,
  598. the rest is some obsolete cruft
  599. */