array.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. "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, p->pid,
  167. pid_alive(p) ? rcu_dereference(p->real_parent)->tgid : 0,
  168. pid_alive(p) && p->ptrace ? rcu_dereference(p->parent)->pid : 0,
  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. 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_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 = 0, 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. char tcomm[sizeof(task->comm)];
  299. unsigned long flags;
  300. state = *get_task_state(task);
  301. vsize = eip = esp = 0;
  302. mm = get_task_mm(task);
  303. if (mm) {
  304. vsize = task_vsize(mm);
  305. eip = KSTK_EIP(task);
  306. esp = KSTK_ESP(task);
  307. }
  308. get_task_comm(tcomm, task);
  309. sigemptyset(&sigign);
  310. sigemptyset(&sigcatch);
  311. cutime = cstime = utime = stime = cputime_zero;
  312. mutex_lock(&tty_mutex);
  313. rcu_read_lock();
  314. if (lock_task_sighand(task, &flags)) {
  315. struct signal_struct *sig = task->signal;
  316. struct tty_struct *tty = sig->tty;
  317. if (tty) {
  318. /*
  319. * sig->tty is not stable, but tty_mutex
  320. * protects us from release_dev(tty)
  321. */
  322. barrier();
  323. tty_pgrp = tty->pgrp;
  324. tty_nr = new_encode_dev(tty_devnum(tty));
  325. }
  326. num_threads = atomic_read(&sig->count);
  327. collect_sigign_sigcatch(task, &sigign, &sigcatch);
  328. cmin_flt = sig->cmin_flt;
  329. cmaj_flt = sig->cmaj_flt;
  330. cutime = sig->cutime;
  331. cstime = sig->cstime;
  332. rsslim = sig->rlim[RLIMIT_RSS].rlim_cur;
  333. /* add up live thread stats at the group level */
  334. if (whole) {
  335. struct task_struct *t = task;
  336. do {
  337. min_flt += t->min_flt;
  338. maj_flt += t->maj_flt;
  339. utime = cputime_add(utime, t->utime);
  340. stime = cputime_add(stime, t->stime);
  341. t = next_thread(t);
  342. } while (t != task);
  343. min_flt += sig->min_flt;
  344. maj_flt += sig->maj_flt;
  345. utime = cputime_add(utime, sig->utime);
  346. stime = cputime_add(stime, sig->stime);
  347. }
  348. sid = sig->session;
  349. pgid = process_group(task);
  350. ppid = rcu_dereference(task->real_parent)->tgid;
  351. unlock_task_sighand(task, &flags);
  352. }
  353. rcu_read_unlock();
  354. mutex_unlock(&tty_mutex);
  355. if (!whole || num_threads<2)
  356. wchan = get_wchan(task);
  357. if (!whole) {
  358. min_flt = task->min_flt;
  359. maj_flt = task->maj_flt;
  360. utime = task->utime;
  361. stime = task->stime;
  362. }
  363. /* scale priority and nice values from timeslices to -20..20 */
  364. /* to make it look like a "normal" Unix priority/nice value */
  365. priority = task_prio(task);
  366. nice = task_nice(task);
  367. /* Temporary variable needed for gcc-2.96 */
  368. /* convert timespec -> nsec*/
  369. start_time = (unsigned long long)task->start_time.tv_sec * NSEC_PER_SEC
  370. + task->start_time.tv_nsec;
  371. /* convert nsec -> ticks */
  372. start_time = nsec_to_clock_t(start_time);
  373. res = sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
  374. %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
  375. %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
  376. task->pid,
  377. tcomm,
  378. state,
  379. ppid,
  380. pgid,
  381. sid,
  382. tty_nr,
  383. tty_pgrp,
  384. task->flags,
  385. min_flt,
  386. cmin_flt,
  387. maj_flt,
  388. cmaj_flt,
  389. cputime_to_clock_t(utime),
  390. cputime_to_clock_t(stime),
  391. cputime_to_clock_t(cutime),
  392. cputime_to_clock_t(cstime),
  393. priority,
  394. nice,
  395. num_threads,
  396. start_time,
  397. vsize,
  398. mm ? get_mm_rss(mm) : 0,
  399. rsslim,
  400. mm ? mm->start_code : 0,
  401. mm ? mm->end_code : 0,
  402. mm ? mm->start_stack : 0,
  403. esp,
  404. eip,
  405. /* The signal information here is obsolete.
  406. * It must be decimal for Linux 2.0 compatibility.
  407. * Use /proc/#/status for real-time signals.
  408. */
  409. task->pending.signal.sig[0] & 0x7fffffffUL,
  410. task->blocked.sig[0] & 0x7fffffffUL,
  411. sigign .sig[0] & 0x7fffffffUL,
  412. sigcatch .sig[0] & 0x7fffffffUL,
  413. wchan,
  414. 0UL,
  415. 0UL,
  416. task->exit_signal,
  417. task_cpu(task),
  418. task->rt_priority,
  419. task->policy,
  420. (unsigned long long)delayacct_blkio_ticks(task));
  421. if(mm)
  422. mmput(mm);
  423. return res;
  424. }
  425. int proc_tid_stat(struct task_struct *task, char * buffer)
  426. {
  427. return do_task_stat(task, buffer, 0);
  428. }
  429. int proc_tgid_stat(struct task_struct *task, char * buffer)
  430. {
  431. return do_task_stat(task, buffer, 1);
  432. }
  433. int proc_pid_statm(struct task_struct *task, char *buffer)
  434. {
  435. int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
  436. struct mm_struct *mm = get_task_mm(task);
  437. if (mm) {
  438. size = task_statm(mm, &shared, &text, &data, &resident);
  439. mmput(mm);
  440. }
  441. return sprintf(buffer,"%d %d %d %d %d %d %d\n",
  442. size, resident, shared, text, lib, data, 0);
  443. }