array.c 14 KB

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