3
0

procps.c 17 KB

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