array.c 15 KB

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