OSDN Git Service

Merge branches 'acpi-scan', 'acpi-utils' and 'acpi-pm'
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / fs / proc / fd.c
1 #include <linux/sched.h>
2 #include <linux/errno.h>
3 #include <linux/dcache.h>
4 #include <linux/path.h>
5 #include <linux/fdtable.h>
6 #include <linux/namei.h>
7 #include <linux/pid.h>
8 #include <linux/security.h>
9 #include <linux/file.h>
10 #include <linux/seq_file.h>
11
12 #include <linux/proc_fs.h>
13
14 #include "../mount.h"
15 #include "internal.h"
16 #include "fd.h"
17
18 static int seq_show(struct seq_file *m, void *v)
19 {
20         struct files_struct *files = NULL;
21         int f_flags = 0, ret = -ENOENT;
22         struct file *file = NULL;
23         struct task_struct *task;
24
25         task = get_proc_task(m->private);
26         if (!task)
27                 return -ENOENT;
28
29         files = get_files_struct(task);
30         put_task_struct(task);
31
32         if (files) {
33                 int fd = proc_fd(m->private);
34
35                 spin_lock(&files->file_lock);
36                 file = fcheck_files(files, fd);
37                 if (file) {
38                         struct fdtable *fdt = files_fdtable(files);
39
40                         f_flags = file->f_flags;
41                         if (close_on_exec(fd, fdt))
42                                 f_flags |= O_CLOEXEC;
43
44                         get_file(file);
45                         ret = 0;
46                 }
47                 spin_unlock(&files->file_lock);
48                 put_files_struct(files);
49         }
50
51         if (!ret) {
52                 seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\n",
53                            (long long)file->f_pos, f_flags,
54                            real_mount(file->f_path.mnt)->mnt_id);
55                 if (file->f_op->show_fdinfo)
56                         file->f_op->show_fdinfo(m, file);
57                 ret = seq_has_overflowed(m);
58                 fput(file);
59         }
60
61         return ret;
62 }
63
64 static int seq_fdinfo_open(struct inode *inode, struct file *file)
65 {
66         return single_open(file, seq_show, inode);
67 }
68
69 static const struct file_operations proc_fdinfo_file_operations = {
70         .open           = seq_fdinfo_open,
71         .read           = seq_read,
72         .llseek         = seq_lseek,
73         .release        = single_release,
74 };
75
76 static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
77 {
78         struct files_struct *files;
79         struct task_struct *task;
80         const struct cred *cred;
81         struct inode *inode;
82         int fd;
83
84         if (flags & LOOKUP_RCU)
85                 return -ECHILD;
86
87         inode = dentry->d_inode;
88         task = get_proc_task(inode);
89         fd = proc_fd(inode);
90
91         if (task) {
92                 files = get_files_struct(task);
93                 if (files) {
94                         struct file *file;
95
96                         rcu_read_lock();
97                         file = fcheck_files(files, fd);
98                         if (file) {
99                                 unsigned f_mode = file->f_mode;
100
101                                 rcu_read_unlock();
102                                 put_files_struct(files);
103
104                                 if (task_dumpable(task)) {
105                                         rcu_read_lock();
106                                         cred = __task_cred(task);
107                                         inode->i_uid = cred->euid;
108                                         inode->i_gid = cred->egid;
109                                         rcu_read_unlock();
110                                 } else {
111                                         inode->i_uid = GLOBAL_ROOT_UID;
112                                         inode->i_gid = GLOBAL_ROOT_GID;
113                                 }
114
115                                 if (S_ISLNK(inode->i_mode)) {
116                                         unsigned i_mode = S_IFLNK;
117                                         if (f_mode & FMODE_READ)
118                                                 i_mode |= S_IRUSR | S_IXUSR;
119                                         if (f_mode & FMODE_WRITE)
120                                                 i_mode |= S_IWUSR | S_IXUSR;
121                                         inode->i_mode = i_mode;
122                                 }
123
124                                 security_task_to_inode(task, inode);
125                                 put_task_struct(task);
126                                 return 1;
127                         }
128                         rcu_read_unlock();
129                         put_files_struct(files);
130                 }
131                 put_task_struct(task);
132         }
133         return 0;
134 }
135
136 static const struct dentry_operations tid_fd_dentry_operations = {
137         .d_revalidate   = tid_fd_revalidate,
138         .d_delete       = pid_delete_dentry,
139 };
140
141 static int proc_fd_link(struct dentry *dentry, struct path *path)
142 {
143         struct files_struct *files = NULL;
144         struct task_struct *task;
145         int ret = -ENOENT;
146
147         task = get_proc_task(dentry->d_inode);
148         if (task) {
149                 files = get_files_struct(task);
150                 put_task_struct(task);
151         }
152
153         if (files) {
154                 int fd = proc_fd(dentry->d_inode);
155                 struct file *fd_file;
156
157                 spin_lock(&files->file_lock);
158                 fd_file = fcheck_files(files, fd);
159                 if (fd_file) {
160                         *path = fd_file->f_path;
161                         path_get(&fd_file->f_path);
162                         ret = 0;
163                 }
164                 spin_unlock(&files->file_lock);
165                 put_files_struct(files);
166         }
167
168         return ret;
169 }
170
171 static int
172 proc_fd_instantiate(struct inode *dir, struct dentry *dentry,
173                     struct task_struct *task, const void *ptr)
174 {
175         unsigned fd = (unsigned long)ptr;
176         struct proc_inode *ei;
177         struct inode *inode;
178
179         inode = proc_pid_make_inode(dir->i_sb, task);
180         if (!inode)
181                 goto out;
182
183         ei = PROC_I(inode);
184         ei->fd = fd;
185
186         inode->i_mode = S_IFLNK;
187         inode->i_op = &proc_pid_link_inode_operations;
188         inode->i_size = 64;
189
190         ei->op.proc_get_link = proc_fd_link;
191
192         d_set_d_op(dentry, &tid_fd_dentry_operations);
193         d_add(dentry, inode);
194
195         /* Close the race of the process dying before we return the dentry */
196         if (tid_fd_revalidate(dentry, 0))
197                 return 0;
198  out:
199         return -ENOENT;
200 }
201
202 static struct dentry *proc_lookupfd_common(struct inode *dir,
203                                            struct dentry *dentry,
204                                            instantiate_t instantiate)
205 {
206         struct task_struct *task = get_proc_task(dir);
207         int result = -ENOENT;
208         unsigned fd = name_to_int(&dentry->d_name);
209
210         if (!task)
211                 goto out_no_task;
212         if (fd == ~0U)
213                 goto out;
214
215         result = instantiate(dir, dentry, task, (void *)(unsigned long)fd);
216 out:
217         put_task_struct(task);
218 out_no_task:
219         return ERR_PTR(result);
220 }
221
222 static int proc_readfd_common(struct file *file, struct dir_context *ctx,
223                               instantiate_t instantiate)
224 {
225         struct task_struct *p = get_proc_task(file_inode(file));
226         struct files_struct *files;
227         unsigned int fd;
228
229         if (!p)
230                 return -ENOENT;
231
232         if (!dir_emit_dots(file, ctx))
233                 goto out;
234         files = get_files_struct(p);
235         if (!files)
236                 goto out;
237
238         rcu_read_lock();
239         for (fd = ctx->pos - 2;
240              fd < files_fdtable(files)->max_fds;
241              fd++, ctx->pos++) {
242                 char name[PROC_NUMBUF];
243                 int len;
244
245                 if (!fcheck_files(files, fd))
246                         continue;
247                 rcu_read_unlock();
248
249                 len = snprintf(name, sizeof(name), "%d", fd);
250                 if (!proc_fill_cache(file, ctx,
251                                      name, len, instantiate, p,
252                                      (void *)(unsigned long)fd))
253                         goto out_fd_loop;
254                 rcu_read_lock();
255         }
256         rcu_read_unlock();
257 out_fd_loop:
258         put_files_struct(files);
259 out:
260         put_task_struct(p);
261         return 0;
262 }
263
264 static int proc_readfd(struct file *file, struct dir_context *ctx)
265 {
266         return proc_readfd_common(file, ctx, proc_fd_instantiate);
267 }
268
269 const struct file_operations proc_fd_operations = {
270         .read           = generic_read_dir,
271         .iterate        = proc_readfd,
272         .llseek         = default_llseek,
273 };
274
275 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
276                                     unsigned int flags)
277 {
278         return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
279 }
280
281 /*
282  * /proc/pid/fd needs a special permission handler so that a process can still
283  * access /proc/self/fd after it has executed a setuid().
284  */
285 int proc_fd_permission(struct inode *inode, int mask)
286 {
287         int rv = generic_permission(inode, mask);
288         if (rv == 0)
289                 return 0;
290         if (task_tgid(current) == proc_pid(inode))
291                 rv = 0;
292         return rv;
293 }
294
295 const struct inode_operations proc_fd_inode_operations = {
296         .lookup         = proc_lookupfd,
297         .permission     = proc_fd_permission,
298         .setattr        = proc_setattr,
299 };
300
301 static int
302 proc_fdinfo_instantiate(struct inode *dir, struct dentry *dentry,
303                         struct task_struct *task, const void *ptr)
304 {
305         unsigned fd = (unsigned long)ptr;
306         struct proc_inode *ei;
307         struct inode *inode;
308
309         inode = proc_pid_make_inode(dir->i_sb, task);
310         if (!inode)
311                 goto out;
312
313         ei = PROC_I(inode);
314         ei->fd = fd;
315
316         inode->i_mode = S_IFREG | S_IRUSR;
317         inode->i_fop = &proc_fdinfo_file_operations;
318
319         d_set_d_op(dentry, &tid_fd_dentry_operations);
320         d_add(dentry, inode);
321
322         /* Close the race of the process dying before we return the dentry */
323         if (tid_fd_revalidate(dentry, 0))
324                 return 0;
325  out:
326         return -ENOENT;
327 }
328
329 static struct dentry *
330 proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
331 {
332         return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
333 }
334
335 static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
336 {
337         return proc_readfd_common(file, ctx,
338                                   proc_fdinfo_instantiate);
339 }
340
341 const struct inode_operations proc_fdinfo_inode_operations = {
342         .lookup         = proc_lookupfdinfo,
343         .setattr        = proc_setattr,
344 };
345
346 const struct file_operations proc_fdinfo_operations = {
347         .read           = generic_read_dir,
348         .iterate        = proc_readfdinfo,
349         .llseek         = default_llseek,
350 };