This source file includes following definitions.
- read_core
- get_loadavg
- get_kstat
- get_uptime
- get_meminfo
- get_version
- get_task
- get_phys_addr
- get_array
- get_env
- get_arg
- get_wchan
- get_stat
- get_statm
- get_maps
- array_read
1
2
3
4
5
6
7
8
9
10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/kernel_stat.h>
15 #include <linux/tty.h>
16 #include <linux/user.h>
17 #include <linux/a.out.h>
18 #include <linux/string.h>
19 #include <linux/mman.h>
20
21 #include <asm/segment.h>
22 #include <asm/io.h>
23
24 #define LOAD_INT(x) ((x) >> FSHIFT)
25 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
26
27 #ifdef CONFIG_DEBUG_MALLOC
28 int get_malloc(char * buffer);
29 #endif
30
31 static int read_core(struct inode * inode, struct file * file,char * buf, int count)
32 {
33 unsigned long p = file->f_pos;
34 int read;
35 int count1;
36 char * pnt;
37 struct user dump;
38
39 memset(&dump, 0, sizeof(struct user));
40 dump.magic = CMAGIC;
41 dump.u_dsize = high_memory >> 12;
42
43 if (count < 0)
44 return -EINVAL;
45 if (p >= high_memory)
46 return 0;
47 if (count > high_memory - p)
48 count = high_memory - p;
49 read = 0;
50
51 if (p < sizeof(struct user) && count > 0) {
52 count1 = count;
53 if (p + count1 > sizeof(struct user))
54 count1 = sizeof(struct user)-p;
55 pnt = (char *) &dump + p;
56 memcpy_tofs(buf,(void *) pnt, count1);
57 buf += count1;
58 p += count1;
59 count -= count1;
60 read += count1;
61 }
62
63 while (p < 2*PAGE_SIZE && count > 0) {
64 put_fs_byte(0,buf);
65 buf++;
66 p++;
67 count--;
68 read++;
69 }
70 memcpy_tofs(buf,(void *) (p - PAGE_SIZE),count);
71 read += count;
72 file->f_pos += read;
73 return read;
74 }
75
76 static int get_loadavg(char * buffer)
77 {
78 int a, b, c;
79
80 a = avenrun[0] + (FIXED_1/200);
81 b = avenrun[1] + (FIXED_1/200);
82 c = avenrun[2] + (FIXED_1/200);
83 return sprintf(buffer,"%d.%02d %d.%02d %d.%02d\n",
84 LOAD_INT(a), LOAD_FRAC(a),
85 LOAD_INT(b), LOAD_FRAC(b),
86 LOAD_INT(c), LOAD_FRAC(c));
87 }
88
89 static int get_kstat(char * buffer)
90 {
91 return sprintf(buffer, "cpu %u %u %u %lu\n"
92 "disk %u %u %u %u\n"
93 "page %u %u\n"
94 "swap %u %u\n"
95 "intr %u\n"
96 "ctxt %u\n"
97 "btime %lu\n",
98 kstat.cpu_user,
99 kstat.cpu_nice,
100 kstat.cpu_system,
101 jiffies - (kstat.cpu_user + kstat.cpu_nice + kstat.cpu_system),
102 kstat.dk_drive[0],
103 kstat.dk_drive[1],
104 kstat.dk_drive[2],
105 kstat.dk_drive[3],
106 kstat.pgpgin,
107 kstat.pgpgout,
108 kstat.pswpin,
109 kstat.pswpout,
110 kstat.interrupts,
111 kstat.context_swtch,
112 xtime.tv_sec - jiffies / HZ);
113 }
114
115
116 static int get_uptime(char * buffer)
117 {
118 unsigned long uptime;
119 unsigned long idle;
120
121 uptime = jiffies;
122 idle = task[0]->utime + task[0]->stime;
123 return sprintf(buffer,"%lu.%02lu %lu.%02lu\n",
124 uptime / HZ,
125 uptime % HZ,
126 idle / HZ,
127 idle % HZ);
128 }
129
130 static int get_meminfo(char * buffer)
131 {
132 struct sysinfo i;
133
134 si_meminfo(&i);
135 si_swapinfo(&i);
136 return sprintf(buffer, " total: used: free: shared: buffers:\n"
137 "Mem: %8lu %8lu %8lu %8lu %8lu\n"
138 "Swap: %8lu %8lu %8lu\n",
139 i.totalram, i.totalram-i.freeram, i.freeram, i.sharedram, i.bufferram,
140 i.totalswap, i.totalswap-i.freeswap, i.freeswap);
141 }
142
143 static int get_version(char * buffer)
144 {
145 extern char *linux_banner;
146
147 strcpy(buffer, linux_banner);
148 return strlen(buffer);
149 }
150
151 static struct task_struct ** get_task(pid_t pid)
152 {
153 struct task_struct ** p;
154
155 p = task;
156 while (++p < task+NR_TASKS) {
157 if (*p && (*p)->pid == pid)
158 return p;
159 }
160 return NULL;
161 }
162
163 static unsigned long get_phys_addr(struct task_struct ** p, unsigned long ptr)
164 {
165 unsigned long page;
166
167 if (!p || !*p || ptr >= TASK_SIZE)
168 return 0;
169 page = *PAGE_DIR_OFFSET((*p)->tss.cr3,ptr);
170 if (!(page & 1))
171 return 0;
172 page &= PAGE_MASK;
173 page += PAGE_PTR(ptr);
174 page = *(unsigned long *) page;
175 if (!(page & 1))
176 return 0;
177 page &= PAGE_MASK;
178 page += ptr & ~PAGE_MASK;
179 return page;
180 }
181
182 static int get_array(struct task_struct ** p, unsigned long start, unsigned long end, char * buffer)
183 {
184 unsigned long addr;
185 int size = 0, result = 0;
186 char c;
187
188 if (start >= end)
189 return result;
190 for (;;) {
191 addr = get_phys_addr(p, start);
192 if (!addr)
193 return result;
194 do {
195 c = *(char *) addr;
196 if (!c)
197 result = size;
198 if (size < PAGE_SIZE)
199 buffer[size++] = c;
200 else
201 return result;
202 addr++;
203 start++;
204 if (start >= end)
205 return result;
206 } while (!(addr & ~PAGE_MASK));
207 }
208 }
209
210 static int get_env(int pid, char * buffer)
211 {
212 struct task_struct ** p = get_task(pid);
213
214 if (!p || !*p)
215 return 0;
216 return get_array(p, (*p)->env_start, (*p)->env_end, buffer);
217 }
218
219 static int get_arg(int pid, char * buffer)
220 {
221 struct task_struct ** p = get_task(pid);
222
223 if (!p || !*p)
224 return 0;
225 return get_array(p, (*p)->arg_start, (*p)->arg_end, buffer);
226 }
227
228 static unsigned long get_wchan(struct task_struct *p)
229 {
230 unsigned long ebp, eip;
231 unsigned long stack_page;
232 int count = 0;
233
234 if (!p || p == current || p->state == TASK_RUNNING)
235 return 0;
236 stack_page = p->kernel_stack_page;
237 if (!stack_page)
238 return 0;
239 ebp = p->tss.ebp;
240 do {
241 if (ebp < stack_page || ebp >= 4092+stack_page)
242 return 0;
243 eip = *(unsigned long *) (ebp+4);
244 if ((void *)eip != sleep_on &&
245 (void *)eip != interruptible_sleep_on)
246 return eip;
247 ebp = *(unsigned long *) ebp;
248 } while (count++ < 16);
249 return 0;
250 }
251
252 #define KSTK_EIP(stack) (((unsigned long *)stack)[1019])
253 #define KSTK_ESP(stack) (((unsigned long *)stack)[1022])
254
255 static int get_stat(int pid, char * buffer)
256 {
257 struct task_struct ** p = get_task(pid);
258 unsigned long sigignore=0, sigcatch=0, bit=1, wchan;
259 unsigned long vsize, eip, esp;
260 int i,tty_pgrp;
261 char state;
262
263 if (!p || !*p)
264 return 0;
265 if ((*p)->state < 0 || (*p)->state > 5)
266 state = '.';
267 else
268 state = "RSDZTD"[(*p)->state];
269 eip = esp = 0;
270 vsize = (*p)->kernel_stack_page;
271 if (vsize) {
272 eip = KSTK_EIP(vsize);
273 esp = KSTK_ESP(vsize);
274 vsize = (*p)->brk - (*p)->start_code + PAGE_SIZE-1;
275 if (esp)
276 vsize += TASK_SIZE - esp;
277 }
278 wchan = get_wchan(*p);
279 for(i=0; i<32; ++i) {
280 switch((int) (*p)->sigaction[i].sa_handler) {
281 case 1: sigignore |= bit; break;
282 case 0: break;
283 default: sigcatch |= bit;
284 } bit <<= 1;
285 }
286 tty_pgrp = (*p)->tty;
287 if (tty_pgrp > 0 && tty_table[tty_pgrp])
288 tty_pgrp = tty_table[tty_pgrp]->pgrp;
289 else
290 tty_pgrp = -1;
291 return sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
292 %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %u %u %lu %lu %lu %lu %lu %lu \
293 %lu %lu %lu %lu\n",
294 pid,
295 (*p)->comm,
296 state,
297 (*p)->p_pptr->pid,
298 (*p)->pgrp,
299 (*p)->session,
300 (*p)->tty,
301 tty_pgrp,
302 (*p)->flags,
303 (*p)->min_flt,
304 (*p)->cmin_flt,
305 (*p)->maj_flt,
306 (*p)->cmaj_flt,
307 (*p)->utime,
308 (*p)->stime,
309 (*p)->cutime,
310 (*p)->cstime,
311 (*p)->counter,
312
313 (*p)->priority,
314
315 (*p)->timeout,
316 (*p)->it_real_value,
317 (*p)->start_time,
318 vsize,
319 (*p)->rss,
320 (*p)->rlim[RLIMIT_RSS].rlim_cur,
321 (*p)->start_code,
322 (*p)->end_code,
323 (*p)->start_stack,
324 esp,
325 eip,
326 (*p)->signal,
327 (*p)->blocked,
328 sigignore,
329 sigcatch,
330 wchan);
331 }
332
333 static int get_statm(int pid, char * buffer)
334 {
335 struct task_struct ** p = get_task(pid);
336 int i, tpag;
337 int size=0, resident=0, share=0, trs=0, lrs=0, drs=0, dt=0;
338 unsigned long ptbl, *buf, *pte, *pagedir, map_nr;
339
340 if (!p || !*p)
341 return 0;
342 tpag = (*p)->end_code / PAGE_SIZE;
343 if ((*p)->state != TASK_ZOMBIE) {
344 pagedir = (unsigned long *) (*p)->tss.cr3;
345 for (i = 0; i < 0x300; ++i) {
346 if ((ptbl = pagedir[i]) == 0) {
347 tpag -= PTRS_PER_PAGE;
348 continue;
349 }
350 buf = (unsigned long *)(ptbl & PAGE_MASK);
351 for (pte = buf; pte < (buf + PTRS_PER_PAGE); ++pte) {
352 if (*pte != 0) {
353 ++size;
354 if (*pte & 1) {
355 ++resident;
356 if (tpag > 0)
357 ++trs;
358 else
359 ++drs;
360 if (i >= 15 && i < 0x2f0) {
361 ++lrs;
362 if (*pte & 0x40)
363 ++dt;
364 else
365 --drs;
366 }
367 map_nr = MAP_NR(*pte);
368 if (map_nr < (high_memory / PAGE_SIZE) && mem_map[map_nr] > 1)
369 ++share;
370 }
371 }
372 --tpag;
373 }
374 }
375 }
376 return sprintf(buffer,"%d %d %d %d %d %d %d\n",
377 size, resident, share, trs, lrs, drs, dt);
378 }
379
380 static int get_maps(int pid, char *buf)
381 {
382 int sz = 0;
383 struct task_struct **p = get_task(pid);
384 struct vm_area_struct *map;
385
386 if (!p || !*p)
387 return 0;
388
389 for(map = (*p)->mmap; map != NULL; map = map->vm_next) {
390 char str[7], *cp = str;
391 int prot = map->vm_page_prot;
392 int perms, flags;
393 int end = sz + 80;
394 dev_t dev;
395 unsigned long ino;
396
397
398
399
400
401
402
403 flags = perms = 0;
404
405 if ((prot & PAGE_READONLY) == PAGE_READONLY)
406 perms |= PROT_READ | PROT_EXEC;
407 if (prot & (PAGE_COW|PAGE_RW)) {
408 perms |= PROT_WRITE | PROT_READ;
409 flags = prot & PAGE_COW ? MAP_PRIVATE : MAP_SHARED;
410 }
411
412 *cp++ = perms & PROT_READ ? 'r' : '-';
413 *cp++ = perms & PROT_WRITE ? 'w' : '-';
414 *cp++ = perms & PROT_EXEC ? 'x' : '-';
415 *cp++ = flags & MAP_SHARED ? 's' : '-';
416 *cp++ = flags & MAP_PRIVATE ? 'p' : '-';
417 *cp++ = 0;
418
419 if (end >= PAGE_SIZE) {
420 sprintf(buf+sz, "...\n");
421 break;
422 }
423
424 if (map->vm_inode != NULL) {
425 dev = map->vm_inode->i_dev;
426 ino = map->vm_inode->i_ino;
427 } else {
428 dev = 0;
429 ino = 0;
430 }
431
432 sz += sprintf(buf+sz, "%08lx-%08lx %s %08lx %02x:%02x %lu\n",
433 map->vm_start, map->vm_end, str, map->vm_offset,
434 MAJOR(dev),MINOR(dev), ino);
435 if (sz > end) {
436 printk("get_maps: end(%d) < sz(%d)\n", end, sz);
437 break;
438 }
439 }
440
441 return sz;
442 }
443
444 asmlinkage int get_module_list( char *);
445
446 static int array_read(struct inode * inode, struct file * file,char * buf, int count)
447 {
448 char * page;
449 int length;
450 int end;
451 unsigned int type, pid;
452
453 if (count < 0)
454 return -EINVAL;
455 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
456 return -ENOMEM;
457 type = inode->i_ino;
458 pid = type >> 16;
459 type &= 0x0000ffff;
460 switch (type) {
461 case 2:
462 length = get_loadavg(page);
463 break;
464 case 3:
465 length = get_uptime(page);
466 break;
467 case 4:
468 length = get_meminfo(page);
469 break;
470 case 6:
471 length = get_version(page);
472 break;
473 case 9:
474 length = get_env(pid, page);
475 break;
476 case 10:
477 length = get_arg(pid, page);
478 break;
479 case 11:
480 length = get_stat(pid, page);
481 break;
482 case 12:
483 length = get_statm(pid, page);
484 break;
485 #ifdef CONFIG_DEBUG_MALLOC
486 case 13:
487 length = get_malloc(page);
488 break;
489 #endif
490 case 14:
491 free_page((unsigned long) page);
492 return read_core(inode, file, buf, count);
493 case 15:
494 length = get_maps(pid, page);
495 break;
496 case 16:
497 length = get_module_list(page);
498 break;
499 case 17:
500 length = get_kstat(page);
501 break;
502 default:
503 free_page((unsigned long) page);
504 return -EBADF;
505 }
506 if (file->f_pos >= length) {
507 free_page((unsigned long) page);
508 return 0;
509 }
510 if (count + file->f_pos > length)
511 count = length - file->f_pos;
512 end = count + file->f_pos;
513 memcpy_tofs(buf, page + file->f_pos, count);
514 free_page((unsigned long) page);
515 file->f_pos = end;
516 return count;
517 }
518
519 static struct file_operations proc_array_operations = {
520 NULL,
521 array_read,
522 NULL,
523 NULL,
524 NULL,
525 NULL,
526 NULL,
527 NULL,
528 NULL,
529 NULL
530 };
531
532 struct inode_operations proc_array_inode_operations = {
533 &proc_array_operations,
534 NULL,
535 NULL,
536 NULL,
537 NULL,
538 NULL,
539 NULL,
540 NULL,
541 NULL,
542 NULL,
543 NULL,
544 NULL,
545 NULL,
546 NULL,
547 NULL
548 };