This is an automated email from the ASF dual-hosted git repository. gnutt pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git
commit 4b5360362e29583bdb2adf353f0f915112374cec Author: Xiang Xiao <xiaoxi...@xiaomi.com> AuthorDate: Sun May 3 17:38:48 2020 +0800 fs/vfs: Add nx_ioctl/nx_fcntl function Signed-off-by: Xiang Xiao <xiaoxi...@xiaomi.com> --- fs/vfs/fs_fcntl.c | 110 +++++++++++++++++++++++++++++++++++++------------- fs/vfs/fs_ioctl.c | 93 +++++++++++++++++++++++++----------------- include/nuttx/fs/fs.h | 44 ++++++++++++++++++++ 3 files changed, 182 insertions(+), 65 deletions(-) diff --git a/fs/vfs/fs_fcntl.c b/fs/vfs/fs_fcntl.c index 084186c..eafc0fe 100644 --- a/fs/vfs/fs_fcntl.c +++ b/fs/vfs/fs_fcntl.c @@ -94,9 +94,9 @@ int file_vfcntl(FAR struct file *filep, int cmd, va_list ap) * or equal to the third argument, arg, taken as an integer of type * int. The new file descriptor shall refer to the same open file * description as the original file descriptor, and shall share any - * locks. The FD_CLOEXEC flag associated with the new file descriptor - * shall be cleared to keep the file open across calls to one of the - * exec functions. + * locks. The FD_CLOEXEC flag associated with the new file + * descriptor shall be cleared to keep the file open across calls to + * one of the exec functions. */ { @@ -107,10 +107,10 @@ int file_vfcntl(FAR struct file *filep, int cmd, va_list ap) break; case F_GETFD: - /* Get the file descriptor flags defined in <fcntl.h> that are associated - * with the file descriptor fd. File descriptor flags are associated - * with a single file descriptor and do not affect other file descriptors - * that refer to the same file. + /* Get the file descriptor flags defined in <fcntl.h> that are + * associated with the file descriptor fd. File descriptor flags are + * associated with a single file descriptor and do not affect other + * file descriptors that refer to the same file. */ { @@ -281,41 +281,33 @@ int file_fcntl(FAR struct file *filep, int cmd, ...) } /**************************************************************************** - * Name: fcntl + * Name: nx_fcntl and nx_vfcntl * * Description: - * fcntl() will perform the operation specified by 'cmd' on an open file. + * nx_fcntl() is similar to the standard 'fcntl' interface except that is + * not a cancellation point and it does not modify the errno variable. * - * Input Parameters: - * fd - File descriptor of the open file - * cmd - Identifies the operation to be performed. Command specific - * arguments may follow. + * nx_vfcntl() is identical except that it accepts a va_list as an argument + * versus taking a variable length list of arguments. + * + * nx_fcntl() and nx_vfcntl are internal NuttX interface and should not be + * called from applications. * * Returned Value: - * The returned value depends on the nature of the command but for all - * commands the return value of -1 (ERROR) indicates that an error has - * occurred and, in this case, the errno variable will be set - * appropriately + * Returns a non-negative number on success; A negated errno value is + * returned on any failure (see comments fcntl() for a list of appropriate + * errno values). * ****************************************************************************/ -int fcntl(int fd, int cmd, ...) +int nx_vfcntl(int fd, int cmd, va_list ap) { FAR struct file *filep; - va_list ap; int ret; - /* fcntl() is a cancellation point */ - - enter_cancellation_point(); - - /* Setup to access the variable argument list */ - - va_start(ap, cmd); - /* Did we get a valid file descriptor? */ - if ((unsigned int)fd < CONFIG_NFILE_DESCRIPTORS) + if (fd < CONFIG_NFILE_DESCRIPTORS) { /* Get the file structure corresponding to the file descriptor. */ @@ -336,7 +328,7 @@ int fcntl(int fd, int cmd, ...) /* No... check for operations on a socket descriptor */ #ifdef CONFIG_NET - if ((unsigned int)fd < (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS)) + if (fd < (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS)) { /* Yes.. defer socket descriptor operations to net_vfcntl(). The * errno is not set on failures. @@ -353,6 +345,66 @@ int fcntl(int fd, int cmd, ...) } } + return ret; +} + +int nx_fcntl(int fd, int cmd, ...) +{ + va_list ap; + int ret; + + /* Setup to access the variable argument list */ + + va_start(ap, cmd); + + /* Let nx_vfcntl() do the real work. The errno is not set on + * failures. + */ + + ret = nx_vfcntl(fd, cmd, ap); + + va_end(ap); + return ret; +} + +/**************************************************************************** + * Name: fcntl + * + * Description: + * fcntl() will perform the operation specified by 'cmd' on an open file. + * + * Input Parameters: + * fd - File descriptor of the open file + * cmd - Identifies the operation to be performed. Command specific + * arguments may follow. + * + * Returned Value: + * The returned value depends on the nature of the command but for all + * commands the return value of -1 (ERROR) indicates that an error has + * occurred and, in this case, the errno variable will be set + * appropriately + * + ****************************************************************************/ + +int fcntl(int fd, int cmd, ...) +{ + va_list ap; + int ret; + + /* fcntl() is a cancellation point */ + + enter_cancellation_point(); + + /* Setup to access the variable argument list */ + + va_start(ap, cmd); + + /* Let nx_vfcntl() do the real work. The errno is not set on + * failures. + */ + + ret = nx_vfcntl(fd, cmd, ap); + va_end(ap); if (ret < 0) diff --git a/fs/vfs/fs_ioctl.c b/fs/vfs/fs_ioctl.c index 43e96c7..3ea13d9 100644 --- a/fs/vfs/fs_ioctl.c +++ b/fs/vfs/fs_ioctl.c @@ -103,60 +103,42 @@ int file_ioctl(FAR struct file *filep, int req, unsigned long arg) } /**************************************************************************** - * Name: ioctl/fs_ioctl + * Name: nx_ioctl * * Description: - * Perform device specific operations. + * nx_ioctl() is similar to the standard 'ioctl' interface except that is + * not a cancellation point and it does not modify the errno variable. * - * Input Parameters: - * fd File/socket descriptor of device - * req The ioctl command - * arg The argument of the ioctl cmd + * nx_ioctl() is an internal NuttX interface and should not be called from + * applications. * * Returned Value: - * >=0 on success (positive non-zero values are cmd-specific) - * -1 on failure with errno set properly: - * - * EBADF - * 'fd' is not a valid descriptor. - * EFAULT - * 'arg' references an inaccessible memory area. - * EINVAL - * 'cmd' or 'arg' is not valid. - * ENOTTY - * 'fd' is not associated with a character special device. - * ENOTTY - * The specified request does not apply to the kind of object that the - * descriptor 'fd' references. + * Returns a non-negative number on success; A negated errno value is + * returned on any failure (see comments ioctl() for a list of appropriate + * errno values). * ****************************************************************************/ -#ifdef CONFIG_LIBC_IOCTL_VARIADIC -int fs_ioctl(int fd, int req, unsigned long arg) -#else -int ioctl(int fd, int req, unsigned long arg) -#endif +int nx_ioctl(int fd, int req, unsigned long arg) { FAR struct file *filep; int ret; /* Did we get a valid file descriptor? */ - if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS) + if (fd >= CONFIG_NFILE_DESCRIPTORS) { /* Perform the socket ioctl */ #ifdef CONFIG_NET - if ((unsigned int)fd < - (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS)) + if (fd < (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS)) { ret = netdev_ioctl(fd, req, arg); } else #endif { - ret = -EBADF; - goto errout; + return -EBADF; } } else @@ -166,7 +148,7 @@ int ioctl(int fd, int req, unsigned long arg) ret = fs_getfilep(fd, &filep); if (ret < 0) { - goto errout; + return ret; } DEBUGASSERT(filep != NULL); @@ -190,21 +172,60 @@ int ioctl(int fd, int req, unsigned long arg) if (*(FAR int *)((uintptr_t)arg)) { - return fcntl(fd, F_SETFL, - fcntl(fd, F_GETFL) | O_NONBLOCK); + ret = nx_fcntl(fd, F_SETFL, + nx_fcntl(fd, F_GETFL) | O_NONBLOCK); } else { - return fcntl(fd, F_SETFL, - fcntl(fd, F_GETFL) & ~O_NONBLOCK); + ret = nx_fcntl(fd, F_SETFL, + nx_fcntl(fd, F_GETFL) & ~O_NONBLOCK); } } break; } } -errout: + return ret; +} + +/**************************************************************************** + * Name: ioctl/fs_ioctl + * + * Description: + * Perform device specific operations. + * + * Input Parameters: + * fd File/socket descriptor of device + * req The ioctl command + * arg The argument of the ioctl cmd + * + * Returned Value: + * >=0 on success (positive non-zero values are cmd-specific) + * -1 on failure with errno set properly: + * + * EBADF + * 'fd' is not a valid descriptor. + * EFAULT + * 'arg' references an inaccessible memory area. + * EINVAL + * 'cmd' or 'arg' is not valid. + * ENOTTY + * 'fd' is not associated with a character special device. + * ENOTTY + * The specified request does not apply to the kind of object that the + * descriptor 'fd' references. + * + ****************************************************************************/ + +#ifdef CONFIG_LIBC_IOCTL_VARIADIC +int fs_ioctl(int fd, int req, unsigned long arg) +#else +int ioctl(int fd, int req, unsigned long arg) +#endif +{ + int ret; + ret = nx_ioctl(fd, req, arg); if (ret < 0) { set_errno(-ret); diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h index f55fb74..91147d9 100644 --- a/include/nuttx/fs/fs.h +++ b/include/nuttx/fs/fs.h @@ -81,6 +81,7 @@ # define _NX_READ(f,b,s) nx_read(f,b,s) # define _NX_WRITE(f,b,s) nx_write(f,b,s) # define _NX_SEEK(f,o,w) nx_seek(f,o,w) +# define _NX_IOCTL(f,r,a) nx_ioctl(f,r,a) # define _NX_STAT(p,s) nx_stat(p,s) # define _NX_GETERRNO(r) (-(r)) # define _NX_SETERRNO(r) set_errno(-(r)) @@ -95,6 +96,7 @@ # define _NX_READ(f,b,s) read(f,b,s) # define _NX_WRITE(f,b,s) write(f,b,s) # define _NX_SEEK(f,o,w) lseek(f,o,w) +# define _NX_IOCTL(f,r,a) ioctl(f,r,a) # define _NX_STAT(p,s) stat(p,s) # define _NX_GETERRNO(r) errno # define _NX_SETERRNO(r) @@ -1249,6 +1251,25 @@ int file_truncate(FAR struct file *filep, off_t length); int file_ioctl(FAR struct file *filep, int req, unsigned long arg); /**************************************************************************** + * Name: nx_ioctl + * + * Description: + * nx_ioctl() is similar to the standard 'ioctl' interface except that is + * not a cancellation point and it does not modify the errno variable. + * + * nx_ioctl() is an internal NuttX interface and should not be called from + * applications. + * + * Returned Value: + * Returns a non-negative number on success; A negated errno value is + * returned on any failure (see comments ioctl() for a list of appropriate + * errno values). + * + ****************************************************************************/ + +int nx_ioctl(int fd, int req, unsigned long arg); + +/**************************************************************************** * Name: file_vfcntl * * Description: @@ -1291,6 +1312,29 @@ int file_vfcntl(FAR struct file *filep, int cmd, va_list ap); int file_fcntl(FAR struct file *filep, int cmd, ...); /**************************************************************************** + * Name: nx_fcntl and nx_vfcntl + * + * Description: + * nx_fcntl() is similar to the standard 'fcntl' interface except that is + * not a cancellation point and it does not modify the errno variable. + * + * nx_vfcntl() is identical except that it accepts a va_list as an argument + * versus taking a variable length list of arguments. + * + * nx_fcntl() and nx_vfcntl are internal NuttX interface and should not be + * called from applications. + * + * Returned Value: + * Returns a non-negative number on success; A negated errno value is + * returned on any failure (see comments fcntl() for a list of appropriate + * errno values). + * + ****************************************************************************/ + +int nx_vfcntl(int fd, int cmd, va_list ap); +int nx_fcntl(int fd, int cmd, ...); + +/**************************************************************************** * Name: file_poll * * Description: