Skip to content

Commit

Permalink
take rlimit check to callers of expand_files()
Browse files Browse the repository at this point in the history
... except for one in android, where the check is different
and already done in caller.  No need to recalculate rlimit
many times in alloc_fd() either.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
  • Loading branch information
Al Viro committed Sep 27, 2012
1 parent 352e3b2 commit f33ff99
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
3 changes: 3 additions & 0 deletions fs/fcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
if (unlikely(oldfd == newfd))
return -EINVAL;

if (newfd >= rlimit(RLIMIT_NOFILE))
return -EMFILE;

spin_lock(&files->file_lock);
err = expand_files(files, newfd);
file = fcheck(oldfd);
Expand Down
16 changes: 9 additions & 7 deletions fs/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,6 @@ int expand_files(struct files_struct *files, int nr)

fdt = files_fdtable(files);

/*
* N.B. For clone tasks sharing a files structure, this test
* will limit the total number of files that can be opened.
*/
if (nr >= rlimit(RLIMIT_NOFILE))
return -EMFILE;

/* Do we need to expand? */
if (nr < fdt->max_fds)
return 0;
Expand Down Expand Up @@ -431,6 +424,7 @@ int alloc_fd(unsigned start, unsigned flags)
{
struct files_struct *files = current->files;
unsigned int fd;
unsigned end = rlimit(RLIMIT_NOFILE);
int error;
struct fdtable *fdt;

Expand All @@ -444,6 +438,14 @@ int alloc_fd(unsigned start, unsigned flags)
if (fd < fdt->max_fds)
fd = find_next_zero_bit(fdt->open_fds, fdt->max_fds, fd);

/*
* N.B. For clone tasks sharing a files structure, this test
* will limit the total number of files that can be opened.
*/
error = -EMFILE;
if (fd >= end)
goto out;

error = expand_files(files, fd);
if (error < 0)
goto out;
Expand Down

0 comments on commit f33ff99

Please sign in to comment.