- Kai Germaschewski: ymfpci cleanups and resource leak fixes
[davej-history.git] / include / linux / file.h
blob02edb29c31da485131c7df666b9e76ee28a4a4e1
1 /*
2 * Wrapper functions for accessing the file_struct fd array.
3 */
5 #ifndef __LINUX_FILE_H
6 #define __LINUX_FILE_H
8 extern void FASTCALL(fput(struct file *));
9 extern struct file * FASTCALL(fget(unsigned int fd));
11 static inline int get_close_on_exec(unsigned int fd)
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;
21 static inline void set_close_on_exec(unsigned int fd, int flag)
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);
32 static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
34 struct file * file = NULL;
36 if (fd < files->max_fds)
37 file = files->fd[fd];
38 return file;
42 * Check whether the specified fd has an open file.
44 static inline struct file * fcheck(unsigned int fd)
46 struct file * file = NULL;
47 struct files_struct *files = current->files;
49 if (fd < files->max_fds)
50 file = files->fd[fd];
51 return file;
54 extern void put_filp(struct file *);
56 extern int get_unused_fd(void);
58 static inline void __put_unused_fd(struct files_struct *files, unsigned int fd)
60 FD_CLR(fd, files->open_fds);
61 if (fd < files->next_fd)
62 files->next_fd = fd;
65 static inline void put_unused_fd(unsigned int fd)
67 struct files_struct *files = current->files;
69 write_lock(&files->file_lock);
70 __put_unused_fd(files, fd);
71 write_unlock(&files->file_lock);
75 * Install a file pointer in the fd array.
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.
83 * It should never happen - if we allow dup2() do it, _really_ bad things
84 * will follow.
87 static inline void fd_install(unsigned int fd, struct file * file)
89 struct files_struct *files = current->files;
91 write_lock(&files->file_lock);
92 if (files->fd[fd])
93 BUG();
94 files->fd[fd] = file;
95 write_unlock(&files->file_lock);
98 void put_files_struct(struct files_struct *fs);
100 #endif /* __LINUX_FILE_H */