array.c 14 KB

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