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>
8 #include <linux/security.h>
9 #include <linux/file.h>
10 #include <linux/seq_file.h>
12 #include <linux/proc_fs.h>
17 static int seq_show(struct seq_file
*m
, void *v
)
19 struct files_struct
*files
= NULL
;
20 int f_flags
= 0, ret
= -ENOENT
;
21 struct file
*file
= NULL
;
22 struct task_struct
*task
;
24 task
= get_proc_task(m
->private);
28 files
= get_files_struct(task
);
29 put_task_struct(task
);
32 int fd
= proc_fd(m
->private);
34 spin_lock(&files
->file_lock
);
35 file
= fcheck_files(files
, fd
);
37 struct fdtable
*fdt
= files_fdtable(files
);
39 f_flags
= file
->f_flags
;
40 if (close_on_exec(fd
, fdt
))
46 spin_unlock(&files
->file_lock
);
47 put_files_struct(files
);
51 seq_printf(m
, "pos:\t%lli\nflags:\t0%o\n",
52 (long long)file
->f_pos
, f_flags
);
59 static int seq_fdinfo_open(struct inode
*inode
, struct file
*file
)
61 return single_open(file
, seq_show
, inode
);
64 static const struct file_operations proc_fdinfo_file_operations
= {
65 .open
= seq_fdinfo_open
,
68 .release
= single_release
,
71 static int tid_fd_revalidate(struct dentry
*dentry
, unsigned int flags
)
73 struct files_struct
*files
;
74 struct task_struct
*task
;
75 const struct cred
*cred
;
79 if (flags
& LOOKUP_RCU
)
82 inode
= dentry
->d_inode
;
83 task
= get_proc_task(inode
);
87 files
= get_files_struct(task
);
92 file
= fcheck_files(files
, fd
);
94 unsigned f_mode
= file
->f_mode
;
97 put_files_struct(files
);
99 if (task_dumpable(task
)) {
101 cred
= __task_cred(task
);
102 inode
->i_uid
= cred
->euid
;
103 inode
->i_gid
= cred
->egid
;
106 inode
->i_uid
= GLOBAL_ROOT_UID
;
107 inode
->i_gid
= GLOBAL_ROOT_GID
;
110 if (S_ISLNK(inode
->i_mode
)) {
111 unsigned i_mode
= S_IFLNK
;
112 if (f_mode
& FMODE_READ
)
113 i_mode
|= S_IRUSR
| S_IXUSR
;
114 if (f_mode
& FMODE_WRITE
)
115 i_mode
|= S_IWUSR
| S_IXUSR
;
116 inode
->i_mode
= i_mode
;
119 security_task_to_inode(task
, inode
);
120 put_task_struct(task
);
124 put_files_struct(files
);
126 put_task_struct(task
);
133 static const struct dentry_operations tid_fd_dentry_operations
= {
134 .d_revalidate
= tid_fd_revalidate
,
135 .d_delete
= pid_delete_dentry
,
138 static int proc_fd_link(struct dentry
*dentry
, struct path
*path
)
140 struct files_struct
*files
= NULL
;
141 struct task_struct
*task
;
144 task
= get_proc_task(dentry
->d_inode
);
146 files
= get_files_struct(task
);
147 put_task_struct(task
);
151 int fd
= proc_fd(dentry
->d_inode
);
152 struct file
*fd_file
;
154 spin_lock(&files
->file_lock
);
155 fd_file
= fcheck_files(files
, fd
);
157 *path
= fd_file
->f_path
;
158 path_get(&fd_file
->f_path
);
161 spin_unlock(&files
->file_lock
);
162 put_files_struct(files
);
168 static struct dentry
*
169 proc_fd_instantiate(struct inode
*dir
, struct dentry
*dentry
,
170 struct task_struct
*task
, const void *ptr
)
172 struct dentry
*error
= ERR_PTR(-ENOENT
);
173 unsigned fd
= (unsigned long)ptr
;
174 struct proc_inode
*ei
;
177 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
184 inode
->i_mode
= S_IFLNK
;
185 inode
->i_op
= &proc_pid_link_inode_operations
;
188 ei
->op
.proc_get_link
= proc_fd_link
;
190 d_set_d_op(dentry
, &tid_fd_dentry_operations
);
191 d_add(dentry
, inode
);
193 /* Close the race of the process dying before we return the dentry */
194 if (tid_fd_revalidate(dentry
, 0))
200 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
201 struct dentry
*dentry
,
202 instantiate_t instantiate
)
204 struct task_struct
*task
= get_proc_task(dir
);
205 struct dentry
*result
= ERR_PTR(-ENOENT
);
206 unsigned fd
= name_to_int(dentry
);
213 result
= instantiate(dir
, dentry
, task
, (void *)(unsigned long)fd
);
215 put_task_struct(task
);
220 static int proc_readfd_common(struct file
* filp
, void * dirent
,
221 filldir_t filldir
, instantiate_t instantiate
)
223 struct dentry
*dentry
= filp
->f_path
.dentry
;
224 struct inode
*inode
= dentry
->d_inode
;
225 struct task_struct
*p
= get_proc_task(inode
);
226 struct files_struct
*files
;
227 unsigned int fd
, ino
;
238 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
242 ino
= parent_ino(dentry
);
243 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
247 files
= get_files_struct(p
);
251 for (fd
= filp
->f_pos
- 2;
252 fd
< files_fdtable(files
)->max_fds
;
253 fd
++, filp
->f_pos
++) {
254 char name
[PROC_NUMBUF
];
258 if (!fcheck_files(files
, fd
))
262 len
= snprintf(name
, sizeof(name
), "%d", fd
);
263 rv
= proc_fill_cache(filp
, dirent
, filldir
,
264 name
, len
, instantiate
, p
,
265 (void *)(unsigned long)fd
);
272 put_files_struct(files
);
280 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
282 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
285 const struct file_operations proc_fd_operations
= {
286 .read
= generic_read_dir
,
287 .readdir
= proc_readfd
,
288 .llseek
= default_llseek
,
291 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
294 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
298 * /proc/pid/fd needs a special permission handler so that a process can still
299 * access /proc/self/fd after it has executed a setuid().
301 int proc_fd_permission(struct inode
*inode
, int mask
)
303 int rv
= generic_permission(inode
, mask
);
306 if (task_pid(current
) == proc_pid(inode
))
311 const struct inode_operations proc_fd_inode_operations
= {
312 .lookup
= proc_lookupfd
,
313 .permission
= proc_fd_permission
,
314 .setattr
= proc_setattr
,
317 static struct dentry
*
318 proc_fdinfo_instantiate(struct inode
*dir
, struct dentry
*dentry
,
319 struct task_struct
*task
, const void *ptr
)
321 struct dentry
*error
= ERR_PTR(-ENOENT
);
322 unsigned fd
= (unsigned long)ptr
;
323 struct proc_inode
*ei
;
326 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
333 inode
->i_mode
= S_IFREG
| S_IRUSR
;
334 inode
->i_fop
= &proc_fdinfo_file_operations
;
336 d_set_d_op(dentry
, &tid_fd_dentry_operations
);
337 d_add(dentry
, inode
);
339 /* Close the race of the process dying before we return the dentry */
340 if (tid_fd_revalidate(dentry
, 0))
346 static struct dentry
*
347 proc_lookupfdinfo(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
349 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
352 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
354 return proc_readfd_common(filp
, dirent
, filldir
,
355 proc_fdinfo_instantiate
);
358 const struct inode_operations proc_fdinfo_inode_operations
= {
359 .lookup
= proc_lookupfdinfo
,
360 .setattr
= proc_setattr
,
363 const struct file_operations proc_fdinfo_operations
= {
364 .read
= generic_read_dir
,
365 .readdir
= proc_readfdinfo
,
366 .llseek
= default_llseek
,