Skip to content

Commit

Permalink
Unbreak FD allocation when RLIMIT_NOFILE is RLIM_INFINITY
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed Feb 8, 2019
1 parent 91b9acf commit 2627dec
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
21 changes: 11 additions & 10 deletions fs/fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ int fd_close(struct fd *fd) {
return err;
}

static int fdtable_resize(struct fdtable *table, int size);
static int fdtable_resize(struct fdtable *table, unsigned size);

struct fdtable *fdtable_new(int size) {
struct fdtable *fdt = malloc(sizeof(struct fdtable));
Expand All @@ -86,7 +86,7 @@ static int fdtable_close(struct fdtable *table, fd_t f);
void fdtable_release(struct fdtable *table) {
lock(&table->lock);
if (--table->refcount == 0) {
for (fd_t f = 0; f < table->size; f++)
for (fd_t f = 0; (unsigned) f < table->size; f++)
fdtable_close(table, f);
free(table->files);
free(table->cloexec);
Expand All @@ -97,7 +97,7 @@ void fdtable_release(struct fdtable *table) {
}
}

static int fdtable_resize(struct fdtable *table, int size) {
static int fdtable_resize(struct fdtable *table, unsigned size) {
// currently the only legitimate use of this is to expand the table
assert(size > table->size);

Expand Down Expand Up @@ -143,16 +143,16 @@ struct fdtable *fdtable_copy(struct fdtable *table) {
}

static int fdtable_expand(struct fdtable *table, fd_t max) {
int size = max + 1;
unsigned size = max + 1;
if (table->size >= size)
return 0;
if (size > (int) rlimit(RLIMIT_NOFILE_))
if (size > rlimit(RLIMIT_NOFILE_))
return _EMFILE;
return fdtable_resize(table, max + 1);
}

struct fd *fdtable_get(struct fdtable *table, fd_t f) {
if (f < 0 || f >= current->files->size)
if (f < 0 || (unsigned) f >= current->files->size)
return NULL;
return table->files[f];
}
Expand All @@ -165,16 +165,17 @@ struct fd *f_get(fd_t f) {
}

static fd_t f_install_start(struct fd *fd, fd_t start) {
assert(start >= 0);
struct fdtable *table = current->files;
int size = rlimit(RLIMIT_NOFILE_);
unsigned size = rlimit(RLIMIT_NOFILE_);
if (size > table->size)
size = table->size;

fd_t f;
for (f = start; f < size; f++)
for (f = start; (unsigned) f < size; f++)
if (table->files[f] == NULL)
break;
if (f >= size) {
if ((unsigned) f >= size) {
int err = fdtable_expand(table, f);
if (err < 0)
f = err;
Expand Down Expand Up @@ -228,7 +229,7 @@ dword_t sys_close(fd_t f) {

void fdtable_do_cloexec(struct fdtable *table) {
lock(&table->lock);
for (fd_t f = 0; f < table->size; f++)
for (fd_t f = 0; (unsigned) f < table->size; f++)
if (bit_test(f, table->cloexec))
fdtable_close(table, f);
unlock(&table->lock);
Expand Down
2 changes: 1 addition & 1 deletion fs/fd.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ struct fd_ops {

struct fdtable {
atomic_uint refcount;
int size;
unsigned size;
struct fd **files;
bits_t *cloexec;
lock_t lock;
Expand Down
4 changes: 2 additions & 2 deletions fs/proc/pid.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ static bool proc_pid_fd_readdir(struct proc_entry *entry, unsigned long *index,
if (task == NULL)
return _ESRCH;
lock(&task->files->lock);
while ((int) *index < task->files->size && task->files->files[*index] == NULL)
while (*index < task->files->size && task->files->files[*index] == NULL)
(*index)++;
fd_t f = (*index)++;
bool any_left = f < task->files->size;
bool any_left = (unsigned) f < task->files->size;
unlock(&task->files->lock);
proc_put_task(task);
*next_entry = (struct proc_entry) {&proc_pid_fd, .pid = entry->pid, .fd = f};
Expand Down

0 comments on commit 2627dec

Please sign in to comment.