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/config.h> 53 #include <linux/errno.h> 54 #include <linux/time.h> 55 #include <linux/proc_fs.h> 56 #include <linux/stat.h> 57 #include <linux/init.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/namespace.h> 63 #include <linux/mm.h> 64 #include <linux/smp_lock.h> 65 #include <linux/kallsyms.h> 66 #include <linux/mount.h> 67 #include <linux/security.h> 68 #include <linux/ptrace.h> 69 #include <linux/seccomp.h> 70 #include <linux/cpuset.h> 71 #include <linux/audit.h> 72 #include "internal.h" 73 74 /* 75 * For hysterical raisins we keep the same inumbers as in the old procfs. 76 * Feel free to change the macro below - just keep the range distinct from 77 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff). 78 * As soon as we'll get a separate superblock we will be able to forget 79 * about magical ranges too. 80 */ 81 82 #define fake_ino(pid,ino) (((pid)<<16)|(ino)) 83 84 enum pid_directory_inos { 85 PROC_TGID_INO = 2, 86 PROC_TGID_TASK, 87 PROC_TGID_STATUS, 88 PROC_TGID_MEM, 89 #ifdef CONFIG_SECCOMP 90 PROC_TGID_SECCOMP, 91 #endif 92 PROC_TGID_CWD, 93 PROC_TGID_ROOT, 94 PROC_TGID_EXE, 95 PROC_TGID_FD, 96 PROC_TGID_ENVIRON, 97 PROC_TGID_AUXV, 98 PROC_TGID_CMDLINE, 99 PROC_TGID_STAT, 100 PROC_TGID_STATM, 101 PROC_TGID_MAPS, 102 PROC_TGID_NUMA_MAPS, 103 PROC_TGID_MOUNTS, 104 PROC_TGID_WCHAN, 105 PROC_TGID_SMAPS, 106 #ifdef CONFIG_SCHEDSTATS 107 PROC_TGID_SCHEDSTAT, 108 #endif 109 #ifdef CONFIG_CPUSETS 110 PROC_TGID_CPUSET, 111 #endif 112 #ifdef CONFIG_SECURITY 113 PROC_TGID_ATTR, 114 PROC_TGID_ATTR_CURRENT, 115 PROC_TGID_ATTR_PREV, 116 PROC_TGID_ATTR_EXEC, 117 PROC_TGID_ATTR_FSCREATE, 118 #endif 119 #ifdef CONFIG_AUDITSYSCALL 120 PROC_TGID_LOGINUID, 121 #endif 122 PROC_TGID_OOM_SCORE, 123 PROC_TGID_OOM_ADJUST, 124 PROC_TID_INO, 125 PROC_TID_STATUS, 126 PROC_TID_MEM, 127 #ifdef CONFIG_SECCOMP 128 PROC_TID_SECCOMP, 129 #endif 130 PROC_TID_CWD, 131 PROC_TID_ROOT, 132 PROC_TID_EXE, 133 PROC_TID_FD, 134 PROC_TID_ENVIRON, 135 PROC_TID_AUXV, 136 PROC_TID_CMDLINE, 137 PROC_TID_STAT, 138 PROC_TID_STATM, 139 PROC_TID_MAPS, 140 PROC_TID_NUMA_MAPS, 141 PROC_TID_MOUNTS, 142 PROC_TID_WCHAN, 143 PROC_TID_SMAPS, 144 #ifdef CONFIG_SCHEDSTATS 145 PROC_TID_SCHEDSTAT, 146 #endif 147 #ifdef CONFIG_CPUSETS 148 PROC_TID_CPUSET, 149 #endif 150 #ifdef CONFIG_SECURITY 151 PROC_TID_ATTR, 152 PROC_TID_ATTR_CURRENT, 153 PROC_TID_ATTR_PREV, 154 PROC_TID_ATTR_EXEC, 155 PROC_TID_ATTR_FSCREATE, 156 #endif 157 #ifdef CONFIG_AUDITSYSCALL 158 PROC_TID_LOGINUID, 159 #endif 160 PROC_TID_OOM_SCORE, 161 PROC_TID_OOM_ADJUST, 162 163 /* Add new entries before this */ 164 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */ 165 }; 166 167 struct pid_entry { 168 int type; 169 int len; 170 char *name; 171 mode_t mode; 172 }; 173 174 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)} 175 176 static struct pid_entry tgid_base_stuff[] = { 177 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO), 178 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR), 179 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR), 180 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR), 181 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO), 182 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO), 183 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO), 184 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO), 185 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO), 186 #ifdef CONFIG_NUMA 187 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO), 188 #endif 189 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR), 190 #ifdef CONFIG_SECCOMP 191 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR), 192 #endif 193 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO), 194 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO), 195 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO), 196 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO), 197 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO), 198 #ifdef CONFIG_SECURITY 199 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO), 200 #endif 201 #ifdef CONFIG_KALLSYMS 202 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO), 203 #endif 204 #ifdef CONFIG_SCHEDSTATS 205 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO), 206 #endif 207 #ifdef CONFIG_CPUSETS 208 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO), 209 #endif 210 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO), 211 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR), 212 #ifdef CONFIG_AUDITSYSCALL 213 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO), 214 #endif 215 {0,0,NULL,0} 216 }; 217 static struct pid_entry tid_base_stuff[] = { 218 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR), 219 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR), 220 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR), 221 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO), 222 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO), 223 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO), 224 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO), 225 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO), 226 #ifdef CONFIG_NUMA 227 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO), 228 #endif 229 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR), 230 #ifdef CONFIG_SECCOMP 231 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR), 232 #endif 233 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO), 234 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO), 235 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO), 236 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO), 237 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO), 238 #ifdef CONFIG_SECURITY 239 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO), 240 #endif 241 #ifdef CONFIG_KALLSYMS 242 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO), 243 #endif 244 #ifdef CONFIG_SCHEDSTATS 245 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO), 246 #endif 247 #ifdef CONFIG_CPUSETS 248 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO), 249 #endif 250 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO), 251 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR), 252 #ifdef CONFIG_AUDITSYSCALL 253 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO), 254 #endif 255 {0,0,NULL,0} 256 }; 257 258 #ifdef CONFIG_SECURITY 259 static struct pid_entry tgid_attr_stuff[] = { 260 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO), 261 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO), 262 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO), 263 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO), 264 {0,0,NULL,0} 265 }; 266 static struct pid_entry tid_attr_stuff[] = { 267 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO), 268 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO), 269 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO), 270 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO), 271 {0,0,NULL,0} 272 }; 273 #endif 274 275 #undef E 276 277 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) 278 { 279 struct task_struct *task = proc_task(inode); 280 struct files_struct *files; 281 struct file *file; 282 int fd = proc_type(inode) - PROC_TID_FD_DIR; 283 284 files = get_files_struct(task); 285 if (files) { 286 spin_lock(&files->file_lock); 287 file = fcheck_files(files, fd); 288 if (file) { 289 *mnt = mntget(file->f_vfsmnt); 290 *dentry = dget(file->f_dentry); 291 spin_unlock(&files->file_lock); 292 put_files_struct(files); 293 return 0; 294 } 295 spin_unlock(&files->file_lock); 296 put_files_struct(files); 297 } 298 return -ENOENT; 299 } 300 301 static struct fs_struct *get_fs_struct(struct task_struct *task) 302 { 303 struct fs_struct *fs; 304 task_lock(task); 305 fs = task->fs; 306 if(fs) 307 atomic_inc(&fs->count); 308 task_unlock(task); 309 return fs; 310 } 311 312 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) 313 { 314 struct fs_struct *fs = get_fs_struct(proc_task(inode)); 315 int result = -ENOENT; 316 if (fs) { 317 read_lock(&fs->lock); 318 *mnt = mntget(fs->pwdmnt); 319 *dentry = dget(fs->pwd); 320 read_unlock(&fs->lock); 321 result = 0; 322 put_fs_struct(fs); 323 } 324 return result; 325 } 326 327 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) 328 { 329 struct fs_struct *fs = get_fs_struct(proc_task(inode)); 330 int result = -ENOENT; 331 if (fs) { 332 read_lock(&fs->lock); 333 *mnt = mntget(fs->rootmnt); 334 *dentry = dget(fs->root); 335 read_unlock(&fs->lock); 336 result = 0; 337 put_fs_struct(fs); 338 } 339 return result; 340 } 341 342 #define MAY_PTRACE(task) \ 343 (task == current || \ 344 (task->parent == current && \ 345 (task->ptrace & PT_PTRACED) && \ 346 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \ 347 security_ptrace(current,task) == 0)) 348 349 static int proc_pid_environ(struct task_struct *task, char * buffer) 350 { 351 int res = 0; 352 struct mm_struct *mm = get_task_mm(task); 353 if (mm) { 354 unsigned int len = mm->env_end - mm->env_start; 355 if (len > PAGE_SIZE) 356 len = PAGE_SIZE; 357 res = access_process_vm(task, mm->env_start, buffer, len, 0); 358 if (!ptrace_may_attach(task)) 359 res = -ESRCH; 360 mmput(mm); 361 } 362 return res; 363 } 364 365 static int proc_pid_cmdline(struct task_struct *task, char * buffer) 366 { 367 int res = 0; 368 unsigned int len; 369 struct mm_struct *mm = get_task_mm(task); 370 if (!mm) 371 goto out; 372 if (!mm->arg_end) 373 goto out_mm; /* Shh! No looking before we're done */ 374 375 len = mm->arg_end - mm->arg_start; 376 377 if (len > PAGE_SIZE) 378 len = PAGE_SIZE; 379 380 res = access_process_vm(task, mm->arg_start, buffer, len, 0); 381 382 // If the nul at the end of args has been overwritten, then 383 // assume application is using setproctitle(3). 384 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 385 len = strnlen(buffer, res); 386 if (len < res) { 387 res = len; 388 } else { 389 len = mm->env_end - mm->env_start; 390 if (len > PAGE_SIZE - res) 391 len = PAGE_SIZE - res; 392 res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 393 res = strnlen(buffer, res); 394 } 395 } 396 out_mm: 397 mmput(mm); 398 out: 399 return res; 400 } 401 402 static int proc_pid_auxv(struct task_struct *task, char *buffer) 403 { 404 int res = 0; 405 struct mm_struct *mm = get_task_mm(task); 406 if (mm) { 407 unsigned int nwords = 0; 408 do 409 nwords += 2; 410 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 411 res = nwords * sizeof(mm->saved_auxv[0]); 412 if (res > PAGE_SIZE) 413 res = PAGE_SIZE; 414 memcpy(buffer, mm->saved_auxv, res); 415 mmput(mm); 416 } 417 return res; 418 } 419 420 421 #ifdef CONFIG_KALLSYMS 422 /* 423 * Provides a wchan file via kallsyms in a proper one-value-per-file format. 424 * Returns the resolved symbol. If that fails, simply return the address. 425 */ 426 static int proc_pid_wchan(struct task_struct *task, char *buffer) 427 { 428 char *modname; 429 const char *sym_name; 430 unsigned long wchan, size, offset; 431 char namebuf[KSYM_NAME_LEN+1]; 432 433 wchan = get_wchan(task); 434 435 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf); 436 if (sym_name) 437 return sprintf(buffer, "%s", sym_name); 438 return sprintf(buffer, "%lu", wchan); 439 } 440 #endif /* CONFIG_KALLSYMS */ 441 442 #ifdef CONFIG_SCHEDSTATS 443 /* 444 * Provides /proc/PID/schedstat 445 */ 446 static int proc_pid_schedstat(struct task_struct *task, char *buffer) 447 { 448 return sprintf(buffer, "%lu %lu %lu\n", 449 task->sched_info.cpu_time, 450 task->sched_info.run_delay, 451 task->sched_info.pcnt); 452 } 453 #endif 454 455 /* The badness from the OOM killer */ 456 unsigned long badness(struct task_struct *p, unsigned long uptime); 457 static int proc_oom_score(struct task_struct *task, char *buffer) 458 { 459 unsigned long points; 460 struct timespec uptime; 461 462 do_posix_clock_monotonic_gettime(&uptime); 463 points = badness(task, uptime.tv_sec); 464 return sprintf(buffer, "%lu\n", points); 465 } 466 467 /************************************************************************/ 468 /* Here the fs part begins */ 469 /************************************************************************/ 470 471 /* permission checks */ 472 473 static int proc_check_root(struct inode *inode) 474 { 475 struct dentry *de, *base, *root; 476 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt; 477 int res = 0; 478 479 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */ 480 return -ENOENT; 481 read_lock(¤t->fs->lock); 482 our_vfsmnt = mntget(current->fs->rootmnt); 483 base = dget(current->fs->root); 484 read_unlock(¤t->fs->lock); 485 486 spin_lock(&vfsmount_lock); 487 de = root; 488 mnt = vfsmnt; 489 490 while (vfsmnt != our_vfsmnt) { 491 if (vfsmnt == vfsmnt->mnt_parent) 492 goto out; 493 de = vfsmnt->mnt_mountpoint; 494 vfsmnt = vfsmnt->mnt_parent; 495 } 496 497 if (!is_subdir(de, base)) 498 goto out; 499 spin_unlock(&vfsmount_lock); 500 501 exit: 502 dput(base); 503 mntput(our_vfsmnt); 504 dput(root); 505 mntput(mnt); 506 return res; 507 out: 508 spin_unlock(&vfsmount_lock); 509 res = -EACCES; 510 goto exit; 511 } 512 513 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd) 514 { 515 if (generic_permission(inode, mask, NULL) != 0) 516 return -EACCES; 517 return proc_check_root(inode); 518 } 519 520 extern struct seq_operations proc_pid_maps_op; 521 static int maps_open(struct inode *inode, struct file *file) 522 { 523 struct task_struct *task = proc_task(inode); 524 int ret = seq_open(file, &proc_pid_maps_op); 525 if (!ret) { 526 struct seq_file *m = file->private_data; 527 m->private = task; 528 } 529 return ret; 530 } 531 532 static struct file_operations proc_maps_operations = { 533 .open = maps_open, 534 .read = seq_read, 535 .llseek = seq_lseek, 536 .release = seq_release, 537 }; 538 539 #ifdef CONFIG_NUMA 540 extern struct seq_operations proc_pid_numa_maps_op; 541 static int numa_maps_open(struct inode *inode, struct file *file) 542 { 543 struct task_struct *task = proc_task(inode); 544 int ret = seq_open(file, &proc_pid_numa_maps_op); 545 if (!ret) { 546 struct seq_file *m = file->private_data; 547 m->private = task; 548 } 549 return ret; 550 } 551 552 static struct file_operations proc_numa_maps_operations = { 553 .open = numa_maps_open, 554 .read = seq_read, 555 .llseek = seq_lseek, 556 .release = seq_release, 557 }; 558 #endif 559 560 extern struct seq_operations proc_pid_smaps_op; 561 static int smaps_open(struct inode *inode, struct file *file) 562 { 563 struct task_struct *task = proc_task(inode); 564 int ret = seq_open(file, &proc_pid_smaps_op); 565 if (!ret) { 566 struct seq_file *m = file->private_data; 567 m->private = task; 568 } 569 return ret; 570 } 571 572 static struct file_operations proc_smaps_operations = { 573 .open = smaps_open, 574 .read = seq_read, 575 .llseek = seq_lseek, 576 .release = seq_release, 577 }; 578 579 extern struct seq_operations mounts_op; 580 static int mounts_open(struct inode *inode, struct file *file) 581 { 582 struct task_struct *task = proc_task(inode); 583 int ret = seq_open(file, &mounts_op); 584 585 if (!ret) { 586 struct seq_file *m = file->private_data; 587 struct namespace *namespace; 588 task_lock(task); 589 namespace = task->namespace; 590 if (namespace) 591 get_namespace(namespace); 592 task_unlock(task); 593 594 if (namespace) 595 m->private = namespace; 596 else { 597 seq_release(inode, file); 598 ret = -EINVAL; 599 } 600 } 601 return ret; 602 } 603 604 static int mounts_release(struct inode *inode, struct file *file) 605 { 606 struct seq_file *m = file->private_data; 607 struct namespace *namespace = m->private; 608 put_namespace(namespace); 609 return seq_release(inode, file); 610 } 611 612 static struct file_operations proc_mounts_operations = { 613 .open = mounts_open, 614 .read = seq_read, 615 .llseek = seq_lseek, 616 .release = mounts_release, 617 }; 618 619 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 620 621 static ssize_t proc_info_read(struct file * file, char __user * buf, 622 size_t count, loff_t *ppos) 623 { 624 struct inode * inode = file->f_dentry->d_inode; 625 unsigned long page; 626 ssize_t length; 627 struct task_struct *task = proc_task(inode); 628 629 if (count > PROC_BLOCK_SIZE) 630 count = PROC_BLOCK_SIZE; 631 if (!(page = __get_free_page(GFP_KERNEL))) 632 return -ENOMEM; 633 634 length = PROC_I(inode)->op.proc_read(task, (char*)page); 635 636 if (length >= 0) 637 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 638 free_page(page); 639 return length; 640 } 641 642 static struct file_operations proc_info_file_operations = { 643 .read = proc_info_read, 644 }; 645 646 static int mem_open(struct inode* inode, struct file* file) 647 { 648 file->private_data = (void*)((long)current->self_exec_id); 649 return 0; 650 } 651 652 static ssize_t mem_read(struct file * file, char __user * buf, 653 size_t count, loff_t *ppos) 654 { 655 struct task_struct *task = proc_task(file->f_dentry->d_inode); 656 char *page; 657 unsigned long src = *ppos; 658 int ret = -ESRCH; 659 struct mm_struct *mm; 660 661 if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 662 goto out; 663 664 ret = -ENOMEM; 665 page = (char *)__get_free_page(GFP_USER); 666 if (!page) 667 goto out; 668 669 ret = 0; 670 671 mm = get_task_mm(task); 672 if (!mm) 673 goto out_free; 674 675 ret = -EIO; 676 677 if (file->private_data != (void*)((long)current->self_exec_id)) 678 goto out_put; 679 680 ret = 0; 681 682 while (count > 0) { 683 int this_len, retval; 684 685 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 686 retval = access_process_vm(task, src, page, this_len, 0); 687 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) { 688 if (!ret) 689 ret = -EIO; 690 break; 691 } 692 693 if (copy_to_user(buf, page, retval)) { 694 ret = -EFAULT; 695 break; 696 } 697 698 ret += retval; 699 src += retval; 700 buf += retval; 701 count -= retval; 702 } 703 *ppos = src; 704 705 out_put: 706 mmput(mm); 707 out_free: 708 free_page((unsigned long) page); 709 out: 710 return ret; 711 } 712 713 #define mem_write NULL 714 715 #ifndef mem_write 716 /* This is a security hazard */ 717 static ssize_t mem_write(struct file * file, const char * buf, 718 size_t count, loff_t *ppos) 719 { 720 int copied = 0; 721 char *page; 722 struct task_struct *task = proc_task(file->f_dentry->d_inode); 723 unsigned long dst = *ppos; 724 725 if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 726 return -ESRCH; 727 728 page = (char *)__get_free_page(GFP_USER); 729 if (!page) 730 return -ENOMEM; 731 732 while (count > 0) { 733 int this_len, retval; 734 735 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 736 if (copy_from_user(page, buf, this_len)) { 737 copied = -EFAULT; 738 break; 739 } 740 retval = access_process_vm(task, dst, page, this_len, 1); 741 if (!retval) { 742 if (!copied) 743 copied = -EIO; 744 break; 745 } 746 copied += retval; 747 buf += retval; 748 dst += retval; 749 count -= retval; 750 } 751 *ppos = dst; 752 free_page((unsigned long) page); 753 return copied; 754 } 755 #endif 756 757 static loff_t mem_lseek(struct file * file, loff_t offset, int orig) 758 { 759 switch (orig) { 760 case 0: 761 file->f_pos = offset; 762 break; 763 case 1: 764 file->f_pos += offset; 765 break; 766 default: 767 return -EINVAL; 768 } 769 force_successful_syscall_return(); 770 return file->f_pos; 771 } 772 773 static struct file_operations proc_mem_operations = { 774 .llseek = mem_lseek, 775 .read = mem_read, 776 .write = mem_write, 777 .open = mem_open, 778 }; 779 780 static ssize_t oom_adjust_read(struct file *file, char __user *buf, 781 size_t count, loff_t *ppos) 782 { 783 struct task_struct *task = proc_task(file->f_dentry->d_inode); 784 char buffer[8]; 785 size_t len; 786 int oom_adjust = task->oomkilladj; 787 loff_t __ppos = *ppos; 788 789 len = sprintf(buffer, "%i\n", oom_adjust); 790 if (__ppos >= len) 791 return 0; 792 if (count > len-__ppos) 793 count = len-__ppos; 794 if (copy_to_user(buf, buffer + __ppos, count)) 795 return -EFAULT; 796 *ppos = __ppos + count; 797 return count; 798 } 799 800 static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 801 size_t count, loff_t *ppos) 802 { 803 struct task_struct *task = proc_task(file->f_dentry->d_inode); 804 char buffer[8], *end; 805 int oom_adjust; 806 807 if (!capable(CAP_SYS_RESOURCE)) 808 return -EPERM; 809 memset(buffer, 0, 8); 810 if (count > 6) 811 count = 6; 812 if (copy_from_user(buffer, buf, count)) 813 return -EFAULT; 814 oom_adjust = simple_strtol(buffer, &end, 0); 815 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE) 816 return -EINVAL; 817 if (*end == '\n') 818 end++; 819 task->oomkilladj = oom_adjust; 820 if (end - buffer == 0) 821 return -EIO; 822 return end - buffer; 823 } 824 825 static struct file_operations proc_oom_adjust_operations = { 826 .read = oom_adjust_read, 827 .write = oom_adjust_write, 828 }; 829 830 static struct inode_operations proc_mem_inode_operations = { 831 .permission = proc_permission, 832 }; 833 834 #ifdef CONFIG_AUDITSYSCALL 835 #define TMPBUFLEN 21 836 static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 837 size_t count, loff_t *ppos) 838 { 839 struct inode * inode = file->f_dentry->d_inode; 840 struct task_struct *task = proc_task(inode); 841 ssize_t length; 842 char tmpbuf[TMPBUFLEN]; 843 844 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 845 audit_get_loginuid(task->audit_context)); 846 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 847 } 848 849 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 850 size_t count, loff_t *ppos) 851 { 852 struct inode * inode = file->f_dentry->d_inode; 853 char *page, *tmp; 854 ssize_t length; 855 struct task_struct *task = proc_task(inode); 856 uid_t loginuid; 857 858 if (!capable(CAP_AUDIT_CONTROL)) 859 return -EPERM; 860 861 if (current != task) 862 return -EPERM; 863 864 if (count > PAGE_SIZE) 865 count = PAGE_SIZE; 866 867 if (*ppos != 0) { 868 /* No partial writes. */ 869 return -EINVAL; 870 } 871 page = (char*)__get_free_page(GFP_USER); 872 if (!page) 873 return -ENOMEM; 874 length = -EFAULT; 875 if (copy_from_user(page, buf, count)) 876 goto out_free_page; 877 878 loginuid = simple_strtoul(page, &tmp, 10); 879 if (tmp == page) { 880 length = -EINVAL; 881 goto out_free_page; 882 883 } 884 length = audit_set_loginuid(task, loginuid); 885 if (likely(length == 0)) 886 length = count; 887 888 out_free_page: 889 free_page((unsigned long) page); 890 return length; 891 } 892 893 static struct file_operations proc_loginuid_operations = { 894 .read = proc_loginuid_read, 895 .write = proc_loginuid_write, 896 }; 897 #endif 898 899 #ifdef CONFIG_SECCOMP 900 static ssize_t seccomp_read(struct file *file, char __user *buf, 901 size_t count, loff_t *ppos) 902 { 903 struct task_struct *tsk = proc_task(file->f_dentry->d_inode); 904 char __buf[20]; 905 loff_t __ppos = *ppos; 906 size_t len; 907 908 /* no need to print the trailing zero, so use only len */ 909 len = sprintf(__buf, "%u\n", tsk->seccomp.mode); 910 if (__ppos >= len) 911 return 0; 912 if (count > len - __ppos) 913 count = len - __ppos; 914 if (copy_to_user(buf, __buf + __ppos, count)) 915 return -EFAULT; 916 *ppos = __ppos + count; 917 return count; 918 } 919 920 static ssize_t seccomp_write(struct file *file, const char __user *buf, 921 size_t count, loff_t *ppos) 922 { 923 struct task_struct *tsk = proc_task(file->f_dentry->d_inode); 924 char __buf[20], *end; 925 unsigned int seccomp_mode; 926 927 /* can set it only once to be even more secure */ 928 if (unlikely(tsk->seccomp.mode)) 929 return -EPERM; 930 931 memset(__buf, 0, sizeof(__buf)); 932 count = min(count, sizeof(__buf) - 1); 933 if (copy_from_user(__buf, buf, count)) 934 return -EFAULT; 935 seccomp_mode = simple_strtoul(__buf, &end, 0); 936 if (*end == '\n') 937 end++; 938 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) { 939 tsk->seccomp.mode = seccomp_mode; 940 set_tsk_thread_flag(tsk, TIF_SECCOMP); 941 } else 942 return -EINVAL; 943 if (unlikely(!(end - __buf))) 944 return -EIO; 945 return end - __buf; 946 } 947 948 static struct file_operations proc_seccomp_operations = { 949 .read = seccomp_read, 950 .write = seccomp_write, 951 }; 952 #endif /* CONFIG_SECCOMP */ 953 954 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 955 { 956 struct inode *inode = dentry->d_inode; 957 int error = -EACCES; 958 959 /* We don't need a base pointer in the /proc filesystem */ 960 path_release(nd); 961 962 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE)) 963 goto out; 964 error = proc_check_root(inode); 965 if (error) 966 goto out; 967 968 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt); 969 nd->last_type = LAST_BIND; 970 out: 971 return ERR_PTR(error); 972 } 973 974 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt, 975 char __user *buffer, int buflen) 976 { 977 struct inode * inode; 978 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path; 979 int len; 980 981 if (!tmp) 982 return -ENOMEM; 983 984 inode = dentry->d_inode; 985 path = d_path(dentry, mnt, tmp, PAGE_SIZE); 986 len = PTR_ERR(path); 987 if (IS_ERR(path)) 988 goto out; 989 len = tmp + PAGE_SIZE - 1 - path; 990 991 if (len > buflen) 992 len = buflen; 993 if (copy_to_user(buffer, path, len)) 994 len = -EFAULT; 995 out: 996 free_page((unsigned long)tmp); 997 return len; 998 } 999 1000 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 1001 { 1002 int error = -EACCES; 1003 struct inode *inode = dentry->d_inode; 1004 struct dentry *de; 1005 struct vfsmount *mnt = NULL; 1006 1007 lock_kernel(); 1008 1009 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE)) 1010 goto out; 1011 error = proc_check_root(inode); 1012 if (error) 1013 goto out; 1014 1015 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt); 1016 if (error) 1017 goto out; 1018 1019 error = do_proc_readlink(de, mnt, buffer, buflen); 1020 dput(de); 1021 mntput(mnt); 1022 out: 1023 unlock_kernel(); 1024 return error; 1025 } 1026 1027 static struct inode_operations proc_pid_link_inode_operations = { 1028 .readlink = proc_pid_readlink, 1029 .follow_link = proc_pid_follow_link 1030 }; 1031 1032 #define NUMBUF 10 1033 1034 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir) 1035 { 1036 struct inode *inode = filp->f_dentry->d_inode; 1037 struct task_struct *p = proc_task(inode); 1038 unsigned int fd, tid, ino; 1039 int retval; 1040 char buf[NUMBUF]; 1041 struct files_struct * files; 1042 1043 retval = -ENOENT; 1044 if (!pid_alive(p)) 1045 goto out; 1046 retval = 0; 1047 tid = p->pid; 1048 1049 fd = filp->f_pos; 1050 switch (fd) { 1051 case 0: 1052 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 1053 goto out; 1054 filp->f_pos++; 1055 case 1: 1056 ino = fake_ino(tid, PROC_TID_INO); 1057 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 1058 goto out; 1059 filp->f_pos++; 1060 default: 1061 files = get_files_struct(p); 1062 if (!files) 1063 goto out; 1064 spin_lock(&files->file_lock); 1065 for (fd = filp->f_pos-2; 1066 fd < files->max_fds; 1067 fd++, filp->f_pos++) { 1068 unsigned int i,j; 1069 1070 if (!fcheck_files(files, fd)) 1071 continue; 1072 spin_unlock(&files->file_lock); 1073 1074 j = NUMBUF; 1075 i = fd; 1076 do { 1077 j--; 1078 buf[j] = '0' + (i % 10); 1079 i /= 10; 1080 } while (i); 1081 1082 ino = fake_ino(tid, PROC_TID_FD_DIR + fd); 1083 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) { 1084 spin_lock(&files->file_lock); 1085 break; 1086 } 1087 spin_lock(&files->file_lock); 1088 } 1089 spin_unlock(&files->file_lock); 1090 put_files_struct(files); 1091 } 1092 out: 1093 return retval; 1094 } 1095 1096 static int proc_pident_readdir(struct file *filp, 1097 void *dirent, filldir_t filldir, 1098 struct pid_entry *ents, unsigned int nents) 1099 { 1100 int i; 1101 int pid; 1102 struct dentry *dentry = filp->f_dentry; 1103 struct inode *inode = dentry->d_inode; 1104 struct pid_entry *p; 1105 ino_t ino; 1106 int ret; 1107 1108 ret = -ENOENT; 1109 if (!pid_alive(proc_task(inode))) 1110 goto out; 1111 1112 ret = 0; 1113 pid = proc_task(inode)->pid; 1114 i = filp->f_pos; 1115 switch (i) { 1116 case 0: 1117 ino = inode->i_ino; 1118 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 1119 goto out; 1120 i++; 1121 filp->f_pos++; 1122 /* fall through */ 1123 case 1: 1124 ino = parent_ino(dentry); 1125 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 1126 goto out; 1127 i++; 1128 filp->f_pos++; 1129 /* fall through */ 1130 default: 1131 i -= 2; 1132 if (i >= nents) { 1133 ret = 1; 1134 goto out; 1135 } 1136 p = ents + i; 1137 while (p->name) { 1138 if (filldir(dirent, p->name, p->len, filp->f_pos, 1139 fake_ino(pid, p->type), p->mode >> 12) < 0) 1140 goto out; 1141 filp->f_pos++; 1142 p++; 1143 } 1144 } 1145 1146 ret = 1; 1147 out: 1148 return ret; 1149 } 1150 1151 static int proc_tgid_base_readdir(struct file * filp, 1152 void * dirent, filldir_t filldir) 1153 { 1154 return proc_pident_readdir(filp,dirent,filldir, 1155 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 1156 } 1157 1158 static int proc_tid_base_readdir(struct file * filp, 1159 void * dirent, filldir_t filldir) 1160 { 1161 return proc_pident_readdir(filp,dirent,filldir, 1162 tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 1163 } 1164 1165 /* building an inode */ 1166 1167 static int task_dumpable(struct task_struct *task) 1168 { 1169 int dumpable = 0; 1170 struct mm_struct *mm; 1171 1172 task_lock(task); 1173 mm = task->mm; 1174 if (mm) 1175 dumpable = mm->dumpable; 1176 task_unlock(task); 1177 if(dumpable == 1) 1178 return 1; 1179 return 0; 1180 } 1181 1182 1183 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino) 1184 { 1185 struct inode * inode; 1186 struct proc_inode *ei; 1187 1188 /* We need a new inode */ 1189 1190 inode = new_inode(sb); 1191 if (!inode) 1192 goto out; 1193 1194 /* Common stuff */ 1195 ei = PROC_I(inode); 1196 ei->task = NULL; 1197 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 1198 inode->i_ino = fake_ino(task->pid, ino); 1199 1200 if (!pid_alive(task)) 1201 goto out_unlock; 1202 1203 /* 1204 * grab the reference to task. 1205 */ 1206 get_task_struct(task); 1207 ei->task = task; 1208 ei->type = ino; 1209 inode->i_uid = 0; 1210 inode->i_gid = 0; 1211 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) { 1212 inode->i_uid = task->euid; 1213 inode->i_gid = task->egid; 1214 } 1215 security_task_to_inode(task, inode); 1216 1217 out: 1218 return inode; 1219 1220 out_unlock: 1221 ei->pde = NULL; 1222 iput(inode); 1223 return NULL; 1224 } 1225 1226 /* dentry stuff */ 1227 1228 /* 1229 * Exceptional case: normally we are not allowed to unhash a busy 1230 * directory. In this case, however, we can do it - no aliasing problems 1231 * due to the way we treat inodes. 1232 * 1233 * Rewrite the inode's ownerships here because the owning task may have 1234 * performed a setuid(), etc. 1235 */ 1236 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 1237 { 1238 struct inode *inode = dentry->d_inode; 1239 struct task_struct *task = proc_task(inode); 1240 if (pid_alive(task)) { 1241 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) { 1242 inode->i_uid = task->euid; 1243 inode->i_gid = task->egid; 1244 } else { 1245 inode->i_uid = 0; 1246 inode->i_gid = 0; 1247 } 1248 security_task_to_inode(task, inode); 1249 return 1; 1250 } 1251 d_drop(dentry); 1252 return 0; 1253 } 1254 1255 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 1256 { 1257 struct inode *inode = dentry->d_inode; 1258 struct task_struct *task = proc_task(inode); 1259 int fd = proc_type(inode) - PROC_TID_FD_DIR; 1260 struct files_struct *files; 1261 1262 files = get_files_struct(task); 1263 if (files) { 1264 spin_lock(&files->file_lock); 1265 if (fcheck_files(files, fd)) { 1266 spin_unlock(&files->file_lock); 1267 put_files_struct(files); 1268 if (task_dumpable(task)) { 1269 inode->i_uid = task->euid; 1270 inode->i_gid = task->egid; 1271 } else { 1272 inode->i_uid = 0; 1273 inode->i_gid = 0; 1274 } 1275 security_task_to_inode(task, inode); 1276 return 1; 1277 } 1278 spin_unlock(&files->file_lock); 1279 put_files_struct(files); 1280 } 1281 d_drop(dentry); 1282 return 0; 1283 } 1284 1285 static void pid_base_iput(struct dentry *dentry, struct inode *inode) 1286 { 1287 struct task_struct *task = proc_task(inode); 1288 spin_lock(&task->proc_lock); 1289 if (task->proc_dentry == dentry) 1290 task->proc_dentry = NULL; 1291 spin_unlock(&task->proc_lock); 1292 iput(inode); 1293 } 1294 1295 static int pid_delete_dentry(struct dentry * dentry) 1296 { 1297 /* Is the task we represent dead? 1298 * If so, then don't put the dentry on the lru list, 1299 * kill it immediately. 1300 */ 1301 return !pid_alive(proc_task(dentry->d_inode)); 1302 } 1303 1304 static struct dentry_operations tid_fd_dentry_operations = 1305 { 1306 .d_revalidate = tid_fd_revalidate, 1307 .d_delete = pid_delete_dentry, 1308 }; 1309 1310 static struct dentry_operations pid_dentry_operations = 1311 { 1312 .d_revalidate = pid_revalidate, 1313 .d_delete = pid_delete_dentry, 1314 }; 1315 1316 static struct dentry_operations pid_base_dentry_operations = 1317 { 1318 .d_revalidate = pid_revalidate, 1319 .d_iput = pid_base_iput, 1320 .d_delete = pid_delete_dentry, 1321 }; 1322 1323 /* Lookups */ 1324 1325 static unsigned name_to_int(struct dentry *dentry) 1326 { 1327 const char *name = dentry->d_name.name; 1328 int len = dentry->d_name.len; 1329 unsigned n = 0; 1330 1331 if (len > 1 && *name == '0') 1332 goto out; 1333 while (len-- > 0) { 1334 unsigned c = *name++ - '0'; 1335 if (c > 9) 1336 goto out; 1337 if (n >= (~0U-9)/10) 1338 goto out; 1339 n *= 10; 1340 n += c; 1341 } 1342 return n; 1343 out: 1344 return ~0U; 1345 } 1346 1347 /* SMP-safe */ 1348 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd) 1349 { 1350 struct task_struct *task = proc_task(dir); 1351 unsigned fd = name_to_int(dentry); 1352 struct file * file; 1353 struct files_struct * files; 1354 struct inode *inode; 1355 struct proc_inode *ei; 1356 1357 if (fd == ~0U) 1358 goto out; 1359 if (!pid_alive(task)) 1360 goto out; 1361 1362 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd); 1363 if (!inode) 1364 goto out; 1365 ei = PROC_I(inode); 1366 files = get_files_struct(task); 1367 if (!files) 1368 goto out_unlock; 1369 inode->i_mode = S_IFLNK; 1370 spin_lock(&files->file_lock); 1371 file = fcheck_files(files, fd); 1372 if (!file) 1373 goto out_unlock2; 1374 if (file->f_mode & 1) 1375 inode->i_mode |= S_IRUSR | S_IXUSR; 1376 if (file->f_mode & 2) 1377 inode->i_mode |= S_IWUSR | S_IXUSR; 1378 spin_unlock(&files->file_lock); 1379 put_files_struct(files); 1380 inode->i_op = &proc_pid_link_inode_operations; 1381 inode->i_size = 64; 1382 ei->op.proc_get_link = proc_fd_link; 1383 dentry->d_op = &tid_fd_dentry_operations; 1384 d_add(dentry, inode); 1385 return NULL; 1386 1387 out_unlock2: 1388 spin_unlock(&files->file_lock); 1389 put_files_struct(files); 1390 out_unlock: 1391 iput(inode); 1392 out: 1393 return ERR_PTR(-ENOENT); 1394 } 1395 1396 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir); 1397 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd); 1398 1399 static struct file_operations proc_fd_operations = { 1400 .read = generic_read_dir, 1401 .readdir = proc_readfd, 1402 }; 1403 1404 static struct file_operations proc_task_operations = { 1405 .read = generic_read_dir, 1406 .readdir = proc_task_readdir, 1407 }; 1408 1409 /* 1410 * proc directories can do almost nothing.. 1411 */ 1412 static struct inode_operations proc_fd_inode_operations = { 1413 .lookup = proc_lookupfd, 1414 .permission = proc_permission, 1415 }; 1416 1417 static struct inode_operations proc_task_inode_operations = { 1418 .lookup = proc_task_lookup, 1419 .permission = proc_permission, 1420 }; 1421 1422 #ifdef CONFIG_SECURITY 1423 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 1424 size_t count, loff_t *ppos) 1425 { 1426 struct inode * inode = file->f_dentry->d_inode; 1427 unsigned long page; 1428 ssize_t length; 1429 struct task_struct *task = proc_task(inode); 1430 1431 if (count > PAGE_SIZE) 1432 count = PAGE_SIZE; 1433 if (!(page = __get_free_page(GFP_KERNEL))) 1434 return -ENOMEM; 1435 1436 length = security_getprocattr(task, 1437 (char*)file->f_dentry->d_name.name, 1438 (void*)page, count); 1439 if (length >= 0) 1440 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 1441 free_page(page); 1442 return length; 1443 } 1444 1445 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 1446 size_t count, loff_t *ppos) 1447 { 1448 struct inode * inode = file->f_dentry->d_inode; 1449 char *page; 1450 ssize_t length; 1451 struct task_struct *task = proc_task(inode); 1452 1453 if (count > PAGE_SIZE) 1454 count = PAGE_SIZE; 1455 if (*ppos != 0) { 1456 /* No partial writes. */ 1457 return -EINVAL; 1458 } 1459 page = (char*)__get_free_page(GFP_USER); 1460 if (!page) 1461 return -ENOMEM; 1462 length = -EFAULT; 1463 if (copy_from_user(page, buf, count)) 1464 goto out; 1465 1466 length = security_setprocattr(task, 1467 (char*)file->f_dentry->d_name.name, 1468 (void*)page, count); 1469 out: 1470 free_page((unsigned long) page); 1471 return length; 1472 } 1473 1474 static struct file_operations proc_pid_attr_operations = { 1475 .read = proc_pid_attr_read, 1476 .write = proc_pid_attr_write, 1477 }; 1478 1479 static struct file_operations proc_tid_attr_operations; 1480 static struct inode_operations proc_tid_attr_inode_operations; 1481 static struct file_operations proc_tgid_attr_operations; 1482 static struct inode_operations proc_tgid_attr_inode_operations; 1483 #endif 1484 1485 static int get_tid_list(int index, unsigned int *tids, struct inode *dir); 1486 1487 /* SMP-safe */ 1488 static struct dentry *proc_pident_lookup(struct inode *dir, 1489 struct dentry *dentry, 1490 struct pid_entry *ents) 1491 { 1492 struct inode *inode; 1493 int error; 1494 struct task_struct *task = proc_task(dir); 1495 struct pid_entry *p; 1496 struct proc_inode *ei; 1497 1498 error = -ENOENT; 1499 inode = NULL; 1500 1501 if (!pid_alive(task)) 1502 goto out; 1503 1504 for (p = ents; p->name; p++) { 1505 if (p->len != dentry->d_name.len) 1506 continue; 1507 if (!memcmp(dentry->d_name.name, p->name, p->len)) 1508 break; 1509 } 1510 if (!p->name) 1511 goto out; 1512 1513 error = -EINVAL; 1514 inode = proc_pid_make_inode(dir->i_sb, task, p->type); 1515 if (!inode) 1516 goto out; 1517 1518 ei = PROC_I(inode); 1519 inode->i_mode = p->mode; 1520 /* 1521 * Yes, it does not scale. And it should not. Don't add 1522 * new entries into /proc/<tgid>/ without very good reasons. 1523 */ 1524 switch(p->type) { 1525 case PROC_TGID_TASK: 1526 inode->i_nlink = 2 + get_tid_list(2, NULL, dir); 1527 inode->i_op = &proc_task_inode_operations; 1528 inode->i_fop = &proc_task_operations; 1529 break; 1530 case PROC_TID_FD: 1531 case PROC_TGID_FD: 1532 inode->i_nlink = 2; 1533 inode->i_op = &proc_fd_inode_operations; 1534 inode->i_fop = &proc_fd_operations; 1535 break; 1536 case PROC_TID_EXE: 1537 case PROC_TGID_EXE: 1538 inode->i_op = &proc_pid_link_inode_operations; 1539 ei->op.proc_get_link = proc_exe_link; 1540 break; 1541 case PROC_TID_CWD: 1542 case PROC_TGID_CWD: 1543 inode->i_op = &proc_pid_link_inode_operations; 1544 ei->op.proc_get_link = proc_cwd_link; 1545 break; 1546 case PROC_TID_ROOT: 1547 case PROC_TGID_ROOT: 1548 inode->i_op = &proc_pid_link_inode_operations; 1549 ei->op.proc_get_link = proc_root_link; 1550 break; 1551 case PROC_TID_ENVIRON: 1552 case PROC_TGID_ENVIRON: 1553 inode->i_fop = &proc_info_file_operations; 1554 ei->op.proc_read = proc_pid_environ; 1555 break; 1556 case PROC_TID_AUXV: 1557 case PROC_TGID_AUXV: 1558 inode->i_fop = &proc_info_file_operations; 1559 ei->op.proc_read = proc_pid_auxv; 1560 break; 1561 case PROC_TID_STATUS: 1562 case PROC_TGID_STATUS: 1563 inode->i_fop = &proc_info_file_operations; 1564 ei->op.proc_read = proc_pid_status; 1565 break; 1566 case PROC_TID_STAT: 1567 inode->i_fop = &proc_info_file_operations; 1568 ei->op.proc_read = proc_tid_stat; 1569 break; 1570 case PROC_TGID_STAT: 1571 inode->i_fop = &proc_info_file_operations; 1572 ei->op.proc_read = proc_tgid_stat; 1573 break; 1574 case PROC_TID_CMDLINE: 1575 case PROC_TGID_CMDLINE: 1576 inode->i_fop = &proc_info_file_operations; 1577 ei->op.proc_read = proc_pid_cmdline; 1578 break; 1579 case PROC_TID_STATM: 1580 case PROC_TGID_STATM: 1581 inode->i_fop = &proc_info_file_operations; 1582 ei->op.proc_read = proc_pid_statm; 1583 break; 1584 case PROC_TID_MAPS: 1585 case PROC_TGID_MAPS: 1586 inode->i_fop = &proc_maps_operations; 1587 break; 1588 #ifdef CONFIG_NUMA 1589 case PROC_TID_NUMA_MAPS: 1590 case PROC_TGID_NUMA_MAPS: 1591 inode->i_fop = &proc_numa_maps_operations; 1592 break; 1593 #endif 1594 case PROC_TID_MEM: 1595 case PROC_TGID_MEM: 1596 inode->i_op = &proc_mem_inode_operations; 1597 inode->i_fop = &proc_mem_operations; 1598 break; 1599 #ifdef CONFIG_SECCOMP 1600 case PROC_TID_SECCOMP: 1601 case PROC_TGID_SECCOMP: 1602 inode->i_fop = &proc_seccomp_operations; 1603 break; 1604 #endif /* CONFIG_SECCOMP */ 1605 case PROC_TID_MOUNTS: 1606 case PROC_TGID_MOUNTS: 1607 inode->i_fop = &proc_mounts_operations; 1608 break; 1609 case PROC_TID_SMAPS: 1610 case PROC_TGID_SMAPS: 1611 inode->i_fop = &proc_smaps_operations; 1612 break; 1613 #ifdef CONFIG_SECURITY 1614 case PROC_TID_ATTR: 1615 inode->i_nlink = 2; 1616 inode->i_op = &proc_tid_attr_inode_operations; 1617 inode->i_fop = &proc_tid_attr_operations; 1618 break; 1619 case PROC_TGID_ATTR: 1620 inode->i_nlink = 2; 1621 inode->i_op = &proc_tgid_attr_inode_operations; 1622 inode->i_fop = &proc_tgid_attr_operations; 1623 break; 1624 case PROC_TID_ATTR_CURRENT: 1625 case PROC_TGID_ATTR_CURRENT: 1626 case PROC_TID_ATTR_PREV: 1627 case PROC_TGID_ATTR_PREV: 1628 case PROC_TID_ATTR_EXEC: 1629 case PROC_TGID_ATTR_EXEC: 1630 case PROC_TID_ATTR_FSCREATE: 1631 case PROC_TGID_ATTR_FSCREATE: 1632 inode->i_fop = &proc_pid_attr_operations; 1633 break; 1634 #endif 1635 #ifdef CONFIG_KALLSYMS 1636 case PROC_TID_WCHAN: 1637 case PROC_TGID_WCHAN: 1638 inode->i_fop = &proc_info_file_operations; 1639 ei->op.proc_read = proc_pid_wchan; 1640 break; 1641 #endif 1642 #ifdef CONFIG_SCHEDSTATS 1643 case PROC_TID_SCHEDSTAT: 1644 case PROC_TGID_SCHEDSTAT: 1645 inode->i_fop = &proc_info_file_operations; 1646 ei->op.proc_read = proc_pid_schedstat; 1647 break; 1648 #endif 1649 #ifdef CONFIG_CPUSETS 1650 case PROC_TID_CPUSET: 1651 case PROC_TGID_CPUSET: 1652 inode->i_fop = &proc_cpuset_operations; 1653 break; 1654 #endif 1655 case PROC_TID_OOM_SCORE: 1656 case PROC_TGID_OOM_SCORE: 1657 inode->i_fop = &proc_info_file_operations; 1658 ei->op.proc_read = proc_oom_score; 1659 break; 1660 case PROC_TID_OOM_ADJUST: 1661 case PROC_TGID_OOM_ADJUST: 1662 inode->i_fop = &proc_oom_adjust_operations; 1663 break; 1664 #ifdef CONFIG_AUDITSYSCALL 1665 case PROC_TID_LOGINUID: 1666 case PROC_TGID_LOGINUID: 1667 inode->i_fop = &proc_loginuid_operations; 1668 break; 1669 #endif 1670 default: 1671 printk("procfs: impossible type (%d)",p->type); 1672 iput(inode); 1673 return ERR_PTR(-EINVAL); 1674 } 1675 dentry->d_op = &pid_dentry_operations; 1676 d_add(dentry, inode); 1677 return NULL; 1678 1679 out: 1680 return ERR_PTR(error); 1681 } 1682 1683 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 1684 return proc_pident_lookup(dir, dentry, tgid_base_stuff); 1685 } 1686 1687 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 1688 return proc_pident_lookup(dir, dentry, tid_base_stuff); 1689 } 1690 1691 static struct file_operations proc_tgid_base_operations = { 1692 .read = generic_read_dir, 1693 .readdir = proc_tgid_base_readdir, 1694 }; 1695 1696 static struct file_operations proc_tid_base_operations = { 1697 .read = generic_read_dir, 1698 .readdir = proc_tid_base_readdir, 1699 }; 1700 1701 static struct inode_operations proc_tgid_base_inode_operations = { 1702 .lookup = proc_tgid_base_lookup, 1703 }; 1704 1705 static struct inode_operations proc_tid_base_inode_operations = { 1706 .lookup = proc_tid_base_lookup, 1707 }; 1708 1709 #ifdef CONFIG_SECURITY 1710 static int proc_tgid_attr_readdir(struct file * filp, 1711 void * dirent, filldir_t filldir) 1712 { 1713 return proc_pident_readdir(filp,dirent,filldir, 1714 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff)); 1715 } 1716 1717 static int proc_tid_attr_readdir(struct file * filp, 1718 void * dirent, filldir_t filldir) 1719 { 1720 return proc_pident_readdir(filp,dirent,filldir, 1721 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff)); 1722 } 1723 1724 static struct file_operations proc_tgid_attr_operations = { 1725 .read = generic_read_dir, 1726 .readdir = proc_tgid_attr_readdir, 1727 }; 1728 1729 static struct file_operations proc_tid_attr_operations = { 1730 .read = generic_read_dir, 1731 .readdir = proc_tid_attr_readdir, 1732 }; 1733 1734 static struct dentry *proc_tgid_attr_lookup(struct inode *dir, 1735 struct dentry *dentry, struct nameidata *nd) 1736 { 1737 return proc_pident_lookup(dir, dentry, tgid_attr_stuff); 1738 } 1739 1740 static struct dentry *proc_tid_attr_lookup(struct inode *dir, 1741 struct dentry *dentry, struct nameidata *nd) 1742 { 1743 return proc_pident_lookup(dir, dentry, tid_attr_stuff); 1744 } 1745 1746 static struct inode_operations proc_tgid_attr_inode_operations = { 1747 .lookup = proc_tgid_attr_lookup, 1748 }; 1749 1750 static struct inode_operations proc_tid_attr_inode_operations = { 1751 .lookup = proc_tid_attr_lookup, 1752 }; 1753 #endif 1754 1755 /* 1756 * /proc/self: 1757 */ 1758 static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 1759 int buflen) 1760 { 1761 char tmp[30]; 1762 sprintf(tmp, "%d", current->tgid); 1763 return vfs_readlink(dentry,buffer,buflen,tmp); 1764 } 1765 1766 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 1767 { 1768 char tmp[30]; 1769 sprintf(tmp, "%d", current->tgid); 1770 return ERR_PTR(vfs_follow_link(nd,tmp)); 1771 } 1772 1773 static struct inode_operations proc_self_inode_operations = { 1774 .readlink = proc_self_readlink, 1775 .follow_link = proc_self_follow_link, 1776 }; 1777 1778 /** 1779 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache. 1780 * @p: task that should be flushed. 1781 * 1782 * Drops the /proc/@pid dcache entry from the hash chains. 1783 * 1784 * Dropping /proc/@pid entries and detach_pid must be synchroneous, 1785 * otherwise e.g. /proc/@pid/exe might point to the wrong executable, 1786 * if the pid value is immediately reused. This is enforced by 1787 * - caller must acquire spin_lock(p->proc_lock) 1788 * - must be called before detach_pid() 1789 * - proc_pid_lookup acquires proc_lock, and checks that 1790 * the target is not dead by looking at the attach count 1791 * of PIDTYPE_PID. 1792 */ 1793 1794 struct dentry *proc_pid_unhash(struct task_struct *p) 1795 { 1796 struct dentry *proc_dentry; 1797 1798 proc_dentry = p->proc_dentry; 1799 if (proc_dentry != NULL) { 1800 1801 spin_lock(&dcache_lock); 1802 spin_lock(&proc_dentry->d_lock); 1803 if (!d_unhashed(proc_dentry)) { 1804 dget_locked(proc_dentry); 1805 __d_drop(proc_dentry); 1806 spin_unlock(&proc_dentry->d_lock); 1807 } else { 1808 spin_unlock(&proc_dentry->d_lock); 1809 proc_dentry = NULL; 1810 } 1811 spin_unlock(&dcache_lock); 1812 } 1813 return proc_dentry; 1814 } 1815 1816 /** 1817 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries 1818 * @proc_dentry: directoy to prune. 1819 * 1820 * Shrink the /proc directory that was used by the just killed thread. 1821 */ 1822 1823 void proc_pid_flush(struct dentry *proc_dentry) 1824 { 1825 might_sleep(); 1826 if(proc_dentry != NULL) { 1827 shrink_dcache_parent(proc_dentry); 1828 dput(proc_dentry); 1829 } 1830 } 1831 1832 /* SMP-safe */ 1833 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 1834 { 1835 struct task_struct *task; 1836 struct inode *inode; 1837 struct proc_inode *ei; 1838 unsigned tgid; 1839 int died; 1840 1841 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) { 1842 inode = new_inode(dir->i_sb); 1843 if (!inode) 1844 return ERR_PTR(-ENOMEM); 1845 ei = PROC_I(inode); 1846 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 1847 inode->i_ino = fake_ino(0, PROC_TGID_INO); 1848 ei->pde = NULL; 1849 inode->i_mode = S_IFLNK|S_IRWXUGO; 1850 inode->i_uid = inode->i_gid = 0; 1851 inode->i_size = 64; 1852 inode->i_op = &proc_self_inode_operations; 1853 d_add(dentry, inode); 1854 return NULL; 1855 } 1856 tgid = name_to_int(dentry); 1857 if (tgid == ~0U) 1858 goto out; 1859 1860 read_lock(&tasklist_lock); 1861 task = find_task_by_pid(tgid); 1862 if (task) 1863 get_task_struct(task); 1864 read_unlock(&tasklist_lock); 1865 if (!task) 1866 goto out; 1867 1868 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO); 1869 1870 1871 if (!inode) { 1872 put_task_struct(task); 1873 goto out; 1874 } 1875 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 1876 inode->i_op = &proc_tgid_base_inode_operations; 1877 inode->i_fop = &proc_tgid_base_operations; 1878 inode->i_flags|=S_IMMUTABLE; 1879 #ifdef CONFIG_SECURITY 1880 inode->i_nlink = 5; 1881 #else 1882 inode->i_nlink = 4; 1883 #endif 1884 1885 dentry->d_op = &pid_base_dentry_operations; 1886 1887 died = 0; 1888 d_add(dentry, inode); 1889 spin_lock(&task->proc_lock); 1890 task->proc_dentry = dentry; 1891 if (!pid_alive(task)) { 1892 dentry = proc_pid_unhash(task); 1893 died = 1; 1894 } 1895 spin_unlock(&task->proc_lock); 1896 1897 put_task_struct(task); 1898 if (died) { 1899 proc_pid_flush(dentry); 1900 goto out; 1901 } 1902 return NULL; 1903 out: 1904 return ERR_PTR(-ENOENT); 1905 } 1906 1907 /* SMP-safe */ 1908 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 1909 { 1910 struct task_struct *task; 1911 struct task_struct *leader = proc_task(dir); 1912 struct inode *inode; 1913 unsigned tid; 1914 1915 tid = name_to_int(dentry); 1916 if (tid == ~0U) 1917 goto out; 1918 1919 read_lock(&tasklist_lock); 1920 task = find_task_by_pid(tid); 1921 if (task) 1922 get_task_struct(task); 1923 read_unlock(&tasklist_lock); 1924 if (!task) 1925 goto out; 1926 if (leader->tgid != task->tgid) 1927 goto out_drop_task; 1928 1929 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO); 1930 1931 1932 if (!inode) 1933 goto out_drop_task; 1934 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 1935 inode->i_op = &proc_tid_base_inode_operations; 1936 inode->i_fop = &proc_tid_base_operations; 1937 inode->i_flags|=S_IMMUTABLE; 1938 #ifdef CONFIG_SECURITY 1939 inode->i_nlink = 4; 1940 #else 1941 inode->i_nlink = 3; 1942 #endif 1943 1944 dentry->d_op = &pid_base_dentry_operations; 1945 1946 d_add(dentry, inode); 1947 1948 put_task_struct(task); 1949 return NULL; 1950 out_drop_task: 1951 put_task_struct(task); 1952 out: 1953 return ERR_PTR(-ENOENT); 1954 } 1955 1956 #define PROC_NUMBUF 10 1957 #define PROC_MAXPIDS 20 1958 1959 /* 1960 * Get a few tgid's to return for filldir - we need to hold the 1961 * tasklist lock while doing this, and we must release it before 1962 * we actually do the filldir itself, so we use a temp buffer.. 1963 */ 1964 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids) 1965 { 1966 struct task_struct *p; 1967 int nr_tgids = 0; 1968 1969 index--; 1970 read_lock(&tasklist_lock); 1971 p = NULL; 1972 if (version) { 1973 p = find_task_by_pid(version); 1974 if (p && !thread_group_leader(p)) 1975 p = NULL; 1976 } 1977 1978 if (p) 1979 index = 0; 1980 else 1981 p = next_task(&init_task); 1982 1983 for ( ; p != &init_task; p = next_task(p)) { 1984 int tgid = p->pid; 1985 if (!pid_alive(p)) 1986 continue; 1987 if (--index >= 0) 1988 continue; 1989 tgids[nr_tgids] = tgid; 1990 nr_tgids++; 1991 if (nr_tgids >= PROC_MAXPIDS) 1992 break; 1993 } 1994 read_unlock(&tasklist_lock); 1995 return nr_tgids; 1996 } 1997 1998 /* 1999 * Get a few tid's to return for filldir - we need to hold the 2000 * tasklist lock while doing this, and we must release it before 2001 * we actually do the filldir itself, so we use a temp buffer.. 2002 */ 2003 static int get_tid_list(int index, unsigned int *tids, struct inode *dir) 2004 { 2005 struct task_struct *leader_task = proc_task(dir); 2006 struct task_struct *task = leader_task; 2007 int nr_tids = 0; 2008 2009 index -= 2; 2010 read_lock(&tasklist_lock); 2011 /* 2012 * The starting point task (leader_task) might be an already 2013 * unlinked task, which cannot be used to access the task-list 2014 * via next_thread(). 2015 */ 2016 if (pid_alive(task)) do { 2017 int tid = task->pid; 2018 2019 if (--index >= 0) 2020 continue; 2021 if (tids != NULL) 2022 tids[nr_tids] = tid; 2023 nr_tids++; 2024 if (nr_tids >= PROC_MAXPIDS) 2025 break; 2026 } while ((task = next_thread(task)) != leader_task); 2027 read_unlock(&tasklist_lock); 2028 return nr_tids; 2029 } 2030 2031 /* for the /proc/ directory itself, after non-process stuff has been done */ 2032 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 2033 { 2034 unsigned int tgid_array[PROC_MAXPIDS]; 2035 char buf[PROC_NUMBUF]; 2036 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 2037 unsigned int nr_tgids, i; 2038 int next_tgid; 2039 2040 if (!nr) { 2041 ino_t ino = fake_ino(0,PROC_TGID_INO); 2042 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0) 2043 return 0; 2044 filp->f_pos++; 2045 nr++; 2046 } 2047 2048 /* f_version caches the tgid value that the last readdir call couldn't 2049 * return. lseek aka telldir automagically resets f_version to 0. 2050 */ 2051 next_tgid = filp->f_version; 2052 filp->f_version = 0; 2053 for (;;) { 2054 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array); 2055 if (!nr_tgids) { 2056 /* no more entries ! */ 2057 break; 2058 } 2059 next_tgid = 0; 2060 2061 /* do not use the last found pid, reserve it for next_tgid */ 2062 if (nr_tgids == PROC_MAXPIDS) { 2063 nr_tgids--; 2064 next_tgid = tgid_array[nr_tgids]; 2065 } 2066 2067 for (i=0;i<nr_tgids;i++) { 2068 int tgid = tgid_array[i]; 2069 ino_t ino = fake_ino(tgid,PROC_TGID_INO); 2070 unsigned long j = PROC_NUMBUF; 2071 2072 do 2073 buf[--j] = '0' + (tgid % 10); 2074 while ((tgid /= 10) != 0); 2075 2076 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) { 2077 /* returning this tgid failed, save it as the first 2078 * pid for the next readir call */ 2079 filp->f_version = tgid_array[i]; 2080 goto out; 2081 } 2082 filp->f_pos++; 2083 nr++; 2084 } 2085 } 2086 out: 2087 return 0; 2088 } 2089 2090 /* for the /proc/TGID/task/ directories */ 2091 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 2092 { 2093 unsigned int tid_array[PROC_MAXPIDS]; 2094 char buf[PROC_NUMBUF]; 2095 unsigned int nr_tids, i; 2096 struct dentry *dentry = filp->f_dentry; 2097 struct inode *inode = dentry->d_inode; 2098 int retval = -ENOENT; 2099 ino_t ino; 2100 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ 2101 2102 if (!pid_alive(proc_task(inode))) 2103 goto out; 2104 retval = 0; 2105 2106 switch (pos) { 2107 case 0: 2108 ino = inode->i_ino; 2109 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) 2110 goto out; 2111 pos++; 2112 /* fall through */ 2113 case 1: 2114 ino = parent_ino(dentry); 2115 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) 2116 goto out; 2117 pos++; 2118 /* fall through */ 2119 } 2120 2121 nr_tids = get_tid_list(pos, tid_array, inode); 2122 inode->i_nlink = pos + nr_tids; 2123 2124 for (i = 0; i < nr_tids; i++) { 2125 unsigned long j = PROC_NUMBUF; 2126 int tid = tid_array[i]; 2127 2128 ino = fake_ino(tid,PROC_TID_INO); 2129 2130 do 2131 buf[--j] = '0' + (tid % 10); 2132 while ((tid /= 10) != 0); 2133 2134 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0) 2135 break; 2136 pos++; 2137 } 2138 out: 2139 filp->f_pos = pos; 2140 return retval; 2141 } 2142