array.c 12 KB

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