diff --git a/fs/inode/fs_files.c b/fs/inode/fs_files.c index 51b8437e23..48482526fa 100644 --- a/fs/inode/fs_files.c +++ b/fs/inode/fs_files.c @@ -98,7 +98,7 @@ static int files_extend(FAR struct filelist *list, size_t row) return 0; } - if (row * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK > OPEN_MAX) + if (files_countlist(list) > OPEN_MAX) { return -EMFILE; } @@ -214,6 +214,7 @@ static int nx_dup3_from_tcb(FAR struct tcb_s *tcb, int fd1, int fd2, FAR struct filelist *list; FAR struct file *filep; FAR struct file file; + int count; int ret; if (fd1 == fd2) @@ -228,15 +229,17 @@ static int nx_dup3_from_tcb(FAR struct tcb_s *tcb, int fd1, int fd2, list = nxsched_get_files_from_tcb(tcb); + count = files_countlist(list); + /* Get the file descriptor list. It should not be NULL in this context. */ - if (fd1 < 0 || fd1 >= CONFIG_NFILE_DESCRIPTORS_PER_BLOCK * list->fl_rows || + if (fd1 < 0 || fd1 >= count || fd2 < 0) { return -EBADF; } - if (fd2 >= CONFIG_NFILE_DESCRIPTORS_PER_BLOCK * list->fl_rows) + if (fd2 >= count) { ret = files_extend(list, fd2 / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + 1); if (ret < 0) @@ -315,6 +318,28 @@ void files_releaselist(FAR struct filelist *list) kmm_free(list->fl_files); } +/**************************************************************************** + * Name: files_countlist + * + * Description: + * Given a file descriptor, return the corresponding instance of struct + * file. + * + * Input Parameters: + * fd - The file descriptor + * filep - The location to return the struct file instance + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * any failure. + * + ****************************************************************************/ + +int files_countlist(FAR struct filelist *list) +{ + return list->fl_rows * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK; +} + /**************************************************************************** * Name: file_allocate_from_tcb * @@ -562,7 +587,7 @@ int fs_getfilep(int fd, FAR struct file **filep) return -EAGAIN; } - if (fd < 0 || fd >= list->fl_rows * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK) + if (fd < 0 || fd >= files_countlist(list)) { return -EBADF; } @@ -714,7 +739,7 @@ int nx_close_from_tcb(FAR struct tcb_s *tcb, int fd) /* Perform the protected close operation */ - if (fd < 0 || fd >= list->fl_rows * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK) + if (fd < 0 || fd >= files_countlist(list)) { return -EBADF; } diff --git a/fs/procfs/fs_procfsproc.c b/fs/procfs/fs_procfsproc.c index 7e18d3b7e6..04c83d53f7 100644 --- a/fs/procfs/fs_procfsproc.c +++ b/fs/procfs/fs_procfsproc.c @@ -1188,17 +1188,24 @@ static ssize_t proc_groupfd(FAR struct proc_file_s *procfile, size_t buflen, off_t offset) { FAR struct task_group_s *group = tcb->group; - FAR struct file *file; + FAR struct file *filep; char path[PATH_MAX]; size_t remaining; size_t linesize; size_t copysize; size_t totalsize; + int count; + int ret; int i; - int j; DEBUGASSERT(group != NULL); + count = files_countlist(&group->tg_filelist); + if (count == 0) + { + return 0; + } + remaining = buflen; totalsize = 0; @@ -1219,41 +1226,34 @@ static ssize_t proc_groupfd(FAR struct proc_file_s *procfile, /* Examine each open file descriptor */ - for (i = 0; i < group->tg_filelist.fl_rows; i++) + for (i = 0; i < count; i++) { - for (j = 0, file = group->tg_filelist.fl_files[i]; - j < CONFIG_NFILE_DESCRIPTORS_PER_BLOCK; - j++, file++) + ret = fs_getfilep(i, &filep); + if (ret != OK || filep == NULL) { - /* Is there an inode associated with the file descriptor? */ + continue; + } - if (file->f_inode == NULL) - { - continue; - } + if (file_ioctl(filep, FIOC_FILEPATH, path) < 0) + { + path[0] = '\0'; + } - if (file_ioctl(file, FIOC_FILEPATH, path) < 0) - { - path[0] = '\0'; - } + linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, + "%-3d %-7d %-4x %-9ld %s\n", + i, filep->f_oflags, + INODE_GET_TYPE(filep->f_inode), + (long)filep->f_pos, path); + copysize = procfs_memcpy(procfile->line, linesize, + buffer, remaining, &offset); - linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, - "%-3d %-7d %-4x %-9ld %s\n", - i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK - + j, file->f_oflags, - INODE_GET_TYPE(file->f_inode), - (long)file->f_pos, path); - copysize = procfs_memcpy(procfile->line, linesize, - buffer, remaining, &offset); + totalsize += copysize; + buffer += copysize; + remaining -= copysize; - totalsize += copysize; - buffer += copysize; - remaining -= copysize; - - if (totalsize >= buflen) - { - return totalsize; - } + if (totalsize >= buflen) + { + return totalsize; } } diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h index f944c22d99..7e3813695c 100644 --- a/include/nuttx/fs/fs.h +++ b/include/nuttx/fs/fs.h @@ -852,6 +852,19 @@ void files_initlist(FAR struct filelist *list); void files_releaselist(FAR struct filelist *list); +/**************************************************************************** + * Name: files_countlist + * + * Description: + * Get file count from file list + * + * Returned Value: + * file count of file list + * + ****************************************************************************/ + +int files_countlist(FAR struct filelist *list); + /**************************************************************************** * Name: files_duplist *