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"
|
2024-05-15 08:31:28 +02:00
|
|
|
#include "lock.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
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-30 06:51:09 +02:00
|
|
|
static 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (req)
|
|
|
|
{
|
|
|
|
case FIONBIO:
|
2022-08-30 07:46:46 +02:00
|
|
|
if (ret == OK || ret == -ENOTTY)
|
|
|
|
{
|
|
|
|
FAR int *nonblock = (FAR int *)(uintptr_t)arg;
|
|
|
|
if (nonblock && *nonblock)
|
|
|
|
{
|
|
|
|
filep->f_oflags |= O_NONBLOCK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
filep->f_oflags &= ~O_NONBLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
2021-07-08 07:53:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FIOCLEX:
|
2022-08-30 07:46:46 +02:00
|
|
|
if (ret == OK || ret == -ENOTTY)
|
|
|
|
{
|
|
|
|
filep->f_oflags |= O_CLOEXEC;
|
|
|
|
ret = OK;
|
|
|
|
}
|
2021-07-08 07:53:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FIONCLEX:
|
2022-08-30 07:46:46 +02:00
|
|
|
if (ret == OK || ret == -ENOTTY)
|
|
|
|
{
|
|
|
|
filep->f_oflags &= ~O_CLOEXEC;
|
|
|
|
ret = OK;
|
|
|
|
}
|
2021-07-08 07:53:59 +02:00
|
|
|
break;
|
2021-07-08 05:11:28 +02:00
|
|
|
|
|
|
|
case FIOC_FILEPATH:
|
2022-08-30 07:46:46 +02:00
|
|
|
if (ret == -ENOTTY && !INODE_IS_MOUNTPT(inode))
|
2021-07-08 05:11:28 +02:00
|
|
|
{
|
2023-03-05 14:13:32 +01:00
|
|
|
ret = inode_getpath(inode, (FAR char *)(uintptr_t)arg, PATH_MAX);
|
2021-07-08 05:11:28 +02:00
|
|
|
}
|
|
|
|
break;
|
2022-07-11 15:02:48 +02:00
|
|
|
|
2024-05-15 08:31:28 +02:00
|
|
|
case FIOC_GETLK:
|
|
|
|
if (ret == -ENOTTY)
|
|
|
|
{
|
|
|
|
ret = file_getlk(filep, (FAR struct flock *)(uintptr_t)arg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FIOC_SETLK:
|
|
|
|
if (ret == -ENOTTY)
|
|
|
|
{
|
|
|
|
ret = file_setlk(filep, (FAR struct flock *)(uintptr_t)arg,
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FIOC_SETLKW:
|
|
|
|
if (ret == -ENOTTY)
|
|
|
|
{
|
|
|
|
ret = file_setlk(filep, (FAR struct flock *)(uintptr_t)arg,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-05-08 01:37:45 +02:00
|
|
|
#ifdef CONFIG_FDSAN
|
2023-11-08 11:00:07 +01:00
|
|
|
case FIOC_SETTAG_FDSAN:
|
|
|
|
filep->f_tag_fdsan = *(FAR uint64_t *)arg;
|
|
|
|
ret = OK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FIOC_GETTAG_FDSAN:
|
|
|
|
*(FAR uint64_t *)arg = filep->f_tag_fdsan;
|
|
|
|
ret = OK;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_FDCHECK
|
|
|
|
case FIOC_SETTAG_FDCHECK:
|
|
|
|
filep->f_tag_fdcheck = *(FAR uint8_t *)arg;
|
2023-05-08 01:37:45 +02:00
|
|
|
ret = OK;
|
|
|
|
break;
|
|
|
|
|
2023-11-08 11:00:07 +01:00
|
|
|
case FIOC_GETTAG_FDCHECK:
|
|
|
|
*(FAR uint8_t *)arg = filep->f_tag_fdcheck;
|
2023-05-08 01:37:45 +02:00
|
|
|
ret = OK;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2022-07-11 15:02:48 +02:00
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
case BIOC_BLKSSZGET:
|
2022-08-30 07:46:46 +02:00
|
|
|
if (ret == -ENOTTY && inode->u.i_ops != NULL &&
|
|
|
|
inode->u.i_ops->ioctl != NULL)
|
2022-07-11 15:02:48 +02:00
|
|
|
{
|
|
|
|
struct geometry geo;
|
|
|
|
ret = inode->u.i_ops->ioctl(filep, BIOC_GEOMETRY,
|
|
|
|
(unsigned long)(uintptr_t)&geo);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
*(FAR blksize_t *)(uintptr_t)arg = geo.geo_sectorsize;
|
|
|
|
}
|
|
|
|
}
|
2023-08-18 03:45:22 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BIOC_BLKGETSIZE:
|
|
|
|
if (ret == -ENOTTY && inode->u.i_ops != NULL &&
|
|
|
|
inode->u.i_ops->ioctl != NULL)
|
|
|
|
{
|
|
|
|
struct geometry geo;
|
|
|
|
ret = inode->u.i_ops->ioctl(filep, BIOC_GEOMETRY,
|
|
|
|
(unsigned long)(uintptr_t)&geo);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2023-11-04 13:11:19 +01:00
|
|
|
*(FAR blkcnt_t *)(uintptr_t)arg = geo.geo_nsectors;
|
2023-08-18 03:45:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-07-11 15:02:48 +02:00
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-10-26 07:40:18 +02:00
|
|
|
FAR struct file *filep;
|
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
|
|
|
|
2022-10-26 07:40:18 +02:00
|
|
|
/* Get the file structure corresponding to the file descriptor. */
|
|
|
|
|
|
|
|
ret = fs_getfilep(fd, &filep);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Let file_vioctl() do the real work. */
|
2020-05-04 11:00:51 +02:00
|
|
|
|
|
|
|
va_start(ap, req);
|
2022-10-26 07:40:18 +02:00
|
|
|
ret = file_vioctl(filep, req, ap);
|
2020-05-04 11:00:51 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
2017-10-11 16:39:19 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-10-26 07:40:18 +02:00
|
|
|
goto err;
|
2017-10-11 16:39:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2022-10-26 07:40:18 +02:00
|
|
|
|
|
|
|
err:
|
|
|
|
set_errno(-ret);
|
|
|
|
return ERROR;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|