1 /*
2 * Wrapper functions for accessing the file_struct fd array.
3 */
4
5 #ifndef __LINUX_FILE_H
6 #define __LINUX_FILE_H
7
8 extern void FASTCALL(fput(struct file *));
9 extern struct file * FASTCALL(fget(unsigned int fd));
10
11 static inline int get_close_on_exec(unsigned int fd)
12 {
13 struct files_struct *files = current->files;
14 int res;
15 read_lock(&files->file_lock);
16 res = FD_ISSET(fd, files->close_on_exec);
17 read_unlock(&files->file_lock);
18 return res;
19 }
20
21 static inline void set_close_on_exec(unsigned int fd, int flag)
22 {
23 struct files_struct *files = current->files;
24 write_lock(&files->file_lock);
25 if (flag)
26 FD_SET(fd, files->close_on_exec);
27 else
28 FD_CLR(fd, files->close_on_exec);
29 write_unlock(&files->file_lock);
30 }
31
32 static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
33 {
34 struct file * file = NULL;
35
36 if (fd < files->max_fds)
37 file = files->fd[fd];
38 return file;
39 }
40
41 /*
42 * Check whether the specified fd has an open file.
43 */
44 static inline struct file * fcheck(unsigned int fd)
45 {
46 struct file * file = NULL;
47 struct files_struct *files = current->files;
48
49 if (fd < files->max_fds)
50 file = files->fd[fd];
51 return file;
52 }
53
54 extern void put_filp(struct file *);
55
56 extern int get_unused_fd(void);
57
58 static inline void __put_unused_fd(struct files_struct *files, unsigned int fd)
59 {
60 FD_CLR(fd, files->open_fds);
61 if (fd < files->next_fd)
62 files->next_fd = fd;
63 }
64
65 static inline void put_unused_fd(unsigned int fd)
66 {
67 struct files_struct *files = current->files;
68
69 write_lock(&files->file_lock);
70 __put_unused_fd(files, fd);
71 write_unlock(&files->file_lock);
72 }
73
74 /*
75 * Install a file pointer in the fd array.
76 *
77 * The VFS is full of places where we drop the files lock between
78 * setting the open_fds bitmap and installing the file in the file
79 * array. At any such point, we are vulnerable to a dup2() race
80 * installing a file in the array before us. We need to detect this and
81 * fput() the struct file we are about to overwrite in this case.
82 *
83 * It should never happen - if we allow dup2() do it, _really_ bad things
84 * will follow.
85 */
86
87 static inline void fd_install(unsigned int fd, struct file * file)
88 {
89 struct files_struct *files = current->files;
90
91 write_lock(&files->file_lock);
92 if (files->fd[fd])
93 BUG();
94 files->fd[fd] = file;
95 write_unlock(&files->file_lock);
96 }
97
98 void put_files_struct(struct files_struct *fs);
99
100 #endif /* __LINUX_FILE_H */
101
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.