From 58599e8e2ee76bd77d0ce76837c557e1e130d15c Mon Sep 17 00:00:00 2001 From: "chao.an" Date: Wed, 19 Feb 2020 12:08:08 -0600 Subject: [PATCH] fs/vfs/fs_ioctl.c: Add FIONBIO support --- fs/vfs/fs_ioctl.c | 75 +++++++++++++++++++++++++--------------- include/nuttx/fs/ioctl.h | 4 +++ 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/fs/vfs/fs_ioctl.c b/fs/vfs/fs_ioctl.c index 28d006d6e7..43e96c731a 100644 --- a/fs/vfs/fs_ioctl.c +++ b/fs/vfs/fs_ioctl.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -136,7 +137,6 @@ int fs_ioctl(int fd, int req, unsigned long arg) int ioctl(int fd, int req, unsigned long arg) #endif { - int errcode; FAR struct file *filep; int ret; @@ -147,50 +147,69 @@ int ioctl(int fd, int req, unsigned long arg) /* Perform the socket ioctl */ #ifdef CONFIG_NET - if ((unsigned int)fd < (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS)) + if ((unsigned int)fd < + (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS)) { ret = netdev_ioctl(fd, req, arg); - if (ret < 0) - { - errcode = -ret; - goto errout; - } - - return ret; } else #endif { - errcode = EBADF; + ret = -EBADF; goto errout; } } - - /* Get the file structure corresponding to the file descriptor. */ - - ret = fs_getfilep(fd, &filep); - if (ret < 0) + else { - errcode = -ret; - goto errout; + /* Get the file structure corresponding to the file descriptor. */ + + ret = fs_getfilep(fd, &filep); + if (ret < 0) + { + goto errout; + } + + DEBUGASSERT(filep != NULL); + + /* Perform the file ioctl. */ + + ret = file_ioctl(filep, req, arg); } - DEBUGASSERT(filep != NULL); - - /* Perform the file ioctl. If file_ioctl() fails, it will set the errno - * value appropriately. + /* Check for File system IOCTL commands that can be implemented via + * fcntl() */ - ret = file_ioctl(filep, req, arg); + if (ret == -ENOTTY) + { + switch (req) + { + case FIONBIO: + { + DEBUGASSERT(arg != 0); + + if (*(FAR int *)((uintptr_t)arg)) + { + return fcntl(fd, F_SETFL, + fcntl(fd, F_GETFL) | O_NONBLOCK); + } + else + { + return fcntl(fd, F_SETFL, + fcntl(fd, F_GETFL) & ~O_NONBLOCK); + } + } + break; + } + } + +errout: + if (ret < 0) { - errcode = -ret; - goto errout; + set_errno(-ret); + ret = ERROR; } return ret; - -errout: - set_errno(errcode); - return ERROR; } diff --git a/include/nuttx/fs/ioctl.h b/include/nuttx/fs/ioctl.h index 31809d6022..a685599b64 100644 --- a/include/nuttx/fs/ioctl.h +++ b/include/nuttx/fs/ioctl.h @@ -172,6 +172,10 @@ * OUT: Instance number is returned on * success. */ +#define FIONBIO _FIOC(0x000b) /* IN: Boolean option takes an + * int value. + * OUT: Origin option. + */ /* NuttX file system ioctl definitions **************************************/