array.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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/config.h>
  55. #include <linux/types.h>
  56. #include <linux/errno.h>
  57. #include <linux/time.h>
  58. #include <linux/kernel.h>
  59. #include <linux/kernel_stat.h>
  60. #include <linux/tty.h>
  61. #include <linux/string.h>
  62. #include <linux/mman.h>
  63. #include <linux/proc_fs.h>
  64. #include <linux/ioport.h>
  65. #include <linux/mm.h>
  66. #include <linux/hugetlb.h>
  67. #include <linux/pagemap.h>
  68. #include <linux/swap.h>
  69. #include <linux/slab.h>
  70. #include <linux/smp.h>
  71. #include <linux/signal.h>
  72. #include <linux/highmem.h>
  73. #include <linux/file.h>
  74. #include <linux/times.h>
  75. #include <linux/cpuset.h>
  76. #include <linux/rcupdate.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. read_lock(&tasklist_lock);
  155. buffer += sprintf(buffer,
  156. "State:\t%s\n"
  157. "SleepAVG:\t%lu%%\n"
  158. "Tgid:\t%d\n"
  159. "Pid:\t%d\n"
  160. "PPid:\t%d\n"
  161. "TracerPid:\t%d\n"
  162. "Uid:\t%d\t%d\t%d\t%d\n"
  163. "Gid:\t%d\t%d\t%d\t%d\n",
  164. get_task_state(p),
  165. (p->sleep_avg/1024)*100/(1020000000/1024),
  166. p->tgid,
  167. p->pid, pid_alive(p) ? p->group_leader->real_parent->tgid : 0,
  168. pid_alive(p) && p->ptrace ? p->parent->pid : 0,
  169. p->uid, p->euid, p->suid, p->fsuid,
  170. p->gid, p->egid, p->sgid, p->fsgid);
  171. read_unlock(&tasklist_lock);
  172. task_lock(p);
  173. rcu_read_lock();
  174. if (p->files)
  175. fdt = files_fdtable(p->files);
  176. buffer += sprintf(buffer,
  177. "FDSize:\t%d\n"
  178. "Groups:\t",
  179. fdt ? fdt->max_fds : 0);
  180. rcu_read_unlock();
  181. group_info = p->group_info;
  182. get_group_info(group_info);
  183. task_unlock(p);
  184. for (g = 0; g < min(group_info->ngroups,NGROUPS_SMALL); g++)
  185. buffer += sprintf(buffer, "%d ", GROUP_AT(group_info,g));
  186. put_group_info(group_info);
  187. buffer += sprintf(buffer, "\n");
  188. return buffer;
  189. }
  190. static char * render_sigset_t(const char *header, sigset_t *set, char *buffer)
  191. {
  192. int i, len;
  193. len = strlen(header);
  194. memcpy(buffer, header, len);
  195. buffer += len;
  196. i = _NSIG;
  197. do {
  198. int x = 0;
  199. i -= 4;
  200. if (sigismember(set, i+1)) x |= 1;
  201. if (sigismember(set, i+2)) x |= 2;
  202. if (sigismember(set, i+3)) x |= 4;
  203. if (sigismember(set, i+4)) x |= 8;
  204. *buffer++ = (x < 10 ? '0' : 'a' - 10) + x;
  205. } while (i >= 4);
  206. *buffer++ = '\n';
  207. *buffer = 0;
  208. return buffer;
  209. }
  210. static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
  211. sigset_t *catch)
  212. {
  213. struct k_sigaction *k;
  214. int i;
  215. k = p->sighand->action;
  216. for (i = 1; i <= _NSIG; ++i, ++k) {
  217. if (k->sa.sa_handler == SIG_IGN)
  218. sigaddset(ign, i);
  219. else if (k->sa.sa_handler != SIG_DFL)
  220. sigaddset(catch, i);
  221. }
  222. }
  223. static inline char * task_sig(struct task_struct *p, char *buffer)
  224. {
  225. sigset_t pending, shpending, blocked, ignored, caught;
  226. int num_threads = 0;
  227. unsigned long qsize = 0;
  228. unsigned long qlim = 0;
  229. sigemptyset(&pending);
  230. sigemptyset(&shpending);
  231. sigemptyset(&blocked);
  232. sigemptyset(&ignored);
  233. sigemptyset(&caught);
  234. /* Gather all the data with the appropriate locks held */
  235. read_lock(&tasklist_lock);
  236. if (p->sighand) {
  237. spin_lock_irq(&p->sighand->siglock);
  238. pending = p->pending.signal;
  239. shpending = p->signal->shared_pending.signal;
  240. blocked = p->blocked;
  241. collect_sigign_sigcatch(p, &ignored, &caught);
  242. num_threads = atomic_read(&p->signal->count);
  243. qsize = atomic_read(&p->user->sigpending);
  244. qlim = p->signal->rlim[RLIMIT_SIGPENDING].rlim_cur;
  245. spin_unlock_irq(&p->sighand->siglock);
  246. }
  247. read_unlock(&tasklist_lock);
  248. buffer += sprintf(buffer, "Threads:\t%d\n", num_threads);
  249. buffer += sprintf(buffer, "SigQ:\t%lu/%lu\n", qsize, qlim);
  250. /* render them all */
  251. buffer = render_sigset_t("SigPnd:\t", &pending, buffer);
  252. buffer = render_sigset_t("ShdPnd:\t", &shpending, buffer);
  253. buffer = render_sigset_t("SigBlk:\t", &blocked, buffer);
  254. buffer = render_sigset_t("SigIgn:\t", &ignored, buffer);
  255. buffer = render_sigset_t("SigCgt:\t", &caught, buffer);
  256. return buffer;
  257. }
  258. static inline char *task_cap(struct task_struct *p, char *buffer)
  259. {
  260. return buffer + sprintf(buffer, "CapInh:\t%016x\n"
  261. "CapPrm:\t%016x\n"
  262. "CapEff:\t%016x\n",
  263. cap_t(p->cap_inheritable),
  264. cap_t(p->cap_permitted),
  265. cap_t(p->cap_effective));
  266. }
  267. int proc_pid_status(struct task_struct *task, char * buffer)
  268. {
  269. char * orig = buffer;
  270. struct mm_struct *mm = get_task_mm(task);
  271. buffer = task_name(task, buffer);
  272. buffer = task_state(task, buffer);
  273. if (mm) {
  274. buffer = task_mem(mm, buffer);
  275. mmput(mm);
  276. }
  277. buffer = task_sig(task, buffer);
  278. buffer = task_cap(task, buffer);
  279. buffer = cpuset_task_status_allowed(task, buffer);
  280. #if defined(CONFIG_S390)
  281. buffer = task_show_regs(task, buffer);
  282. #endif
  283. return buffer - orig;
  284. }
  285. static int do_task_stat(struct task_struct *task, char * buffer, int whole)
  286. {
  287. unsigned long vsize, eip, esp, wchan = ~0UL;
  288. long priority, nice;
  289. int tty_pgrp = -1, tty_nr = 0;
  290. sigset_t sigign, sigcatch;
  291. char state;
  292. int res;
  293. pid_t ppid, pgid = -1, sid = -1;
  294. int num_threads = 0;
  295. struct mm_struct *mm;
  296. unsigned long long start_time;
  297. unsigned long cmin_flt = 0, cmaj_flt = 0;
  298. unsigned long min_flt = 0, maj_flt = 0;
  299. cputime_t cutime, cstime, utime, stime;
  300. unsigned long rsslim = 0;
  301. DEFINE_KTIME(it_real_value);
  302. struct task_struct *t;
  303. char tcomm[sizeof(task->comm)];
  304. state = *get_task_state(task);
  305. vsize = eip = esp = 0;
  306. mm = get_task_mm(task);
  307. if (mm) {
  308. vsize = task_vsize(mm);
  309. eip = KSTK_EIP(task);
  310. esp = KSTK_ESP(task);
  311. }
  312. get_task_comm(tcomm, task);
  313. sigemptyset(&sigign);
  314. sigemptyset(&sigcatch);
  315. cutime = cstime = utime = stime = cputime_zero;
  316. read_lock(&tasklist_lock);
  317. if (task->sighand) {
  318. spin_lock_irq(&task->sighand->siglock);
  319. num_threads = atomic_read(&task->signal->count);
  320. collect_sigign_sigcatch(task, &sigign, &sigcatch);
  321. /* add up live thread stats at the group level */
  322. if (whole) {
  323. t = task;
  324. do {
  325. min_flt += t->min_flt;
  326. maj_flt += t->maj_flt;
  327. utime = cputime_add(utime, t->utime);
  328. stime = cputime_add(stime, t->stime);
  329. t = next_thread(t);
  330. } while (t != task);
  331. }
  332. spin_unlock_irq(&task->sighand->siglock);
  333. }
  334. if (task->signal) {
  335. if (task->signal->tty) {
  336. tty_pgrp = task->signal->tty->pgrp;
  337. tty_nr = new_encode_dev(tty_devnum(task->signal->tty));
  338. }
  339. pgid = process_group(task);
  340. sid = task->signal->session;
  341. cmin_flt = task->signal->cmin_flt;
  342. cmaj_flt = task->signal->cmaj_flt;
  343. cutime = task->signal->cutime;
  344. cstime = task->signal->cstime;
  345. rsslim = task->signal->rlim[RLIMIT_RSS].rlim_cur;
  346. if (whole) {
  347. min_flt += task->signal->min_flt;
  348. maj_flt += task->signal->maj_flt;
  349. utime = cputime_add(utime, task->signal->utime);
  350. stime = cputime_add(stime, task->signal->stime);
  351. }
  352. it_real_value = task->signal->real_timer.expires;
  353. }
  354. ppid = pid_alive(task) ? task->group_leader->real_parent->tgid : 0;
  355. read_unlock(&tasklist_lock);
  356. if (!whole || num_threads<2)
  357. wchan = get_wchan(task);
  358. if (!whole) {
  359. min_flt = task->min_flt;
  360. maj_flt = task->maj_flt;
  361. utime = task->utime;
  362. stime = task->stime;
  363. }
  364. /* scale priority and nice values from timeslices to -20..20 */
  365. /* to make it look like a "normal" Unix priority/nice value */
  366. priority = task_prio(task);
  367. nice = task_nice(task);
  368. /* Temporary variable needed for gcc-2.96 */
  369. /* convert timespec -> nsec*/
  370. start_time = (unsigned long long)task->start_time.tv_sec * NSEC_PER_SEC
  371. + task->start_time.tv_nsec;
  372. /* convert nsec -> ticks */
  373. start_time = nsec_to_clock_t(start_time);
  374. res = sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
  375. %lu %lu %lu %lu %lu %ld %ld %ld %ld %d %ld %llu %lu %ld %lu %lu %lu %lu %lu \
  376. %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu\n",
  377. task->pid,
  378. tcomm,
  379. state,
  380. ppid,
  381. pgid,
  382. sid,
  383. tty_nr,
  384. tty_pgrp,
  385. task->flags,
  386. min_flt,
  387. cmin_flt,
  388. maj_flt,
  389. cmaj_flt,
  390. cputime_to_clock_t(utime),
  391. cputime_to_clock_t(stime),
  392. cputime_to_clock_t(cutime),
  393. cputime_to_clock_t(cstime),
  394. priority,
  395. nice,
  396. num_threads,
  397. (long) ktime_to_clock_t(it_real_value),
  398. start_time,
  399. vsize,
  400. mm ? get_mm_rss(mm) : 0,
  401. rsslim,
  402. mm ? mm->start_code : 0,
  403. mm ? mm->end_code : 0,
  404. mm ? mm->start_stack : 0,
  405. esp,
  406. eip,
  407. /* The signal information here is obsolete.
  408. * It must be decimal for Linux 2.0 compatibility.
  409. * Use /proc/#/status for real-time signals.
  410. */
  411. task->pending.signal.sig[0] & 0x7fffffffUL,
  412. task->blocked.sig[0] & 0x7fffffffUL,
  413. sigign .sig[0] & 0x7fffffffUL,
  414. sigcatch .sig[0] & 0x7fffffffUL,
  415. wchan,
  416. 0UL,
  417. 0UL,
  418. task->exit_signal,
  419. task_cpu(task),
  420. task->rt_priority,
  421. task->policy);
  422. if(mm)
  423. mmput(mm);
  424. return res;
  425. }
  426. int proc_tid_stat(struct task_struct *task, char * buffer)
  427. {
  428. return do_task_stat(task, buffer, 0);
  429. }
  430. int proc_tgid_stat(struct task_struct *task, char * buffer)
  431. {
  432. return do_task_stat(task, buffer, 1);
  433. }
  434. int proc_pid_statm(struct task_struct *task, char *buffer)
  435. {
  436. int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
  437. struct mm_struct *mm = get_task_mm(task);
  438. if (mm) {
  439. size = task_statm(mm, &shared, &text, &data, &resident);
  440. mmput(mm);
  441. }
  442. return sprintf(buffer,"%d %d %d %d %d %d %d\n",
  443. size, resident, shared, text, lib, data, 0);
  444. }