You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2020/02/19 18:08:56 UTC

[incubator-nuttx] branch master updated: fs/vfs/fs_ioctl.c: Add FIONBIO support

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


The following commit(s) were added to refs/heads/master by this push:
     new 58599e8  fs/vfs/fs_ioctl.c: Add FIONBIO support
58599e8 is described below

commit 58599e8e2ee76bd77d0ce76837c557e1e130d15c
Author: chao.an <an...@xiaomi.com>
AuthorDate: Wed Feb 19 12:08:08 2020 -0600

    fs/vfs/fs_ioctl.c: Add FIONBIO support
---
 fs/vfs/fs_ioctl.c        | 73 ++++++++++++++++++++++++++++++------------------
 include/nuttx/fs/ioctl.h |  4 +++
 2 files changed, 50 insertions(+), 27 deletions(-)

diff --git a/fs/vfs/fs_ioctl.c b/fs/vfs/fs_ioctl.c
index 28d006d..43e96c7 100644
--- a/fs/vfs/fs_ioctl.c
+++ b/fs/vfs/fs_ioctl.c
@@ -43,6 +43,7 @@
 #include <sys/ioctl.h>
 #include <sched.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <assert.h>
 
 #include <net/if.h>
@@ -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;
         }
     }
+  else
+    {
+      /* Get the file structure corresponding to the file descriptor. */
+
+      ret = fs_getfilep(fd, &filep);
+      if (ret < 0)
+        {
+          goto errout;
+        }
 
-  /* Get the file structure corresponding to the file descriptor. */
+      DEBUGASSERT(filep != NULL);
 
-  ret = fs_getfilep(fd, &filep);
-  if (ret < 0)
-    {
-      errcode = -ret;
-      goto errout;
-    }
+      /* Perform the file ioctl. */
 
-  DEBUGASSERT(filep != NULL);
+      ret = file_ioctl(filep, req, arg);
+    }
 
-  /* 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 31809d6..a685599 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 **************************************/