array.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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/seq_file.h>
  80. #include <linux/pid_namespace.h>
  81. #include <asm/pgtable.h>
  82. #include <asm/processor.h>
  83. #include "internal.h"
  84. /* Gcc optimizes away "strlen(x)" for constant x */
  85. #define ADDBUF(buffer, string) \
  86. do { memcpy(buffer, string, strlen(string)); \
  87. buffer += strlen(string); } while (0)
  88. static inline void task_name(struct seq_file *m, struct task_struct *p)
  89. {
  90. int i;
  91. char *buf, *end;
  92. char *name;
  93. char tcomm[sizeof(p->comm)];
  94. get_task_comm(tcomm, p);
  95. seq_printf(m, "Name:\t");
  96. end = m->buf + m->size;
  97. buf = m->buf + m->count;
  98. name = tcomm;
  99. i = sizeof(tcomm);
  100. while (i && (buf < end)) {
  101. unsigned char c = *name;
  102. name++;
  103. i--;
  104. *buf = c;
  105. if (!c)
  106. break;
  107. if (c == '\\') {
  108. buf++;
  109. if (buf < end)
  110. *buf++ = c;
  111. continue;
  112. }
  113. if (c == '\n') {
  114. *buf++ = '\\';
  115. if (buf < end)
  116. *buf++ = 'n';
  117. continue;
  118. }
  119. buf++;
  120. }
  121. m->count = buf - m->buf;
  122. seq_printf(m, "\n");
  123. }
  124. /*
  125. * The task state array is a strange "bitmap" of
  126. * reasons to sleep. Thus "running" is zero, and
  127. * you can test for combinations of others with
  128. * simple bit tests.
  129. */
  130. static const char *task_state_array[] = {
  131. "R (running)", /* 0 */
  132. "S (sleeping)", /* 1 */
  133. "D (disk sleep)", /* 2 */
  134. "T (stopped)", /* 4 */
  135. "T (tracing stop)", /* 8 */
  136. "Z (zombie)", /* 16 */
  137. "X (dead)" /* 32 */
  138. };
  139. static inline const char *get_task_state(struct task_struct *tsk)
  140. {
  141. unsigned int state = (tsk->state & TASK_REPORT) | 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 void task_state(struct seq_file *m, struct pid_namespace *ns,
  150. struct pid *pid, struct task_struct *p)
  151. {
  152. struct group_info *group_info;
  153. int g;
  154. struct fdtable *fdt = NULL;
  155. pid_t ppid, tpid;
  156. rcu_read_lock();
  157. ppid = pid_alive(p) ?
  158. task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
  159. tpid = pid_alive(p) && p->ptrace ?
  160. task_pid_nr_ns(rcu_dereference(p->parent), ns) : 0;
  161. seq_printf(m,
  162. "State:\t%s\n"
  163. "Tgid:\t%d\n"
  164. "Pid:\t%d\n"
  165. "PPid:\t%d\n"
  166. "TracerPid:\t%d\n"
  167. "Uid:\t%d\t%d\t%d\t%d\n"
  168. "Gid:\t%d\t%d\t%d\t%d\n",
  169. get_task_state(p),
  170. task_tgid_nr_ns(p, ns),
  171. pid_nr_ns(pid, ns),
  172. ppid, tpid,
  173. p->uid, p->euid, p->suid, p->fsuid,
  174. p->gid, p->egid, p->sgid, p->fsgid);
  175. task_lock(p);
  176. if (p->files)
  177. fdt = files_fdtable(p->files);
  178. seq_printf(m,
  179. "FDSize:\t%d\n"
  180. "Groups:\t",
  181. fdt ? fdt->max_fds : 0);
  182. rcu_read_unlock();
  183. group_info = p->group_info;
  184. get_group_info(group_info);
  185. task_unlock(p);
  186. for (g = 0; g < min(group_info->ngroups, NGROUPS_SMALL); g++)
  187. seq_printf(m, "%d ", GROUP_AT(group_info, g));
  188. put_group_info(group_info);
  189. seq_printf(m, "\n");
  190. }
  191. static void render_sigset_t(struct seq_file *m, const char *header,
  192. sigset_t *set)
  193. {
  194. int i;
  195. seq_printf(m, "%s", header);
  196. i = _NSIG;
  197. do {
  198. int x = 0;
  199. i -= 4;
  200. if (sigismember(set, i+1)) x |= 1;
  201. if (sigismember(set, i+2)) x |= 2;
  202. if (sigismember(set, i+3)) x |= 4;
  203. if (sigismember(set, i+4)) x |= 8;
  204. seq_printf(m, "%x", x);
  205. } while (i >= 4);
  206. seq_printf(m, "\n");
  207. }
  208. static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
  209. sigset_t *catch)
  210. {
  211. struct k_sigaction *k;
  212. int i;
  213. k = p->sighand->action;
  214. for (i = 1; i <= _NSIG; ++i, ++k) {
  215. if (k->sa.sa_handler == SIG_IGN)
  216. sigaddset(ign, i);
  217. else if (k->sa.sa_handler != SIG_DFL)
  218. sigaddset(catch, i);
  219. }
  220. }
  221. static inline void task_sig(struct seq_file *m, struct task_struct *p)
  222. {
  223. unsigned long flags;
  224. sigset_t pending, shpending, blocked, ignored, caught;
  225. int num_threads = 0;
  226. unsigned long qsize = 0;
  227. unsigned long qlim = 0;
  228. sigemptyset(&pending);
  229. sigemptyset(&shpending);
  230. sigemptyset(&blocked);
  231. sigemptyset(&ignored);
  232. sigemptyset(&caught);
  233. rcu_read_lock();
  234. if (lock_task_sighand(p, &flags)) {
  235. pending = p->pending.signal;
  236. shpending = p->signal->shared_pending.signal;
  237. blocked = p->blocked;
  238. collect_sigign_sigcatch(p, &ignored, &caught);
  239. num_threads = atomic_read(&p->signal->count);
  240. qsize = atomic_read(&p->user->sigpending);
  241. qlim = p->signal->rlim[RLIMIT_SIGPENDING].rlim_cur;
  242. unlock_task_sighand(p, &flags);
  243. }
  244. rcu_read_unlock();
  245. seq_printf(m, "Threads:\t%d\n", num_threads);
  246. seq_printf(m, "SigQ:\t%lu/%lu\n", qsize, qlim);
  247. /* render them all */
  248. render_sigset_t(m, "SigPnd:\t", &pending);
  249. render_sigset_t(m, "ShdPnd:\t", &shpending);
  250. render_sigset_t(m, "SigBlk:\t", &blocked);
  251. render_sigset_t(m, "SigIgn:\t", &ignored);
  252. render_sigset_t(m, "SigCgt:\t", &caught);
  253. }
  254. static void render_cap_t(struct seq_file *m, const char *header,
  255. kernel_cap_t *a)
  256. {
  257. unsigned __capi;
  258. seq_printf(m, "%s", header);
  259. CAP_FOR_EACH_U32(__capi) {
  260. seq_printf(m, "%08x",
  261. a->cap[(_LINUX_CAPABILITY_U32S-1) - __capi]);
  262. }
  263. seq_printf(m, "\n");
  264. }
  265. static inline void task_cap(struct seq_file *m, struct task_struct *p)
  266. {
  267. render_cap_t(m, "CapInh:\t", &p->cap_inheritable);
  268. render_cap_t(m, "CapPrm:\t", &p->cap_permitted);
  269. render_cap_t(m, "CapEff:\t", &p->cap_effective);
  270. }
  271. static inline void task_context_switch_counts(struct seq_file *m,
  272. struct task_struct *p)
  273. {
  274. seq_printf(m, "voluntary_ctxt_switches:\t%lu\n"
  275. "nonvoluntary_ctxt_switches:\t%lu\n",
  276. p->nvcsw,
  277. p->nivcsw);
  278. }
  279. int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
  280. struct pid *pid, struct task_struct *task)
  281. {
  282. struct mm_struct *mm = get_task_mm(task);
  283. task_name(m, task);
  284. task_state(m, ns, pid, task);
  285. if (mm) {
  286. task_mem(m, mm);
  287. mmput(mm);
  288. }
  289. task_sig(m, task);
  290. task_cap(m, task);
  291. cpuset_task_status_allowed(m, task);
  292. #if defined(CONFIG_S390)
  293. task_show_regs(m, task);
  294. #endif
  295. task_context_switch_counts(m, task);
  296. return 0;
  297. }
  298. /*
  299. * Use precise platform statistics if available:
  300. */
  301. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  302. static cputime_t task_utime(struct task_struct *p)
  303. {
  304. return p->utime;
  305. }
  306. static cputime_t task_stime(struct task_struct *p)
  307. {
  308. return p->stime;
  309. }
  310. #else
  311. static cputime_t task_utime(struct task_struct *p)
  312. {
  313. clock_t utime = cputime_to_clock_t(p->utime),
  314. total = utime + cputime_to_clock_t(p->stime);
  315. u64 temp;
  316. /*
  317. * Use CFS's precise accounting:
  318. */
  319. temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
  320. if (total) {
  321. temp *= utime;
  322. do_div(temp, total);
  323. }
  324. utime = (clock_t)temp;
  325. p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime));
  326. return p->prev_utime;
  327. }
  328. static cputime_t task_stime(struct task_struct *p)
  329. {
  330. clock_t stime;
  331. /*
  332. * Use CFS's precise accounting. (we subtract utime from
  333. * the total, to make sure the total observed by userspace
  334. * grows monotonically - apps rely on that):
  335. */
  336. stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
  337. cputime_to_clock_t(task_utime(p));
  338. if (stime >= 0)
  339. p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime));
  340. return p->prev_stime;
  341. }
  342. #endif
  343. static cputime_t task_gtime(struct task_struct *p)
  344. {
  345. return p->gtime;
  346. }
  347. static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
  348. struct pid *pid, struct task_struct *task, int whole)
  349. {
  350. unsigned long vsize, eip, esp, wchan = ~0UL;
  351. long priority, nice;
  352. int tty_pgrp = -1, tty_nr = 0;
  353. sigset_t sigign, sigcatch;
  354. char state;
  355. pid_t ppid = 0, pgid = -1, sid = -1;
  356. int num_threads = 0;
  357. struct mm_struct *mm;
  358. unsigned long long start_time;
  359. unsigned long cmin_flt = 0, cmaj_flt = 0;
  360. unsigned long min_flt = 0, maj_flt = 0;
  361. cputime_t cutime, cstime, utime, stime;
  362. cputime_t cgtime, gtime;
  363. unsigned long rsslim = 0;
  364. char tcomm[sizeof(task->comm)];
  365. unsigned long flags;
  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. ppid = task_tgid_nr_ns(task->real_parent, ns);
  413. pgid = task_pgrp_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. seq_printf(m, "%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. pid_nr_ns(pid, 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 0;
  490. }
  491. int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
  492. struct pid *pid, struct task_struct *task)
  493. {
  494. return do_task_stat(m, ns, pid, task, 0);
  495. }
  496. int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
  497. struct pid *pid, struct task_struct *task)
  498. {
  499. return do_task_stat(m, ns, pid, task, 1);
  500. }
  501. int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
  502. struct pid *pid, struct task_struct *task)
  503. {
  504. int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
  505. struct mm_struct *mm = get_task_mm(task);
  506. if (mm) {
  507. size = task_statm(mm, &shared, &text, &data, &resident);
  508. mmput(mm);
  509. }
  510. seq_printf(m, "%d %d %d %d %d %d %d\n",
  511. size, resident, shared, text, lib, data, 0);
  512. return 0;
  513. }