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
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
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
|
|
|
|
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
|
|
|
/****************************************************************************
|
2021-01-04 04:33:16 +01:00
|
|
|
* Private Functions
|
2007-09-16 19:46:25 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-05 20:12:44 +02:00
|
|
|
/****************************************************************************
|
2021-01-04 04:33:16 +01:00
|
|
|
* Name: file_vioctl
|
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;
|
2021-07-08 07:53:59 +02:00
|
|
|
unsigned long arg;
|
|
|
|
int ret = -ENOTTY;
|
2016-07-09 15:23:12 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL);
|
2016-07-05 20:12:44 +02:00
|
|
|
|
2021-07-08 07:53:59 +02:00
|
|
|
arg = va_arg(ap, unsigned long);
|
|
|
|
|
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? */
|
|
|
|
|
2021-07-08 07:53:59 +02:00
|
|
|
if (inode->u.i_ops != NULL && inode->u.i_ops->ioctl != NULL)
|
|
|
|
{
|
|
|
|
/* Yes on both accounts. Let the driver perform the ioctl command */
|
|
|
|
|
|
|
|
ret = inode->u.i_ops->ioctl(filep, req, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for File system IOCTL commands that can be implemented via
|
|
|
|
* fcntl()
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret != -ENOTTY)
|
2016-07-09 15:23:12 +02:00
|
|
|
{
|
2021-07-08 07:53:59 +02:00
|
|
|
return ret;
|
2016-07-09 15:23:12 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 07:53:59 +02:00
|
|
|
switch (req)
|
|
|
|
{
|
|
|
|
case FIONBIO:
|
|
|
|
{
|
|
|
|
FAR int *nonblock = (FAR int *)(uintptr_t)arg;
|
|
|
|
if (nonblock && *nonblock)
|
|
|
|
{
|
2021-11-25 06:43:10 +01:00
|
|
|
filep->f_oflags |= O_NONBLOCK;
|
2021-07-08 07:53:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-25 06:43:10 +01:00
|
|
|
filep->f_oflags &= ~O_NONBLOCK;
|
2021-07-08 07:53:59 +02:00
|
|
|
}
|
2021-11-25 06:43:10 +01:00
|
|
|
|
|
|
|
ret = OK;
|
2021-07-08 07:53:59 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FIOCLEX:
|
|
|
|
ret = file_fcntl(filep, F_SETFD,
|
|
|
|
file_fcntl(filep, F_GETFD) | FD_CLOEXEC);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FIONCLEX:
|
|
|
|
ret = file_fcntl(filep, F_SETFD,
|
|
|
|
file_fcntl(filep, F_GETFD) & ~FD_CLOEXEC);
|
|
|
|
break;
|
2021-07-08 05:11:28 +02:00
|
|
|
|
|
|
|
case FIOC_FILEPATH:
|
|
|
|
if (!INODE_IS_MOUNTPT(inode))
|
|
|
|
{
|
|
|
|
ret = inode_getpath(inode, (FAR char *)(uintptr_t)arg);
|
|
|
|
}
|
|
|
|
break;
|
2021-07-08 07:53:59 +02:00
|
|
|
}
|
2016-07-09 15:23:12 +02:00
|
|
|
|
2021-07-08 07:53:59 +02:00
|
|
|
return ret;
|
2020-05-04 11:00:51 +02:00
|
|
|
}
|
|
|
|
|
2007-09-16 19:46:25 +02:00
|
|
|
/****************************************************************************
|
2021-01-04 04:33:16 +01:00
|
|
|
* Name: nx_vioctl
|
2007-09-16 19:46:25 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2021-01-04 04:33:16 +01:00
|
|
|
static 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;
|
2017-10-11 16:39:19 +02:00
|
|
|
int ret;
|
2007-09-16 19:46:25 +02:00
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
/* Get the file structure corresponding to the file descriptor. */
|
2007-09-16 19:46:25 +02:00
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
ret = fs_getfilep(fd, &filep);
|
|
|
|
if (ret < 0)
|
2007-09-16 19:46:25 +02:00
|
|
|
{
|
2021-02-23 11:04:13 +01:00
|
|
|
return ret;
|
2007-09-16 19:46:25 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 07:53:59 +02:00
|
|
|
/* Let file_vioctl() do the real work. */
|
2020-02-19 19:08:08 +01:00
|
|
|
|
2021-07-08 07:53:59 +02:00
|
|
|
return file_vioctl(filep, req, ap);
|
2020-05-03 11:38:48 +02:00
|
|
|
}
|
|
|
|
|
2021-01-04 04:33:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: file_ioctl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform device specific operations.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* file File structure instance
|
|
|
|
* req The ioctl command
|
|
|
|
* ap The argument of the ioctl cmd
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 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).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nx_ioctl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* nx_ioctl() is similar to the standard 'ioctl' interface except that is
|
|
|
|
* not a cancellation point and it does not modify the errno variable.
|
|
|
|
*
|
|
|
|
* nx_ioctl() is an internal NuttX interface and should not be called from
|
|
|
|
* applications.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 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).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
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
|
|
|
}
|