array.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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/fdtable.h>
  76. #include <linux/times.h>
  77. #include <linux/cpuset.h>
  78. #include <linux/rcupdate.h>
  79. #include <linux/delayacct.h>
  80. #include <linux/seq_file.h>
  81. #include <linux/pid_namespace.h>
  82. #include <linux/tracehook.h>
  83. #include <asm/pgtable.h>
  84. #include <asm/processor.h>
  85. #include "internal.h"
  86. /* Gcc optimizes away "strlen(x)" for constant x */
  87. #define ADDBUF(buffer, string) \
  88. do { memcpy(buffer, string, strlen(string)); \
  89. buffer += strlen(string); } while (0)
  90. static inline void task_name(struct seq_file *m, struct task_struct *p)
  91. {
  92. int i;
  93. char *buf, *end;
  94. char *name;
  95. char tcomm[sizeof(p->comm)];
  96. get_task_comm(tcomm, p);
  97. seq_printf(m, "Name:\t");
  98. end = m->buf + m->size;
  99. buf = m->buf + m->count;
  100. name = tcomm;
  101. i = sizeof(tcomm);
  102. while (i && (buf < end)) {
  103. unsigned char c = *name;
  104. name++;
  105. i--;
  106. *buf = c;
  107. if (!c)
  108. break;
  109. if (c == '\\') {
  110. buf++;
  111. if (buf < end)
  112. *buf++ = c;
  113. continue;
  114. }
  115. if (c == '\n') {
  116. *buf++ = '\\';
  117. if (buf < end)
  118. *buf++ = 'n';
  119. continue;
  120. }
  121. buf++;
  122. }
  123. m->count = buf - m->buf;
  124. seq_printf(m, "\n");
  125. }
  126. /*
  127. * The task state array is a strange "bitmap" of
  128. * reasons to sleep. Thus "running" is zero, and
  129. * you can test for combinations of others with
  130. * simple bit tests.
  131. */
  132. static const char *task_state_array[] = {
  133. "R (running)", /* 0 */
  134. "S (sleeping)", /* 1 */
  135. "D (disk sleep)", /* 2 */
  136. "T (stopped)", /* 4 */
  137. "T (tracing stop)", /* 8 */
  138. "Z (zombie)", /* 16 */
  139. "X (dead)" /* 32 */
  140. };
  141. static inline const char *get_task_state(struct task_struct *tsk)
  142. {
  143. unsigned int state = (tsk->state & TASK_REPORT) | tsk->exit_state;
  144. const char **p = &task_state_array[0];
  145. while (state) {
  146. p++;
  147. state >>= 1;
  148. }
  149. return *p;
  150. }
  151. static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
  152. struct pid *pid, struct task_struct *p)
  153. {
  154. struct group_info *group_info;
  155. int g;
  156. struct fdtable *fdt = NULL;
  157. pid_t ppid, tpid;
  158. rcu_read_lock();
  159. ppid = pid_alive(p) ?
  160. task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
  161. tpid = 0;
  162. if (pid_alive(p)) {
  163. struct task_struct *tracer = tracehook_tracer_task(p);
  164. if (tracer)
  165. tpid = task_pid_nr_ns(tracer, ns);
  166. }
  167. seq_printf(m,
  168. "State:\t%s\n"
  169. "Tgid:\t%d\n"
  170. "Pid:\t%d\n"
  171. "PPid:\t%d\n"
  172. "TracerPid:\t%d\n"
  173. "Uid:\t%d\t%d\t%d\t%d\n"
  174. "Gid:\t%d\t%d\t%d\t%d\n",
  175. get_task_state(p),
  176. task_tgid_nr_ns(p, ns),
  177. pid_nr_ns(pid, ns),
  178. ppid, tpid,
  179. p->uid, p->euid, p->suid, p->fsuid,
  180. p->gid, p->egid, p->sgid, p->fsgid);
  181. task_lock(p);
  182. if (p->files)
  183. fdt = files_fdtable(p->files);
  184. seq_printf(m,
  185. "FDSize:\t%d\n"
  186. "Groups:\t",
  187. fdt ? fdt->max_fds : 0);
  188. rcu_read_unlock();
  189. group_info = p->group_info;
  190. get_group_info(group_info);
  191. task_unlock(p);
  192. for (g = 0; g < min(group_info->ngroups, NGROUPS_SMALL); g++)
  193. seq_printf(m, "%d ", GROUP_AT(group_info, g));
  194. put_group_info(group_info);
  195. seq_printf(m, "\n");
  196. }
  197. static void render_sigset_t(struct seq_file *m, const char *header,
  198. sigset_t *set)
  199. {
  200. int i;
  201. seq_printf(m, "%s", header);
  202. i = _NSIG;
  203. do {
  204. int x = 0;
  205. i -= 4;
  206. if (sigismember(set, i+1)) x |= 1;
  207. if (sigismember(set, i+2)) x |= 2;
  208. if (sigismember(set, i+3)) x |= 4;
  209. if (sigismember(set, i+4)) x |= 8;
  210. seq_printf(m, "%x", x);
  211. } while (i >= 4);
  212. seq_printf(m, "\n");
  213. }
  214. static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
  215. sigset_t *catch)
  216. {
  217. struct k_sigaction *k;
  218. int i;
  219. k = p->sighand->action;
  220. for (i = 1; i <= _NSIG; ++i, ++k) {
  221. if (k->sa.sa_handler == SIG_IGN)
  222. sigaddset(ign, i);
  223. else if (k->sa.sa_handler != SIG_DFL)
  224. sigaddset(catch, i);
  225. }
  226. }
  227. static inline void task_sig(struct seq_file *m, struct task_struct *p)
  228. {
  229. unsigned long flags;
  230. sigset_t pending, shpending, blocked, ignored, caught;
  231. int num_threads = 0;
  232. unsigned long qsize = 0;
  233. unsigned long qlim = 0;
  234. sigemptyset(&pending);
  235. sigemptyset(&shpending);
  236. sigemptyset(&blocked);
  237. sigemptyset(&ignored);
  238. sigemptyset(&caught);
  239. rcu_read_lock();
  240. if (lock_task_sighand(p, &flags)) {
  241. pending = p->pending.signal;
  242. shpending = p->signal->shared_pending.signal;
  243. blocked = p->blocked;
  244. collect_sigign_sigcatch(p, &ignored, &caught);
  245. num_threads = atomic_read(&p->signal->count);
  246. qsize = atomic_read(&p->user->sigpending);
  247. qlim = p->signal->rlim[RLIMIT_SIGPENDING].rlim_cur;
  248. unlock_task_sighand(p, &flags);
  249. }
  250. rcu_read_unlock();
  251. seq_printf(m, "Threads:\t%d\n", num_threads);
  252. seq_printf(m, "SigQ:\t%lu/%lu\n", qsize, qlim);
  253. /* render them all */
  254. render_sigset_t(m, "SigPnd:\t", &pending);
  255. render_sigset_t(m, "ShdPnd:\t", &shpending);
  256. render_sigset_t(m, "SigBlk:\t", &blocked);
  257. render_sigset_t(m, "SigIgn:\t", &ignored);
  258. render_sigset_t(m, "SigCgt:\t", &caught);
  259. }
  260. static void render_cap_t(struct seq_file *m, const char *header,
  261. kernel_cap_t *a)
  262. {
  263. unsigned __capi;
  264. seq_printf(m, "%s", header);
  265. CAP_FOR_EACH_U32(__capi) {
  266. seq_printf(m, "%08x",
  267. a->cap[(_KERNEL_CAPABILITY_U32S-1) - __capi]);
  268. }
  269. seq_printf(m, "\n");
  270. }
  271. static inline void task_cap(struct seq_file *m, struct task_struct *p)
  272. {
  273. render_cap_t(m, "CapInh:\t", &p->cap_inheritable);
  274. render_cap_t(m, "CapPrm:\t", &p->cap_permitted);
  275. render_cap_t(m, "CapEff:\t", &p->cap_effective);
  276. render_cap_t(m, "CapBnd:\t", &p->cap_bset);
  277. }
  278. static inline void task_context_switch_counts(struct seq_file *m,
  279. struct task_struct *p)
  280. {
  281. seq_printf(m, "voluntary_ctxt_switches:\t%lu\n"
  282. "nonvoluntary_ctxt_switches:\t%lu\n",
  283. p->nvcsw,
  284. p->nivcsw);
  285. }
  286. int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
  287. struct pid *pid, struct task_struct *task)
  288. {
  289. struct mm_struct *mm = get_task_mm(task);
  290. task_name(m, task);
  291. task_state(m, ns, pid, task);
  292. if (mm) {
  293. task_mem(m, mm);
  294. mmput(mm);
  295. }
  296. task_sig(m, task);
  297. task_cap(m, task);
  298. cpuset_task_status_allowed(m, task);
  299. #if defined(CONFIG_S390)
  300. task_show_regs(m, task);
  301. #endif
  302. task_context_switch_counts(m, task);
  303. return 0;
  304. }
  305. /*
  306. * Use precise platform statistics if available:
  307. */
  308. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  309. static cputime_t task_utime(struct task_struct *p)
  310. {
  311. return p->utime;
  312. }
  313. static cputime_t task_stime(struct task_struct *p)
  314. {
  315. return p->stime;
  316. }
  317. #else
  318. static cputime_t task_utime(struct task_struct *p)
  319. {
  320. clock_t utime = cputime_to_clock_t(p->utime),
  321. total = utime + cputime_to_clock_t(p->stime);
  322. u64 temp;
  323. /*
  324. * Use CFS's precise accounting:
  325. */
  326. temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
  327. if (total) {
  328. temp *= utime;
  329. do_div(temp, total);
  330. }
  331. utime = (clock_t)temp;
  332. p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime));
  333. return p->prev_utime;
  334. }
  335. static cputime_t task_stime(struct task_struct *p)
  336. {
  337. clock_t stime;
  338. /*
  339. * Use CFS's precise accounting. (we subtract utime from
  340. * the total, to make sure the total observed by userspace
  341. * grows monotonically - apps rely on that):
  342. */
  343. stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
  344. cputime_to_clock_t(task_utime(p));
  345. if (stime >= 0)
  346. p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime));
  347. return p->prev_stime;
  348. }
  349. #endif
  350. static cputime_t task_gtime(struct task_struct *p)
  351. {
  352. return p->gtime;
  353. }
  354. static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
  355. struct pid *pid, struct task_struct *task, int whole)
  356. {
  357. unsigned long vsize, eip, esp, wchan = ~0UL;
  358. long priority, nice;
  359. int tty_pgrp = -1, tty_nr = 0;
  360. sigset_t sigign, sigcatch;
  361. char state;
  362. pid_t ppid = 0, pgid = -1, sid = -1;
  363. int num_threads = 0;
  364. struct mm_struct *mm;
  365. unsigned long long start_time;
  366. unsigned long cmin_flt = 0, cmaj_flt = 0;
  367. unsigned long min_flt = 0, maj_flt = 0;
  368. cputime_t cutime, cstime, utime, stime;
  369. cputime_t cgtime, gtime;
  370. unsigned long rsslim = 0;
  371. char tcomm[sizeof(task->comm)];
  372. unsigned long flags;
  373. state = *get_task_state(task);
  374. vsize = eip = esp = 0;
  375. mm = get_task_mm(task);
  376. if (mm) {
  377. vsize = task_vsize(mm);
  378. eip = KSTK_EIP(task);
  379. esp = KSTK_ESP(task);
  380. }
  381. get_task_comm(tcomm, task);
  382. sigemptyset(&sigign);
  383. sigemptyset(&sigcatch);
  384. cutime = cstime = utime = stime = cputime_zero;
  385. cgtime = gtime = cputime_zero;
  386. if (lock_task_sighand(task, &flags)) {
  387. struct signal_struct *sig = task->signal;
  388. if (sig->tty) {
  389. struct pid *pgrp = tty_get_pgrp(sig->tty);
  390. tty_pgrp = pid_nr_ns(pgrp, ns);
  391. put_pid(pgrp);
  392. tty_nr = new_encode_dev(tty_devnum(sig->tty));
  393. }
  394. num_threads = atomic_read(&sig->count);
  395. collect_sigign_sigcatch(task, &sigign, &sigcatch);
  396. cmin_flt = sig->cmin_flt;
  397. cmaj_flt = sig->cmaj_flt;
  398. cutime = sig->cutime;
  399. cstime = sig->cstime;
  400. cgtime = sig->cgtime;
  401. rsslim = sig->rlim[RLIMIT_RSS].rlim_cur;
  402. /* add up live thread stats at the group level */
  403. if (whole) {
  404. struct task_struct *t = task;
  405. do {
  406. min_flt += t->min_flt;
  407. maj_flt += t->maj_flt;
  408. utime = cputime_add(utime, task_utime(t));
  409. stime = cputime_add(stime, task_stime(t));
  410. gtime = cputime_add(gtime, task_gtime(t));
  411. t = next_thread(t);
  412. } while (t != task);
  413. min_flt += sig->min_flt;
  414. maj_flt += sig->maj_flt;
  415. utime = cputime_add(utime, sig->utime);
  416. stime = cputime_add(stime, sig->stime);
  417. gtime = cputime_add(gtime, sig->gtime);
  418. }
  419. sid = task_session_nr_ns(task, ns);
  420. ppid = task_tgid_nr_ns(task->real_parent, ns);
  421. pgid = task_pgrp_nr_ns(task, ns);
  422. unlock_task_sighand(task, &flags);
  423. }
  424. if (!whole || num_threads < 2)
  425. wchan = get_wchan(task);
  426. if (!whole) {
  427. min_flt = task->min_flt;
  428. maj_flt = task->maj_flt;
  429. utime = task_utime(task);
  430. stime = task_stime(task);
  431. gtime = task_gtime(task);
  432. }
  433. /* scale priority and nice values from timeslices to -20..20 */
  434. /* to make it look like a "normal" Unix priority/nice value */
  435. priority = task_prio(task);
  436. nice = task_nice(task);
  437. /* Temporary variable needed for gcc-2.96 */
  438. /* convert timespec -> nsec*/
  439. start_time =
  440. (unsigned long long)task->real_start_time.tv_sec * NSEC_PER_SEC
  441. + task->real_start_time.tv_nsec;
  442. /* convert nsec -> ticks */
  443. start_time = nsec_to_clock_t(start_time);
  444. seq_printf(m, "%d (%s) %c %d %d %d %d %d %u %lu \
  445. %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
  446. %lu %lu %lu %lu %lu %lu %lu %lu %d %d %u %u %llu %lu %ld\n",
  447. pid_nr_ns(pid, ns),
  448. tcomm,
  449. state,
  450. ppid,
  451. pgid,
  452. sid,
  453. tty_nr,
  454. tty_pgrp,
  455. task->flags,
  456. min_flt,
  457. cmin_flt,
  458. maj_flt,
  459. cmaj_flt,
  460. cputime_to_clock_t(utime),
  461. cputime_to_clock_t(stime),
  462. cputime_to_clock_t(cutime),
  463. cputime_to_clock_t(cstime),
  464. priority,
  465. nice,
  466. num_threads,
  467. start_time,
  468. vsize,
  469. mm ? get_mm_rss(mm) : 0,
  470. rsslim,
  471. mm ? mm->start_code : 0,
  472. mm ? mm->end_code : 0,
  473. mm ? mm->start_stack : 0,
  474. esp,
  475. eip,
  476. /* The signal information here is obsolete.
  477. * It must be decimal for Linux 2.0 compatibility.
  478. * Use /proc/#/status for real-time signals.
  479. */
  480. task->pending.signal.sig[0] & 0x7fffffffUL,
  481. task->blocked.sig[0] & 0x7fffffffUL,
  482. sigign .sig[0] & 0x7fffffffUL,
  483. sigcatch .sig[0] & 0x7fffffffUL,
  484. wchan,
  485. 0UL,
  486. 0UL,
  487. task->exit_signal,
  488. task_cpu(task),
  489. task->rt_priority,
  490. task->policy,
  491. (unsigned long long)delayacct_blkio_ticks(task),
  492. cputime_to_clock_t(gtime),
  493. cputime_to_clock_t(cgtime));
  494. if (mm)
  495. mmput(mm);
  496. return 0;
  497. }
  498. int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
  499. struct pid *pid, struct task_struct *task)
  500. {
  501. return do_task_stat(m, ns, pid, task, 0);
  502. }
  503. int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
  504. struct pid *pid, struct task_struct *task)
  505. {
  506. return do_task_stat(m, ns, pid, task, 1);
  507. }
  508. int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
  509. struct pid *pid, struct task_struct *task)
  510. {
  511. int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
  512. struct mm_struct *mm = get_task_mm(task);
  513. if (mm) {
  514. size = task_statm(mm, &shared, &text, &data, &resident);
  515. mmput(mm);
  516. }
  517. seq_printf(m, "%d %d %d %d %d %d %d\n",
  518. size, resident, shared, text, lib, data, 0);
  519. return 0;
  520. }