diff options
author | Cong Wang <xiyou.wangcong@gmail.com> | 2012-05-31 19:26:17 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-05-31 20:49:28 -0400 |
commit | b409e578d9a4ec95913e06d8fea2a33f1754ea69 (patch) | |
tree | 408c5080300bd44410191e3a60477a6ca713343f /fs/proc/base.c | |
parent | 168eeccbc956d2ec083c3a513f7706784ee0dc5f (diff) |
proc: clean up /proc/<pid>/environ handling
Similar to e268337dfe26 ("proc: clean up and fix /proc/<pid>/mem
handling"), move the check of permission to open(), this will simplify
read() code.
[akpm@linux-foundation.org: checkpatch fixes]
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Hugh Dickins <hughd@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'fs/proc/base.c')
-rw-r--r-- | fs/proc/base.c | 45 |
1 files changed, 24 insertions, 21 deletions
diff --git a/fs/proc/base.c b/fs/proc/base.c index d7d711876b6a..e2d234243269 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c | |||
@@ -679,7 +679,7 @@ static const struct file_operations proc_single_file_operations = { | |||
679 | .release = single_release, | 679 | .release = single_release, |
680 | }; | 680 | }; |
681 | 681 | ||
682 | static int mem_open(struct inode* inode, struct file* file) | 682 | static int __mem_open(struct inode *inode, struct file *file, unsigned int mode) |
683 | { | 683 | { |
684 | struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); | 684 | struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); |
685 | struct mm_struct *mm; | 685 | struct mm_struct *mm; |
@@ -687,7 +687,7 @@ static int mem_open(struct inode* inode, struct file* file) | |||
687 | if (!task) | 687 | if (!task) |
688 | return -ESRCH; | 688 | return -ESRCH; |
689 | 689 | ||
690 | mm = mm_access(task, PTRACE_MODE_ATTACH); | 690 | mm = mm_access(task, mode); |
691 | put_task_struct(task); | 691 | put_task_struct(task); |
692 | 692 | ||
693 | if (IS_ERR(mm)) | 693 | if (IS_ERR(mm)) |
@@ -707,6 +707,11 @@ static int mem_open(struct inode* inode, struct file* file) | |||
707 | return 0; | 707 | return 0; |
708 | } | 708 | } |
709 | 709 | ||
710 | static int mem_open(struct inode *inode, struct file *file) | ||
711 | { | ||
712 | return __mem_open(inode, file, PTRACE_MODE_ATTACH); | ||
713 | } | ||
714 | |||
710 | static ssize_t mem_rw(struct file *file, char __user *buf, | 715 | static ssize_t mem_rw(struct file *file, char __user *buf, |
711 | size_t count, loff_t *ppos, int write) | 716 | size_t count, loff_t *ppos, int write) |
712 | { | 717 | { |
@@ -803,30 +808,29 @@ static const struct file_operations proc_mem_operations = { | |||
803 | .release = mem_release, | 808 | .release = mem_release, |
804 | }; | 809 | }; |
805 | 810 | ||
811 | static int environ_open(struct inode *inode, struct file *file) | ||
812 | { | ||
813 | return __mem_open(inode, file, PTRACE_MODE_READ); | ||
814 | } | ||
815 | |||
806 | static ssize_t environ_read(struct file *file, char __user *buf, | 816 | static ssize_t environ_read(struct file *file, char __user *buf, |
807 | size_t count, loff_t *ppos) | 817 | size_t count, loff_t *ppos) |
808 | { | 818 | { |
809 | struct task_struct *task = get_proc_task(file->f_dentry->d_inode); | ||
810 | char *page; | 819 | char *page; |
811 | unsigned long src = *ppos; | 820 | unsigned long src = *ppos; |
812 | int ret = -ESRCH; | 821 | int ret = 0; |
813 | struct mm_struct *mm; | 822 | struct mm_struct *mm = file->private_data; |
814 | 823 | ||
815 | if (!task) | 824 | if (!mm) |
816 | goto out_no_task; | 825 | return 0; |
817 | 826 | ||
818 | ret = -ENOMEM; | ||
819 | page = (char *)__get_free_page(GFP_TEMPORARY); | 827 | page = (char *)__get_free_page(GFP_TEMPORARY); |
820 | if (!page) | 828 | if (!page) |
821 | goto out; | 829 | return -ENOMEM; |
822 | |||
823 | |||
824 | mm = mm_for_maps(task); | ||
825 | ret = PTR_ERR(mm); | ||
826 | if (!mm || IS_ERR(mm)) | ||
827 | goto out_free; | ||
828 | 830 | ||
829 | ret = 0; | 831 | ret = 0; |
832 | if (!atomic_inc_not_zero(&mm->mm_users)) | ||
833 | goto free; | ||
830 | while (count > 0) { | 834 | while (count > 0) { |
831 | int this_len, retval, max_len; | 835 | int this_len, retval, max_len; |
832 | 836 | ||
@@ -838,7 +842,7 @@ static ssize_t environ_read(struct file *file, char __user *buf, | |||
838 | max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; | 842 | max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; |
839 | this_len = (this_len > max_len) ? max_len : this_len; | 843 | this_len = (this_len > max_len) ? max_len : this_len; |
840 | 844 | ||
841 | retval = access_process_vm(task, (mm->env_start + src), | 845 | retval = access_remote_vm(mm, (mm->env_start + src), |
842 | page, this_len, 0); | 846 | page, this_len, 0); |
843 | 847 | ||
844 | if (retval <= 0) { | 848 | if (retval <= 0) { |
@@ -857,19 +861,18 @@ static ssize_t environ_read(struct file *file, char __user *buf, | |||
857 | count -= retval; | 861 | count -= retval; |
858 | } | 862 | } |
859 | *ppos = src; | 863 | *ppos = src; |
860 | |||
861 | mmput(mm); | 864 | mmput(mm); |
862 | out_free: | 865 | |
866 | free: | ||
863 | free_page((unsigned long) page); | 867 | free_page((unsigned long) page); |
864 | out: | ||
865 | put_task_struct(task); | ||
866 | out_no_task: | ||
867 | return ret; | 868 | return ret; |
868 | } | 869 | } |
869 | 870 | ||
870 | static const struct file_operations proc_environ_operations = { | 871 | static const struct file_operations proc_environ_operations = { |
872 | .open = environ_open, | ||
871 | .read = environ_read, | 873 | .read = environ_read, |
872 | .llseek = generic_file_llseek, | 874 | .llseek = generic_file_llseek, |
875 | .release = mem_release, | ||
873 | }; | 876 | }; |
874 | 877 | ||
875 | static ssize_t oom_adjust_read(struct file *file, char __user *buf, | 878 | static ssize_t oom_adjust_read(struct file *file, char __user *buf, |