2007-09-16 19:46:25 +02:00
|
|
|
/****************************************************************************
|
2014-09-28 19:46:11 +02:00
|
|
|
* fs/vfs/fs_ioctl.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2017-10-11 16:39:19 +02:00
|
|
|
* Copyright (C) 2007-2010, 2012-2014, 2016-2017 Gregory Nutt. All rights
|
|
|
|
* reserved.
|
2012-03-04 00:18:34 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2008-02-01 23:19:20 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-18 00:21:28 +01:00
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2007-09-16 19:46:25 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-09-16 19:46:25 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2007-09-16 19:46:25 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2008-02-01 23:19:20 +01:00
|
|
|
|
2007-11-30 21:46:29 +01:00
|
|
|
#include <sys/ioctl.h>
|
2007-03-29 15:27:43 +02:00
|
|
|
#include <sched.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <errno.h>
|
2020-02-19 19:08:08 +01:00
|
|
|
#include <fcntl.h>
|
2013-09-28 22:47:49 +02:00
|
|
|
#include <assert.h>
|
2007-11-30 21:46:29 +01:00
|
|
|
|
|
|
|
#include <net/if.h>
|
2007-09-16 19:46:25 +02:00
|
|
|
|
2019-02-11 22:47:25 +01:00
|
|
|
#ifdef CONFIG_NET
|
2012-03-04 00:18:34 +01:00
|
|
|
# include <nuttx/net/net.h>
|
2007-09-16 19:46:25 +02:00
|
|
|
#endif
|
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-09-16 19:46:25 +02:00
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Functions
|
2007-09-16 19:46:25 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-05 20:12:44 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: file_ioctl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform device specific operations.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2016-07-05 20:12:44 +02:00
|
|
|
* file File structure instance
|
|
|
|
* req The ioctl command
|
|
|
|
* arg The argument of the ioctl cmd
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-10-11 16:39:19 +02:00
|
|
|
* 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).
|
2016-07-05 20:12:44 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int file_ioctl(FAR struct file *filep, int req, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
2016-07-09 15:23:12 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL);
|
2016-07-05 20:12:44 +02:00
|
|
|
|
2016-07-09 15:23:12 +02:00
|
|
|
/* Is a driver opened? */
|
2016-07-05 20:12:44 +02:00
|
|
|
|
|
|
|
inode = filep->f_inode;
|
2016-07-09 15:23:12 +02:00
|
|
|
if (!inode)
|
2016-07-05 20:12:44 +02:00
|
|
|
{
|
2017-10-11 16:39:19 +02:00
|
|
|
return -EBADF;
|
2016-07-09 15:23:12 +02:00
|
|
|
}
|
2016-07-05 20:12:44 +02:00
|
|
|
|
2016-07-09 15:23:12 +02:00
|
|
|
/* Does the driver support the ioctl method? */
|
|
|
|
|
|
|
|
if (inode->u.i_ops == NULL || inode->u.i_ops->ioctl == NULL)
|
|
|
|
{
|
2017-10-11 16:39:19 +02:00
|
|
|
return -ENOTTY;
|
2016-07-09 15:23:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes on both accounts. Let the driver perform the ioctl command */
|
|
|
|
|
2017-10-11 16:39:19 +02:00
|
|
|
return (int)inode->u.i_ops->ioctl(filep, req, arg);
|
2016-07-05 20:12:44 +02:00
|
|
|
}
|
|
|
|
|
2007-09-16 19:46:25 +02:00
|
|
|
/****************************************************************************
|
2014-11-29 17:53:22 +01:00
|
|
|
* Name: ioctl/fs_ioctl
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform device specific operations.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2008-02-01 23:19:20 +01:00
|
|
|
* fd File/socket descriptor of device
|
2007-09-16 19:46:25 +02:00
|
|
|
* req The ioctl command
|
|
|
|
* arg The argument of the ioctl cmd
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2007-09-16 19:46:25 +02:00
|
|
|
* >=0 on success (positive non-zero values are cmd-specific)
|
2014-11-30 00:39:40 +01:00
|
|
|
* -1 on failure with errno set properly:
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-11-29 17:53:22 +01:00
|
|
|
#ifdef CONFIG_LIBC_IOCTL_VARIADIC
|
|
|
|
int fs_ioctl(int fd, int req, unsigned long arg)
|
|
|
|
#else
|
2007-02-18 00:21:28 +01:00
|
|
|
int ioctl(int fd, int req, unsigned long arg)
|
2014-11-29 17:53:22 +01:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2016-07-05 20:12:44 +02:00
|
|
|
FAR struct file *filep;
|
2017-10-11 16:39:19 +02:00
|
|
|
int ret;
|
2007-09-16 19:46:25 +02:00
|
|
|
|
|
|
|
/* Did we get a valid file descriptor? */
|
|
|
|
|
|
|
|
if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS)
|
|
|
|
{
|
|
|
|
/* Perform the socket ioctl */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2019-02-11 22:47:25 +01:00
|
|
|
#ifdef CONFIG_NET
|
2020-02-19 19:08:08 +01:00
|
|
|
if ((unsigned int)fd <
|
|
|
|
(CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS))
|
2007-09-16 19:46:25 +02:00
|
|
|
{
|
2017-10-11 16:39:19 +02:00
|
|
|
ret = netdev_ioctl(fd, req, arg);
|
2007-09-16 19:46:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2020-02-19 19:08:08 +01:00
|
|
|
ret = -EBADF;
|
2007-09-16 19:46:25 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 19:08:08 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Get the file structure corresponding to the file descriptor. */
|
|
|
|
|
|
|
|
ret = fs_getfilep(fd, &filep);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
2007-09-16 19:46:25 +02:00
|
|
|
|
2020-02-19 19:08:08 +01:00
|
|
|
DEBUGASSERT(filep != NULL);
|
2007-02-27 22:17:21 +01:00
|
|
|
|
2020-02-19 19:08:08 +01:00
|
|
|
/* Perform the file ioctl. */
|
2007-02-27 22:17:21 +01:00
|
|
|
|
2020-02-19 19:08:08 +01:00
|
|
|
ret = file_ioctl(filep, req, arg);
|
|
|
|
}
|
2017-10-11 16:39:19 +02:00
|
|
|
|
2020-02-19 19:08:08 +01:00
|
|
|
/* Check for File system IOCTL commands that can be implemented via
|
|
|
|
* fcntl()
|
2016-07-09 15:23:12 +02:00
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-02-19 19:08:08 +01:00
|
|
|
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:
|
|
|
|
|
2017-10-11 16:39:19 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-02-19 19:08:08 +01:00
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
2017-10-11 16:39:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|