array.c 14 KB

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