Merge with 2.3.99-pre9.
[linux-2.6/linux-mips.git] / fs / proc / base.c
blobd513987d828b3e1915224f311c790a11fb45bc71
1 /*
2 * linux/fs/proc/base.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
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.
16 #include <asm/uaccess.h>
18 #include <linux/config.h>
19 #include <linux/errno.h>
20 #include <linux/sched.h>
21 #include <linux/proc_fs.h>
22 #include <linux/stat.h>
23 #include <linux/init.h>
24 #include <linux/file.h>
25 #include <linux/string.h>
28 * For hysterical raisins we keep the same inumbers as in the old procfs.
29 * Feel free to change the macro below - just keep the range distinct from
30 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
31 * As soon as we'll get a separate superblock we will be able to forget
32 * about magical ranges too.
35 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
37 ssize_t proc_pid_read_maps(struct task_struct*,struct file*,char*,size_t,loff_t*);
38 int proc_pid_stat(struct task_struct*,char*);
39 int proc_pid_status(struct task_struct*,char*);
40 int proc_pid_statm(struct task_struct*,char*);
41 int proc_pid_cpu(struct task_struct*,char*);
43 /* MOUNT_REWRITE: make all files have non-NULL ->f_vfsmnt (pipefs, sockfs) */
44 /* Until then... */
45 #define NULL_VFSMNT /* remove as soon as pipefs and sockfs will be there */
47 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
49 if (inode->u.proc_i.file) {
50 #ifdef NULL_VFSMNT
51 if (!inode->u.proc_i.file->f_vfsmnt)
52 mntget(*mnt);
53 else
54 #endif
55 *mnt = mntget(inode->u.proc_i.file->f_vfsmnt);
56 *dentry = dget(inode->u.proc_i.file->f_dentry);
57 return 0;
59 return -ENOENT;
62 static int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
64 struct mm_struct * mm;
65 struct vm_area_struct * vma;
66 int result = -ENOENT;
67 struct task_struct *task = inode->u.proc_i.task;
69 task_lock(task);
70 mm = task->mm;
71 if (mm)
72 atomic_inc(&mm->mm_users);
73 task_unlock(task);
74 if (!mm)
75 goto out;
76 down(&mm->mmap_sem);
77 vma = mm->mmap;
78 while (vma) {
79 if ((vma->vm_flags & VM_EXECUTABLE) &&
80 vma->vm_file) {
81 *mnt = mntget(vma->vm_file->f_vfsmnt);
82 *dentry = dget(vma->vm_file->f_dentry);
83 result = 0;
84 break;
86 vma = vma->vm_next;
88 up(&mm->mmap_sem);
89 mmput(mm);
90 out:
91 return result;
94 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
96 struct fs_struct *fs;
97 int result = -ENOENT;
98 task_lock(inode->u.proc_i.task);
99 fs = inode->u.proc_i.task->fs;
100 if(fs)
101 atomic_inc(&fs->count);
102 task_unlock(inode->u.proc_i.task);
103 if (fs) {
104 *mnt = mntget(fs->pwdmnt);
105 *dentry = dget(fs->pwd);
106 result = 0;
107 put_fs_struct(fs);
109 return result;
112 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
114 struct fs_struct *fs;
115 int result = -ENOENT;
116 task_lock(inode->u.proc_i.task);
117 fs = inode->u.proc_i.task->fs;
118 if(fs)
119 atomic_inc(&fs->count);
120 task_unlock(inode->u.proc_i.task);
121 if (fs) {
122 *mnt = mntget(fs->rootmnt);
123 *dentry = dget(fs->root);
124 result = 0;
125 put_fs_struct(fs);
127 return result;
130 static int proc_pid_environ(struct task_struct *task, char * buffer)
132 struct mm_struct *mm;
133 int res = 0;
134 task_lock(task);
135 mm = task->mm;
136 if (mm)
137 atomic_inc(&mm->mm_users);
138 task_unlock(task);
139 if (mm) {
140 int len = mm->env_end - mm->env_start;
141 if (len > PAGE_SIZE)
142 len = PAGE_SIZE;
143 res = access_process_vm(task, mm->env_start, buffer, len, 0);
144 mmput(mm);
146 return res;
149 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
151 struct mm_struct *mm;
152 int res = 0;
153 task_lock(task);
154 mm = task->mm;
155 if (mm)
156 atomic_inc(&mm->mm_users);
157 task_unlock(task);
158 if (mm) {
159 int len = mm->arg_end - mm->arg_start;
160 if (len > PAGE_SIZE)
161 len = PAGE_SIZE;
162 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
163 mmput(mm);
165 return res;
168 /************************************************************************/
169 /* Here the fs part begins */
170 /************************************************************************/
172 /* permission checks */
174 static int standard_permission(struct inode *inode, int mask)
176 int mode = inode->i_mode;
178 if ((mask & S_IWOTH) && IS_RDONLY(inode) &&
179 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
180 return -EROFS; /* Nobody gets write access to a read-only fs */
181 else if ((mask & S_IWOTH) && IS_IMMUTABLE(inode))
182 return -EACCES; /* Nobody gets write access to an immutable file */
183 else if (current->fsuid == inode->i_uid)
184 mode >>= 6;
185 else if (in_group_p(inode->i_gid))
186 mode >>= 3;
187 if (((mode & mask & S_IRWXO) == mask) || capable(CAP_DAC_OVERRIDE))
188 return 0;
189 /* read and search access */
190 if ((mask == S_IROTH) ||
191 (S_ISDIR(mode) && !(mask & ~(S_IROTH | S_IXOTH))))
192 if (capable(CAP_DAC_READ_SEARCH))
193 return 0;
194 return -EACCES;
197 static int proc_permission(struct inode *inode, int mask)
199 struct dentry *de, *base, *root;
200 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
202 if (standard_permission(inode, mask) != 0)
203 return -EACCES;
205 base = current->fs->root;
206 our_vfsmnt = current->fs->rootmnt;
207 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
208 return -ENOENT;
210 de = root;
211 mnt = vfsmnt;
213 while (vfsmnt != our_vfsmnt) {
214 if (vfsmnt == vfsmnt->mnt_parent)
215 goto out;
216 de = vfsmnt->mnt_mountpoint;
217 vfsmnt = vfsmnt->mnt_parent;
220 if (!is_subdir(de, base))
221 goto out;
223 dput(root);
224 mntput(mnt);
225 return 0;
226 out:
227 dput(root);
228 mntput(mnt);
229 return -EACCES;
232 static ssize_t pid_maps_read(struct file * file, char * buf,
233 size_t count, loff_t *ppos)
235 struct inode * inode = file->f_dentry->d_inode;
236 struct task_struct *task = inode->u.proc_i.task;
237 ssize_t res;
239 res = proc_pid_read_maps(task, file, buf, count, ppos);
240 return res;
243 static struct file_operations proc_maps_operations = {
244 read: pid_maps_read,
247 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
249 static ssize_t proc_info_read(struct file * file, char * buf,
250 size_t count, loff_t *ppos)
252 struct inode * inode = file->f_dentry->d_inode;
253 unsigned long page;
254 ssize_t length;
255 ssize_t end;
256 struct task_struct *task = inode->u.proc_i.task;
258 if (count > PROC_BLOCK_SIZE)
259 count = PROC_BLOCK_SIZE;
260 if (!(page = __get_free_page(GFP_KERNEL)))
261 return -ENOMEM;
263 length = inode->u.proc_i.op.proc_read(task, (char*)page);
265 if (length < 0) {
266 free_page(page);
267 return length;
269 /* Static 4kB (or whatever) block capacity */
270 if (*ppos >= length) {
271 free_page(page);
272 return 0;
274 if (count + *ppos > length)
275 count = length - *ppos;
276 end = count + *ppos;
277 copy_to_user(buf, (char *) page + *ppos, count);
278 *ppos = end;
279 free_page(page);
280 return count;
283 static struct file_operations proc_info_file_operations = {
284 read: proc_info_read,
287 #define MAY_PTRACE(p) \
288 (p==current||(p->p_pptr==current&&(p->flags&PF_PTRACED)&&p->state==TASK_STOPPED))
290 static ssize_t mem_read(struct file * file, char * buf,
291 size_t count, loff_t *ppos)
293 struct task_struct *task = file->f_dentry->d_inode->u.proc_i.task;
294 char *page;
295 unsigned long src = *ppos;
296 int copied = 0;
298 if (!MAY_PTRACE(task))
299 return -ESRCH;
301 page = (char *)__get_free_page(GFP_USER);
302 if (!page)
303 return -ENOMEM;
305 while (count > 0) {
306 int this_len, retval;
308 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
309 retval = access_process_vm(task, src, page, this_len, 0);
310 if (!retval) {
311 if (!copied)
312 copied = -EIO;
313 break;
315 if (copy_to_user(buf, page, retval)) {
316 copied = -EFAULT;
317 break;
319 copied += retval;
320 src += retval;
321 buf += retval;
322 count -= retval;
324 *ppos = src;
325 free_page((unsigned long) page);
326 return copied;
329 static ssize_t mem_write(struct file * file, const char * buf,
330 size_t count, loff_t *ppos)
332 int copied = 0;
333 char *page;
334 struct task_struct *task = file->f_dentry->d_inode->u.proc_i.task;
335 unsigned long dst = *ppos;
337 if (!MAY_PTRACE(task))
338 return -ESRCH;
340 page = (char *)__get_free_page(GFP_USER);
341 if (!page)
342 return -ENOMEM;
344 while (count > 0) {
345 int this_len, retval;
347 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
348 if (copy_from_user(page, buf, this_len)) {
349 copied = -EFAULT;
350 break;
352 retval = access_process_vm(task, dst, page, this_len, 1);
353 if (!retval) {
354 if (!copied)
355 copied = -EIO;
356 break;
358 copied += retval;
359 buf += retval;
360 dst += retval;
361 count -= retval;
363 *ppos = dst;
364 free_page((unsigned long) page);
365 return copied;
368 static struct file_operations proc_mem_operations = {
369 read: mem_read,
370 write: mem_write,
373 static struct inode_operations proc_mem_inode_operations = {
374 permission: proc_permission,
377 static int proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
379 struct inode *inode = dentry->d_inode;
380 int error;
381 #ifdef NULL_VFSMNT
382 struct vfsmount *dummy = mntget(nd->mnt);
383 #endif
385 /* We don't need a base pointer in the /proc filesystem */
386 path_release(nd);
388 error = proc_permission(inode, MAY_EXEC);
389 if (error)
390 goto out;
392 error = inode->u.proc_i.op.proc_get_link(inode, &nd->dentry, &nd->mnt);
393 out:
394 #ifdef NULL_VFSMNT
395 mntput(dummy);
396 #endif
397 return error;
400 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
401 char * buffer, int buflen)
403 struct inode * inode;
404 char * tmp = (char*)__get_free_page(GFP_KERNEL), *path, *pattern;
405 int len;
407 if (!tmp)
408 return -ENOMEM;
410 /* Check for special dentries.. */
411 pattern = NULL;
412 inode = dentry->d_inode;
413 if (inode && IS_ROOT(dentry)) {
414 if (S_ISSOCK(inode->i_mode))
415 pattern = "socket:[%lu]";
416 if (S_ISFIFO(inode->i_mode))
417 pattern = "pipe:[%lu]";
420 if (pattern) {
421 len = sprintf(tmp, pattern, inode->i_ino);
422 path = tmp;
423 } else {
424 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
425 len = tmp + PAGE_SIZE - 1 - path;
428 if (len < buflen)
429 buflen = len;
430 copy_to_user(buffer, path, buflen);
431 free_page((unsigned long)tmp);
432 return buflen;
435 static int proc_pid_readlink(struct dentry * dentry, char * buffer, int buflen)
437 int error;
438 struct inode *inode = dentry->d_inode;
439 struct dentry *de;
440 struct vfsmount *mnt = NULL;
442 error = proc_permission(inode, MAY_EXEC);
443 if (error)
444 goto out;
446 error = inode->u.proc_i.op.proc_get_link(inode, &de, &mnt);
447 if (error)
448 goto out;
450 error = do_proc_readlink(de, mnt, buffer, buflen);
451 dput(de);
452 mntput(mnt);
453 out:
454 return error;
457 static struct inode_operations proc_pid_link_inode_operations = {
458 readlink: proc_pid_readlink,
459 follow_link: proc_pid_follow_link
462 struct pid_entry {
463 int type;
464 int len;
465 char *name;
466 mode_t mode;
469 enum pid_directory_inos {
470 PROC_PID_INO = 2,
471 PROC_PID_STATUS,
472 PROC_PID_MEM,
473 PROC_PID_CWD,
474 PROC_PID_ROOT,
475 PROC_PID_EXE,
476 PROC_PID_FD,
477 PROC_PID_ENVIRON,
478 PROC_PID_CMDLINE,
479 PROC_PID_STAT,
480 PROC_PID_STATM,
481 PROC_PID_MAPS,
482 PROC_PID_CPU,
483 PROC_PID_FD_DIR = 0x8000, /* 0x8000-0xffff */
486 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
487 static struct pid_entry base_stuff[] = {
488 E(PROC_PID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
489 E(PROC_PID_ENVIRON, "environ", S_IFREG|S_IRUSR),
490 E(PROC_PID_STATUS, "status", S_IFREG|S_IRUGO),
491 E(PROC_PID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
492 E(PROC_PID_STAT, "stat", S_IFREG|S_IRUGO),
493 E(PROC_PID_STATM, "statm", S_IFREG|S_IRUGO),
494 #ifdef CONFIG_SMP
495 E(PROC_PID_CPU, "cpu", S_IFREG|S_IRUGO),
496 #endif
497 E(PROC_PID_MAPS, "maps", S_IFREG|S_IRUGO),
498 E(PROC_PID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
499 E(PROC_PID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
500 E(PROC_PID_ROOT, "root", S_IFLNK|S_IRWXUGO),
501 E(PROC_PID_EXE, "exe", S_IFLNK|S_IRWXUGO),
502 {0,0,NULL,0}
504 #undef E
506 #define NUMBUF 10
508 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
510 struct inode *inode = filp->f_dentry->d_inode;
511 struct task_struct *p = inode->u.proc_i.task;
512 unsigned int fd, pid, ino;
513 int retval;
514 char buf[NUMBUF];
515 struct files_struct * files;
517 retval = 0;
518 pid = p->pid;
520 fd = filp->f_pos;
521 switch (fd) {
522 case 0:
523 if (filldir(dirent, ".", 1, 0, inode->i_ino) < 0)
524 goto out;
525 filp->f_pos++;
526 case 1:
527 ino = fake_ino(pid, PROC_PID_INO);
528 if (filldir(dirent, "..", 2, 1, ino) < 0)
529 goto out;
530 filp->f_pos++;
531 default:
532 task_lock(p);
533 files = p->files;
534 if (files)
535 atomic_inc(&files->count);
536 task_unlock(p);
537 if (!files)
538 goto out;
539 for (fd = filp->f_pos-2;
540 fd < files->max_fds;
541 fd++, filp->f_pos++) {
542 unsigned int i,j;
544 if (!fcheck_files(files, fd))
545 continue;
547 j = NUMBUF;
548 i = fd;
549 do {
550 j--;
551 buf[j] = '0' + (i % 10);
552 i /= 10;
553 } while (i);
555 ino = fake_ino(pid, PROC_PID_FD_DIR + fd);
556 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino) < 0)
557 break;
559 put_files_struct(files);
561 out:
562 return retval;
565 static int proc_base_readdir(struct file * filp,
566 void * dirent, filldir_t filldir)
568 int i;
569 int pid;
570 struct inode *inode = filp->f_dentry->d_inode;
571 struct pid_entry *p;
573 pid = inode->u.proc_i.task->pid;
574 if (!inode->u.proc_i.task->p_pptr)
575 return -ENOENT;
576 i = filp->f_pos;
577 switch (i) {
578 case 0:
579 if (filldir(dirent, ".", 1, i, inode->i_ino) < 0)
580 return 0;
581 i++;
582 filp->f_pos++;
583 /* fall through */
584 case 1:
585 if (filldir(dirent, "..", 2, i, PROC_ROOT_INO) < 0)
586 return 0;
587 i++;
588 filp->f_pos++;
589 /* fall through */
590 default:
591 i -= 2;
592 if (i>=sizeof(base_stuff)/sizeof(base_stuff[0]))
593 return 1;
594 p = base_stuff + i;
595 while (p->name) {
596 if (filldir(dirent, p->name, p->len, filp->f_pos, fake_ino(pid, p->type)) < 0)
597 return 0;
598 filp->f_pos++;
599 p++;
602 return 1;
605 /* building an inode */
607 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
609 struct inode * inode;
611 /* We need a new inode */
613 inode = get_empty_inode();
614 if (!inode)
615 goto out;
617 /* Common stuff */
619 inode->i_sb = sb;
620 inode->i_dev = sb->s_dev;
621 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
622 inode->i_ino = fake_ino(task->pid, ino);
624 inode->u.proc_i.file = NULL;
626 * grab the reference to task.
628 inode->u.proc_i.task = task;
629 get_task_struct(task);
630 if (!task->p_pptr)
631 goto out_unlock;
633 inode->i_uid = 0;
634 inode->i_gid = 0;
635 if (ino == PROC_PID_INO || task->dumpable) {
636 inode->i_uid = task->euid;
637 inode->i_gid = task->egid;
640 out:
641 return inode;
643 out_unlock:
644 iput(inode);
645 return NULL;
648 /* dentry stuff */
650 static int pid_fd_revalidate(struct dentry * dentry, int flags)
652 return 0;
656 * Exceptional case: normally we are not allowed to unhash a busy
657 * directory. In this case, however, we can do it - no aliasing problems
658 * due to the way we treat inodes.
660 static int pid_base_revalidate(struct dentry * dentry, int flags)
662 if (dentry->d_inode->u.proc_i.task->p_pptr)
663 return 1;
664 d_drop(dentry);
665 return 0;
668 static int pid_delete_dentry(struct dentry * dentry)
670 return 1;
673 static struct dentry_operations pid_fd_dentry_operations =
675 d_revalidate: pid_fd_revalidate,
676 d_delete: pid_delete_dentry,
679 static struct dentry_operations pid_dentry_operations =
681 d_delete: pid_delete_dentry,
684 static struct dentry_operations pid_base_dentry_operations =
686 d_revalidate: pid_base_revalidate,
687 d_delete: pid_delete_dentry,
690 /* Lookups */
692 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry)
694 unsigned int fd, c;
695 struct task_struct *task = dir->u.proc_i.task;
696 struct file * file;
697 struct files_struct * files;
698 struct inode *inode;
699 const char *name;
700 int len;
702 fd = 0;
703 len = dentry->d_name.len;
704 name = dentry->d_name.name;
705 if (len > 1 && *name == '0') goto out;
706 while (len-- > 0) {
707 c = *name - '0';
708 name++;
709 if (c > 9)
710 goto out;
711 fd *= 10;
712 fd += c;
713 if (fd & 0xffff8000)
714 goto out;
717 inode = proc_pid_make_inode(dir->i_sb, task, PROC_PID_FD_DIR+fd);
718 if (!inode)
719 goto out;
720 task_lock(task);
721 files = task->files;
722 if (files)
723 atomic_inc(&files->count);
724 task_unlock(task);
725 if (!files)
726 goto out_unlock;
727 read_lock(&files->file_lock);
728 file = inode->u.proc_i.file = fcheck_files(files, fd);
729 if (!file)
730 goto out_unlock2;
731 get_file(file);
732 read_unlock(&files->file_lock);
733 put_files_struct(files);
734 inode->i_op = &proc_pid_link_inode_operations;
735 inode->i_size = 64;
736 inode->i_mode = S_IFLNK;
737 inode->u.proc_i.op.proc_get_link = proc_fd_link;
738 if (file->f_mode & 1)
739 inode->i_mode |= S_IRUSR | S_IXUSR;
740 if (file->f_mode & 2)
741 inode->i_mode |= S_IWUSR | S_IXUSR;
742 dentry->d_op = &pid_fd_dentry_operations;
743 d_add(dentry, inode);
744 return NULL;
746 out_unlock2:
747 put_files_struct(files);
748 read_unlock(&files->file_lock);
749 out_unlock:
750 iput(inode);
751 out:
752 return ERR_PTR(-ENOENT);
755 static struct file_operations proc_fd_operations = {
756 read: generic_read_dir,
757 readdir: proc_readfd,
761 * proc directories can do almost nothing..
763 static struct inode_operations proc_fd_inode_operations = {
764 lookup: proc_lookupfd,
765 permission: proc_permission,
768 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
770 struct inode *inode;
771 int error;
772 struct task_struct *task = dir->u.proc_i.task;
773 struct pid_entry *p;
775 error = -ENOENT;
776 inode = NULL;
778 for (p = base_stuff; p->name; p++) {
779 if (p->len != dentry->d_name.len)
780 continue;
781 if (!memcmp(dentry->d_name.name, p->name, p->len))
782 break;
784 if (!p->name)
785 goto out;
787 error = -EINVAL;
788 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
789 if (!inode)
790 goto out;
792 inode->i_mode = p->mode;
794 * Yes, it does not scale. And it should not. Don't add
795 * new entries into /proc/<pid>/ without very good reasons.
797 switch(p->type) {
798 case PROC_PID_FD:
799 inode->i_nlink = 2;
800 inode->i_op = &proc_fd_inode_operations;
801 inode->i_fop = &proc_fd_operations;
802 break;
803 case PROC_PID_EXE:
804 inode->i_op = &proc_pid_link_inode_operations;
805 inode->u.proc_i.op.proc_get_link = proc_exe_link;
806 break;
807 case PROC_PID_CWD:
808 inode->i_op = &proc_pid_link_inode_operations;
809 inode->u.proc_i.op.proc_get_link = proc_cwd_link;
810 break;
811 case PROC_PID_ROOT:
812 inode->i_op = &proc_pid_link_inode_operations;
813 inode->u.proc_i.op.proc_get_link = proc_root_link;
814 break;
815 case PROC_PID_ENVIRON:
816 inode->i_fop = &proc_info_file_operations;
817 inode->u.proc_i.op.proc_read = proc_pid_environ;
818 break;
819 case PROC_PID_STATUS:
820 inode->i_fop = &proc_info_file_operations;
821 inode->u.proc_i.op.proc_read = proc_pid_status;
822 break;
823 case PROC_PID_STAT:
824 inode->i_fop = &proc_info_file_operations;
825 inode->u.proc_i.op.proc_read = proc_pid_stat;
826 break;
827 case PROC_PID_CMDLINE:
828 inode->i_fop = &proc_info_file_operations;
829 inode->u.proc_i.op.proc_read = proc_pid_cmdline;
830 break;
831 case PROC_PID_STATM:
832 inode->i_fop = &proc_info_file_operations;
833 inode->u.proc_i.op.proc_read = proc_pid_statm;
834 break;
835 case PROC_PID_MAPS:
836 inode->i_fop = &proc_maps_operations;
837 break;
838 #ifdef CONFIG_SMP
839 case PROC_PID_CPU:
840 inode->i_fop = &proc_info_file_operations;
841 inode->u.proc_i.op.proc_read = proc_pid_cpu;
842 break;
843 #endif
844 case PROC_PID_MEM:
845 inode->i_op = &proc_mem_inode_operations;
846 inode->i_fop = &proc_mem_operations;
847 break;
848 default:
849 printk("procfs: impossible type (%d)",p->type);
850 iput(inode);
851 return ERR_PTR(-EINVAL);
853 dentry->d_op = &pid_dentry_operations;
854 d_add(dentry, inode);
855 return NULL;
857 out:
858 return ERR_PTR(error);
861 static struct file_operations proc_base_operations = {
862 read: generic_read_dir,
863 readdir: proc_base_readdir,
866 static struct inode_operations proc_base_inode_operations = {
867 lookup: proc_base_lookup,
871 * /proc/self:
873 static int proc_self_readlink(struct dentry *dentry, char *buffer, int buflen)
875 char tmp[30];
876 sprintf(tmp, "%d", current->pid);
877 return vfs_readlink(dentry,buffer,buflen,tmp);
880 static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
882 char tmp[30];
883 sprintf(tmp, "%d", current->pid);
884 return vfs_follow_link(nd,tmp);
887 static struct inode_operations proc_self_inode_operations = {
888 readlink: proc_self_readlink,
889 follow_link: proc_self_follow_link,
892 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry)
894 unsigned int pid, c;
895 struct task_struct *task;
896 const char *name;
897 struct inode *inode;
898 int len;
900 pid = 0;
901 name = dentry->d_name.name;
902 len = dentry->d_name.len;
903 if (len == 4 && !memcmp(name, "self", 4)) {
904 inode = get_empty_inode();
905 if (!inode)
906 return ERR_PTR(-ENOMEM);
907 inode->i_sb = dir->i_sb;
908 inode->i_dev = dir->i_sb->s_dev;
909 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
910 inode->i_ino = fake_ino(0, PROC_PID_INO);
911 inode->u.proc_i.file = NULL;
912 inode->u.proc_i.task = NULL;
913 inode->i_mode = S_IFLNK|S_IRWXUGO;
914 inode->i_uid = inode->i_gid = 0;
915 inode->i_size = 64;
916 inode->i_op = &proc_self_inode_operations;
917 d_add(dentry, inode);
918 return NULL;
920 while (len-- > 0) {
921 c = *name - '0';
922 name++;
923 if (c > 9)
924 goto out;
925 pid *= 10;
926 pid += c;
927 if (!pid)
928 goto out;
929 if (pid & 0xffff0000)
930 goto out;
933 read_lock(&tasklist_lock);
934 task = find_task_by_pid(pid);
935 if (task)
936 get_task_struct(task);
937 read_unlock(&tasklist_lock);
938 if (!task)
939 goto out;
941 inode = proc_pid_make_inode(dir->i_sb, task, PROC_PID_INO);
943 free_task_struct(task);
945 if (!inode)
946 goto out;
947 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
948 inode->i_op = &proc_base_inode_operations;
949 inode->i_fop = &proc_base_operations;
950 inode->i_nlink = 3;
951 inode->i_flags|=S_IMMUTABLE;
953 dentry->d_op = &pid_base_dentry_operations;
954 d_add(dentry, inode);
955 return NULL;
956 out:
957 return ERR_PTR(-ENOENT);
960 void proc_pid_delete_inode(struct inode *inode)
962 if (inode->u.proc_i.file)
963 fput(inode->u.proc_i.file);
964 if (inode->u.proc_i.task)
965 free_task_struct(inode->u.proc_i.task);
968 #define PROC_NUMBUF 10
969 #define PROC_MAXPIDS 20
972 * Get a few pid's to return for filldir - we need to hold the
973 * tasklist lock while doing this, and we must release it before
974 * we actually do the filldir itself, so we use a temp buffer..
976 static int get_pid_list(int index, unsigned int *pids)
978 struct task_struct *p;
979 int nr_pids = 0;
981 index--;
982 read_lock(&tasklist_lock);
983 for_each_task(p) {
984 int pid = p->pid;
985 if (!pid)
986 continue;
987 if (--index >= 0)
988 continue;
989 pids[nr_pids] = pid;
990 nr_pids++;
991 if (nr_pids >= PROC_MAXPIDS)
992 break;
994 read_unlock(&tasklist_lock);
995 return nr_pids;
998 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1000 unsigned int pid_array[PROC_MAXPIDS];
1001 char buf[PROC_NUMBUF];
1002 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1003 unsigned int nr_pids, i;
1005 if (!nr) {
1006 ino_t ino = fake_ino(0,PROC_PID_INO);
1007 if (filldir(dirent, "self", 4, filp->f_pos, ino) < 0)
1008 return 0;
1009 filp->f_pos++;
1010 nr++;
1013 nr_pids = get_pid_list(nr, pid_array);
1015 for (i = 0; i < nr_pids; i++) {
1016 int pid = pid_array[i];
1017 ino_t ino = fake_ino(pid,PROC_PID_INO);
1018 unsigned long j = PROC_NUMBUF;
1020 do buf[--j] = '0' + (pid % 10); while (pid/=10);
1022 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino) < 0)
1023 break;
1024 filp->f_pos++;
1026 return 0;