array.c 13 KB

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