Skip to content

Commit

Permalink
Wrap accesses to the fd_sets in struct fdtable
Browse files Browse the repository at this point in the history
Wrap accesses to the fd_sets in struct fdtable (for recording open files and
close-on-exec flags) so that we can move away from using fd_sets since we
abuse the fd_set structs by not allocating the full-sized structure under
normal circumstances and by non-core code looking at the internals of the
fd_sets.

The first abuse means that use of FD_ZERO() on these fd_sets is not permitted,
since that cannot be told about their abnormal lengths.

This introduces six wrapper functions for setting, clearing and testing
close-on-exec flags and fd-is-open flags:

	void __set_close_on_exec(int fd, struct fdtable *fdt);
	void __clear_close_on_exec(int fd, struct fdtable *fdt);
	bool close_on_exec(int fd, const struct fdtable *fdt);
	void __set_open_fd(int fd, struct fdtable *fdt);
	void __clear_open_fd(int fd, struct fdtable *fdt);
	bool fd_is_open(int fd, const struct fdtable *fdt);

Note that I've prepended '__' to the names of the set/clear functions because
they require the caller to hold a lock to use them.

Note also that I haven't added wrappers for looking behind the scenes at the
the array.  Possibly that should exist too.

Signed-off-by: David Howells <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: H. Peter Anvin <[email protected]>
Cc: Al Viro <[email protected]>
  • Loading branch information
dhowells authored and H. Peter Anvin committed Feb 19, 2012
1 parent 8b3d1cd commit 1dce27c
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Documentation/filesystems/files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ the fdtable structure -
if (fd >= 0) {
/* locate_fd() may have expanded fdtable, load the ptr */
fdt = files_fdtable(files);
FD_SET(fd, fdt->open_fds);
FD_CLR(fd, fdt->close_on_exec);
__set_open_fd(fd, fdt);
__clear_close_on_exec(fd, fdt);
spin_unlock(&files->file_lock);
.....

Expand Down
2 changes: 1 addition & 1 deletion arch/powerpc/platforms/cell/spufs/coredump.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ static struct spu_context *coredump_next_context(int *fd)
struct spu_context *ctx = NULL;

for (; *fd < fdt->max_fds; (*fd)++) {
if (!FD_ISSET(*fd, fdt->open_fds))
if (!fd_is_open(*fd, fdt))
continue;

file = fcheck(*fd);
Expand Down
10 changes: 5 additions & 5 deletions drivers/staging/android/binder.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,11 @@ int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
goto repeat;
}

FD_SET(fd, fdt->open_fds);
__set_open_fd(fd, fdt);
if (flags & O_CLOEXEC)
FD_SET(fd, fdt->close_on_exec);
__set_close_on_exec(fd, fdt);
else
FD_CLR(fd, fdt->close_on_exec);
__clear_close_on_exec(fd, fdt);
files->next_fd = fd + 1;
#if 1
/* Sanity check */
Expand Down Expand Up @@ -453,7 +453,7 @@ static void task_fd_install(
static void __put_unused_fd(struct files_struct *files, unsigned int fd)
{
struct fdtable *fdt = files_fdtable(files);
__FD_CLR(fd, fdt->open_fds);
__clear_open_fd(fd, fdt);
if (fd < files->next_fd)
files->next_fd = fd;
}
Expand All @@ -479,7 +479,7 @@ static long task_close_fd(struct binder_proc *proc, unsigned int fd)
if (!filp)
goto out_unlock;
rcu_assign_pointer(fdt->fd[fd], NULL);
FD_CLR(fd, fdt->close_on_exec);
__clear_close_on_exec(fd, fdt);
__put_unused_fd(files, fd);
spin_unlock(&files->file_lock);
retval = filp_close(filp, files);
Expand Down
2 changes: 1 addition & 1 deletion fs/autofs4/dev-ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ static void autofs_dev_ioctl_fd_install(unsigned int fd, struct file *file)
fdt = files_fdtable(files);
BUG_ON(fdt->fd[fd] != NULL);
rcu_assign_pointer(fdt->fd[fd], file);
FD_SET(fd, fdt->close_on_exec);
__set_close_on_exec(fd, fdt);
spin_unlock(&files->file_lock);
}

Expand Down
4 changes: 2 additions & 2 deletions fs/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -2078,8 +2078,8 @@ static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
fd_install(0, rp);
spin_lock(&cf->file_lock);
fdt = files_fdtable(cf);
FD_SET(0, fdt->open_fds);
FD_CLR(0, fdt->close_on_exec);
__set_open_fd(0, fdt);
__clear_close_on_exec(0, fdt);
spin_unlock(&cf->file_lock);

/* and disallow core files too */
Expand Down
18 changes: 9 additions & 9 deletions fs/fcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ void set_close_on_exec(unsigned int fd, int flag)
spin_lock(&files->file_lock);
fdt = files_fdtable(files);
if (flag)
FD_SET(fd, fdt->close_on_exec);
__set_close_on_exec(fd, fdt);
else
FD_CLR(fd, fdt->close_on_exec);
__clear_close_on_exec(fd, fdt);
spin_unlock(&files->file_lock);
}

static int get_close_on_exec(unsigned int fd)
static bool get_close_on_exec(unsigned int fd)
{
struct files_struct *files = current->files;
struct fdtable *fdt;
int res;
bool res;
rcu_read_lock();
fdt = files_fdtable(files);
res = FD_ISSET(fd, fdt->close_on_exec);
res = close_on_exec(fd, fdt);
rcu_read_unlock();
return res;
}
Expand Down Expand Up @@ -90,15 +90,15 @@ SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
err = -EBUSY;
fdt = files_fdtable(files);
tofree = fdt->fd[newfd];
if (!tofree && FD_ISSET(newfd, fdt->open_fds))
if (!tofree && fd_is_open(newfd, fdt))
goto out_unlock;
get_file(file);
rcu_assign_pointer(fdt->fd[newfd], file);
FD_SET(newfd, fdt->open_fds);
__set_open_fd(newfd, fdt);
if (flags & O_CLOEXEC)
FD_SET(newfd, fdt->close_on_exec);
__set_close_on_exec(newfd, fdt);
else
FD_CLR(newfd, fdt->close_on_exec);
__clear_close_on_exec(newfd, fdt);
spin_unlock(&files->file_lock);

if (tofree)
Expand Down
8 changes: 4 additions & 4 deletions fs/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
* is partway through open(). So make sure that this
* fd is available to the new process.
*/
FD_CLR(open_files - i, new_fdt->open_fds);
__clear_open_fd(open_files - i, new_fdt);
}
rcu_assign_pointer(*new_fds++, f);
}
Expand Down Expand Up @@ -460,11 +460,11 @@ int alloc_fd(unsigned start, unsigned flags)
if (start <= files->next_fd)
files->next_fd = fd + 1;

FD_SET(fd, fdt->open_fds);
__set_open_fd(fd, fdt);
if (flags & O_CLOEXEC)
FD_SET(fd, fdt->close_on_exec);
__set_close_on_exec(fd, fdt);
else
FD_CLR(fd, fdt->close_on_exec);
__clear_close_on_exec(fd, fdt);
error = fd;
#if 1
/* Sanity check */
Expand Down
4 changes: 2 additions & 2 deletions fs/open.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ EXPORT_SYMBOL(dentry_open);
static void __put_unused_fd(struct files_struct *files, unsigned int fd)
{
struct fdtable *fdt = files_fdtable(files);
__FD_CLR(fd, fdt->open_fds);
__clear_open_fd(fd, fdt);
if (fd < files->next_fd)
files->next_fd = fd;
}
Expand Down Expand Up @@ -1080,7 +1080,7 @@ SYSCALL_DEFINE1(close, unsigned int, fd)
if (!filp)
goto out_unlock;
rcu_assign_pointer(fdt->fd[fd], NULL);
FD_CLR(fd, fdt->close_on_exec);
__clear_close_on_exec(fd, fdt);
__put_unused_fd(files, fd);
spin_unlock(&files->file_lock);
retval = filp_close(filp, files);
Expand Down
2 changes: 1 addition & 1 deletion fs/proc/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,7 @@ static int proc_fd_info(struct inode *inode, struct path *path, char *info)

fdt = files_fdtable(files);
f_flags = file->f_flags & ~O_CLOEXEC;
if (FD_ISSET(fd, fdt->close_on_exec))
if (close_on_exec(fd, fdt))
f_flags |= O_CLOEXEC;

if (path) {
Expand Down
30 changes: 30 additions & 0 deletions include/linux/fdtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,36 @@ struct fdtable {
struct fdtable *next;
};

static inline void __set_close_on_exec(int fd, struct fdtable *fdt)
{
FD_SET(fd, fdt->close_on_exec);
}

static inline void __clear_close_on_exec(int fd, struct fdtable *fdt)
{
FD_CLR(fd, fdt->close_on_exec);
}

static inline bool close_on_exec(int fd, const struct fdtable *fdt)
{
return FD_ISSET(fd, fdt->close_on_exec);
}

static inline void __set_open_fd(int fd, struct fdtable *fdt)
{
FD_SET(fd, fdt->open_fds);
}

static inline void __clear_open_fd(int fd, struct fdtable *fdt)
{
FD_CLR(fd, fdt->open_fds);
}

static inline bool fd_is_open(int fd, const struct fdtable *fdt)
{
return FD_ISSET(fd, fdt->open_fds);
}

/*
* Open file table structure
*/
Expand Down

0 comments on commit 1dce27c

Please sign in to comment.