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
|
|
|
/****************************************************************************
|
2020-05-04 11:00:51 +02:00
|
|
|
* Name: file_ioctl and file_vioctl
|
2016-07-05 20:12:44 +02:00
|
|
|
*
|
|
|
|
* 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
|
2020-05-04 11:00:51 +02:00
|
|
|
* ap The argument of the ioctl cmd
|
2016-07-05 20:12:44 +02:00
|
|
|
*
|
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
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-04 11:00:51 +02:00
|
|
|
int file_vioctl(FAR struct file *filep, int req, va_list ap)
|
2016-07-05 20:12:44 +02:00
|
|
|
{
|
|
|
|
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 */
|
|
|
|
|
2020-05-04 11:00:51 +02:00
|
|
|
return inode->u.i_ops->ioctl(filep, req, va_arg(ap, unsigned long));
|
|
|
|
}
|
|
|
|
|
|
|
|
int file_ioctl(FAR struct file *filep, int req, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Let file_vioctl() do the real work. */
|
|
|
|
|
|
|
|
va_start(ap, req);
|
|
|
|
ret = file_vioctl(filep, req, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return ret;
|
2016-07-05 20:12:44 +02:00
|
|
|
}
|
|
|
|
|
2007-09-16 19:46:25 +02:00
|
|
|
/****************************************************************************
|
2020-05-04 11:00:51 +02:00
|
|
|
* Name: nx_ioctl and nx_vioctl
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2020-05-03 11:38:48 +02:00
|
|
|
* nx_ioctl() is similar to the standard 'ioctl' interface except that is
|
|
|
|
* not a cancellation point and it does not modify the errno variable.
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
2020-05-03 11:38:48 +02:00
|
|
|
* nx_ioctl() is an internal NuttX interface and should not be called from
|
|
|
|
* applications.
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2020-05-03 11:38:48 +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).
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-05-04 11:00:51 +02:00
|
|
|
int nx_vioctl(int fd, int req, va_list ap)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2016-07-05 20:12:44 +02:00
|
|
|
FAR struct file *filep;
|
2020-05-04 11:00:51 +02:00
|
|
|
FAR int *arg;
|
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? */
|
|
|
|
|
2020-05-03 11:38:48 +02:00
|
|
|
if (fd >= CONFIG_NFILE_DESCRIPTORS)
|
2007-09-16 19:46:25 +02:00
|
|
|
{
|
|
|
|
/* Perform the socket ioctl */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2019-02-11 22:47:25 +01:00
|
|
|
#ifdef CONFIG_NET
|
2020-05-03 11:38:48 +02:00
|
|
|
if (fd < (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS))
|
2007-09-16 19:46:25 +02:00
|
|
|
{
|
2020-05-04 11:00:51 +02:00
|
|
|
ret = netdev_vioctl(fd, req, ap);
|
2007-09-16 19:46:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2020-05-03 11:38:48 +02:00
|
|
|
return -EBADF;
|
2007-09-16 19:46:25 +02:00
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
{
|
2020-05-03 11:38:48 +02:00
|
|
|
return ret;
|
2020-02-19 19:08:08 +01:00
|
|
|
}
|
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-05-04 11:00:51 +02:00
|
|
|
ret = file_vioctl(filep, req, ap);
|
2020-02-19 19:08:08 +01:00
|
|
|
}
|
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:
|
2020-05-04 11:00:51 +02:00
|
|
|
arg = va_arg(ap, FAR int *);
|
|
|
|
if (arg && *arg)
|
|
|
|
{
|
|
|
|
ret = nx_fcntl(fd, F_SETFL,
|
|
|
|
nx_fcntl(fd, F_GETFL) | O_NONBLOCK);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = nx_fcntl(fd, F_SETFL,
|
|
|
|
nx_fcntl(fd, F_GETFL) & ~O_NONBLOCK);
|
|
|
|
}
|
2020-02-19 19:08:08 +01:00
|
|
|
break;
|
2020-09-16 07:04:20 +02:00
|
|
|
case FIOCLEX:
|
2020-09-16 09:53:56 +02:00
|
|
|
ret = nx_fcntl(fd, F_SETFD, nx_fcntl(fd, F_GETFD) | FD_CLOEXEC);
|
2020-09-16 07:04:20 +02:00
|
|
|
break;
|
|
|
|
case FIONCLEX:
|
2020-09-16 09:53:56 +02:00
|
|
|
ret = nx_fcntl(fd, F_SETFD, nx_fcntl(fd, F_GETFD) & ~FD_CLOEXEC);
|
2020-09-16 07:04:20 +02:00
|
|
|
break;
|
2020-02-19 19:08:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 11:38:48 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-04 11:00:51 +02:00
|
|
|
int nx_ioctl(int fd, int req, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Let nx_vioctl() do the real work. */
|
|
|
|
|
|
|
|
va_start(ap, req);
|
|
|
|
ret = nx_vioctl(fd, req, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-03 11:38:48 +02:00
|
|
|
/****************************************************************************
|
2020-05-04 11:00:51 +02:00
|
|
|
* Name: ioctl
|
2020-05-03 11:38:48 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform device specific operations.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* fd File/socket descriptor of device
|
|
|
|
* req The ioctl command
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-04 11:00:51 +02:00
|
|
|
int ioctl(int fd, int req, ...)
|
2020-05-03 11:38:48 +02:00
|
|
|
{
|
2020-05-04 11:00:51 +02:00
|
|
|
va_list ap;
|
2020-05-03 11:38:48 +02:00
|
|
|
int ret;
|
2020-02-19 19:08:08 +01:00
|
|
|
|
2020-05-04 11:00:51 +02:00
|
|
|
/* Let nx_vioctl() do the real work. */
|
|
|
|
|
|
|
|
va_start(ap, req);
|
|
|
|
ret = nx_vioctl(fd, req, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
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
|
|
|
}
|