1 /* 2 * linux/fs/proc/base.c 3 * 4 * Copyright (C) 1991, 1992 Linus Torvalds 5 * 6 * proc base directory handling functions 7 * 8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part. 9 * Instead of using magical inumbers to determine the kind of object 10 * we allocate and fill in-core inodes upon lookup. They don't even 11 * go into icache. We cache the reference to task_struct upon lookup too. 12 * Eventually it should become a filesystem in its own. We don't use the 13 * rest of procfs anymore. 14 * 15 * 16 * Changelog: 17 * 17-Jan-2005 18 * Allan Bezerra 19 * Bruna Moreira <bruna.moreira@indt.org.br> 20 * Edjard Mota <edjard.mota@indt.org.br> 21 * Ilias Biris <ilias.biris@indt.org.br> 22 * Mauricio Lin <mauricio.lin@indt.org.br> 23 * 24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 25 * 26 * A new process specific entry (smaps) included in /proc. It shows the 27 * size of rss for each memory area. The maps entry lacks information 28 * about physical memory size (rss) for each mapped file, i.e., 29 * rss information for executables and library files. 30 * This additional information is useful for any tools that need to know 31 * about physical memory consumption for a process specific library. 32 * 33 * Changelog: 34 * 21-Feb-2005 35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 36 * Pud inclusion in the page table walking. 37 * 38 * ChangeLog: 39 * 10-Mar-2005 40 * 10LE Instituto Nokia de Tecnologia - INdT: 41 * A better way to walks through the page table as suggested by Hugh Dickins. 42 * 43 * Simo Piiroinen <simo.piiroinen@nokia.com>: 44 * Smaps information related to shared, private, clean and dirty pages. 45 * 46 * Paul Mundt <paul.mundt@nokia.com>: 47 * Overall revision about smaps. 48 */ 49 50 #include <asm/uaccess.h> 51 52 #include <linux/errno.h> 53 #include <linux/time.h> 54 #include <linux/proc_fs.h> 55 #include <linux/stat.h> 56 #include <linux/init.h> 57 #include <linux/capability.h> 58 #include <linux/file.h> 59 #include <linux/string.h> 60 #include <linux/seq_file.h> 61 #include <linux/namei.h> 62 #include <linux/mnt_namespace.h> 63 #include <linux/mm.h> 64 #include <linux/rcupdate.h> 65 #include <linux/kallsyms.h> 66 #include <linux/resource.h> 67 #include <linux/module.h> 68 #include <linux/mount.h> 69 #include <linux/security.h> 70 #include <linux/ptrace.h> 71 #include <linux/cgroup.h> 72 #include <linux/cpuset.h> 73 #include <linux/audit.h> 74 #include <linux/poll.h> 75 #include <linux/nsproxy.h> 76 #include <linux/oom.h> 77 #include <linux/elf.h> 78 #include <linux/pid_namespace.h> 79 #include "internal.h" 80 81 /* NOTE: 82 * Implementing inode permission operations in /proc is almost 83 * certainly an error. Permission checks need to happen during 84 * each system call not at open time. The reason is that most of 85 * what we wish to check for permissions in /proc varies at runtime. 86 * 87 * The classic example of a problem is opening file descriptors 88 * in /proc for a task before it execs a suid executable. 89 */ 90 91 struct pid_entry { 92 char *name; 93 int len; 94 mode_t mode; 95 const struct inode_operations *iop; 96 const struct file_operations *fop; 97 union proc_op op; 98 }; 99 100 #define NOD(NAME, MODE, IOP, FOP, OP) { \ 101 .name = (NAME), \ 102 .len = sizeof(NAME) - 1, \ 103 .mode = MODE, \ 104 .iop = IOP, \ 105 .fop = FOP, \ 106 .op = OP, \ 107 } 108 109 #define DIR(NAME, MODE, OTYPE) \ 110 NOD(NAME, (S_IFDIR|(MODE)), \ 111 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \ 112 {} ) 113 #define LNK(NAME, OTYPE) \ 114 NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 115 &proc_pid_link_inode_operations, NULL, \ 116 { .proc_get_link = &proc_##OTYPE##_link } ) 117 #define REG(NAME, MODE, OTYPE) \ 118 NOD(NAME, (S_IFREG|(MODE)), NULL, \ 119 &proc_##OTYPE##_operations, {}) 120 #define INF(NAME, MODE, OTYPE) \ 121 NOD(NAME, (S_IFREG|(MODE)), \ 122 NULL, &proc_info_file_operations, \ 123 { .proc_read = &proc_##OTYPE } ) 124 #define ONE(NAME, MODE, OTYPE) \ 125 NOD(NAME, (S_IFREG|(MODE)), \ 126 NULL, &proc_single_file_operations, \ 127 { .proc_show = &proc_##OTYPE } ) 128 129 int maps_protect; 130 EXPORT_SYMBOL(maps_protect); 131 132 static struct fs_struct *get_fs_struct(struct task_struct *task) 133 { 134 struct fs_struct *fs; 135 task_lock(task); 136 fs = task->fs; 137 if(fs) 138 atomic_inc(&fs->count); 139 task_unlock(task); 140 return fs; 141 } 142 143 static int get_nr_threads(struct task_struct *tsk) 144 { 145 /* Must be called with the rcu_read_lock held */ 146 unsigned long flags; 147 int count = 0; 148 149 if (lock_task_sighand(tsk, &flags)) { 150 count = atomic_read(&tsk->signal->count); 151 unlock_task_sighand(tsk, &flags); 152 } 153 return count; 154 } 155 156 static int proc_cwd_link(struct inode *inode, struct path *path) 157 { 158 struct task_struct *task = get_proc_task(inode); 159 struct fs_struct *fs = NULL; 160 int result = -ENOENT; 161 162 if (task) { 163 fs = get_fs_struct(task); 164 put_task_struct(task); 165 } 166 if (fs) { 167 read_lock(&fs->lock); 168 *path = fs->pwd; 169 path_get(&fs->pwd); 170 read_unlock(&fs->lock); 171 result = 0; 172 put_fs_struct(fs); 173 } 174 return result; 175 } 176 177 static int proc_root_link(struct inode *inode, struct path *path) 178 { 179 struct task_struct *task = get_proc_task(inode); 180 struct fs_struct *fs = NULL; 181 int result = -ENOENT; 182 183 if (task) { 184 fs = get_fs_struct(task); 185 put_task_struct(task); 186 } 187 if (fs) { 188 read_lock(&fs->lock); 189 *path = fs->root; 190 path_get(&fs->root); 191 read_unlock(&fs->lock); 192 result = 0; 193 put_fs_struct(fs); 194 } 195 return result; 196 } 197 198 #define MAY_PTRACE(task) \ 199 (task == current || \ 200 (task->parent == current && \ 201 (task->ptrace & PT_PTRACED) && \ 202 (task_is_stopped_or_traced(task)) && \ 203 security_ptrace(current,task) == 0)) 204 205 struct mm_struct *mm_for_maps(struct task_struct *task) 206 { 207 struct mm_struct *mm = get_task_mm(task); 208 if (!mm) 209 return NULL; 210 down_read(&mm->mmap_sem); 211 task_lock(task); 212 if (task->mm != mm) 213 goto out; 214 if (task->mm != current->mm && __ptrace_may_attach(task) < 0) 215 goto out; 216 task_unlock(task); 217 return mm; 218 out: 219 task_unlock(task); 220 up_read(&mm->mmap_sem); 221 mmput(mm); 222 return NULL; 223 } 224 225 static int proc_pid_cmdline(struct task_struct *task, char * buffer) 226 { 227 int res = 0; 228 unsigned int len; 229 struct mm_struct *mm = get_task_mm(task); 230 if (!mm) 231 goto out; 232 if (!mm->arg_end) 233 goto out_mm; /* Shh! No looking before we're done */ 234 235 len = mm->arg_end - mm->arg_start; 236 237 if (len > PAGE_SIZE) 238 len = PAGE_SIZE; 239 240 res = access_process_vm(task, mm->arg_start, buffer, len, 0); 241 242 // If the nul at the end of args has been overwritten, then 243 // assume application is using setproctitle(3). 244 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 245 len = strnlen(buffer, res); 246 if (len < res) { 247 res = len; 248 } else { 249 len = mm->env_end - mm->env_start; 250 if (len > PAGE_SIZE - res) 251 len = PAGE_SIZE - res; 252 res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 253 res = strnlen(buffer, res); 254 } 255 } 256 out_mm: 257 mmput(mm); 258 out: 259 return res; 260 } 261 262 static int proc_pid_auxv(struct task_struct *task, char *buffer) 263 { 264 int res = 0; 265 struct mm_struct *mm = get_task_mm(task); 266 if (mm) { 267 unsigned int nwords = 0; 268 do 269 nwords += 2; 270 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 271 res = nwords * sizeof(mm->saved_auxv[0]); 272 if (res > PAGE_SIZE) 273 res = PAGE_SIZE; 274 memcpy(buffer, mm->saved_auxv, res); 275 mmput(mm); 276 } 277 return res; 278 } 279 280 281 #ifdef CONFIG_KALLSYMS 282 /* 283 * Provides a wchan file via kallsyms in a proper one-value-per-file format. 284 * Returns the resolved symbol. If that fails, simply return the address. 285 */ 286 static int proc_pid_wchan(struct task_struct *task, char *buffer) 287 { 288 unsigned long wchan; 289 char symname[KSYM_NAME_LEN]; 290 291 wchan = get_wchan(task); 292 293 if (lookup_symbol_name(wchan, symname) < 0) 294 return sprintf(buffer, "%lu", wchan); 295 else 296 return sprintf(buffer, "%s", symname); 297 } 298 #endif /* CONFIG_KALLSYMS */ 299 300 #ifdef CONFIG_SCHEDSTATS 301 /* 302 * Provides /proc/PID/schedstat 303 */ 304 static int proc_pid_schedstat(struct task_struct *task, char *buffer) 305 { 306 return sprintf(buffer, "%llu %llu %lu\n", 307 task->sched_info.cpu_time, 308 task->sched_info.run_delay, 309 task->sched_info.pcount); 310 } 311 #endif 312 313 #ifdef CONFIG_LATENCYTOP 314 static int lstats_show_proc(struct seq_file *m, void *v) 315 { 316 int i; 317 struct task_struct *task = m->private; 318 seq_puts(m, "Latency Top version : v0.1\n"); 319 320 for (i = 0; i < 32; i++) { 321 if (task->latency_record[i].backtrace[0]) { 322 int q; 323 seq_printf(m, "%i %li %li ", 324 task->latency_record[i].count, 325 task->latency_record[i].time, 326 task->latency_record[i].max); 327 for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 328 char sym[KSYM_NAME_LEN]; 329 char *c; 330 if (!task->latency_record[i].backtrace[q]) 331 break; 332 if (task->latency_record[i].backtrace[q] == ULONG_MAX) 333 break; 334 sprint_symbol(sym, task->latency_record[i].backtrace[q]); 335 c = strchr(sym, '+'); 336 if (c) 337 *c = 0; 338 seq_printf(m, "%s ", sym); 339 } 340 seq_printf(m, "\n"); 341 } 342 343 } 344 return 0; 345 } 346 347 static int lstats_open(struct inode *inode, struct file *file) 348 { 349 int ret; 350 struct seq_file *m; 351 struct task_struct *task = get_proc_task(inode); 352 353 ret = single_open(file, lstats_show_proc, NULL); 354 if (!ret) { 355 m = file->private_data; 356 m->private = task; 357 } 358 return ret; 359 } 360 361 static ssize_t lstats_write(struct file *file, const char __user *buf, 362 size_t count, loff_t *offs) 363 { 364 struct seq_file *m; 365 struct task_struct *task; 366 367 m = file->private_data; 368 task = m->private; 369 clear_all_latency_tracing(task); 370 371 return count; 372 } 373 374 static const struct file_operations proc_lstats_operations = { 375 .open = lstats_open, 376 .read = seq_read, 377 .write = lstats_write, 378 .llseek = seq_lseek, 379 .release = single_release, 380 }; 381 382 #endif 383 384 /* The badness from the OOM killer */ 385 unsigned long badness(struct task_struct *p, unsigned long uptime); 386 static int proc_oom_score(struct task_struct *task, char *buffer) 387 { 388 unsigned long points; 389 struct timespec uptime; 390 391 do_posix_clock_monotonic_gettime(&uptime); 392 read_lock(&tasklist_lock); 393 points = badness(task, uptime.tv_sec); 394 read_unlock(&tasklist_lock); 395 return sprintf(buffer, "%lu\n", points); 396 } 397 398 struct limit_names { 399 char *name; 400 char *unit; 401 }; 402 403 static const struct limit_names lnames[RLIM_NLIMITS] = { 404 [RLIMIT_CPU] = {"Max cpu time", "ms"}, 405 [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 406 [RLIMIT_DATA] = {"Max data size", "bytes"}, 407 [RLIMIT_STACK] = {"Max stack size", "bytes"}, 408 [RLIMIT_CORE] = {"Max core file size", "bytes"}, 409 [RLIMIT_RSS] = {"Max resident set", "bytes"}, 410 [RLIMIT_NPROC] = {"Max processes", "processes"}, 411 [RLIMIT_NOFILE] = {"Max open files", "files"}, 412 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 413 [RLIMIT_AS] = {"Max address space", "bytes"}, 414 [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 415 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 416 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 417 [RLIMIT_NICE] = {"Max nice priority", NULL}, 418 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 419 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 420 }; 421 422 /* Display limits for a process */ 423 static int proc_pid_limits(struct task_struct *task, char *buffer) 424 { 425 unsigned int i; 426 int count = 0; 427 unsigned long flags; 428 char *bufptr = buffer; 429 430 struct rlimit rlim[RLIM_NLIMITS]; 431 432 rcu_read_lock(); 433 if (!lock_task_sighand(task,&flags)) { 434 rcu_read_unlock(); 435 return 0; 436 } 437 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 438 unlock_task_sighand(task, &flags); 439 rcu_read_unlock(); 440 441 /* 442 * print the file header 443 */ 444 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n", 445 "Limit", "Soft Limit", "Hard Limit", "Units"); 446 447 for (i = 0; i < RLIM_NLIMITS; i++) { 448 if (rlim[i].rlim_cur == RLIM_INFINITY) 449 count += sprintf(&bufptr[count], "%-25s %-20s ", 450 lnames[i].name, "unlimited"); 451 else 452 count += sprintf(&bufptr[count], "%-25s %-20lu ", 453 lnames[i].name, rlim[i].rlim_cur); 454 455 if (rlim[i].rlim_max == RLIM_INFINITY) 456 count += sprintf(&bufptr[count], "%-20s ", "unlimited"); 457 else 458 count += sprintf(&bufptr[count], "%-20lu ", 459 rlim[i].rlim_max); 460 461 if (lnames[i].unit) 462 count += sprintf(&bufptr[count], "%-10s\n", 463 lnames[i].unit); 464 else 465 count += sprintf(&bufptr[count], "\n"); 466 } 467 468 return count; 469 } 470 471 /************************************************************************/ 472 /* Here the fs part begins */ 473 /************************************************************************/ 474 475 /* permission checks */ 476 static int proc_fd_access_allowed(struct inode *inode) 477 { 478 struct task_struct *task; 479 int allowed = 0; 480 /* Allow access to a task's file descriptors if it is us or we 481 * may use ptrace attach to the process and find out that 482 * information. 483 */ 484 task = get_proc_task(inode); 485 if (task) { 486 allowed = ptrace_may_attach(task); 487 put_task_struct(task); 488 } 489 return allowed; 490 } 491 492 static int proc_setattr(struct dentry *dentry, struct iattr *attr) 493 { 494 int error; 495 struct inode *inode = dentry->d_inode; 496 497 if (attr->ia_valid & ATTR_MODE) 498 return -EPERM; 499 500 error = inode_change_ok(inode, attr); 501 if (!error) 502 error = inode_setattr(inode, attr); 503 return error; 504 } 505 506 static const struct inode_operations proc_def_inode_operations = { 507 .setattr = proc_setattr, 508 }; 509 510 extern const struct seq_operations mounts_op; 511 struct proc_mounts { 512 struct seq_file m; 513 int event; 514 }; 515 516 static int mounts_open(struct inode *inode, struct file *file) 517 { 518 struct task_struct *task = get_proc_task(inode); 519 struct nsproxy *nsp; 520 struct mnt_namespace *ns = NULL; 521 struct proc_mounts *p; 522 int ret = -EINVAL; 523 524 if (task) { 525 rcu_read_lock(); 526 nsp = task_nsproxy(task); 527 if (nsp) { 528 ns = nsp->mnt_ns; 529 if (ns) 530 get_mnt_ns(ns); 531 } 532 rcu_read_unlock(); 533 534 put_task_struct(task); 535 } 536 537 if (ns) { 538 ret = -ENOMEM; 539 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 540 if (p) { 541 file->private_data = &p->m; 542 ret = seq_open(file, &mounts_op); 543 if (!ret) { 544 p->m.private = ns; 545 p->event = ns->event; 546 return 0; 547 } 548 kfree(p); 549 } 550 put_mnt_ns(ns); 551 } 552 return ret; 553 } 554 555 static int mounts_release(struct inode *inode, struct file *file) 556 { 557 struct seq_file *m = file->private_data; 558 struct mnt_namespace *ns = m->private; 559 put_mnt_ns(ns); 560 return seq_release(inode, file); 561 } 562 563 static unsigned mounts_poll(struct file *file, poll_table *wait) 564 { 565 struct proc_mounts *p = file->private_data; 566 struct mnt_namespace *ns = p->m.private; 567 unsigned res = 0; 568 569 poll_wait(file, &ns->poll, wait); 570 571 spin_lock(&vfsmount_lock); 572 if (p->event != ns->event) { 573 p->event = ns->event; 574 res = POLLERR; 575 } 576 spin_unlock(&vfsmount_lock); 577 578 return res; 579 } 580 581 static const struct file_operations proc_mounts_operations = { 582 .open = mounts_open, 583 .read = seq_read, 584 .llseek = seq_lseek, 585 .release = mounts_release, 586 .poll = mounts_poll, 587 }; 588 589 extern const struct seq_operations mountstats_op; 590 static int mountstats_open(struct inode *inode, struct file *file) 591 { 592 int ret = seq_open(file, &mountstats_op); 593 594 if (!ret) { 595 struct seq_file *m = file->private_data; 596 struct nsproxy *nsp; 597 struct mnt_namespace *mnt_ns = NULL; 598 struct task_struct *task = get_proc_task(inode); 599 600 if (task) { 601 rcu_read_lock(); 602 nsp = task_nsproxy(task); 603 if (nsp) { 604 mnt_ns = nsp->mnt_ns; 605 if (mnt_ns) 606 get_mnt_ns(mnt_ns); 607 } 608 rcu_read_unlock(); 609 610 put_task_struct(task); 611 } 612 613 if (mnt_ns) 614 m->private = mnt_ns; 615 else { 616 seq_release(inode, file); 617 ret = -EINVAL; 618 } 619 } 620 return ret; 621 } 622 623 static const struct file_operations proc_mountstats_operations = { 624 .open = mountstats_open, 625 .read = seq_read, 626 .llseek = seq_lseek, 627 .release = mounts_release, 628 }; 629 630 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 631 632 static ssize_t proc_info_read(struct file * file, char __user * buf, 633 size_t count, loff_t *ppos) 634 { 635 struct inode * inode = file->f_path.dentry->d_inode; 636 unsigned long page; 637 ssize_t length; 638 struct task_struct *task = get_proc_task(inode); 639 640 length = -ESRCH; 641 if (!task) 642 goto out_no_task; 643 644 if (count > PROC_BLOCK_SIZE) 645 count = PROC_BLOCK_SIZE; 646 647 length = -ENOMEM; 648 if (!(page = __get_free_page(GFP_TEMPORARY))) 649 goto out; 650 651 length = PROC_I(inode)->op.proc_read(task, (char*)page); 652 653 if (length >= 0) 654 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 655 free_page(page); 656 out: 657 put_task_struct(task); 658 out_no_task: 659 return length; 660 } 661 662 static const struct file_operations proc_info_file_operations = { 663 .read = proc_info_read, 664 }; 665 666 static int proc_single_show(struct seq_file *m, void *v) 667 { 668 struct inode *inode = m->private; 669 struct pid_namespace *ns; 670 struct pid *pid; 671 struct task_struct *task; 672 int ret; 673 674 ns = inode->i_sb->s_fs_info; 675 pid = proc_pid(inode); 676 task = get_pid_task(pid, PIDTYPE_PID); 677 if (!task) 678 return -ESRCH; 679 680 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 681 682 put_task_struct(task); 683 return ret; 684 } 685 686 static int proc_single_open(struct inode *inode, struct file *filp) 687 { 688 int ret; 689 ret = single_open(filp, proc_single_show, NULL); 690 if (!ret) { 691 struct seq_file *m = filp->private_data; 692 693 m->private = inode; 694 } 695 return ret; 696 } 697 698 static const struct file_operations proc_single_file_operations = { 699 .open = proc_single_open, 700 .read = seq_read, 701 .llseek = seq_lseek, 702 .release = single_release, 703 }; 704 705 static int mem_open(struct inode* inode, struct file* file) 706 { 707 file->private_data = (void*)((long)current->self_exec_id); 708 return 0; 709 } 710 711 static ssize_t mem_read(struct file * file, char __user * buf, 712 size_t count, loff_t *ppos) 713 { 714 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 715 char *page; 716 unsigned long src = *ppos; 717 int ret = -ESRCH; 718 struct mm_struct *mm; 719 720 if (!task) 721 goto out_no_task; 722 723 if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 724 goto out; 725 726 ret = -ENOMEM; 727 page = (char *)__get_free_page(GFP_TEMPORARY); 728 if (!page) 729 goto out; 730 731 ret = 0; 732 733 mm = get_task_mm(task); 734 if (!mm) 735 goto out_free; 736 737 ret = -EIO; 738 739 if (file->private_data != (void*)((long)current->self_exec_id)) 740 goto out_put; 741 742 ret = 0; 743 744 while (count > 0) { 745 int this_len, retval; 746 747 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 748 retval = access_process_vm(task, src, page, this_len, 0); 749 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) { 750 if (!ret) 751 ret = -EIO; 752 break; 753 } 754 755 if (copy_to_user(buf, page, retval)) { 756 ret = -EFAULT; 757 break; 758 } 759 760 ret += retval; 761 src += retval; 762 buf += retval; 763 count -= retval; 764 } 765 *ppos = src; 766 767 out_put: 768 mmput(mm); 769 out_free: 770 free_page((unsigned long) page); 771 out: 772 put_task_struct(task); 773 out_no_task: 774 return ret; 775 } 776 777 #define mem_write NULL 778 779 #ifndef mem_write 780 /* This is a security hazard */ 781 static ssize_t mem_write(struct file * file, const char __user *buf, 782 size_t count, loff_t *ppos) 783 { 784 int copied; 785 char *page; 786 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 787 unsigned long dst = *ppos; 788 789 copied = -ESRCH; 790 if (!task) 791 goto out_no_task; 792 793 if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 794 goto out; 795 796 copied = -ENOMEM; 797 page = (char *)__get_free_page(GFP_TEMPORARY); 798 if (!page) 799 goto out; 800 801 copied = 0; 802 while (count > 0) { 803 int this_len, retval; 804 805 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 806 if (copy_from_user(page, buf, this_len)) { 807 copied = -EFAULT; 808 break; 809 } 810 retval = access_process_vm(task, dst, page, this_len, 1); 811 if (!retval) { 812 if (!copied) 813 copied = -EIO; 814 break; 815 } 816 copied += retval; 817 buf += retval; 818 dst += retval; 819 count -= retval; 820 } 821 *ppos = dst; 822 free_page((unsigned long) page); 823 out: 824 put_task_struct(task); 825 out_no_task: 826 return copied; 827 } 828 #endif 829 830 loff_t mem_lseek(struct file *file, loff_t offset, int orig) 831 { 832 switch (orig) { 833 case 0: 834 file->f_pos = offset; 835 break; 836 case 1: 837 file->f_pos += offset; 838 break; 839 default: 840 return -EINVAL; 841 } 842 force_successful_syscall_return(); 843 return file->f_pos; 844 } 845 846 static const struct file_operations proc_mem_operations = { 847 .llseek = mem_lseek, 848 .read = mem_read, 849 .write = mem_write, 850 .open = mem_open, 851 }; 852 853 static ssize_t environ_read(struct file *file, char __user *buf, 854 size_t count, loff_t *ppos) 855 { 856 struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 857 char *page; 858 unsigned long src = *ppos; 859 int ret = -ESRCH; 860 struct mm_struct *mm; 861 862 if (!task) 863 goto out_no_task; 864 865 if (!ptrace_may_attach(task)) 866 goto out; 867 868 ret = -ENOMEM; 869 page = (char *)__get_free_page(GFP_TEMPORARY); 870 if (!page) 871 goto out; 872 873 ret = 0; 874 875 mm = get_task_mm(task); 876 if (!mm) 877 goto out_free; 878 879 while (count > 0) { 880 int this_len, retval, max_len; 881 882 this_len = mm->env_end - (mm->env_start + src); 883 884 if (this_len <= 0) 885 break; 886 887 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 888 this_len = (this_len > max_len) ? max_len : this_len; 889 890 retval = access_process_vm(task, (mm->env_start + src), 891 page, this_len, 0); 892 893 if (retval <= 0) { 894 ret = retval; 895 break; 896 } 897 898 if (copy_to_user(buf, page, retval)) { 899 ret = -EFAULT; 900 break; 901 } 902 903 ret += retval; 904 src += retval; 905 buf += retval; 906 count -= retval; 907 } 908 *ppos = src; 909 910 mmput(mm); 911 out_free: 912 free_page((unsigned long) page); 913 out: 914 put_task_struct(task); 915 out_no_task: 916 return ret; 917 } 918 919 static const struct file_operations proc_environ_operations = { 920 .read = environ_read, 921 }; 922 923 static ssize_t oom_adjust_read(struct file *file, char __user *buf, 924 size_t count, loff_t *ppos) 925 { 926 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 927 char buffer[PROC_NUMBUF]; 928 size_t len; 929 int oom_adjust; 930 931 if (!task) 932 return -ESRCH; 933 oom_adjust = task->oomkilladj; 934 put_task_struct(task); 935 936 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 937 938 return simple_read_from_buffer(buf, count, ppos, buffer, len); 939 } 940 941 static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 942 size_t count, loff_t *ppos) 943 { 944 struct task_struct *task; 945 char buffer[PROC_NUMBUF], *end; 946 int oom_adjust; 947 948 memset(buffer, 0, sizeof(buffer)); 949 if (count > sizeof(buffer) - 1) 950 count = sizeof(buffer) - 1; 951 if (copy_from_user(buffer, buf, count)) 952 return -EFAULT; 953 oom_adjust = simple_strtol(buffer, &end, 0); 954 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 955 oom_adjust != OOM_DISABLE) 956 return -EINVAL; 957 if (*end == '\n') 958 end++; 959 task = get_proc_task(file->f_path.dentry->d_inode); 960 if (!task) 961 return -ESRCH; 962 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) { 963 put_task_struct(task); 964 return -EACCES; 965 } 966 task->oomkilladj = oom_adjust; 967 put_task_struct(task); 968 if (end - buffer == 0) 969 return -EIO; 970 return end - buffer; 971 } 972 973 static const struct file_operations proc_oom_adjust_operations = { 974 .read = oom_adjust_read, 975 .write = oom_adjust_write, 976 }; 977 978 #ifdef CONFIG_AUDITSYSCALL 979 #define TMPBUFLEN 21 980 static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 981 size_t count, loff_t *ppos) 982 { 983 struct inode * inode = file->f_path.dentry->d_inode; 984 struct task_struct *task = get_proc_task(inode); 985 ssize_t length; 986 char tmpbuf[TMPBUFLEN]; 987 988 if (!task) 989 return -ESRCH; 990 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 991 audit_get_loginuid(task)); 992 put_task_struct(task); 993 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 994 } 995 996 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 997 size_t count, loff_t *ppos) 998 { 999 struct inode * inode = file->f_path.dentry->d_inode; 1000 char *page, *tmp; 1001 ssize_t length; 1002 uid_t loginuid; 1003 1004 if (!capable(CAP_AUDIT_CONTROL)) 1005 return -EPERM; 1006 1007 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 1008 return -EPERM; 1009 1010 if (count >= PAGE_SIZE) 1011 count = PAGE_SIZE - 1; 1012 1013 if (*ppos != 0) { 1014 /* No partial writes. */ 1015 return -EINVAL; 1016 } 1017 page = (char*)__get_free_page(GFP_TEMPORARY); 1018 if (!page) 1019 return -ENOMEM; 1020 length = -EFAULT; 1021 if (copy_from_user(page, buf, count)) 1022 goto out_free_page; 1023 1024 page[count] = '\0'; 1025 loginuid = simple_strtoul(page, &tmp, 10); 1026 if (tmp == page) { 1027 length = -EINVAL; 1028 goto out_free_page; 1029 1030 } 1031 length = audit_set_loginuid(current, loginuid); 1032 if (likely(length == 0)) 1033 length = count; 1034 1035 out_free_page: 1036 free_page((unsigned long) page); 1037 return length; 1038 } 1039 1040 static const struct file_operations proc_loginuid_operations = { 1041 .read = proc_loginuid_read, 1042 .write = proc_loginuid_write, 1043 }; 1044 #endif 1045 1046 #ifdef CONFIG_FAULT_INJECTION 1047 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1048 size_t count, loff_t *ppos) 1049 { 1050 struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 1051 char buffer[PROC_NUMBUF]; 1052 size_t len; 1053 int make_it_fail; 1054 1055 if (!task) 1056 return -ESRCH; 1057 make_it_fail = task->make_it_fail; 1058 put_task_struct(task); 1059 1060 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 1061 1062 return simple_read_from_buffer(buf, count, ppos, buffer, len); 1063 } 1064 1065 static ssize_t proc_fault_inject_write(struct file * file, 1066 const char __user * buf, size_t count, loff_t *ppos) 1067 { 1068 struct task_struct *task; 1069 char buffer[PROC_NUMBUF], *end; 1070 int make_it_fail; 1071 1072 if (!capable(CAP_SYS_RESOURCE)) 1073 return -EPERM; 1074 memset(buffer, 0, sizeof(buffer)); 1075 if (count > sizeof(buffer) - 1) 1076 count = sizeof(buffer) - 1; 1077 if (copy_from_user(buffer, buf, count)) 1078 return -EFAULT; 1079 make_it_fail = simple_strtol(buffer, &end, 0); 1080 if (*end == '\n') 1081 end++; 1082 task = get_proc_task(file->f_dentry->d_inode); 1083 if (!task) 1084 return -ESRCH; 1085 task->make_it_fail = make_it_fail; 1086 put_task_struct(task); 1087 if (end - buffer == 0) 1088 return -EIO; 1089 return end - buffer; 1090 } 1091 1092 static const struct file_operations proc_fault_inject_operations = { 1093 .read = proc_fault_inject_read, 1094 .write = proc_fault_inject_write, 1095 }; 1096 #endif 1097 1098 1099 #ifdef CONFIG_SCHED_DEBUG 1100 /* 1101 * Print out various scheduling related per-task fields: 1102 */ 1103 static int sched_show(struct seq_file *m, void *v) 1104 { 1105 struct inode *inode = m->private; 1106 struct task_struct *p; 1107 1108 WARN_ON(!inode); 1109 1110 p = get_proc_task(inode); 1111 if (!p) 1112 return -ESRCH; 1113 proc_sched_show_task(p, m); 1114 1115 put_task_struct(p); 1116 1117 return 0; 1118 } 1119 1120 static ssize_t 1121 sched_write(struct file *file, const char __user *buf, 1122 size_t count, loff_t *offset) 1123 { 1124 struct inode *inode = file->f_path.dentry->d_inode; 1125 struct task_struct *p; 1126 1127 WARN_ON(!inode); 1128 1129 p = get_proc_task(inode); 1130 if (!p) 1131 return -ESRCH; 1132 proc_sched_set_task(p); 1133 1134 put_task_struct(p); 1135 1136 return count; 1137 } 1138 1139 static int sched_open(struct inode *inode, struct file *filp) 1140 { 1141 int ret; 1142 1143 ret = single_open(filp, sched_show, NULL); 1144 if (!ret) { 1145 struct seq_file *m = filp->private_data; 1146 1147 m->private = inode; 1148 } 1149 return ret; 1150 } 1151 1152 static const struct file_operations proc_pid_sched_operations = { 1153 .open = sched_open, 1154 .read = seq_read, 1155 .write = sched_write, 1156 .llseek = seq_lseek, 1157 .release = single_release, 1158 }; 1159 1160 #endif 1161 1162 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 1163 { 1164 struct inode *inode = dentry->d_inode; 1165 int error = -EACCES; 1166 1167 /* We don't need a base pointer in the /proc filesystem */ 1168 path_put(&nd->path); 1169 1170 /* Are we allowed to snoop on the tasks file descriptors? */ 1171 if (!proc_fd_access_allowed(inode)) 1172 goto out; 1173 1174 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path); 1175 nd->last_type = LAST_BIND; 1176 out: 1177 return ERR_PTR(error); 1178 } 1179 1180 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 1181 { 1182 char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 1183 char *pathname; 1184 int len; 1185 1186 if (!tmp) 1187 return -ENOMEM; 1188 1189 pathname = d_path(path, tmp, PAGE_SIZE); 1190 len = PTR_ERR(pathname); 1191 if (IS_ERR(pathname)) 1192 goto out; 1193 len = tmp + PAGE_SIZE - 1 - pathname; 1194 1195 if (len > buflen) 1196 len = buflen; 1197 if (copy_to_user(buffer, pathname, len)) 1198 len = -EFAULT; 1199 out: 1200 free_page((unsigned long)tmp); 1201 return len; 1202 } 1203 1204 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 1205 { 1206 int error = -EACCES; 1207 struct inode *inode = dentry->d_inode; 1208 struct path path; 1209 1210 /* Are we allowed to snoop on the tasks file descriptors? */ 1211 if (!proc_fd_access_allowed(inode)) 1212 goto out; 1213 1214 error = PROC_I(inode)->op.proc_get_link(inode, &path); 1215 if (error) 1216 goto out; 1217 1218 error = do_proc_readlink(&path, buffer, buflen); 1219 path_put(&path); 1220 out: 1221 return error; 1222 } 1223 1224 static const struct inode_operations proc_pid_link_inode_operations = { 1225 .readlink = proc_pid_readlink, 1226 .follow_link = proc_pid_follow_link, 1227 .setattr = proc_setattr, 1228 }; 1229 1230 1231 /* building an inode */ 1232 1233 static int task_dumpable(struct task_struct *task) 1234 { 1235 int dumpable = 0; 1236 struct mm_struct *mm; 1237 1238 task_lock(task); 1239 mm = task->mm; 1240 if (mm) 1241 dumpable = get_dumpable(mm); 1242 task_unlock(task); 1243 if(dumpable == 1) 1244 return 1; 1245 return 0; 1246 } 1247 1248 1249 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 1250 { 1251 struct inode * inode; 1252 struct proc_inode *ei; 1253 1254 /* We need a new inode */ 1255 1256 inode = new_inode(sb); 1257 if (!inode) 1258 goto out; 1259 1260 /* Common stuff */ 1261 ei = PROC_I(inode); 1262 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 1263 inode->i_op = &proc_def_inode_operations; 1264 1265 /* 1266 * grab the reference to task. 1267 */ 1268 ei->pid = get_task_pid(task, PIDTYPE_PID); 1269 if (!ei->pid) 1270 goto out_unlock; 1271 1272 inode->i_uid = 0; 1273 inode->i_gid = 0; 1274 if (task_dumpable(task)) { 1275 inode->i_uid = task->euid; 1276 inode->i_gid = task->egid; 1277 } 1278 security_task_to_inode(task, inode); 1279 1280 out: 1281 return inode; 1282 1283 out_unlock: 1284 iput(inode); 1285 return NULL; 1286 } 1287 1288 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 1289 { 1290 struct inode *inode = dentry->d_inode; 1291 struct task_struct *task; 1292 generic_fillattr(inode, stat); 1293 1294 rcu_read_lock(); 1295 stat->uid = 0; 1296 stat->gid = 0; 1297 task = pid_task(proc_pid(inode), PIDTYPE_PID); 1298 if (task) { 1299 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 1300 task_dumpable(task)) { 1301 stat->uid = task->euid; 1302 stat->gid = task->egid; 1303 } 1304 } 1305 rcu_read_unlock(); 1306 return 0; 1307 } 1308 1309 /* dentry stuff */ 1310 1311 /* 1312 * Exceptional case: normally we are not allowed to unhash a busy 1313 * directory. In this case, however, we can do it - no aliasing problems 1314 * due to the way we treat inodes. 1315 * 1316 * Rewrite the inode's ownerships here because the owning task may have 1317 * performed a setuid(), etc. 1318 * 1319 * Before the /proc/pid/status file was created the only way to read 1320 * the effective uid of a /process was to stat /proc/pid. Reading 1321 * /proc/pid/status is slow enough that procps and other packages 1322 * kept stating /proc/pid. To keep the rules in /proc simple I have 1323 * made this apply to all per process world readable and executable 1324 * directories. 1325 */ 1326 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 1327 { 1328 struct inode *inode = dentry->d_inode; 1329 struct task_struct *task = get_proc_task(inode); 1330 if (task) { 1331 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 1332 task_dumpable(task)) { 1333 inode->i_uid = task->euid; 1334 inode->i_gid = task->egid; 1335 } else { 1336 inode->i_uid = 0; 1337 inode->i_gid = 0; 1338 } 1339 inode->i_mode &= ~(S_ISUID | S_ISGID); 1340 security_task_to_inode(task, inode); 1341 put_task_struct(task); 1342 return 1; 1343 } 1344 d_drop(dentry); 1345 return 0; 1346 } 1347 1348 static int pid_delete_dentry(struct dentry * dentry) 1349 { 1350 /* Is the task we represent dead? 1351 * If so, then don't put the dentry on the lru list, 1352 * kill it immediately. 1353 */ 1354 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 1355 } 1356 1357 static struct dentry_operations pid_dentry_operations = 1358 { 1359 .d_revalidate = pid_revalidate, 1360 .d_delete = pid_delete_dentry, 1361 }; 1362 1363 /* Lookups */ 1364 1365 typedef struct dentry *instantiate_t(struct inode *, struct dentry *, 1366 struct task_struct *, const void *); 1367 1368 /* 1369 * Fill a directory entry. 1370 * 1371 * If possible create the dcache entry and derive our inode number and 1372 * file type from dcache entry. 1373 * 1374 * Since all of the proc inode numbers are dynamically generated, the inode 1375 * numbers do not exist until the inode is cache. This means creating the 1376 * the dcache entry in readdir is necessary to keep the inode numbers 1377 * reported by readdir in sync with the inode numbers reported 1378 * by stat. 1379 */ 1380 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 1381 char *name, int len, 1382 instantiate_t instantiate, struct task_struct *task, const void *ptr) 1383 { 1384 struct dentry *child, *dir = filp->f_path.dentry; 1385 struct inode *inode; 1386 struct qstr qname; 1387 ino_t ino = 0; 1388 unsigned type = DT_UNKNOWN; 1389 1390 qname.name = name; 1391 qname.len = len; 1392 qname.hash = full_name_hash(name, len); 1393 1394 child = d_lookup(dir, &qname); 1395 if (!child) { 1396 struct dentry *new; 1397 new = d_alloc(dir, &qname); 1398 if (new) { 1399 child = instantiate(dir->d_inode, new, task, ptr); 1400 if (child) 1401 dput(new); 1402 else 1403 child = new; 1404 } 1405 } 1406 if (!child || IS_ERR(child) || !child->d_inode) 1407 goto end_instantiate; 1408 inode = child->d_inode; 1409 if (inode) { 1410 ino = inode->i_ino; 1411 type = inode->i_mode >> 12; 1412 } 1413 dput(child); 1414 end_instantiate: 1415 if (!ino) 1416 ino = find_inode_number(dir, &qname); 1417 if (!ino) 1418 ino = 1; 1419 return filldir(dirent, name, len, filp->f_pos, ino, type); 1420 } 1421 1422 static unsigned name_to_int(struct dentry *dentry) 1423 { 1424 const char *name = dentry->d_name.name; 1425 int len = dentry->d_name.len; 1426 unsigned n = 0; 1427 1428 if (len > 1 && *name == '0') 1429 goto out; 1430 while (len-- > 0) { 1431 unsigned c = *name++ - '0'; 1432 if (c > 9) 1433 goto out; 1434 if (n >= (~0U-9)/10) 1435 goto out; 1436 n *= 10; 1437 n += c; 1438 } 1439 return n; 1440 out: 1441 return ~0U; 1442 } 1443 1444 #define PROC_FDINFO_MAX 64 1445 1446 static int proc_fd_info(struct inode *inode, struct path *path, char *info) 1447 { 1448 struct task_struct *task = get_proc_task(inode); 1449 struct files_struct *files = NULL; 1450 struct file *file; 1451 int fd = proc_fd(inode); 1452 1453 if (task) { 1454 files = get_files_struct(task); 1455 put_task_struct(task); 1456 } 1457 if (files) { 1458 /* 1459 * We are not taking a ref to the file structure, so we must 1460 * hold ->file_lock. 1461 */ 1462 spin_lock(&files->file_lock); 1463 file = fcheck_files(files, fd); 1464 if (file) { 1465 if (path) { 1466 *path = file->f_path; 1467 path_get(&file->f_path); 1468 } 1469 if (info) 1470 snprintf(info, PROC_FDINFO_MAX, 1471 "pos:\t%lli\n" 1472 "flags:\t0%o\n", 1473 (long long) file->f_pos, 1474 file->f_flags); 1475 spin_unlock(&files->file_lock); 1476 put_files_struct(files); 1477 return 0; 1478 } 1479 spin_unlock(&files->file_lock); 1480 put_files_struct(files); 1481 } 1482 return -ENOENT; 1483 } 1484 1485 static int proc_fd_link(struct inode *inode, struct path *path) 1486 { 1487 return proc_fd_info(inode, path, NULL); 1488 } 1489 1490 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 1491 { 1492 struct inode *inode = dentry->d_inode; 1493 struct task_struct *task = get_proc_task(inode); 1494 int fd = proc_fd(inode); 1495 struct files_struct *files; 1496 1497 if (task) { 1498 files = get_files_struct(task); 1499 if (files) { 1500 rcu_read_lock(); 1501 if (fcheck_files(files, fd)) { 1502 rcu_read_unlock(); 1503 put_files_struct(files); 1504 if (task_dumpable(task)) { 1505 inode->i_uid = task->euid; 1506 inode->i_gid = task->egid; 1507 } else { 1508 inode->i_uid = 0; 1509 inode->i_gid = 0; 1510 } 1511 inode->i_mode &= ~(S_ISUID | S_ISGID); 1512 security_task_to_inode(task, inode); 1513 put_task_struct(task); 1514 return 1; 1515 } 1516 rcu_read_unlock(); 1517 put_files_struct(files); 1518 } 1519 put_task_struct(task); 1520 } 1521 d_drop(dentry); 1522 return 0; 1523 } 1524 1525 static struct dentry_operations tid_fd_dentry_operations = 1526 { 1527 .d_revalidate = tid_fd_revalidate, 1528 .d_delete = pid_delete_dentry, 1529 }; 1530 1531 static struct dentry *proc_fd_instantiate(struct inode *dir, 1532 struct dentry *dentry, struct task_struct *task, const void *ptr) 1533 { 1534 unsigned fd = *(const unsigned *)ptr; 1535 struct file *file; 1536 struct files_struct *files; 1537 struct inode *inode; 1538 struct proc_inode *ei; 1539 struct dentry *error = ERR_PTR(-ENOENT); 1540 1541 inode = proc_pid_make_inode(dir->i_sb, task); 1542 if (!inode) 1543 goto out; 1544 ei = PROC_I(inode); 1545 ei->fd = fd; 1546 files = get_files_struct(task); 1547 if (!files) 1548 goto out_iput; 1549 inode->i_mode = S_IFLNK; 1550 1551 /* 1552 * We are not taking a ref to the file structure, so we must 1553 * hold ->file_lock. 1554 */ 1555 spin_lock(&files->file_lock); 1556 file = fcheck_files(files, fd); 1557 if (!file) 1558 goto out_unlock; 1559 if (file->f_mode & 1) 1560 inode->i_mode |= S_IRUSR | S_IXUSR; 1561 if (file->f_mode & 2) 1562 inode->i_mode |= S_IWUSR | S_IXUSR; 1563 spin_unlock(&files->file_lock); 1564 put_files_struct(files); 1565 1566 inode->i_op = &proc_pid_link_inode_operations; 1567 inode->i_size = 64; 1568 ei->op.proc_get_link = proc_fd_link; 1569 dentry->d_op = &tid_fd_dentry_operations; 1570 d_add(dentry, inode); 1571 /* Close the race of the process dying before we return the dentry */ 1572 if (tid_fd_revalidate(dentry, NULL)) 1573 error = NULL; 1574 1575 out: 1576 return error; 1577 out_unlock: 1578 spin_unlock(&files->file_lock); 1579 put_files_struct(files); 1580 out_iput: 1581 iput(inode); 1582 goto out; 1583 } 1584 1585 static struct dentry *proc_lookupfd_common(struct inode *dir, 1586 struct dentry *dentry, 1587 instantiate_t instantiate) 1588 { 1589 struct task_struct *task = get_proc_task(dir); 1590 unsigned fd = name_to_int(dentry); 1591 struct dentry *result = ERR_PTR(-ENOENT); 1592 1593 if (!task) 1594 goto out_no_task; 1595 if (fd == ~0U) 1596 goto out; 1597 1598 result = instantiate(dir, dentry, task, &fd); 1599 out: 1600 put_task_struct(task); 1601 out_no_task: 1602 return result; 1603 } 1604 1605 static int proc_readfd_common(struct file * filp, void * dirent, 1606 filldir_t filldir, instantiate_t instantiate) 1607 { 1608 struct dentry *dentry = filp->f_path.dentry; 1609 struct inode *inode = dentry->d_inode; 1610 struct task_struct *p = get_proc_task(inode); 1611 unsigned int fd, ino; 1612 int retval; 1613 struct files_struct * files; 1614 struct fdtable *fdt; 1615 1616 retval = -ENOENT; 1617 if (!p) 1618 goto out_no_task; 1619 retval = 0; 1620 1621 fd = filp->f_pos; 1622 switch (fd) { 1623 case 0: 1624 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 1625 goto out; 1626 filp->f_pos++; 1627 case 1: 1628 ino = parent_ino(dentry); 1629 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 1630 goto out; 1631 filp->f_pos++; 1632 default: 1633 files = get_files_struct(p); 1634 if (!files) 1635 goto out; 1636 rcu_read_lock(); 1637 fdt = files_fdtable(files); 1638 for (fd = filp->f_pos-2; 1639 fd < fdt->max_fds; 1640 fd++, filp->f_pos++) { 1641 char name[PROC_NUMBUF]; 1642 int len; 1643 1644 if (!fcheck_files(files, fd)) 1645 continue; 1646 rcu_read_unlock(); 1647 1648 len = snprintf(name, sizeof(name), "%d", fd); 1649 if (proc_fill_cache(filp, dirent, filldir, 1650 name, len, instantiate, 1651 p, &fd) < 0) { 1652 rcu_read_lock(); 1653 break; 1654 } 1655 rcu_read_lock(); 1656 } 1657 rcu_read_unlock(); 1658 put_files_struct(files); 1659 } 1660 out: 1661 put_task_struct(p); 1662 out_no_task: 1663 return retval; 1664 } 1665 1666 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 1667 struct nameidata *nd) 1668 { 1669 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 1670 } 1671 1672 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 1673 { 1674 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 1675 } 1676 1677 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 1678 size_t len, loff_t *ppos) 1679 { 1680 char tmp[PROC_FDINFO_MAX]; 1681 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp); 1682 if (!err) 1683 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 1684 return err; 1685 } 1686 1687 static const struct file_operations proc_fdinfo_file_operations = { 1688 .open = nonseekable_open, 1689 .read = proc_fdinfo_read, 1690 }; 1691 1692 static const struct file_operations proc_fd_operations = { 1693 .read = generic_read_dir, 1694 .readdir = proc_readfd, 1695 }; 1696 1697 /* 1698 * /proc/pid/fd needs a special permission handler so that a process can still 1699 * access /proc/self/fd after it has executed a setuid(). 1700 */ 1701 static int proc_fd_permission(struct inode *inode, int mask, 1702 struct nameidata *nd) 1703 { 1704 int rv; 1705 1706 rv = generic_permission(inode, mask, NULL); 1707 if (rv == 0) 1708 return 0; 1709 if (task_pid(current) == proc_pid(inode)) 1710 rv = 0; 1711 return rv; 1712 } 1713 1714 /* 1715 * proc directories can do almost nothing.. 1716 */ 1717 static const struct inode_operations proc_fd_inode_operations = { 1718 .lookup = proc_lookupfd, 1719 .permission = proc_fd_permission, 1720 .setattr = proc_setattr, 1721 }; 1722 1723 static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 1724 struct dentry *dentry, struct task_struct *task, const void *ptr) 1725 { 1726 unsigned fd = *(unsigned *)ptr; 1727 struct inode *inode; 1728 struct proc_inode *ei; 1729 struct dentry *error = ERR_PTR(-ENOENT); 1730 1731 inode = proc_pid_make_inode(dir->i_sb, task); 1732 if (!inode) 1733 goto out; 1734 ei = PROC_I(inode); 1735 ei->fd = fd; 1736 inode->i_mode = S_IFREG | S_IRUSR; 1737 inode->i_fop = &proc_fdinfo_file_operations; 1738 dentry->d_op = &tid_fd_dentry_operations; 1739 d_add(dentry, inode); 1740 /* Close the race of the process dying before we return the dentry */ 1741 if (tid_fd_revalidate(dentry, NULL)) 1742 error = NULL; 1743 1744 out: 1745 return error; 1746 } 1747 1748 static struct dentry *proc_lookupfdinfo(struct inode *dir, 1749 struct dentry *dentry, 1750 struct nameidata *nd) 1751 { 1752 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 1753 } 1754 1755 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 1756 { 1757 return proc_readfd_common(filp, dirent, filldir, 1758 proc_fdinfo_instantiate); 1759 } 1760 1761 static const struct file_operations proc_fdinfo_operations = { 1762 .read = generic_read_dir, 1763 .readdir = proc_readfdinfo, 1764 }; 1765 1766 /* 1767 * proc directories can do almost nothing.. 1768 */ 1769 static const struct inode_operations proc_fdinfo_inode_operations = { 1770 .lookup = proc_lookupfdinfo, 1771 .setattr = proc_setattr, 1772 }; 1773 1774 1775 static struct dentry *proc_pident_instantiate(struct inode *dir, 1776 struct dentry *dentry, struct task_struct *task, const void *ptr) 1777 { 1778 const struct pid_entry *p = ptr; 1779 struct inode *inode; 1780 struct proc_inode *ei; 1781 struct dentry *error = ERR_PTR(-EINVAL); 1782 1783 inode = proc_pid_make_inode(dir->i_sb, task); 1784 if (!inode) 1785 goto out; 1786 1787 ei = PROC_I(inode); 1788 inode->i_mode = p->mode; 1789 if (S_ISDIR(inode->i_mode)) 1790 inode->i_nlink = 2; /* Use getattr to fix if necessary */ 1791 if (p->iop) 1792 inode->i_op = p->iop; 1793 if (p->fop) 1794 inode->i_fop = p->fop; 1795 ei->op = p->op; 1796 dentry->d_op = &pid_dentry_operations; 1797 d_add(dentry, inode); 1798 /* Close the race of the process dying before we return the dentry */ 1799 if (pid_revalidate(dentry, NULL)) 1800 error = NULL; 1801 out: 1802 return error; 1803 } 1804 1805 static struct dentry *proc_pident_lookup(struct inode *dir, 1806 struct dentry *dentry, 1807 const struct pid_entry *ents, 1808 unsigned int nents) 1809 { 1810 struct inode *inode; 1811 struct dentry *error; 1812 struct task_struct *task = get_proc_task(dir); 1813 const struct pid_entry *p, *last; 1814 1815 error = ERR_PTR(-ENOENT); 1816 inode = NULL; 1817 1818 if (!task) 1819 goto out_no_task; 1820 1821 /* 1822 * Yes, it does not scale. And it should not. Don't add 1823 * new entries into /proc/<tgid>/ without very good reasons. 1824 */ 1825 last = &ents[nents - 1]; 1826 for (p = ents; p <= last; p++) { 1827 if (p->len != dentry->d_name.len) 1828 continue; 1829 if (!memcmp(dentry->d_name.name, p->name, p->len)) 1830 break; 1831 } 1832 if (p > last) 1833 goto out; 1834 1835 error = proc_pident_instantiate(dir, dentry, task, p); 1836 out: 1837 put_task_struct(task); 1838 out_no_task: 1839 return error; 1840 } 1841 1842 static int proc_pident_fill_cache(struct file *filp, void *dirent, 1843 filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 1844 { 1845 return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 1846 proc_pident_instantiate, task, p); 1847 } 1848 1849 static int proc_pident_readdir(struct file *filp, 1850 void *dirent, filldir_t filldir, 1851 const struct pid_entry *ents, unsigned int nents) 1852 { 1853 int i; 1854 struct dentry *dentry = filp->f_path.dentry; 1855 struct inode *inode = dentry->d_inode; 1856 struct task_struct *task = get_proc_task(inode); 1857 const struct pid_entry *p, *last; 1858 ino_t ino; 1859 int ret; 1860 1861 ret = -ENOENT; 1862 if (!task) 1863 goto out_no_task; 1864 1865 ret = 0; 1866 i = filp->f_pos; 1867 switch (i) { 1868 case 0: 1869 ino = inode->i_ino; 1870 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 1871 goto out; 1872 i++; 1873 filp->f_pos++; 1874 /* fall through */ 1875 case 1: 1876 ino = parent_ino(dentry); 1877 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 1878 goto out; 1879 i++; 1880 filp->f_pos++; 1881 /* fall through */ 1882 default: 1883 i -= 2; 1884 if (i >= nents) { 1885 ret = 1; 1886 goto out; 1887 } 1888 p = ents + i; 1889 last = &ents[nents - 1]; 1890 while (p <= last) { 1891 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 1892 goto out; 1893 filp->f_pos++; 1894 p++; 1895 } 1896 } 1897 1898 ret = 1; 1899 out: 1900 put_task_struct(task); 1901 out_no_task: 1902 return ret; 1903 } 1904 1905 #ifdef CONFIG_SECURITY 1906 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 1907 size_t count, loff_t *ppos) 1908 { 1909 struct inode * inode = file->f_path.dentry->d_inode; 1910 char *p = NULL; 1911 ssize_t length; 1912 struct task_struct *task = get_proc_task(inode); 1913 1914 if (!task) 1915 return -ESRCH; 1916 1917 length = security_getprocattr(task, 1918 (char*)file->f_path.dentry->d_name.name, 1919 &p); 1920 put_task_struct(task); 1921 if (length > 0) 1922 length = simple_read_from_buffer(buf, count, ppos, p, length); 1923 kfree(p); 1924 return length; 1925 } 1926 1927 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 1928 size_t count, loff_t *ppos) 1929 { 1930 struct inode * inode = file->f_path.dentry->d_inode; 1931 char *page; 1932 ssize_t length; 1933 struct task_struct *task = get_proc_task(inode); 1934 1935 length = -ESRCH; 1936 if (!task) 1937 goto out_no_task; 1938 if (count > PAGE_SIZE) 1939 count = PAGE_SIZE; 1940 1941 /* No partial writes. */ 1942 length = -EINVAL; 1943 if (*ppos != 0) 1944 goto out; 1945 1946 length = -ENOMEM; 1947 page = (char*)__get_free_page(GFP_TEMPORARY); 1948 if (!page) 1949 goto out; 1950 1951 length = -EFAULT; 1952 if (copy_from_user(page, buf, count)) 1953 goto out_free; 1954 1955 length = security_setprocattr(task, 1956 (char*)file->f_path.dentry->d_name.name, 1957 (void*)page, count); 1958 out_free: 1959 free_page((unsigned long) page); 1960 out: 1961 put_task_struct(task); 1962 out_no_task: 1963 return length; 1964 } 1965 1966 static const struct file_operations proc_pid_attr_operations = { 1967 .read = proc_pid_attr_read, 1968 .write = proc_pid_attr_write, 1969 }; 1970 1971 static const struct pid_entry attr_dir_stuff[] = { 1972 REG("current", S_IRUGO|S_IWUGO, pid_attr), 1973 REG("prev", S_IRUGO, pid_attr), 1974 REG("exec", S_IRUGO|S_IWUGO, pid_attr), 1975 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr), 1976 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr), 1977 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr), 1978 }; 1979 1980 static int proc_attr_dir_readdir(struct file * filp, 1981 void * dirent, filldir_t filldir) 1982 { 1983 return proc_pident_readdir(filp,dirent,filldir, 1984 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 1985 } 1986 1987 static const struct file_operations proc_attr_dir_operations = { 1988 .read = generic_read_dir, 1989 .readdir = proc_attr_dir_readdir, 1990 }; 1991 1992 static struct dentry *proc_attr_dir_lookup(struct inode *dir, 1993 struct dentry *dentry, struct nameidata *nd) 1994 { 1995 return proc_pident_lookup(dir, dentry, 1996 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 1997 } 1998 1999 static const struct inode_operations proc_attr_dir_inode_operations = { 2000 .lookup = proc_attr_dir_lookup, 2001 .getattr = pid_getattr, 2002 .setattr = proc_setattr, 2003 }; 2004 2005 #endif 2006 2007 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 2008 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 2009 size_t count, loff_t *ppos) 2010 { 2011 struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 2012 struct mm_struct *mm; 2013 char buffer[PROC_NUMBUF]; 2014 size_t len; 2015 int ret; 2016 2017 if (!task) 2018 return -ESRCH; 2019 2020 ret = 0; 2021 mm = get_task_mm(task); 2022 if (mm) { 2023 len = snprintf(buffer, sizeof(buffer), "%08lx\n", 2024 ((mm->flags & MMF_DUMP_FILTER_MASK) >> 2025 MMF_DUMP_FILTER_SHIFT)); 2026 mmput(mm); 2027 ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 2028 } 2029 2030 put_task_struct(task); 2031 2032 return ret; 2033 } 2034 2035 static ssize_t proc_coredump_filter_write(struct file *file, 2036 const char __user *buf, 2037 size_t count, 2038 loff_t *ppos) 2039 { 2040 struct task_struct *task; 2041 struct mm_struct *mm; 2042 char buffer[PROC_NUMBUF], *end; 2043 unsigned int val; 2044 int ret; 2045 int i; 2046 unsigned long mask; 2047 2048 ret = -EFAULT; 2049 memset(buffer, 0, sizeof(buffer)); 2050 if (count > sizeof(buffer) - 1) 2051 count = sizeof(buffer) - 1; 2052 if (copy_from_user(buffer, buf, count)) 2053 goto out_no_task; 2054 2055 ret = -EINVAL; 2056 val = (unsigned int)simple_strtoul(buffer, &end, 0); 2057 if (*end == '\n') 2058 end++; 2059 if (end - buffer == 0) 2060 goto out_no_task; 2061 2062 ret = -ESRCH; 2063 task = get_proc_task(file->f_dentry->d_inode); 2064 if (!task) 2065 goto out_no_task; 2066 2067 ret = end - buffer; 2068 mm = get_task_mm(task); 2069 if (!mm) 2070 goto out_no_mm; 2071 2072 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 2073 if (val & mask) 2074 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 2075 else 2076 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 2077 } 2078 2079 mmput(mm); 2080 out_no_mm: 2081 put_task_struct(task); 2082 out_no_task: 2083 return ret; 2084 } 2085 2086 static const struct file_operations proc_coredump_filter_operations = { 2087 .read = proc_coredump_filter_read, 2088 .write = proc_coredump_filter_write, 2089 }; 2090 #endif 2091 2092 /* 2093 * /proc/self: 2094 */ 2095 static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 2096 int buflen) 2097 { 2098 struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2099 pid_t tgid = task_tgid_nr_ns(current, ns); 2100 char tmp[PROC_NUMBUF]; 2101 if (!tgid) 2102 return -ENOENT; 2103 sprintf(tmp, "%d", tgid); 2104 return vfs_readlink(dentry,buffer,buflen,tmp); 2105 } 2106 2107 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 2108 { 2109 struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2110 pid_t tgid = task_tgid_nr_ns(current, ns); 2111 char tmp[PROC_NUMBUF]; 2112 if (!tgid) 2113 return ERR_PTR(-ENOENT); 2114 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns)); 2115 return ERR_PTR(vfs_follow_link(nd,tmp)); 2116 } 2117 2118 static const struct inode_operations proc_self_inode_operations = { 2119 .readlink = proc_self_readlink, 2120 .follow_link = proc_self_follow_link, 2121 }; 2122 2123 /* 2124 * proc base 2125 * 2126 * These are the directory entries in the root directory of /proc 2127 * that properly belong to the /proc filesystem, as they describe 2128 * describe something that is process related. 2129 */ 2130 static const struct pid_entry proc_base_stuff[] = { 2131 NOD("self", S_IFLNK|S_IRWXUGO, 2132 &proc_self_inode_operations, NULL, {}), 2133 }; 2134 2135 /* 2136 * Exceptional case: normally we are not allowed to unhash a busy 2137 * directory. In this case, however, we can do it - no aliasing problems 2138 * due to the way we treat inodes. 2139 */ 2140 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd) 2141 { 2142 struct inode *inode = dentry->d_inode; 2143 struct task_struct *task = get_proc_task(inode); 2144 if (task) { 2145 put_task_struct(task); 2146 return 1; 2147 } 2148 d_drop(dentry); 2149 return 0; 2150 } 2151 2152 static struct dentry_operations proc_base_dentry_operations = 2153 { 2154 .d_revalidate = proc_base_revalidate, 2155 .d_delete = pid_delete_dentry, 2156 }; 2157 2158 static struct dentry *proc_base_instantiate(struct inode *dir, 2159 struct dentry *dentry, struct task_struct *task, const void *ptr) 2160 { 2161 const struct pid_entry *p = ptr; 2162 struct inode *inode; 2163 struct proc_inode *ei; 2164 struct dentry *error = ERR_PTR(-EINVAL); 2165 2166 /* Allocate the inode */ 2167 error = ERR_PTR(-ENOMEM); 2168 inode = new_inode(dir->i_sb); 2169 if (!inode) 2170 goto out; 2171 2172 /* Initialize the inode */ 2173 ei = PROC_I(inode); 2174 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2175 2176 /* 2177 * grab the reference to the task. 2178 */ 2179 ei->pid = get_task_pid(task, PIDTYPE_PID); 2180 if (!ei->pid) 2181 goto out_iput; 2182 2183 inode->i_uid = 0; 2184 inode->i_gid = 0; 2185 inode->i_mode = p->mode; 2186 if (S_ISDIR(inode->i_mode)) 2187 inode->i_nlink = 2; 2188 if (S_ISLNK(inode->i_mode)) 2189 inode->i_size = 64; 2190 if (p->iop) 2191 inode->i_op = p->iop; 2192 if (p->fop) 2193 inode->i_fop = p->fop; 2194 ei->op = p->op; 2195 dentry->d_op = &proc_base_dentry_operations; 2196 d_add(dentry, inode); 2197 error = NULL; 2198 out: 2199 return error; 2200 out_iput: 2201 iput(inode); 2202 goto out; 2203 } 2204 2205 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2206 { 2207 struct dentry *error; 2208 struct task_struct *task = get_proc_task(dir); 2209 const struct pid_entry *p, *last; 2210 2211 error = ERR_PTR(-ENOENT); 2212 2213 if (!task) 2214 goto out_no_task; 2215 2216 /* Lookup the directory entry */ 2217 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 2218 for (p = proc_base_stuff; p <= last; p++) { 2219 if (p->len != dentry->d_name.len) 2220 continue; 2221 if (!memcmp(dentry->d_name.name, p->name, p->len)) 2222 break; 2223 } 2224 if (p > last) 2225 goto out; 2226 2227 error = proc_base_instantiate(dir, dentry, task, p); 2228 2229 out: 2230 put_task_struct(task); 2231 out_no_task: 2232 return error; 2233 } 2234 2235 static int proc_base_fill_cache(struct file *filp, void *dirent, 2236 filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 2237 { 2238 return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 2239 proc_base_instantiate, task, p); 2240 } 2241 2242 #ifdef CONFIG_TASK_IO_ACCOUNTING 2243 static int proc_pid_io_accounting(struct task_struct *task, char *buffer) 2244 { 2245 return sprintf(buffer, 2246 #ifdef CONFIG_TASK_XACCT 2247 "rchar: %llu\n" 2248 "wchar: %llu\n" 2249 "syscr: %llu\n" 2250 "syscw: %llu\n" 2251 #endif 2252 "read_bytes: %llu\n" 2253 "write_bytes: %llu\n" 2254 "cancelled_write_bytes: %llu\n", 2255 #ifdef CONFIG_TASK_XACCT 2256 (unsigned long long)task->rchar, 2257 (unsigned long long)task->wchar, 2258 (unsigned long long)task->syscr, 2259 (unsigned long long)task->syscw, 2260 #endif 2261 (unsigned long long)task->ioac.read_bytes, 2262 (unsigned long long)task->ioac.write_bytes, 2263 (unsigned long long)task->ioac.cancelled_write_bytes); 2264 } 2265 #endif 2266 2267 /* 2268 * Thread groups 2269 */ 2270 static const struct file_operations proc_task_operations; 2271 static const struct inode_operations proc_task_inode_operations; 2272 2273 static const struct pid_entry tgid_base_stuff[] = { 2274 DIR("task", S_IRUGO|S_IXUGO, task), 2275 DIR("fd", S_IRUSR|S_IXUSR, fd), 2276 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2277 REG("environ", S_IRUSR, environ), 2278 INF("auxv", S_IRUSR, pid_auxv), 2279 ONE("status", S_IRUGO, pid_status), 2280 INF("limits", S_IRUSR, pid_limits), 2281 #ifdef CONFIG_SCHED_DEBUG 2282 REG("sched", S_IRUGO|S_IWUSR, pid_sched), 2283 #endif 2284 INF("cmdline", S_IRUGO, pid_cmdline), 2285 ONE("stat", S_IRUGO, tgid_stat), 2286 ONE("statm", S_IRUGO, pid_statm), 2287 REG("maps", S_IRUGO, maps), 2288 #ifdef CONFIG_NUMA 2289 REG("numa_maps", S_IRUGO, numa_maps), 2290 #endif 2291 REG("mem", S_IRUSR|S_IWUSR, mem), 2292 LNK("cwd", cwd), 2293 LNK("root", root), 2294 LNK("exe", exe), 2295 REG("mounts", S_IRUGO, mounts), 2296 REG("mountstats", S_IRUSR, mountstats), 2297 #ifdef CONFIG_PROC_PAGE_MONITOR 2298 REG("clear_refs", S_IWUSR, clear_refs), 2299 REG("smaps", S_IRUGO, smaps), 2300 REG("pagemap", S_IRUSR, pagemap), 2301 #endif 2302 #ifdef CONFIG_SECURITY 2303 DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 2304 #endif 2305 #ifdef CONFIG_KALLSYMS 2306 INF("wchan", S_IRUGO, pid_wchan), 2307 #endif 2308 #ifdef CONFIG_SCHEDSTATS 2309 INF("schedstat", S_IRUGO, pid_schedstat), 2310 #endif 2311 #ifdef CONFIG_LATENCYTOP 2312 REG("latency", S_IRUGO, lstats), 2313 #endif 2314 #ifdef CONFIG_PROC_PID_CPUSET 2315 REG("cpuset", S_IRUGO, cpuset), 2316 #endif 2317 #ifdef CONFIG_CGROUPS 2318 REG("cgroup", S_IRUGO, cgroup), 2319 #endif 2320 INF("oom_score", S_IRUGO, oom_score), 2321 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 2322 #ifdef CONFIG_AUDITSYSCALL 2323 REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 2324 #endif 2325 #ifdef CONFIG_FAULT_INJECTION 2326 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2327 #endif 2328 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 2329 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter), 2330 #endif 2331 #ifdef CONFIG_TASK_IO_ACCOUNTING 2332 INF("io", S_IRUGO, pid_io_accounting), 2333 #endif 2334 }; 2335 2336 static int proc_tgid_base_readdir(struct file * filp, 2337 void * dirent, filldir_t filldir) 2338 { 2339 return proc_pident_readdir(filp,dirent,filldir, 2340 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 2341 } 2342 2343 static const struct file_operations proc_tgid_base_operations = { 2344 .read = generic_read_dir, 2345 .readdir = proc_tgid_base_readdir, 2346 }; 2347 2348 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 2349 return proc_pident_lookup(dir, dentry, 2350 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 2351 } 2352 2353 static const struct inode_operations proc_tgid_base_inode_operations = { 2354 .lookup = proc_tgid_base_lookup, 2355 .getattr = pid_getattr, 2356 .setattr = proc_setattr, 2357 }; 2358 2359 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 2360 { 2361 struct dentry *dentry, *leader, *dir; 2362 char buf[PROC_NUMBUF]; 2363 struct qstr name; 2364 2365 name.name = buf; 2366 name.len = snprintf(buf, sizeof(buf), "%d", pid); 2367 dentry = d_hash_and_lookup(mnt->mnt_root, &name); 2368 if (dentry) { 2369 if (!(current->flags & PF_EXITING)) 2370 shrink_dcache_parent(dentry); 2371 d_drop(dentry); 2372 dput(dentry); 2373 } 2374 2375 if (tgid == 0) 2376 goto out; 2377 2378 name.name = buf; 2379 name.len = snprintf(buf, sizeof(buf), "%d", tgid); 2380 leader = d_hash_and_lookup(mnt->mnt_root, &name); 2381 if (!leader) 2382 goto out; 2383 2384 name.name = "task"; 2385 name.len = strlen(name.name); 2386 dir = d_hash_and_lookup(leader, &name); 2387 if (!dir) 2388 goto out_put_leader; 2389 2390 name.name = buf; 2391 name.len = snprintf(buf, sizeof(buf), "%d", pid); 2392 dentry = d_hash_and_lookup(dir, &name); 2393 if (dentry) { 2394 shrink_dcache_parent(dentry); 2395 d_drop(dentry); 2396 dput(dentry); 2397 } 2398 2399 dput(dir); 2400 out_put_leader: 2401 dput(leader); 2402 out: 2403 return; 2404 } 2405 2406 /** 2407 * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 2408 * @task: task that should be flushed. 2409 * 2410 * When flushing dentries from proc, one needs to flush them from global 2411 * proc (proc_mnt) and from all the namespaces' procs this task was seen 2412 * in. This call is supposed to do all of this job. 2413 * 2414 * Looks in the dcache for 2415 * /proc/@pid 2416 * /proc/@tgid/task/@pid 2417 * if either directory is present flushes it and all of it'ts children 2418 * from the dcache. 2419 * 2420 * It is safe and reasonable to cache /proc entries for a task until 2421 * that task exits. After that they just clog up the dcache with 2422 * useless entries, possibly causing useful dcache entries to be 2423 * flushed instead. This routine is proved to flush those useless 2424 * dcache entries at process exit time. 2425 * 2426 * NOTE: This routine is just an optimization so it does not guarantee 2427 * that no dcache entries will exist at process exit time it 2428 * just makes it very unlikely that any will persist. 2429 */ 2430 2431 void proc_flush_task(struct task_struct *task) 2432 { 2433 int i; 2434 struct pid *pid, *tgid = NULL; 2435 struct upid *upid; 2436 2437 pid = task_pid(task); 2438 if (thread_group_leader(task)) 2439 tgid = task_tgid(task); 2440 2441 for (i = 0; i <= pid->level; i++) { 2442 upid = &pid->numbers[i]; 2443 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 2444 tgid ? tgid->numbers[i].nr : 0); 2445 } 2446 2447 upid = &pid->numbers[pid->level]; 2448 if (upid->nr == 1) 2449 pid_ns_release_proc(upid->ns); 2450 } 2451 2452 static struct dentry *proc_pid_instantiate(struct inode *dir, 2453 struct dentry * dentry, 2454 struct task_struct *task, const void *ptr) 2455 { 2456 struct dentry *error = ERR_PTR(-ENOENT); 2457 struct inode *inode; 2458 2459 inode = proc_pid_make_inode(dir->i_sb, task); 2460 if (!inode) 2461 goto out; 2462 2463 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2464 inode->i_op = &proc_tgid_base_inode_operations; 2465 inode->i_fop = &proc_tgid_base_operations; 2466 inode->i_flags|=S_IMMUTABLE; 2467 inode->i_nlink = 5; 2468 #ifdef CONFIG_SECURITY 2469 inode->i_nlink += 1; 2470 #endif 2471 2472 dentry->d_op = &pid_dentry_operations; 2473 2474 d_add(dentry, inode); 2475 /* Close the race of the process dying before we return the dentry */ 2476 if (pid_revalidate(dentry, NULL)) 2477 error = NULL; 2478 out: 2479 return error; 2480 } 2481 2482 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 2483 { 2484 struct dentry *result = ERR_PTR(-ENOENT); 2485 struct task_struct *task; 2486 unsigned tgid; 2487 struct pid_namespace *ns; 2488 2489 result = proc_base_lookup(dir, dentry); 2490 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2491 goto out; 2492 2493 tgid = name_to_int(dentry); 2494 if (tgid == ~0U) 2495 goto out; 2496 2497 ns = dentry->d_sb->s_fs_info; 2498 rcu_read_lock(); 2499 task = find_task_by_pid_ns(tgid, ns); 2500 if (task) 2501 get_task_struct(task); 2502 rcu_read_unlock(); 2503 if (!task) 2504 goto out; 2505 2506 result = proc_pid_instantiate(dir, dentry, task, NULL); 2507 put_task_struct(task); 2508 out: 2509 return result; 2510 } 2511 2512 /* 2513 * Find the first task with tgid >= tgid 2514 * 2515 */ 2516 struct tgid_iter { 2517 unsigned int tgid; 2518 struct task_struct *task; 2519 }; 2520 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 2521 { 2522 struct pid *pid; 2523 2524 if (iter.task) 2525 put_task_struct(iter.task); 2526 rcu_read_lock(); 2527 retry: 2528 iter.task = NULL; 2529 pid = find_ge_pid(iter.tgid, ns); 2530 if (pid) { 2531 iter.tgid = pid_nr_ns(pid, ns); 2532 iter.task = pid_task(pid, PIDTYPE_PID); 2533 /* What we to know is if the pid we have find is the 2534 * pid of a thread_group_leader. Testing for task 2535 * being a thread_group_leader is the obvious thing 2536 * todo but there is a window when it fails, due to 2537 * the pid transfer logic in de_thread. 2538 * 2539 * So we perform the straight forward test of seeing 2540 * if the pid we have found is the pid of a thread 2541 * group leader, and don't worry if the task we have 2542 * found doesn't happen to be a thread group leader. 2543 * As we don't care in the case of readdir. 2544 */ 2545 if (!iter.task || !has_group_leader_pid(iter.task)) { 2546 iter.tgid += 1; 2547 goto retry; 2548 } 2549 get_task_struct(iter.task); 2550 } 2551 rcu_read_unlock(); 2552 return iter; 2553 } 2554 2555 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 2556 2557 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 2558 struct tgid_iter iter) 2559 { 2560 char name[PROC_NUMBUF]; 2561 int len = snprintf(name, sizeof(name), "%d", iter.tgid); 2562 return proc_fill_cache(filp, dirent, filldir, name, len, 2563 proc_pid_instantiate, iter.task, NULL); 2564 } 2565 2566 /* for the /proc/ directory itself, after non-process stuff has been done */ 2567 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 2568 { 2569 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 2570 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode); 2571 struct tgid_iter iter; 2572 struct pid_namespace *ns; 2573 2574 if (!reaper) 2575 goto out_no_task; 2576 2577 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2578 const struct pid_entry *p = &proc_base_stuff[nr]; 2579 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2580 goto out; 2581 } 2582 2583 ns = filp->f_dentry->d_sb->s_fs_info; 2584 iter.task = NULL; 2585 iter.tgid = filp->f_pos - TGID_OFFSET; 2586 for (iter = next_tgid(ns, iter); 2587 iter.task; 2588 iter.tgid += 1, iter = next_tgid(ns, iter)) { 2589 filp->f_pos = iter.tgid + TGID_OFFSET; 2590 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) { 2591 put_task_struct(iter.task); 2592 goto out; 2593 } 2594 } 2595 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 2596 out: 2597 put_task_struct(reaper); 2598 out_no_task: 2599 return 0; 2600 } 2601 2602 /* 2603 * Tasks 2604 */ 2605 static const struct pid_entry tid_base_stuff[] = { 2606 DIR("fd", S_IRUSR|S_IXUSR, fd), 2607 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2608 REG("environ", S_IRUSR, environ), 2609 INF("auxv", S_IRUSR, pid_auxv), 2610 ONE("status", S_IRUGO, pid_status), 2611 INF("limits", S_IRUSR, pid_limits), 2612 #ifdef CONFIG_SCHED_DEBUG 2613 REG("sched", S_IRUGO|S_IWUSR, pid_sched), 2614 #endif 2615 INF("cmdline", S_IRUGO, pid_cmdline), 2616 ONE("stat", S_IRUGO, tid_stat), 2617 ONE("statm", S_IRUGO, pid_statm), 2618 REG("maps", S_IRUGO, maps), 2619 #ifdef CONFIG_NUMA 2620 REG("numa_maps", S_IRUGO, numa_maps), 2621 #endif 2622 REG("mem", S_IRUSR|S_IWUSR, mem), 2623 LNK("cwd", cwd), 2624 LNK("root", root), 2625 LNK("exe", exe), 2626 REG("mounts", S_IRUGO, mounts), 2627 #ifdef CONFIG_PROC_PAGE_MONITOR 2628 REG("clear_refs", S_IWUSR, clear_refs), 2629 REG("smaps", S_IRUGO, smaps), 2630 REG("pagemap", S_IRUSR, pagemap), 2631 #endif 2632 #ifdef CONFIG_SECURITY 2633 DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 2634 #endif 2635 #ifdef CONFIG_KALLSYMS 2636 INF("wchan", S_IRUGO, pid_wchan), 2637 #endif 2638 #ifdef CONFIG_SCHEDSTATS 2639 INF("schedstat", S_IRUGO, pid_schedstat), 2640 #endif 2641 #ifdef CONFIG_LATENCYTOP 2642 REG("latency", S_IRUGO, lstats), 2643 #endif 2644 #ifdef CONFIG_PROC_PID_CPUSET 2645 REG("cpuset", S_IRUGO, cpuset), 2646 #endif 2647 #ifdef CONFIG_CGROUPS 2648 REG("cgroup", S_IRUGO, cgroup), 2649 #endif 2650 INF("oom_score", S_IRUGO, oom_score), 2651 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 2652 #ifdef CONFIG_AUDITSYSCALL 2653 REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 2654 #endif 2655 #ifdef CONFIG_FAULT_INJECTION 2656 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2657 #endif 2658 }; 2659 2660 static int proc_tid_base_readdir(struct file * filp, 2661 void * dirent, filldir_t filldir) 2662 { 2663 return proc_pident_readdir(filp,dirent,filldir, 2664 tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 2665 } 2666 2667 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 2668 return proc_pident_lookup(dir, dentry, 2669 tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 2670 } 2671 2672 static const struct file_operations proc_tid_base_operations = { 2673 .read = generic_read_dir, 2674 .readdir = proc_tid_base_readdir, 2675 }; 2676 2677 static const struct inode_operations proc_tid_base_inode_operations = { 2678 .lookup = proc_tid_base_lookup, 2679 .getattr = pid_getattr, 2680 .setattr = proc_setattr, 2681 }; 2682 2683 static struct dentry *proc_task_instantiate(struct inode *dir, 2684 struct dentry *dentry, struct task_struct *task, const void *ptr) 2685 { 2686 struct dentry *error = ERR_PTR(-ENOENT); 2687 struct inode *inode; 2688 inode = proc_pid_make_inode(dir->i_sb, task); 2689 2690 if (!inode) 2691 goto out; 2692 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2693 inode->i_op = &proc_tid_base_inode_operations; 2694 inode->i_fop = &proc_tid_base_operations; 2695 inode->i_flags|=S_IMMUTABLE; 2696 inode->i_nlink = 4; 2697 #ifdef CONFIG_SECURITY 2698 inode->i_nlink += 1; 2699 #endif 2700 2701 dentry->d_op = &pid_dentry_operations; 2702 2703 d_add(dentry, inode); 2704 /* Close the race of the process dying before we return the dentry */ 2705 if (pid_revalidate(dentry, NULL)) 2706 error = NULL; 2707 out: 2708 return error; 2709 } 2710 2711 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 2712 { 2713 struct dentry *result = ERR_PTR(-ENOENT); 2714 struct task_struct *task; 2715 struct task_struct *leader = get_proc_task(dir); 2716 unsigned tid; 2717 struct pid_namespace *ns; 2718 2719 if (!leader) 2720 goto out_no_task; 2721 2722 tid = name_to_int(dentry); 2723 if (tid == ~0U) 2724 goto out; 2725 2726 ns = dentry->d_sb->s_fs_info; 2727 rcu_read_lock(); 2728 task = find_task_by_pid_ns(tid, ns); 2729 if (task) 2730 get_task_struct(task); 2731 rcu_read_unlock(); 2732 if (!task) 2733 goto out; 2734 if (!same_thread_group(leader, task)) 2735 goto out_drop_task; 2736 2737 result = proc_task_instantiate(dir, dentry, task, NULL); 2738 out_drop_task: 2739 put_task_struct(task); 2740 out: 2741 put_task_struct(leader); 2742 out_no_task: 2743 return result; 2744 } 2745 2746 /* 2747 * Find the first tid of a thread group to return to user space. 2748 * 2749 * Usually this is just the thread group leader, but if the users 2750 * buffer was too small or there was a seek into the middle of the 2751 * directory we have more work todo. 2752 * 2753 * In the case of a short read we start with find_task_by_pid. 2754 * 2755 * In the case of a seek we start with the leader and walk nr 2756 * threads past it. 2757 */ 2758 static struct task_struct *first_tid(struct task_struct *leader, 2759 int tid, int nr, struct pid_namespace *ns) 2760 { 2761 struct task_struct *pos; 2762 2763 rcu_read_lock(); 2764 /* Attempt to start with the pid of a thread */ 2765 if (tid && (nr > 0)) { 2766 pos = find_task_by_pid_ns(tid, ns); 2767 if (pos && (pos->group_leader == leader)) 2768 goto found; 2769 } 2770 2771 /* If nr exceeds the number of threads there is nothing todo */ 2772 pos = NULL; 2773 if (nr && nr >= get_nr_threads(leader)) 2774 goto out; 2775 2776 /* If we haven't found our starting place yet start 2777 * with the leader and walk nr threads forward. 2778 */ 2779 for (pos = leader; nr > 0; --nr) { 2780 pos = next_thread(pos); 2781 if (pos == leader) { 2782 pos = NULL; 2783 goto out; 2784 } 2785 } 2786 found: 2787 get_task_struct(pos); 2788 out: 2789 rcu_read_unlock(); 2790 return pos; 2791 } 2792 2793 /* 2794 * Find the next thread in the thread list. 2795 * Return NULL if there is an error or no next thread. 2796 * 2797 * The reference to the input task_struct is released. 2798 */ 2799 static struct task_struct *next_tid(struct task_struct *start) 2800 { 2801 struct task_struct *pos = NULL; 2802 rcu_read_lock(); 2803 if (pid_alive(start)) { 2804 pos = next_thread(start); 2805 if (thread_group_leader(pos)) 2806 pos = NULL; 2807 else 2808 get_task_struct(pos); 2809 } 2810 rcu_read_unlock(); 2811 put_task_struct(start); 2812 return pos; 2813 } 2814 2815 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 2816 struct task_struct *task, int tid) 2817 { 2818 char name[PROC_NUMBUF]; 2819 int len = snprintf(name, sizeof(name), "%d", tid); 2820 return proc_fill_cache(filp, dirent, filldir, name, len, 2821 proc_task_instantiate, task, NULL); 2822 } 2823 2824 /* for the /proc/TGID/task/ directories */ 2825 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 2826 { 2827 struct dentry *dentry = filp->f_path.dentry; 2828 struct inode *inode = dentry->d_inode; 2829 struct task_struct *leader = NULL; 2830 struct task_struct *task; 2831 int retval = -ENOENT; 2832 ino_t ino; 2833 int tid; 2834 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ 2835 struct pid_namespace *ns; 2836 2837 task = get_proc_task(inode); 2838 if (!task) 2839 goto out_no_task; 2840 rcu_read_lock(); 2841 if (pid_alive(task)) { 2842 leader = task->group_leader; 2843 get_task_struct(leader); 2844 } 2845 rcu_read_unlock(); 2846 put_task_struct(task); 2847 if (!leader) 2848 goto out_no_task; 2849 retval = 0; 2850 2851 switch (pos) { 2852 case 0: 2853 ino = inode->i_ino; 2854 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) 2855 goto out; 2856 pos++; 2857 /* fall through */ 2858 case 1: 2859 ino = parent_ino(dentry); 2860 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) 2861 goto out; 2862 pos++; 2863 /* fall through */ 2864 } 2865 2866 /* f_version caches the tgid value that the last readdir call couldn't 2867 * return. lseek aka telldir automagically resets f_version to 0. 2868 */ 2869 ns = filp->f_dentry->d_sb->s_fs_info; 2870 tid = (int)filp->f_version; 2871 filp->f_version = 0; 2872 for (task = first_tid(leader, tid, pos - 2, ns); 2873 task; 2874 task = next_tid(task), pos++) { 2875 tid = task_pid_nr_ns(task, ns); 2876 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 2877 /* returning this tgid failed, save it as the first 2878 * pid for the next readir call */ 2879 filp->f_version = (u64)tid; 2880 put_task_struct(task); 2881 break; 2882 } 2883 } 2884 out: 2885 filp->f_pos = pos; 2886 put_task_struct(leader); 2887 out_no_task: 2888 return retval; 2889 } 2890 2891 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 2892 { 2893 struct inode *inode = dentry->d_inode; 2894 struct task_struct *p = get_proc_task(inode); 2895 generic_fillattr(inode, stat); 2896 2897 if (p) { 2898 rcu_read_lock(); 2899 stat->nlink += get_nr_threads(p); 2900 rcu_read_unlock(); 2901 put_task_struct(p); 2902 } 2903 2904 return 0; 2905 } 2906 2907 static const struct inode_operations proc_task_inode_operations = { 2908 .lookup = proc_task_lookup, 2909 .getattr = proc_task_getattr, 2910 .setattr = proc_setattr, 2911 }; 2912 2913 static const struct file_operations proc_task_operations = { 2914 .read = generic_read_dir, 2915 .readdir = proc_task_readdir, 2916 }; 2917