array.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * linux/fs/proc/array.c
  3. *
  4. * Copyright (C) 1992 by Linus Torvalds
  5. * based on ideas by Darren Senn
  6. *
  7. * Fixes:
  8. * Michael. K. Johnson: stat,statm extensions.
  9. * <johnsonm@stolaf.edu>
  10. *
  11. * Pauline Middelink : Made cmdline,envline only break at '\0's, to
  12. * make sure SET_PROCTITLE works. Also removed
  13. * bad '!' which forced address recalculation for
  14. * EVERY character on the current page.
  15. * <middelin@polyware.iaf.nl>
  16. *
  17. * Danny ter Haar : added cpuinfo
  18. * <dth@cistron.nl>
  19. *
  20. * Alessandro Rubini : profile extension.
  21. * <rubini@ipvvis.unipv.it>
  22. *
  23. * Jeff Tranter : added BogoMips field to cpuinfo
  24. * <Jeff_Tranter@Mitel.COM>
  25. *
  26. * Bruno Haible : remove 4K limit for the maps file
  27. * <haible@ma2s2.mathematik.uni-karlsruhe.de>
  28. *
  29. * Yves Arrouye : remove removal of trailing spaces in get_array.
  30. * <Yves.Arrouye@marin.fdn.fr>
  31. *
  32. * Jerome Forissier : added per-CPU time information to /proc/stat
  33. * and /proc/<pid>/cpu extension
  34. * <forissier@isia.cma.fr>
  35. * - Incorporation and non-SMP safe operation
  36. * of forissier patch in 2.1.78 by
  37. * Hans Marcus <crowbar@concepts.nl>
  38. *
  39. * aeb@cwi.nl : /proc/partitions
  40. *
  41. *
  42. * Alan Cox : security fixes.
  43. * <Alan.Cox@linux.org>
  44. *
  45. * Al Viro : safe handling of mm_struct
  46. *
  47. * Gerhard Wichert : added BIGMEM support
  48. * Siemens AG <Gerhard.Wichert@pdb.siemens.de>
  49. *
  50. * Al Viro & Jeff Garzik : moved most of the thing into base.c and
  51. * : proc_misc.c. The rest may eventually go into
  52. * : base.c too.
  53. */
  54. #include <linux/types.h>
  55. #include <linux/errno.h>
  56. #include <linux/time.h>
  57. #include <linux/kernel.h>
  58. #include <linux/kernel_stat.h>
  59. #include <linux/tty.h>
  60. #include <linux/string.h>
  61. #include <linux/mman.h>
  62. #include <linux/proc_fs.h>
  63. #include <linux/ioport.h>
  64. #include <linux/uaccess.h>
  65. #include <linux/io.h>
  66. #include <linux/mm.h>
  67. #include <linux/hugetlb.h>
  68. #include <linux/pagemap.h>
  69. #include <linux/swap.h>
  70. #include <linux/slab.h>
  71. #include <linux/smp.h>
  72. #include <linux/signal.h>
  73. #include <linux/highmem.h>
  74. #include <linux/file.h>
  75. #include <linux/times.h>
  76. #include <linux/cpuset.h>
  77. #include <linux/rcupdate.h>
  78. #include <linux/delayacct.h>
  79. #include <linux/pid_namespace.h>
  80. #include <asm/pgtable.h>
  81. #include <asm/processor.h>
  82. #include "internal.h"
  83. /* Gcc optimizes away "strlen(x)" for constant x */
  84. #define ADDBUF(buffer, string) \
  85. do { memcpy(buffer, string, strlen(string)); \
  86. buffer += strlen(string); } while (0)
  87. static inline char *task_name(struct task_struct *p, char *buf)
  88. {
  89. int i;
  90. char *name;
  91. char tcomm[sizeof(p->comm)];
  92. get_task_comm(tcomm, p);
  93. ADDBUF(buf, "Name:\t");
  94. name = tcomm;
  95. i = sizeof(tcomm);
  96. do {
  97. unsigned char c = *name;
  98. name++;
  99. i--;
  100. *buf = c;
  101. if (!c)
  102. break;
  103. if (c == '\\') {
  104. buf[1] = c;
  105. buf += 2;
  106. continue;
  107. }
  108. if (c == '\n') {
  109. buf[0] = '\\';
  110. buf[1] = 'n';
  111. buf += 2;
  112. continue;
  113. }
  114. buf++;
  115. } while (i);
  116. *buf = '\n';
  117. return buf+1;
  118. }
  119. /*
  120. * The task state array is a strange "bitmap" of
  121. * reasons to sleep. Thus "running" is zero, and
  122. * you can test for combinations of others with
  123. * simple bit tests.
  124. */
  125. static const char *task_state_array[] = {
  126. "R (running)", /* 0 */
  127. "S (sleeping)", /* 1 */
  128. "D (disk sleep)", /* 2 */
  129. "T (stopped)", /* 4 */
  130. "T (tracing stop)", /* 8 */
  131. "Z (zombie)", /* 16 */
  132. "X (dead)" /* 32 */
  133. };
  134. static inline const char *get_task_state(struct task_struct *tsk)
  135. {
  136. unsigned int state = (tsk->state & (TASK_RUNNING |
  137. TASK_INTERRUPTIBLE |
  138. TASK_UNINTERRUPTIBLE |
  139. TASK_STOPPED |
  140. TASK_TRACED)) |
  141. tsk->exit_state;
  142. const char **p = &task_state_array[0];
  143. while (state) {
  144. p++;
  145. state >>= 1;
  146. }
  147. return *p;
  148. }
  149. static inline char *task_state(struct task_struct *p, char *buffer)
  150. {
  151. struct group_info *group_info;
  152. int g;
  153. struct fdtable *fdt = NULL;
  154. struct pid_namespace *ns;
  155. pid_t ppid, tpid;
  156. ns = current->nsproxy->pid_ns;
  157. rcu_read_lock();
  158. ppid = pid_alive(p) ?
  159. task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
  160. tpid = pid_alive(p) && p->ptrace ?
  161. task_ppid_nr_ns(rcu_dereference(p->parent), ns) : 0;
  162. buffer += sprintf(buffer,
  163. "State:\t%s\n"
  164. "Tgid:\t%d\n"
  165. "Pid:\t%d\n"
  166. "PPid:\t%d\n"
  167. "TracerPid:\t%d\n"
  168. "Uid:\t%d\t%d\t%d\t%d\n"
  169. "Gid:\t%d\t%d\t%d\t%d\n",
  170. get_task_state(p),
  171. task_tgid_nr_ns(p, ns),
  172. task_pid_nr_ns(p, ns),
  173. ppid, tpid,
  174. p->uid, p->euid, p->suid, p->fsuid,
  175. p->gid, p->egid, p->sgid, p->fsgid);
  176. task_lock(p);
  177. if (p->files)
  178. fdt = files_fdtable(p->files);
  179. buffer += sprintf(buffer,
  180. "FDSize:\t%d\n"
  181. "Groups:\t",
  182. fdt ? fdt->max_fds : 0);
  183. rcu_read_unlock();
  184. group_info = p->group_info;
  185. get_group_info(group_info);
  186. task_unlock(p);
  187. for (g = 0; g < min(group_info->ngroups, NGROUPS_SMALL); g++)
  188. buffer += sprintf(buffer, "%d ", GROUP_AT(group_info, g));
  189. put_group_info(group_info);
  190. buffer += sprintf(buffer, "\n");
  191. return buffer;
  192. }
  193. static char *render_sigset_t(const char *header, sigset_t *set, char *buffer)
  194. {
  195. int i, len;
  196. len = strlen(header);
  197. memcpy(buffer, header, len);
  198. buffer += len;
  199. i = _NSIG;
  200. do {
  201. int x = 0;
  202. i -= 4;
  203. if (sigismember(set, i+1)) x |= 1;
  204. if (sigismember(set, i+2)) x |= 2;
  205. if (sigismember(set, i+3)) x |= 4;
  206. if (sigismember(set, i+4)) x |= 8;
  207. *buffer++ = (x < 10 ? '0' : 'a' - 10) + x;
  208. } while (i >= 4);
  209. *buffer++ = '\n';
  210. *buffer = 0;
  211. return buffer;
  212. }
  213. static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
  214. sigset_t *catch)
  215. {
  216. struct k_sigaction *k;
  217. int i;
  218. k = p->sighand->action;
  219. for (i = 1; i <= _NSIG; ++i, ++k) {
  220. if (k->sa.sa_handler == SIG_IGN)
  221. sigaddset(ign, i);
  222. else if (k->sa.sa_handler != SIG_DFL)
  223. sigaddset(catch, i);
  224. }
  225. }
  226. static inline char *task_sig(struct task_struct *p, char *buffer)
  227. {
  228. unsigned long flags;
  229. sigset_t pending, shpending, blocked, ignored, caught;
  230. int num_threads = 0;
  231. unsigned long qsize = 0;
  232. unsigned long qlim = 0;
  233. sigemptyset(&pending);
  234. sigemptyset(&shpending);
  235. sigemptyset(&blocked);
  236. sigemptyset(&ignored);
  237. sigemptyset(&caught);
  238. rcu_read_lock();
  239. if (lock_task_sighand(p, &flags)) {
  240. pending = p->pending.signal;
  241. shpending = p->signal->shared_pending.signal;
  242. blocked = p->blocked;
  243. collect_sigign_sigcatch(p, &ignored, &caught);
  244. num_threads = atomic_read(&p->signal->count);
  245. qsize = atomic_read(&p->user->sigpending);
  246. qlim = p->signal->rlim[RLIMIT_SIGPENDING].rlim_cur;
  247. unlock_task_sighand(p, &flags);
  248. }
  249. rcu_read_unlock();
  250. buffer += sprintf(buffer, "Threads:\t%d\n", num_threads);
  251. buffer += sprintf(buffer, "SigQ:\t%lu/%lu\n", qsize, qlim);
  252. /* render them all */
  253. buffer = render_sigset_t("SigPnd:\t", &pending, buffer);
  254. buffer = render_sigset_t("ShdPnd:\t", &shpending, buffer);
  255. buffer = render_sigset_t("SigBlk:\t", &blocked, buffer);
  256. buffer = render_sigset_t("SigIgn:\t", &ignored, buffer);
  257. buffer = render_sigset_t("SigCgt:\t", &caught, buffer);
  258. return buffer;
  259. }
  260. static inline char *task_cap(struct task_struct *p, char *buffer)
  261. {
  262. return buffer + sprintf(buffer, "CapInh:\t%016x\n"
  263. "CapPrm:\t%016x\n"
  264. "CapEff:\t%016x\n",
  265. cap_t(p->cap_inheritable),
  266. cap_t(p->cap_permitted),
  267. cap_t(p->cap_effective));
  268. }
  269. static inline char *task_context_switch_counts(struct task_struct *p,
  270. char *buffer)
  271. {
  272. return buffer + sprintf(buffer, "voluntary_ctxt_switches:\t%lu\n"
  273. "nonvoluntary_ctxt_switches:\t%lu\n",
  274. p->nvcsw,
  275. p->nivcsw);
  276. }
  277. int proc_pid_status(struct task_struct *task, char *buffer)
  278. {
  279. char *orig = buffer;
  280. struct mm_struct *mm = get_task_mm(task);
  281. buffer = task_name(task, buffer);
  282. buffer = task_state(task, buffer);
  283. if (mm) {
  284. buffer = task_mem(mm, buffer);
  285. mmput(mm);
  286. }
  287. buffer = task_sig(task, buffer);
  288. buffer = task_cap(task, buffer);
  289. buffer = cpuset_task_status_allowed(task, buffer);
  290. #if defined(CONFIG_S390)
  291. buffer = task_show_regs(task, buffer);
  292. #endif
  293. buffer = task_context_switch_counts(task, buffer);
  294. return buffer - orig;
  295. }
  296. /*
  297. * Use precise platform statistics if available:
  298. */
  299. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  300. static cputime_t task_utime(struct task_struct *p)
  301. {
  302. return p->utime;
  303. }
  304. static cputime_t task_stime(struct task_struct *p)
  305. {
  306. return p->stime;
  307. }
  308. #else
  309. static cputime_t task_utime(struct task_struct *p)
  310. {
  311. clock_t utime = cputime_to_clock_t(p->utime),
  312. total = utime + cputime_to_clock_t(p->stime);
  313. u64 temp;
  314. /*
  315. * Use CFS's precise accounting:
  316. */
  317. temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
  318. if (total) {
  319. temp *= utime;
  320. do_div(temp, total);
  321. }
  322. utime = (clock_t)temp;
  323. p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime));
  324. return p->prev_utime;
  325. }
  326. static cputime_t task_stime(struct task_struct *p)
  327. {
  328. clock_t stime;
  329. /*
  330. * Use CFS's precise accounting. (we subtract utime from
  331. * the total, to make sure the total observed by userspace
  332. * grows monotonically - apps rely on that):
  333. */
  334. stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
  335. cputime_to_clock_t(task_utime(p));
  336. if (stime >= 0)
  337. p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime));
  338. return p->prev_stime;
  339. }
  340. #endif
  341. static cputime_t task_gtime(struct task_struct *p)
  342. {
  343. return p->gtime;
  344. }
  345. static int do_task_stat(struct task_struct *task, char *buffer, int whole)
  346. {
  347. unsigned long vsize, eip, esp, wchan = ~0UL;
  348. long priority, nice;
  349. int tty_pgrp = -1, tty_nr = 0;
  350. sigset_t sigign, sigcatch;
  351. char state;
  352. int res;
  353. pid_t ppid = 0, pgid = -1, sid = -1;
  354. int num_threads = 0;
  355. struct mm_struct *mm;
  356. unsigned long long start_time;
  357. unsigned long cmin_flt = 0, cmaj_flt = 0;
  358. unsigned long min_flt = 0, maj_flt = 0;
  359. cputime_t cutime, cstime, utime, stime;
  360. cputime_t cgtime, gtime;
  361. unsigned long rsslim = 0;
  362. char tcomm[sizeof(task->comm)];
  363. unsigned long flags;
  364. struct pid_namespace *ns;
  365. ns = current->nsproxy->pid_ns;
  366. state = *get_task_state(task);
  367. vsize = eip = esp = 0;
  368. mm = get_task_mm(task);
  369. if (mm) {
  370. vsize = task_vsize(mm);
  371. eip = KSTK_EIP(task);
  372. esp = KSTK_ESP(task);
  373. }
  374. get_task_comm(tcomm, task);
  375. sigemptyset(&sigign);
  376. sigemptyset(&sigcatch);
  377. cutime = cstime = utime = stime = cputime_zero;
  378. cgtime = gtime = cputime_zero;
  379. rcu_read_lock();
  380. if (lock_task_sighand(task, &flags)) {
  381. struct signal_struct *sig = task->signal;
  382. if (sig->tty) {
  383. tty_pgrp = pid_nr_ns(sig->tty->pgrp, ns);
  384. tty_nr = new_encode_dev(tty_devnum(sig->tty));
  385. }
  386. num_threads = atomic_read(&sig->count);
  387. collect_sigign_sigcatch(task, &sigign, &sigcatch);
  388. cmin_flt = sig->cmin_flt;
  389. cmaj_flt = sig->cmaj_flt;
  390. cutime = sig->cutime;
  391. cstime = sig->cstime;
  392. cgtime = sig->cgtime;
  393. rsslim = sig->rlim[RLIMIT_RSS].rlim_cur;
  394. /* add up live thread stats at the group level */
  395. if (whole) {
  396. struct task_struct *t = task;
  397. do {
  398. min_flt += t->min_flt;
  399. maj_flt += t->maj_flt;
  400. utime = cputime_add(utime, task_utime(t));
  401. stime = cputime_add(stime, task_stime(t));
  402. gtime = cputime_add(gtime, task_gtime(t));
  403. t = next_thread(t);
  404. } while (t != task);
  405. min_flt += sig->min_flt;
  406. maj_flt += sig->maj_flt;
  407. utime = cputime_add(utime, sig->utime);
  408. stime = cputime_add(stime, sig->stime);
  409. gtime = cputime_add(gtime, sig->gtime);
  410. }
  411. sid = task_session_nr_ns(task, ns);
  412. pgid = task_pgrp_nr_ns(task, ns);
  413. ppid = task_ppid_nr_ns(task, ns);
  414. unlock_task_sighand(task, &flags);
  415. }
  416. rcu_read_unlock();
  417. if (!whole || num_threads < 2)
  418. wchan = get_wchan(task);
  419. if (!whole) {
  420. min_flt = task->min_flt;
  421. maj_flt = task->maj_flt;
  422. utime = task_utime(task);
  423. stime = task_stime(task);
  424. gtime = task_gtime(task);
  425. }
  426. /* scale priority and nice values from timeslices to -20..20 */
  427. /* to make it look like a "normal" Unix priority/nice value */
  428. priority = task_prio(task);
  429. nice = task_nice(task);
  430. /* Temporary variable needed for gcc-2.96 */
  431. /* convert timespec -> nsec*/
  432. start_time =
  433. (unsigned long long)task->real_start_time.tv_sec * NSEC_PER_SEC
  434. + task->real_start_time.tv_nsec;
  435. /* convert nsec -> ticks */
  436. start_time = nsec_to_clock_t(start_time);
  437. res = sprintf(buffer, "%d (%s) %c %d %d %d %d %d %u %lu \
  438. %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
  439. %lu %lu %lu %lu %lu %lu %lu %lu %d %d %u %u %llu %lu %ld\n",
  440. task_pid_nr_ns(task, ns),
  441. tcomm,
  442. state,
  443. ppid,
  444. pgid,
  445. sid,
  446. tty_nr,
  447. tty_pgrp,
  448. task->flags,
  449. min_flt,
  450. cmin_flt,
  451. maj_flt,
  452. cmaj_flt,
  453. cputime_to_clock_t(utime),
  454. cputime_to_clock_t(stime),
  455. cputime_to_clock_t(cutime),
  456. cputime_to_clock_t(cstime),
  457. priority,
  458. nice,
  459. num_threads,
  460. start_time,
  461. vsize,
  462. mm ? get_mm_rss(mm) : 0,
  463. rsslim,
  464. mm ? mm->start_code : 0,
  465. mm ? mm->end_code : 0,
  466. mm ? mm->start_stack : 0,
  467. esp,
  468. eip,
  469. /* The signal information here is obsolete.
  470. * It must be decimal for Linux 2.0 compatibility.
  471. * Use /proc/#/status for real-time signals.
  472. */
  473. task->pending.signal.sig[0] & 0x7fffffffUL,
  474. task->blocked.sig[0] & 0x7fffffffUL,
  475. sigign .sig[0] & 0x7fffffffUL,
  476. sigcatch .sig[0] & 0x7fffffffUL,
  477. wchan,
  478. 0UL,
  479. 0UL,
  480. task->exit_signal,
  481. task_cpu(task),
  482. task->rt_priority,
  483. task->policy,
  484. (unsigned long long)delayacct_blkio_ticks(task),
  485. cputime_to_clock_t(gtime),
  486. cputime_to_clock_t(cgtime));
  487. if (mm)
  488. mmput(mm);
  489. return res;
  490. }
  491. int proc_tid_stat(struct task_struct *task, char *buffer)
  492. {
  493. return do_task_stat(task, buffer, 0);
  494. }
  495. int proc_tgid_stat(struct task_struct *task, char *buffer)
  496. {
  497. return do_task_stat(task, buffer, 1);
  498. }
  499. int proc_pid_statm(struct task_struct *task, char *buffer)
  500. {
  501. int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
  502. struct mm_struct *mm = get_task_mm(task);
  503. if (mm) {
  504. size = task_statm(mm, &shared, &text, &data, &resident);
  505. mmput(mm);
  506. }
  507. return sprintf(buffer, "%d %d %d %d %d %d %d\n",
  508. size, resident, shared, text, lib, data, 0);
  509. }