array.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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/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. 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_printf(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_printf(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 *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. };
  137. static inline const char *get_task_state(struct task_struct *tsk)
  138. {
  139. unsigned int state = (tsk->state & TASK_REPORT) | tsk->exit_state;
  140. const char **p = &task_state_array[0];
  141. while (state) {
  142. p++;
  143. state >>= 1;
  144. }
  145. return *p;
  146. }
  147. static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
  148. struct pid *pid, struct task_struct *p)
  149. {
  150. struct group_info *group_info;
  151. int g;
  152. struct fdtable *fdt = NULL;
  153. const struct cred *cred;
  154. pid_t ppid, tpid;
  155. rcu_read_lock();
  156. ppid = pid_alive(p) ?
  157. task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
  158. tpid = 0;
  159. if (pid_alive(p)) {
  160. struct task_struct *tracer = tracehook_tracer_task(p);
  161. if (tracer)
  162. tpid = task_pid_nr_ns(tracer, ns);
  163. }
  164. cred = get_cred((struct cred *) __task_cred(p));
  165. seq_printf(m,
  166. "State:\t%s\n"
  167. "Tgid:\t%d\n"
  168. "Pid:\t%d\n"
  169. "PPid:\t%d\n"
  170. "TracerPid:\t%d\n"
  171. "Uid:\t%d\t%d\t%d\t%d\n"
  172. "Gid:\t%d\t%d\t%d\t%d\n",
  173. get_task_state(p),
  174. task_tgid_nr_ns(p, ns),
  175. pid_nr_ns(pid, ns),
  176. ppid, tpid,
  177. cred->uid, cred->euid, cred->suid, cred->fsuid,
  178. cred->gid, cred->egid, cred->sgid, cred->fsgid);
  179. task_lock(p);
  180. if (p->files)
  181. fdt = files_fdtable(p->files);
  182. seq_printf(m,
  183. "FDSize:\t%d\n"
  184. "Groups:\t",
  185. fdt ? fdt->max_fds : 0);
  186. rcu_read_unlock();
  187. group_info = cred->group_info;
  188. task_unlock(p);
  189. for (g = 0; g < min(group_info->ngroups, NGROUPS_SMALL); g++)
  190. seq_printf(m, "%d ", GROUP_AT(group_info, g));
  191. put_cred(cred);
  192. seq_printf(m, "\n");
  193. }
  194. static void render_sigset_t(struct seq_file *m, const char *header,
  195. sigset_t *set)
  196. {
  197. int i;
  198. seq_printf(m, "%s", header);
  199. i = _NSIG;
  200. do {
  201. int x = 0;
  202. i -= 4;
  203. if (sigismember(set, i+1)) x |= 1;
  204. if (sigismember(set, i+2)) x |= 2;
  205. if (sigismember(set, i+3)) x |= 4;
  206. if (sigismember(set, i+4)) x |= 8;
  207. seq_printf(m, "%x", x);
  208. } while (i >= 4);
  209. seq_printf(m, "\n");
  210. }
  211. static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
  212. sigset_t *catch)
  213. {
  214. struct k_sigaction *k;
  215. int i;
  216. k = p->sighand->action;
  217. for (i = 1; i <= _NSIG; ++i, ++k) {
  218. if (k->sa.sa_handler == SIG_IGN)
  219. sigaddset(ign, i);
  220. else if (k->sa.sa_handler != SIG_DFL)
  221. sigaddset(catch, i);
  222. }
  223. }
  224. static inline void task_sig(struct seq_file *m, struct task_struct *p)
  225. {
  226. unsigned long flags;
  227. sigset_t pending, shpending, blocked, ignored, caught;
  228. int num_threads = 0;
  229. unsigned long qsize = 0;
  230. unsigned long qlim = 0;
  231. sigemptyset(&pending);
  232. sigemptyset(&shpending);
  233. sigemptyset(&blocked);
  234. sigemptyset(&ignored);
  235. sigemptyset(&caught);
  236. if (lock_task_sighand(p, &flags)) {
  237. pending = p->pending.signal;
  238. shpending = p->signal->shared_pending.signal;
  239. blocked = p->blocked;
  240. collect_sigign_sigcatch(p, &ignored, &caught);
  241. num_threads = atomic_read(&p->signal->count);
  242. qsize = atomic_read(&__task_cred(p)->user->sigpending);
  243. qlim = p->signal->rlim[RLIMIT_SIGPENDING].rlim_cur;
  244. unlock_task_sighand(p, &flags);
  245. }
  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. const struct cred *cred;
  269. kernel_cap_t cap_inheritable, cap_permitted, cap_effective, cap_bset;
  270. rcu_read_lock();
  271. cred = __task_cred(p);
  272. cap_inheritable = cred->cap_inheritable;
  273. cap_permitted = cred->cap_permitted;
  274. cap_effective = cred->cap_effective;
  275. cap_bset = cred->cap_bset;
  276. rcu_read_unlock();
  277. render_cap_t(m, "CapInh:\t", &cap_inheritable);
  278. render_cap_t(m, "CapPrm:\t", &cap_permitted);
  279. render_cap_t(m, "CapEff:\t", &cap_effective);
  280. render_cap_t(m, "CapBnd:\t", &cap_bset);
  281. }
  282. static inline void task_context_switch_counts(struct seq_file *m,
  283. struct task_struct *p)
  284. {
  285. seq_printf(m, "voluntary_ctxt_switches:\t%lu\n"
  286. "nonvoluntary_ctxt_switches:\t%lu\n",
  287. p->nvcsw,
  288. p->nivcsw);
  289. }
  290. int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
  291. struct pid *pid, struct task_struct *task)
  292. {
  293. struct mm_struct *mm = get_task_mm(task);
  294. task_name(m, task);
  295. task_state(m, ns, pid, task);
  296. if (mm) {
  297. task_mem(m, mm);
  298. mmput(mm);
  299. }
  300. task_sig(m, task);
  301. task_cap(m, task);
  302. cpuset_task_status_allowed(m, task);
  303. #if defined(CONFIG_S390)
  304. task_show_regs(m, task);
  305. #endif
  306. task_context_switch_counts(m, task);
  307. return 0;
  308. }
  309. static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
  310. struct pid *pid, struct task_struct *task, int whole)
  311. {
  312. unsigned long vsize, eip, esp, wchan = ~0UL;
  313. long priority, nice;
  314. int tty_pgrp = -1, tty_nr = 0;
  315. sigset_t sigign, sigcatch;
  316. char state;
  317. pid_t ppid = 0, pgid = -1, sid = -1;
  318. int num_threads = 0;
  319. struct mm_struct *mm;
  320. unsigned long long start_time;
  321. unsigned long cmin_flt = 0, cmaj_flt = 0;
  322. unsigned long min_flt = 0, maj_flt = 0;
  323. cputime_t cutime, cstime, utime, stime;
  324. cputime_t cgtime, gtime;
  325. unsigned long rsslim = 0;
  326. char tcomm[sizeof(task->comm)];
  327. unsigned long flags;
  328. state = *get_task_state(task);
  329. vsize = eip = esp = 0;
  330. mm = get_task_mm(task);
  331. if (mm) {
  332. vsize = task_vsize(mm);
  333. eip = KSTK_EIP(task);
  334. esp = KSTK_ESP(task);
  335. }
  336. get_task_comm(tcomm, task);
  337. sigemptyset(&sigign);
  338. sigemptyset(&sigcatch);
  339. cutime = cstime = utime = stime = cputime_zero;
  340. cgtime = gtime = cputime_zero;
  341. if (lock_task_sighand(task, &flags)) {
  342. struct signal_struct *sig = task->signal;
  343. if (sig->tty) {
  344. struct pid *pgrp = tty_get_pgrp(sig->tty);
  345. tty_pgrp = pid_nr_ns(pgrp, ns);
  346. put_pid(pgrp);
  347. tty_nr = new_encode_dev(tty_devnum(sig->tty));
  348. }
  349. num_threads = atomic_read(&sig->count);
  350. collect_sigign_sigcatch(task, &sigign, &sigcatch);
  351. cmin_flt = sig->cmin_flt;
  352. cmaj_flt = sig->cmaj_flt;
  353. cutime = sig->cutime;
  354. cstime = sig->cstime;
  355. cgtime = sig->cgtime;
  356. rsslim = sig->rlim[RLIMIT_RSS].rlim_cur;
  357. /* add up live thread stats at the group level */
  358. if (whole) {
  359. struct task_cputime cputime;
  360. struct task_struct *t = task;
  361. do {
  362. min_flt += t->min_flt;
  363. maj_flt += t->maj_flt;
  364. gtime = cputime_add(gtime, task_gtime(t));
  365. t = next_thread(t);
  366. } while (t != task);
  367. min_flt += sig->min_flt;
  368. maj_flt += sig->maj_flt;
  369. thread_group_cputime(task, &cputime);
  370. utime = cputime.utime;
  371. stime = cputime.stime;
  372. gtime = cputime_add(gtime, sig->gtime);
  373. }
  374. sid = task_session_nr_ns(task, ns);
  375. ppid = task_tgid_nr_ns(task->real_parent, ns);
  376. pgid = task_pgrp_nr_ns(task, ns);
  377. unlock_task_sighand(task, &flags);
  378. }
  379. if (!whole || num_threads < 2)
  380. wchan = get_wchan(task);
  381. if (!whole) {
  382. min_flt = task->min_flt;
  383. maj_flt = task->maj_flt;
  384. utime = task_utime(task);
  385. stime = task_stime(task);
  386. gtime = task_gtime(task);
  387. }
  388. /* scale priority and nice values from timeslices to -20..20 */
  389. /* to make it look like a "normal" Unix priority/nice value */
  390. priority = task_prio(task);
  391. nice = task_nice(task);
  392. /* Temporary variable needed for gcc-2.96 */
  393. /* convert timespec -> nsec*/
  394. start_time =
  395. (unsigned long long)task->real_start_time.tv_sec * NSEC_PER_SEC
  396. + task->real_start_time.tv_nsec;
  397. /* convert nsec -> ticks */
  398. start_time = nsec_to_clock_t(start_time);
  399. seq_printf(m, "%d (%s) %c %d %d %d %d %d %u %lu \
  400. %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
  401. %lu %lu %lu %lu %lu %lu %lu %lu %d %d %u %u %llu %lu %ld\n",
  402. pid_nr_ns(pid, ns),
  403. tcomm,
  404. state,
  405. ppid,
  406. pgid,
  407. sid,
  408. tty_nr,
  409. tty_pgrp,
  410. task->flags,
  411. min_flt,
  412. cmin_flt,
  413. maj_flt,
  414. cmaj_flt,
  415. cputime_to_clock_t(utime),
  416. cputime_to_clock_t(stime),
  417. cputime_to_clock_t(cutime),
  418. cputime_to_clock_t(cstime),
  419. priority,
  420. nice,
  421. num_threads,
  422. start_time,
  423. vsize,
  424. mm ? get_mm_rss(mm) : 0,
  425. rsslim,
  426. mm ? mm->start_code : 0,
  427. mm ? mm->end_code : 0,
  428. mm ? mm->start_stack : 0,
  429. esp,
  430. eip,
  431. /* The signal information here is obsolete.
  432. * It must be decimal for Linux 2.0 compatibility.
  433. * Use /proc/#/status for real-time signals.
  434. */
  435. task->pending.signal.sig[0] & 0x7fffffffUL,
  436. task->blocked.sig[0] & 0x7fffffffUL,
  437. sigign .sig[0] & 0x7fffffffUL,
  438. sigcatch .sig[0] & 0x7fffffffUL,
  439. wchan,
  440. 0UL,
  441. 0UL,
  442. task->exit_signal,
  443. task_cpu(task),
  444. task->rt_priority,
  445. task->policy,
  446. (unsigned long long)delayacct_blkio_ticks(task),
  447. cputime_to_clock_t(gtime),
  448. cputime_to_clock_t(cgtime));
  449. if (mm)
  450. mmput(mm);
  451. return 0;
  452. }
  453. int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
  454. struct pid *pid, struct task_struct *task)
  455. {
  456. return do_task_stat(m, ns, pid, task, 0);
  457. }
  458. int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
  459. struct pid *pid, struct task_struct *task)
  460. {
  461. return do_task_stat(m, ns, pid, task, 1);
  462. }
  463. int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
  464. struct pid *pid, struct task_struct *task)
  465. {
  466. int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
  467. struct mm_struct *mm = get_task_mm(task);
  468. if (mm) {
  469. size = task_statm(mm, &shared, &text, &data, &resident);
  470. mmput(mm);
  471. }
  472. seq_printf(m, "%d %d %d %d %d %d %d\n",
  473. size, resident, shared, text, lib, data, 0);
  474. return 0;
  475. }