fs/vfs/fs_ioctl.c: Add FIONBIO support

This commit is contained in:
chao.an 2020-02-19 12:08:08 -06:00 committed by Gregory Nutt
parent 0f284c3025
commit 58599e8e2e
2 changed files with 51 additions and 28 deletions

View File

@ -43,6 +43,7 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sched.h> #include <sched.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <assert.h> #include <assert.h>
#include <net/if.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) int ioctl(int fd, int req, unsigned long arg)
#endif #endif
{ {
int errcode;
FAR struct file *filep; FAR struct file *filep;
int ret; int ret;
@ -147,50 +147,69 @@ int ioctl(int fd, int req, unsigned long arg)
/* Perform the socket ioctl */ /* Perform the socket ioctl */
#ifdef CONFIG_NET #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); ret = netdev_ioctl(fd, req, arg);
if (ret < 0)
{
errcode = -ret;
goto errout;
}
return ret;
} }
else else
#endif #endif
{ {
errcode = EBADF; ret = -EBADF;
goto errout; goto errout;
} }
} }
else
{
/* Get the file structure corresponding to the file descriptor. */ /* Get the file structure corresponding to the file descriptor. */
ret = fs_getfilep(fd, &filep); ret = fs_getfilep(fd, &filep);
if (ret < 0) if (ret < 0)
{ {
errcode = -ret;
goto errout; goto errout;
} }
DEBUGASSERT(filep != NULL); DEBUGASSERT(filep != NULL);
/* Perform the file ioctl. If file_ioctl() fails, it will set the errno /* Perform the file ioctl. */
* value appropriately.
*/
ret = file_ioctl(filep, req, arg); ret = file_ioctl(filep, req, arg);
}
/* Check for File system IOCTL commands that can be implemented via
* fcntl()
*/
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) if (ret < 0)
{ {
errcode = -ret; set_errno(-ret);
goto errout; ret = ERROR;
} }
return ret; return ret;
errout:
set_errno(errcode);
return ERROR;
} }

View File

@ -172,6 +172,10 @@
* OUT: Instance number is returned on * OUT: Instance number is returned on
* success. * success.
*/ */
#define FIONBIO _FIOC(0x000b) /* IN: Boolean option takes an
* int value.
* OUT: Origin option.
*/
/* NuttX file system ioctl definitions **************************************/ /* NuttX file system ioctl definitions **************************************/