array.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 <asm/uaccess.h>
  77. #include <asm/pgtable.h>
  78. #include <asm/io.h>
  79. #include <asm/processor.h>
  80. #include "internal.h"
  81. /* Gcc optimizes away "strlen(x)" for constant x */
  82. #define ADDBUF(buffer, string) \
  83. do { memcpy(buffer, string, strlen(string)); \
  84. buffer += strlen(string); } while (0)
  85. static inline char * task_name(struct task_struct *p, char * buf)
  86. {
  87. int i;
  88. char * name;
  89. char tcomm[sizeof(p->comm)];
  90. get_task_comm(tcomm, p);
  91. ADDBUF(buf, "Name:\t");
  92. name = tcomm;
  93. i = sizeof(tcomm);
  94. do {
  95. unsigned char c = *name;
  96. name++;
  97. i--;
  98. *buf = c;
  99. if (!c)
  100. break;
  101. if (c == '\\') {
  102. buf[1] = c;
  103. buf += 2;
  104. continue;
  105. }
  106. if (c == '\n') {
  107. buf[0] = '\\';
  108. buf[1] = 'n';
  109. buf += 2;
  110. continue;
  111. }
  112. buf++;
  113. } while (i);
  114. *buf = '\n';
  115. return buf+1;
  116. }
  117. /*
  118. * The task state array is a strange "bitmap" of
  119. * reasons to sleep. Thus "running" is zero, and
  120. * you can test for combinations of others with
  121. * simple bit tests.
  122. */
  123. static const char *task_state_array[] = {
  124. "R (running)", /* 0 */
  125. "S (sleeping)", /* 1 */
  126. "D (disk sleep)", /* 2 */
  127. "T (stopped)", /* 4 */
  128. "T (tracing stop)", /* 8 */
  129. "Z (zombie)", /* 16 */
  130. "X (dead)" /* 32 */
  131. };
  132. static inline const char * get_task_state(struct task_struct *tsk)
  133. {
  134. unsigned int state = (tsk->state & (TASK_RUNNING |
  135. TASK_INTERRUPTIBLE |
  136. TASK_UNINTERRUPTIBLE |
  137. TASK_STOPPED |
  138. TASK_TRACED)) |
  139. (tsk->exit_state & (EXIT_ZOMBIE |
  140. EXIT_DEAD));
  141. const char **p = &task_state_array[0];
  142. while (state) {
  143. p++;
  144. state >>= 1;
  145. }
  146. return *p;
  147. }
  148. static inline char * task_state(struct task_struct *p, char *buffer)
  149. {
  150. struct group_info *group_info;
  151. int g;
  152. struct fdtable *fdt = NULL;
  153. read_lock(&tasklist_lock);
  154. buffer += sprintf(buffer,
  155. "State:\t%s\n"
  156. "SleepAVG:\t%lu%%\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->sleep_avg/1024)*100/(1020000000/1024),
  165. p->tgid,
  166. p->pid, pid_alive(p) ? p->group_leader->real_parent->tgid : 0,
  167. pid_alive(p) && p->ptrace ? p->parent->pid : 0,
  168. p->uid, p->euid, p->suid, p->fsuid,
  169. p->gid, p->egid, p->sgid, p->fsgid);
  170. read_unlock(&tasklist_lock);
  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. group_info = p->group_info;
  179. get_group_info(group_info);
  180. task_unlock(p);
  181. for (g = 0; g < min(group_info->ngroups,NGROUPS_SMALL); g++)
  182. buffer += sprintf(buffer, "%d ", GROUP_AT(group_info,g));
  183. put_group_info(group_info);
  184. buffer += sprintf(buffer, "\n");
  185. return buffer;
  186. }
  187. static char * render_sigset_t(const char *header, sigset_t *set, char *buffer)
  188. {
  189. int i, len;
  190. len = strlen(header);
  191. memcpy(buffer, header, len);
  192. buffer += len;
  193. i = _NSIG;
  194. do {
  195. int x = 0;
  196. i -= 4;
  197. if (sigismember(set, i+1)) x |= 1;
  198. if (sigismember(set, i+2)) x |= 2;
  199. if (sigismember(set, i+3)) x |= 4;
  200. if (sigismember(set, i+4)) x |= 8;
  201. *buffer++ = (x < 10 ? '0' : 'a' - 10) + x;
  202. } while (i >= 4);
  203. *buffer++ = '\n';
  204. *buffer = 0;
  205. return buffer;
  206. }
  207. static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
  208. sigset_t *catch)
  209. {
  210. struct k_sigaction *k;
  211. int i;
  212. k = p->sighand->action;
  213. for (i = 1; i <= _NSIG; ++i, ++k) {
  214. if (k->sa.sa_handler == SIG_IGN)
  215. sigaddset(ign, i);
  216. else if (k->sa.sa_handler != SIG_DFL)
  217. sigaddset(catch, i);
  218. }
  219. }
  220. static inline char * task_sig(struct task_struct *p, char *buffer)
  221. {
  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. /* Gather all the data with the appropriate locks held */
  232. read_lock(&tasklist_lock);
  233. if (p->sighand) {
  234. spin_lock_irq(&p->sighand->siglock);
  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. spin_unlock_irq(&p->sighand->siglock);
  243. }
  244. read_unlock(&tasklist_lock);
  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. int proc_pid_status(struct task_struct *task, char * buffer)
  265. {
  266. char * orig = buffer;
  267. struct mm_struct *mm = get_task_mm(task);
  268. buffer = task_name(task, buffer);
  269. buffer = task_state(task, buffer);
  270. if (mm) {
  271. buffer = task_mem(mm, buffer);
  272. mmput(mm);
  273. }
  274. buffer = task_sig(task, buffer);
  275. buffer = task_cap(task, buffer);
  276. buffer = cpuset_task_status_allowed(task, buffer);
  277. #if defined(CONFIG_ARCH_S390)
  278. buffer = task_show_regs(task, buffer);
  279. #endif
  280. return buffer - orig;
  281. }
  282. static int do_task_stat(struct task_struct *task, char * buffer, int whole)
  283. {
  284. unsigned long vsize, eip, esp, wchan = ~0UL;
  285. long priority, nice;
  286. int tty_pgrp = -1, tty_nr = 0;
  287. sigset_t sigign, sigcatch;
  288. char state;
  289. int res;
  290. pid_t ppid, pgid = -1, sid = -1;
  291. int num_threads = 0;
  292. struct mm_struct *mm;
  293. unsigned long long start_time;
  294. unsigned long cmin_flt = 0, cmaj_flt = 0;
  295. unsigned long min_flt = 0, maj_flt = 0;
  296. cputime_t cutime, cstime, utime, stime;
  297. unsigned long rsslim = 0;
  298. unsigned long it_real_value = 0;
  299. struct task_struct *t;
  300. char tcomm[sizeof(task->comm)];
  301. state = *get_task_state(task);
  302. vsize = eip = esp = 0;
  303. mm = get_task_mm(task);
  304. if (mm) {
  305. vsize = task_vsize(mm);
  306. eip = KSTK_EIP(task);
  307. esp = KSTK_ESP(task);
  308. }
  309. get_task_comm(tcomm, task);
  310. sigemptyset(&sigign);
  311. sigemptyset(&sigcatch);
  312. cutime = cstime = utime = stime = cputime_zero;
  313. read_lock(&tasklist_lock);
  314. if (task->sighand) {
  315. spin_lock_irq(&task->sighand->siglock);
  316. num_threads = atomic_read(&task->signal->count);
  317. collect_sigign_sigcatch(task, &sigign, &sigcatch);
  318. /* add up live thread stats at the group level */
  319. if (whole) {
  320. t = task;
  321. do {
  322. min_flt += t->min_flt;
  323. maj_flt += t->maj_flt;
  324. utime = cputime_add(utime, t->utime);
  325. stime = cputime_add(stime, t->stime);
  326. t = next_thread(t);
  327. } while (t != task);
  328. }
  329. spin_unlock_irq(&task->sighand->siglock);
  330. }
  331. if (task->signal) {
  332. if (task->signal->tty) {
  333. tty_pgrp = task->signal->tty->pgrp;
  334. tty_nr = new_encode_dev(tty_devnum(task->signal->tty));
  335. }
  336. pgid = process_group(task);
  337. sid = task->signal->session;
  338. cmin_flt = task->signal->cmin_flt;
  339. cmaj_flt = task->signal->cmaj_flt;
  340. cutime = task->signal->cutime;
  341. cstime = task->signal->cstime;
  342. rsslim = task->signal->rlim[RLIMIT_RSS].rlim_cur;
  343. if (whole) {
  344. min_flt += task->signal->min_flt;
  345. maj_flt += task->signal->maj_flt;
  346. utime = cputime_add(utime, task->signal->utime);
  347. stime = cputime_add(stime, task->signal->stime);
  348. }
  349. it_real_value = task->signal->it_real_value;
  350. }
  351. ppid = pid_alive(task) ? task->group_leader->real_parent->tgid : 0;
  352. read_unlock(&tasklist_lock);
  353. if (!whole || num_threads<2)
  354. wchan = get_wchan(task);
  355. if (!whole) {
  356. min_flt = task->min_flt;
  357. maj_flt = task->maj_flt;
  358. utime = task->utime;
  359. stime = task->stime;
  360. }
  361. /* scale priority and nice values from timeslices to -20..20 */
  362. /* to make it look like a "normal" Unix priority/nice value */
  363. priority = task_prio(task);
  364. nice = task_nice(task);
  365. /* Temporary variable needed for gcc-2.96 */
  366. /* convert timespec -> nsec*/
  367. start_time = (unsigned long long)task->start_time.tv_sec * NSEC_PER_SEC
  368. + task->start_time.tv_nsec;
  369. /* convert nsec -> ticks */
  370. start_time = nsec_to_clock_t(start_time);
  371. res = sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
  372. %lu %lu %lu %lu %lu %ld %ld %ld %ld %d %ld %llu %lu %ld %lu %lu %lu %lu %lu \
  373. %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu\n",
  374. task->pid,
  375. tcomm,
  376. state,
  377. ppid,
  378. pgid,
  379. sid,
  380. tty_nr,
  381. tty_pgrp,
  382. task->flags,
  383. min_flt,
  384. cmin_flt,
  385. maj_flt,
  386. cmaj_flt,
  387. cputime_to_clock_t(utime),
  388. cputime_to_clock_t(stime),
  389. cputime_to_clock_t(cutime),
  390. cputime_to_clock_t(cstime),
  391. priority,
  392. nice,
  393. num_threads,
  394. jiffies_to_clock_t(it_real_value),
  395. start_time,
  396. vsize,
  397. mm ? get_mm_counter(mm, rss) : 0, /* you might want to shift this left 3 */
  398. rsslim,
  399. mm ? mm->start_code : 0,
  400. mm ? mm->end_code : 0,
  401. mm ? mm->start_stack : 0,
  402. esp,
  403. eip,
  404. /* The signal information here is obsolete.
  405. * It must be decimal for Linux 2.0 compatibility.
  406. * Use /proc/#/status for real-time signals.
  407. */
  408. task->pending.signal.sig[0] & 0x7fffffffUL,
  409. task->blocked.sig[0] & 0x7fffffffUL,
  410. sigign .sig[0] & 0x7fffffffUL,
  411. sigcatch .sig[0] & 0x7fffffffUL,
  412. wchan,
  413. 0UL,
  414. 0UL,
  415. task->exit_signal,
  416. task_cpu(task),
  417. task->rt_priority,
  418. task->policy);
  419. if(mm)
  420. mmput(mm);
  421. return res;
  422. }
  423. int proc_tid_stat(struct task_struct *task, char * buffer)
  424. {
  425. return do_task_stat(task, buffer, 0);
  426. }
  427. int proc_tgid_stat(struct task_struct *task, char * buffer)
  428. {
  429. return do_task_stat(task, buffer, 1);
  430. }
  431. int proc_pid_statm(struct task_struct *task, char *buffer)
  432. {
  433. int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
  434. struct mm_struct *mm = get_task_mm(task);
  435. if (mm) {
  436. size = task_statm(mm, &shared, &text, &data, &resident);
  437. mmput(mm);
  438. }
  439. return sprintf(buffer,"%d %d %d %d %d %d %d\n",
  440. size, resident, shared, text, lib, data, 0);
  441. }