array.c 14.5 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/*
 *  linux/fs/proc/array.c
 *
 *  Copyright (C) 1992  by Linus Torvalds
 *  based on ideas by Darren Senn
 *
 * Fixes:
 * Michael. K. Johnson: stat,statm extensions.
 *                      <johnsonm@stolaf.edu>
 *
 * Pauline Middelink :  Made cmdline,envline only break at '\0's, to
 *                      make sure SET_PROCTITLE works. Also removed
 *                      bad '!' which forced address recalculation for
 *                      EVERY character on the current page.
 *                      <middelin@polyware.iaf.nl>
 *
 * Danny ter Haar    :	added cpuinfo
 *			<dth@cistron.nl>
 *
 * Alessandro Rubini :  profile extension.
 *                      <rubini@ipvvis.unipv.it>
 *
 * Jeff Tranter      :  added BogoMips field to cpuinfo
 *                      <Jeff_Tranter@Mitel.COM>
 *
 * Bruno Haible      :  remove 4K limit for the maps file
 *			<haible@ma2s2.mathematik.uni-karlsruhe.de>
 *
 * Yves Arrouye      :  remove removal of trailing spaces in get_array.
 *			<Yves.Arrouye@marin.fdn.fr>
 *
 * Jerome Forissier  :  added per-CPU time information to /proc/stat
 *                      and /proc/<pid>/cpu extension
 *                      <forissier@isia.cma.fr>
 *			- Incorporation and non-SMP safe operation
 *			of forissier patch in 2.1.78 by
 *			Hans Marcus <crowbar@concepts.nl>
 *
 * aeb@cwi.nl        :  /proc/partitions
 *
 *
 * Alan Cox	     :  security fixes.
 *			<Alan.Cox@linux.org>
 *
 * Al Viro           :  safe handling of mm_struct
 *
 * Gerhard Wichert   :  added BIGMEM support
 * Siemens AG           <Gerhard.Wichert@pdb.siemens.de>
 *
 * Al Viro & Jeff Garzik :  moved most of the thing into base.c and
 *			 :  proc_misc.c. The rest may eventually go into
 *			 :  base.c too.
 */

#include <linux/config.h>
#include <linux/types.h>
#include <linux/errno.h>
58
#include <linux/time.h>
Linus Torvalds's avatar
Linus Torvalds committed
59 60 61 62 63 64 65 66
#include <linux/kernel.h>
#include <linux/kernel_stat.h>
#include <linux/tty.h>
#include <linux/string.h>
#include <linux/mman.h>
#include <linux/proc_fs.h>
#include <linux/ioport.h>
#include <linux/mm.h>
67
#include <linux/hugetlb.h>
Linus Torvalds's avatar
Linus Torvalds committed
68 69 70 71 72 73
#include <linux/pagemap.h>
#include <linux/swap.h>
#include <linux/slab.h>
#include <linux/smp.h>
#include <linux/signal.h>
#include <linux/highmem.h>
Linus Torvalds's avatar
Linus Torvalds committed
74
#include <linux/file.h>
75
#include <linux/times.h>
Linus Torvalds's avatar
Linus Torvalds committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

#include <asm/uaccess.h>
#include <asm/pgtable.h>
#include <asm/io.h>
#include <asm/processor.h>

/* Gcc optimizes away "strlen(x)" for constant x */
#define ADDBUF(buffer, string) \
do { memcpy(buffer, string, strlen(string)); \
     buffer += strlen(string); } while (0)

static inline char * task_name(struct task_struct *p, char * buf)
{
	int i;
	char * name;

	ADDBUF(buf, "Name:\t");
	name = p->comm;
	i = sizeof(p->comm);
	do {
		unsigned char c = *name;
		name++;
		i--;
		*buf = c;
		if (!c)
			break;
		if (c == '\\') {
			buf[1] = c;
			buf += 2;
			continue;
		}
		if (c == '\n') {
			buf[0] = '\\';
			buf[1] = 'n';
			buf += 2;
			continue;
		}
		buf++;
	} while (i);
	*buf = '\n';
	return buf+1;
}

/*
 * The task state array is a strange "bitmap" of
 * reasons to sleep. Thus "running" is zero, and
 * you can test for combinations of others with
 * simple bit tests.
 */
static const char *task_state_array[] = {
	"R (running)",		/*  0 */
	"S (sleeping)",		/*  1 */
	"D (disk sleep)",	/*  2 */
	"Z (zombie)",		/*  4 */
	"T (stopped)",		/*  8 */
	"W (paging)"		/* 16 */
};

static inline const char * get_task_state(struct task_struct *tsk)
{
	unsigned int state = tsk->state & (TASK_RUNNING |
					   TASK_INTERRUPTIBLE |
					   TASK_UNINTERRUPTIBLE |
					   TASK_ZOMBIE |
					   TASK_STOPPED);
	const char **p = &task_state_array[0];

	while (state) {
		p++;
		state >>= 1;
	}
	return *p;
}

static inline char * task_state(struct task_struct *p, char *buffer)
{
	int g;

	read_lock(&tasklist_lock);
	buffer += sprintf(buffer,
		"State:\t%s\n"
Linus Torvalds's avatar
Linus Torvalds committed
157
		"Tgid:\t%d\n"
Linus Torvalds's avatar
Linus Torvalds committed
158 159 160 161 162
		"Pid:\t%d\n"
		"PPid:\t%d\n"
		"TracerPid:\t%d\n"
		"Uid:\t%d\t%d\t%d\t%d\n"
		"Gid:\t%d\t%d\t%d\t%d\n",
Linus Torvalds's avatar
Linus Torvalds committed
163
		get_task_state(p), p->tgid,
164
		p->pid, p->pid ? p->real_parent->pid : 0, 0,
Linus Torvalds's avatar
Linus Torvalds committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
		p->uid, p->euid, p->suid, p->fsuid,
		p->gid, p->egid, p->sgid, p->fsgid);
	read_unlock(&tasklist_lock);	
	task_lock(p);
	buffer += sprintf(buffer,
		"FDSize:\t%d\n"
		"Groups:\t",
		p->files ? p->files->max_fds : 0);
	task_unlock(p);

	for (g = 0; g < p->ngroups; g++)
		buffer += sprintf(buffer, "%d ", p->groups[g]);

	buffer += sprintf(buffer, "\n");
	return buffer;
}

static inline char * task_mem(struct mm_struct *mm, char *buffer)
{
	struct vm_area_struct * vma;
	unsigned long data = 0, stack = 0;
	unsigned long exec = 0, lib = 0;

Linus Torvalds's avatar
Linus Torvalds committed
188
	down_read(&mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	for (vma = mm->mmap; vma; vma = vma->vm_next) {
		unsigned long len = (vma->vm_end - vma->vm_start) >> 10;
		if (!vma->vm_file) {
			data += len;
			if (vma->vm_flags & VM_GROWSDOWN)
				stack += len;
			continue;
		}
		if (vma->vm_flags & VM_WRITE)
			continue;
		if (vma->vm_flags & VM_EXEC) {
			exec += len;
			if (vma->vm_flags & VM_EXECUTABLE)
				continue;
			lib += len;
		}
	}
	buffer += sprintf(buffer,
		"VmSize:\t%8lu kB\n"
		"VmLck:\t%8lu kB\n"
		"VmRSS:\t%8lu kB\n"
		"VmData:\t%8lu kB\n"
		"VmStk:\t%8lu kB\n"
		"VmExe:\t%8lu kB\n"
		"VmLib:\t%8lu kB\n",
		mm->total_vm << (PAGE_SHIFT-10),
		mm->locked_vm << (PAGE_SHIFT-10),
		mm->rss << (PAGE_SHIFT-10),
		data - stack, stack,
		exec - lib, lib);
Linus Torvalds's avatar
Linus Torvalds committed
219
	up_read(&mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
220 221 222 223 224 225 226 227 228 229 230 231
	return buffer;
}

static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
				    sigset_t *catch)
{
	struct k_sigaction *k;
	int i;

	sigemptyset(ign);
	sigemptyset(catch);

232
	read_lock(&tasklist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
233
	if (p->sig) {
234
		spin_lock_irq(&p->sig->siglock);
Linus Torvalds's avatar
Linus Torvalds committed
235 236 237 238 239 240 241
		k = p->sig->action;
		for (i = 1; i <= _NSIG; ++i, ++k) {
			if (k->sa.sa_handler == SIG_IGN)
				sigaddset(ign, i);
			else if (k->sa.sa_handler != SIG_DFL)
				sigaddset(catch, i);
		}
242
		spin_unlock_irq(&p->sig->siglock);
Linus Torvalds's avatar
Linus Torvalds committed
243
	}
244
	read_unlock(&tasklist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
}

static inline char * task_sig(struct task_struct *p, char *buffer)
{
	sigset_t ign, catch;

	buffer += sprintf(buffer, "SigPnd:\t");
	buffer = render_sigset_t(&p->pending.signal, buffer);
	*buffer++ = '\n';
	buffer += sprintf(buffer, "SigBlk:\t");
	buffer = render_sigset_t(&p->blocked, buffer);
	*buffer++ = '\n';

	collect_sigign_sigcatch(p, &ign, &catch);
	buffer += sprintf(buffer, "SigIgn:\t");
	buffer = render_sigset_t(&ign, buffer);
	*buffer++ = '\n';
	buffer += sprintf(buffer, "SigCgt:\t"); /* Linux 2.0 uses "SigCgt" */
	buffer = render_sigset_t(&catch, buffer);
	*buffer++ = '\n';

	return buffer;
}

Linus Torvalds's avatar
Linus Torvalds committed
269
static inline char *task_cap(struct task_struct *p, char *buffer)
Linus Torvalds's avatar
Linus Torvalds committed
270 271 272 273 274 275 276 277 278 279 280 281 282
{
    return buffer + sprintf(buffer, "CapInh:\t%016x\n"
			    "CapPrm:\t%016x\n"
			    "CapEff:\t%016x\n",
			    cap_t(p->cap_inheritable),
			    cap_t(p->cap_permitted),
			    cap_t(p->cap_effective));
}


int proc_pid_status(struct task_struct *task, char * buffer)
{
	char * orig = buffer;
283
	struct mm_struct *mm = get_task_mm(task);
Linus Torvalds's avatar
Linus Torvalds committed
284 285 286

	buffer = task_name(task, buffer);
	buffer = task_state(task, buffer);
287
 
Linus Torvalds's avatar
Linus Torvalds committed
288 289 290 291 292 293 294
	if (mm) {
		buffer = task_mem(mm, buffer);
		mmput(mm);
	}
	buffer = task_sig(task, buffer);
	buffer = task_cap(task, buffer);
#if defined(CONFIG_ARCH_S390)
Linus Torvalds's avatar
Linus Torvalds committed
295
	buffer = task_show_regs(task, buffer);
Linus Torvalds's avatar
Linus Torvalds committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
#endif
	return buffer - orig;
}

int proc_pid_stat(struct task_struct *task, char * buffer)
{
	unsigned long vsize, eip, esp, wchan;
	long priority, nice;
	int tty_pgrp = -1, tty_nr = 0;
	sigset_t sigign, sigcatch;
	char state;
	int res;
	pid_t ppid;
	struct mm_struct *mm;

	state = *get_task_state(task);
	vsize = eip = esp = 0;
	task_lock(task);
	mm = task->mm;
	if(mm)
		atomic_inc(&mm->mm_users);
	if (task->tty) {
		tty_pgrp = task->tty->pgrp;
		tty_nr = kdev_t_to_nr(task->tty->device);
	}
	task_unlock(task);
	if (mm) {
		struct vm_area_struct *vma;
Linus Torvalds's avatar
Linus Torvalds committed
324
		down_read(&mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
325 326 327 328 329 330 331
		vma = mm->mmap;
		while (vma) {
			vsize += vma->vm_end - vma->vm_start;
			vma = vma->vm_next;
		}
		eip = KSTK_EIP(task);
		esp = KSTK_ESP(task);
Linus Torvalds's avatar
Linus Torvalds committed
332
		up_read(&mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
333 334 335 336 337 338 339 340
	}

	wchan = get_wchan(task);

	collect_sigign_sigcatch(task, &sigign, &sigcatch);

	/* scale priority and nice values from timeslices to -20..20 */
	/* to make it look like a "normal" Unix priority/nice value  */
Ingo Molnar's avatar
Ingo Molnar committed
341 342
	priority = task_prio(task);
	nice = task_nice(task);
Linus Torvalds's avatar
Linus Torvalds committed
343 344

	read_lock(&tasklist_lock);
345
	ppid = task->pid ? task->real_parent->pid : 0;
Linus Torvalds's avatar
Linus Torvalds committed
346 347 348
	read_unlock(&tasklist_lock);
	res = sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
%lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %lu %lu %lu \
349
%lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu\n",
Linus Torvalds's avatar
Linus Torvalds committed
350 351 352 353 354 355
		task->pid,
		task->comm,
		state,
		ppid,
		task->pgrp,
		task->session,
Dave Jones's avatar
Dave Jones committed
356
		tty_nr,
Linus Torvalds's avatar
Linus Torvalds committed
357 358 359 360 361 362
		tty_pgrp,
		task->flags,
		task->min_flt,
		task->cmin_flt,
		task->maj_flt,
		task->cmaj_flt,
363 364 365 366
		jiffies_to_clock_t(task->utime),
		jiffies_to_clock_t(task->stime),
		jiffies_to_clock_t(task->cutime),
		jiffies_to_clock_t(task->cstime),
Linus Torvalds's avatar
Linus Torvalds committed
367 368 369
		priority,
		nice,
		0UL /* removed */,
370 371
		jiffies_to_clock_t(task->it_real_value),
		jiffies_to_clock_t(task->start_time),
Linus Torvalds's avatar
Linus Torvalds committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
		vsize,
		mm ? mm->rss : 0, /* you might want to shift this left 3 */
		task->rlim[RLIMIT_RSS].rlim_cur,
		mm ? mm->start_code : 0,
		mm ? mm->end_code : 0,
		mm ? mm->start_stack : 0,
		esp,
		eip,
		/* The signal information here is obsolete.
		 * It must be decimal for Linux 2.0 compatibility.
		 * Use /proc/#/status for real-time signals.
		 */
		task->pending.signal.sig[0] & 0x7fffffffUL,
		task->blocked.sig[0] & 0x7fffffffUL,
		sigign      .sig[0] & 0x7fffffffUL,
		sigcatch    .sig[0] & 0x7fffffffUL,
		wchan,
		task->nswap,
		task->cnswap,
		task->exit_signal,
392
		task_cpu(task),
393 394
		task->rt_priority,
		task->policy);
Linus Torvalds's avatar
Linus Torvalds committed
395 396 397 398 399
	if(mm)
		mmput(mm);
	return res;
}
		
400
int proc_pid_statm(task_t *task, char *buffer)
Linus Torvalds's avatar
Linus Torvalds committed
401
{
402 403 404
	int size, resident, shared, text, lib, data, dirty;
	struct mm_struct *mm = get_task_mm(task);
	struct vm_area_struct * vma;
Linus Torvalds's avatar
Linus Torvalds committed
405

406
	size = resident = shared = text = lib = data = dirty = 0;
Linus Torvalds's avatar
Linus Torvalds committed
407

408 409
	if (!mm)
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
410

411 412 413 414
	down_read(&mm->mmap_sem);
	resident = mm->rss;
	for (vma = mm->mmap; vma; vma = vma->vm_next) {
		int pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
Linus Torvalds's avatar
Linus Torvalds committed
415

416 417 418 419 420
		size += pages;
		if (is_vm_hugetlb_page(vma)) {
			if (!(vma->vm_flags & VM_DONTCOPY))
				shared += pages;
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
421
		}
422 423 424 425 426 427
		if (vma->vm_flags & VM_SHARED || !list_empty(&vma->shared))
			shared += pages;
		if (vma->vm_flags & VM_EXECUTABLE)
			text += pages;
		else
			data += pages;
Linus Torvalds's avatar
Linus Torvalds committed
428
	}
429 430 431
	up_read(&mm->mmap_sem);
	mmput(mm);
out:
Linus Torvalds's avatar
Linus Torvalds committed
432
	return sprintf(buffer,"%d %d %d %d %d %d %d\n",
433
		       size, resident, shared, text, lib, data, dirty);
Linus Torvalds's avatar
Linus Torvalds committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
}

/*
 * The way we support synthetic files > 4K
 * - without storing their contents in some buffer and
 * - without walking through the entire synthetic file until we reach the
 *   position of the requested data
 * is to cleverly encode the current position in the file's f_pos field.
 * There is no requirement that a read() call which returns `count' bytes
 * of data increases f_pos by exactly `count'.
 *
 * This idea is Linus' one. Bruno implemented it.
 */

/*
 * For the /proc/<pid>/maps file, we use fixed length records, each containing
 * a single line.
Linus Torvalds's avatar
Linus Torvalds committed
451 452
 *
 * f_pos = (number of the vma in the task->mm->mmap list) * PAGE_SIZE
Linus Torvalds's avatar
Linus Torvalds committed
453 454 455
 *         + (index into the line)
 */
/* for systems with sizeof(void*) == 4: */
456
#define MAPS_LINE_FORMAT4	  "%08lx-%08lx %s %08lx %02x:%02x %lu"
Linus Torvalds's avatar
Linus Torvalds committed
457 458 459
#define MAPS_LINE_MAX4	49 /* sum of 8  1  8  1 4 1 8 1 5 1 10 1 */

/* for systems with sizeof(void*) == 8: */
460
#define MAPS_LINE_FORMAT8	  "%016lx-%016lx %s %016lx %02x:%02x %lu"
Linus Torvalds's avatar
Linus Torvalds committed
461 462
#define MAPS_LINE_MAX8	73 /* sum of 16  1  16  1 4 1 16 1 5 1 10 1 */

Linus Torvalds's avatar
Linus Torvalds committed
463 464
#define MAPS_LINE_FORMAT	(sizeof(void*) == 4 ? MAPS_LINE_FORMAT4 : MAPS_LINE_FORMAT8)
#define MAPS_LINE_MAX	(sizeof(void*) == 4 ?  MAPS_LINE_MAX4 :  MAPS_LINE_MAX8)
Linus Torvalds's avatar
Linus Torvalds committed
465

Linus Torvalds's avatar
Linus Torvalds committed
466 467 468 469 470 471
static int proc_pid_maps_get_line (char *buf, struct vm_area_struct *map)
{
	/* produce the next line */
	char *line;
	char str[5];
	int flags;
472
	dev_t dev;
Linus Torvalds's avatar
Linus Torvalds committed
473 474 475 476 477 478 479 480 481 482 483
	unsigned long ino;
	int len;

	flags = map->vm_flags;

	str[0] = flags & VM_READ ? 'r' : '-';
	str[1] = flags & VM_WRITE ? 'w' : '-';
	str[2] = flags & VM_EXEC ? 'x' : '-';
	str[3] = flags & VM_MAYSHARE ? 's' : 'p';
	str[4] = 0;

484
	dev = 0;
Linus Torvalds's avatar
Linus Torvalds committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
	ino = 0;
	if (map->vm_file != NULL) {
		dev = map->vm_file->f_dentry->d_inode->i_dev;
		ino = map->vm_file->f_dentry->d_inode->i_ino;
		line = d_path(map->vm_file->f_dentry,
			      map->vm_file->f_vfsmnt,
			      buf, PAGE_SIZE);
		buf[PAGE_SIZE-1] = '\n';
		line -= MAPS_LINE_MAX;
		if(line < buf)
			line = buf;
	} else
		line = buf;

	len = sprintf(line,
		      MAPS_LINE_FORMAT,
		      map->vm_start, map->vm_end, str, map->vm_pgoff << PAGE_SHIFT,
502
		      MAJOR(dev), MINOR(dev), ino);
Linus Torvalds's avatar
Linus Torvalds committed
503 504 505 506 507 508 509 510 511 512 513

	if(map->vm_file) {
		int i;
		for(i = len; i < MAPS_LINE_MAX; i++)
			line[i] = ' ';
		len = buf + PAGE_SIZE - line;
		memmove(buf, line, len);
	} else
		line[len++] = '\n';
	return len;
}
Linus Torvalds's avatar
Linus Torvalds committed
514 515 516 517 518

ssize_t proc_pid_read_maps (struct task_struct *task, struct file * file, char * buf,
			  size_t count, loff_t *ppos)
{
	struct mm_struct *mm;
Linus Torvalds's avatar
Linus Torvalds committed
519 520
	struct vm_area_struct * map;
	char *tmp, *kbuf;
Linus Torvalds's avatar
Linus Torvalds committed
521
	long retval;
Linus Torvalds's avatar
Linus Torvalds committed
522
	int off, lineno, loff;
Linus Torvalds's avatar
Linus Torvalds committed
523

Linus Torvalds's avatar
Linus Torvalds committed
524 525 526 527 528 529 530
	/* reject calls with out of range parameters immediately */
	retval = 0;
	if (*ppos > LONG_MAX)
		goto out;
	if (count == 0)
		goto out;
	off = (long)*ppos;
Linus Torvalds's avatar
Linus Torvalds committed
531 532 533 534
	/*
	 * We might sleep getting the page, so get it first.
	 */
	retval = -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
535 536
	kbuf = (char*)__get_free_page(GFP_KERNEL);
	if (!kbuf)
Linus Torvalds's avatar
Linus Torvalds committed
537 538
		goto out;

Linus Torvalds's avatar
Linus Torvalds committed
539 540 541 542
	tmp = (char*)__get_free_page(GFP_KERNEL);
	if (!tmp)
		goto out_free1;

543 544
	mm = get_task_mm(task);
 
Linus Torvalds's avatar
Linus Torvalds committed
545
	retval = 0;
Linus Torvalds's avatar
Linus Torvalds committed
546
	if (!mm)
Linus Torvalds's avatar
Linus Torvalds committed
547
		goto out_free2;
Linus Torvalds's avatar
Linus Torvalds committed
548

Linus Torvalds's avatar
Linus Torvalds committed
549
	down_read(&mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
550 551 552 553 554 555
	map = mm->mmap;
	lineno = 0;
	loff = 0;
	if (count > PAGE_SIZE)
		count = PAGE_SIZE;
	while (map) {
Linus Torvalds's avatar
Linus Torvalds committed
556
		int len;
Linus Torvalds's avatar
Linus Torvalds committed
557 558 559
		if (off > PAGE_SIZE) {
			off -= PAGE_SIZE;
			goto next;
Linus Torvalds's avatar
Linus Torvalds committed
560
		}
Linus Torvalds's avatar
Linus Torvalds committed
561 562 563 564 565 566 567 568 569 570 571 572
		len = proc_pid_maps_get_line(tmp, map);
		len -= off;
		if (len > 0) {
			if (retval+len > count) {
				/* only partial line transfer possible */
				len = count - retval;
				/* save the offset where the next read
				 * must start */
				loff = len+off;
			}
			memcpy(kbuf+retval, tmp+off, len);
			retval += len;
Linus Torvalds's avatar
Linus Torvalds committed
573
		}
Linus Torvalds's avatar
Linus Torvalds committed
574 575 576 577 578
		off = 0;
next:
		if (!loff)
			lineno++;
		if (retval >= count)
Linus Torvalds's avatar
Linus Torvalds committed
579
			break;
Linus Torvalds's avatar
Linus Torvalds committed
580 581
		if (loff) BUG();
		map = map->vm_next;
Linus Torvalds's avatar
Linus Torvalds committed
582
	}
Linus Torvalds's avatar
Linus Torvalds committed
583
	up_read(&mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
584 585
	mmput(mm);

Linus Torvalds's avatar
Linus Torvalds committed
586 587 588 589 590 591 592 593 594 595
	if (retval > count) BUG();
	if (copy_to_user(buf, kbuf, retval))
		retval = -EFAULT;
	else
		*ppos = (lineno << PAGE_SHIFT) + loff;

out_free2:
	free_page((unsigned long)tmp);
out_free1:
	free_page((unsigned long)kbuf);
Linus Torvalds's avatar
Linus Torvalds committed
596 597 598 599 600 601 602 603 604 605 606
out:
	return retval;
}

#ifdef CONFIG_SMP
int proc_pid_cpu(struct task_struct *task, char * buffer)
{
	int i, len;

	len = sprintf(buffer,
		"cpu  %lu %lu\n",
607 608
		jiffies_to_clock_t(task->utime),
		jiffies_to_clock_t(task->stime));
Linus Torvalds's avatar
Linus Torvalds committed
609
		
610 611
	for (i = 0 ; i < NR_CPUS; i++) {
		if (cpu_online(i))
Linus Torvalds's avatar
Linus Torvalds committed
612 613
		len += sprintf(buffer + len, "cpu%d %lu %lu\n",
			i,
614 615
			jiffies_to_clock_t(task->per_cpu_utime[i]),
			jiffies_to_clock_t(task->per_cpu_stime[i]));
Linus Torvalds's avatar
Linus Torvalds committed
616

617
	}
Linus Torvalds's avatar
Linus Torvalds committed
618 619 620
	return len;
}
#endif