2009-07-18 23:39:59 +02:00
|
|
|
/****************************************************************************
|
2014-09-28 19:46:11 +02:00
|
|
|
* fs/vfs/fs_fcntl.c
|
2009-07-18 23:39:59 +02:00
|
|
|
*
|
2016-12-09 20:49:36 +01:00
|
|
|
* Copyright (C) 2009, 2012-2014, 2016 Gregory Nutt. All rights reserved.
|
2012-03-04 00:18:34 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2009-07-18 23:39:59 +02: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.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2009-07-19 00:59:44 +02:00
|
|
|
#include <stdarg.h>
|
2009-07-18 23:39:59 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2013-09-28 22:47:49 +02:00
|
|
|
#include <assert.h>
|
2009-07-18 23:39:59 +02:00
|
|
|
|
2016-12-09 20:49:36 +01:00
|
|
|
#include <nuttx/sched.h>
|
2016-12-10 16:08:26 +01:00
|
|
|
#include <nuttx/cancelpt.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2012-03-04 00:18:34 +01:00
|
|
|
#include <nuttx/net/net.h>
|
2009-07-19 00:59:44 +02:00
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2009-07-18 23:39:59 +02:00
|
|
|
|
2014-10-06 18:53:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-07-14 23:05:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: file_vfcntl
|
2014-10-06 18:53:25 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Similar to the standard vfcntl function except that is accepts a struct
|
|
|
|
* struct file instance instead of a file descriptor. Currently used
|
|
|
|
* only by aio_fcntl();
|
|
|
|
*
|
2012-07-14 23:05:40 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2009-07-19 00:59:44 +02:00
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2014-10-06 18:53:25 +02:00
|
|
|
int file_vfcntl(FAR struct file *filep, int cmd, va_list ap)
|
2009-07-18 23:39:59 +02:00
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
int errcode = 0;
|
2012-05-02 17:36:19 +02:00
|
|
|
int ret = OK;
|
2009-07-19 00:59:44 +02:00
|
|
|
|
|
|
|
/* Was this file opened ? */
|
2009-07-18 23:39:59 +02:00
|
|
|
|
2013-09-28 22:47:49 +02:00
|
|
|
if (!filep->f_inode)
|
2009-07-19 00:59:44 +02:00
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EBADF;
|
2009-07-19 00:59:44 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2009-07-18 23:39:59 +02:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case F_DUPFD:
|
|
|
|
/* Return a new file descriptor which shall be the lowest numbered
|
|
|
|
* available (that is, not already open) file descriptor greater than
|
|
|
|
* or equal to the third argument, arg, taken as an integer of type
|
|
|
|
* int. The new file descriptor shall refer to the same open file
|
|
|
|
* description as the original file descriptor, and shall share any
|
|
|
|
* locks. The FD_CLOEXEC flag associated with the new file descriptor
|
|
|
|
* shall be cleared to keep the file open across calls to one of the
|
|
|
|
* exec functions.
|
|
|
|
*/
|
|
|
|
|
2009-07-19 02:14:46 +02:00
|
|
|
{
|
2014-10-06 18:53:25 +02:00
|
|
|
ret = file_dup(filep, va_arg(ap, int));
|
2009-07-19 02:14:46 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-07-18 23:39:59 +02:00
|
|
|
case F_GETFD:
|
|
|
|
/* Get the file descriptor flags defined in <fcntl.h> that are associated
|
2013-09-28 22:47:49 +02:00
|
|
|
* with the file descriptor fd. File descriptor flags are associated
|
2009-07-18 23:39:59 +02:00
|
|
|
* with a single file descriptor and do not affect other file descriptors
|
|
|
|
* that refer to the same file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case F_SETFD:
|
|
|
|
/* Set the file descriptor flags defined in <fcntl.h>, that are associated
|
2013-09-28 22:47:49 +02:00
|
|
|
* with fd, to the third argument, arg, taken as type int. If the
|
2009-07-18 23:39:59 +02:00
|
|
|
* FD_CLOEXEC flag in the third argument is 0, the file shall remain open
|
|
|
|
* across the exec functions; otherwise, the file shall be closed upon
|
|
|
|
* successful execution of one of the exec functions.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = ENOSYS;
|
2012-07-14 23:05:40 +02:00
|
|
|
break;
|
2009-07-19 00:59:44 +02:00
|
|
|
|
2009-07-18 23:39:59 +02:00
|
|
|
case F_GETFL:
|
|
|
|
/* Get the file status flags and file access modes, defined in <fcntl.h>,
|
2013-09-28 22:47:49 +02:00
|
|
|
* for the file description associated with fd. The file access modes
|
2009-07-18 23:39:59 +02:00
|
|
|
* can be extracted from the return value using the mask O_ACCMODE, which is
|
|
|
|
* defined in <fcntl.h>. File status flags and file access modes are associated
|
|
|
|
* with the file description and do not affect other file descriptors that
|
|
|
|
* refer to the same file with different open file descriptions.
|
|
|
|
*/
|
|
|
|
|
2009-07-19 00:59:44 +02:00
|
|
|
{
|
2013-09-28 22:47:49 +02:00
|
|
|
ret = filep->f_oflags;
|
2009-07-19 00:59:44 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-07-18 23:39:59 +02:00
|
|
|
case F_SETFL:
|
|
|
|
/* Set the file status flags, defined in <fcntl.h>, for the file description
|
2013-09-28 22:47:49 +02:00
|
|
|
* associated with fd from the corresponding bits in the third argument,
|
2009-07-18 23:39:59 +02:00
|
|
|
* arg, taken as type int. Bits corresponding to the file access mode and
|
|
|
|
* the file creation flags, as defined in <fcntl.h>, that are set in arg shall
|
|
|
|
* be ignored. If any bits in arg other than those mentioned here are changed
|
|
|
|
* by the application, the result is unspecified.
|
|
|
|
*/
|
|
|
|
|
2009-07-19 00:59:44 +02:00
|
|
|
{
|
2013-02-12 15:05:30 +01:00
|
|
|
int oflags = va_arg(ap, int);
|
|
|
|
|
2013-09-28 22:47:49 +02:00
|
|
|
oflags &= FFCNTL;
|
|
|
|
filep->f_oflags &= ~FFCNTL;
|
|
|
|
filep->f_oflags |= oflags;
|
2009-07-19 00:59:44 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-07-18 23:39:59 +02:00
|
|
|
case F_GETOWN:
|
2013-09-28 22:47:49 +02:00
|
|
|
/* If fd refers to a socket, get the process or process group ID specified
|
2009-07-18 23:39:59 +02:00
|
|
|
* to receive SIGURG signals when out-of-band data is available. Positive values
|
|
|
|
* indicate a process ID; negative values, other than -1, indicate a process group
|
2013-09-28 22:47:49 +02:00
|
|
|
* ID. If fd does not refer to a socket, the results are unspecified.
|
2009-07-18 23:39:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
case F_SETOWN:
|
2013-09-28 22:47:49 +02:00
|
|
|
/* If fd refers to a socket, set the process or process group ID specified
|
2009-07-18 23:39:59 +02:00
|
|
|
* to receive SIGURG signals when out-of-band data is available, using the value
|
2014-04-14 00:22:22 +02:00
|
|
|
* of the third argument, arg, taken as type int. Positive values indicate a
|
2009-07-18 23:39:59 +02:00
|
|
|
* process ID; negative values, other than -1, indicate a process group ID. If
|
2013-09-28 22:47:49 +02:00
|
|
|
* fd does not refer to a socket, the results are unspecified.
|
2009-07-18 23:39:59 +02:00
|
|
|
*/
|
|
|
|
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EBADF; /* Only valid on socket descriptors */
|
2012-07-14 23:05:40 +02:00
|
|
|
break;
|
2009-07-19 00:59:44 +02:00
|
|
|
|
2009-07-18 23:39:59 +02:00
|
|
|
case F_GETLK:
|
|
|
|
/* Get the first lock which blocks the lock description pointed to by the third
|
|
|
|
* argument, arg, taken as a pointer to type struct flock, defined in <fcntl.h>.
|
|
|
|
* The information retrieved shall overwrite the information passed to fcntl() in
|
|
|
|
* the structure flock. If no lock is found that would prevent this lock from being
|
|
|
|
* created, then the structure shall be left unchanged except for the lock type
|
|
|
|
* which shall be set to F_UNLCK.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case F_SETLK:
|
|
|
|
/* Set or clear a file segment lock according to the lock description pointed to
|
|
|
|
* by the third argument, arg, taken as a pointer to type struct flock, defined in
|
|
|
|
* <fcntl.h>. F_SETLK can establish shared (or read) locks (F_RDLCK) or exclusive
|
|
|
|
* (or write) locks (F_WRLCK), as well as to remove either type of lock (F_UNLCK).
|
|
|
|
* F_RDLCK, F_WRLCK, and F_UNLCK are defined in <fcntl.h>. If a shared or exclusive
|
|
|
|
* lock cannot be set, fcntl() shall return immediately with a return value of -1.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case F_SETLKW:
|
|
|
|
/* This command shall be equivalent to F_SETLK except that if a shared or exclusive
|
|
|
|
* lock is blocked by other locks, the thread shall wait until the request can be
|
|
|
|
* satisfied. If a signal that is to be caught is received while fcntl() is waiting
|
|
|
|
* for a region, fcntl() shall be interrupted. Upon return from the signal handler,
|
|
|
|
* fcntl() shall return -1 with errno set to [EINTR], and the lock operation shall
|
|
|
|
* not be done.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = ENOSYS; /* Not implemented */
|
2012-07-14 23:05:40 +02:00
|
|
|
break;
|
2009-07-18 23:39:59 +02:00
|
|
|
|
|
|
|
default:
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EINVAL;
|
2012-07-14 23:05:40 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-07-18 23:39:59 +02:00
|
|
|
|
2009-07-19 00:59:44 +02:00
|
|
|
errout:
|
2016-06-11 22:40:07 +02:00
|
|
|
if (errcode != 0)
|
2009-07-18 23:39:59 +02:00
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
set_errno(errcode);
|
2009-07-18 23:39:59 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
2012-07-14 23:05:40 +02:00
|
|
|
|
2012-05-02 17:36:19 +02:00
|
|
|
return ret;
|
2009-07-18 23:39:59 +02:00
|
|
|
}
|
2009-07-19 00:59:44 +02:00
|
|
|
#endif /* CONFIG_NFILE_DESCRIPTORS > 0 */
|
|
|
|
|
2012-07-14 23:05:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: fcntl
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-09-28 22:47:49 +02:00
|
|
|
int fcntl(int fd, int cmd, ...)
|
2009-07-19 00:59:44 +02:00
|
|
|
{
|
2014-10-06 18:53:25 +02:00
|
|
|
FAR struct file *filep;
|
2009-07-19 00:59:44 +02:00
|
|
|
va_list ap;
|
|
|
|
int ret;
|
|
|
|
|
2016-12-09 20:49:36 +01:00
|
|
|
/* fcntl() is a cancellation point */
|
|
|
|
|
2016-12-10 15:36:58 +01:00
|
|
|
(void)enter_cancellation_point();
|
2016-12-09 20:49:36 +01:00
|
|
|
|
2009-07-19 00:59:44 +02:00
|
|
|
/* Setup to access the variable argument list */
|
|
|
|
|
|
|
|
va_start(ap, cmd);
|
|
|
|
|
|
|
|
/* Did we get a valid file descriptor? */
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2013-09-28 22:47:49 +02:00
|
|
|
if ((unsigned int)fd < CONFIG_NFILE_DESCRIPTORS)
|
2009-07-19 00:59:44 +02:00
|
|
|
{
|
2014-10-06 18:53:25 +02:00
|
|
|
/* Get the file structure corresponding to the file descriptor. */
|
|
|
|
|
|
|
|
filep = fs_getfilep(fd);
|
|
|
|
if (!filep)
|
|
|
|
{
|
|
|
|
/* The errno value has already been set */
|
2009-07-19 00:59:44 +02:00
|
|
|
|
2016-12-09 20:49:36 +01:00
|
|
|
va_end(ap);
|
|
|
|
leave_cancellation_point();
|
2014-10-06 18:53:25 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Let file_vfcntl() do the real work */
|
|
|
|
|
|
|
|
ret = file_vfcntl(filep, cmd, ap);
|
2009-07-19 00:59:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* No... check for operations on a socket descriptor */
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0
|
2013-09-28 22:47:49 +02:00
|
|
|
if ((unsigned int)fd < (CONFIG_NFILE_DESCRIPTORS+CONFIG_NSOCKET_DESCRIPTORS))
|
2009-07-19 00:59:44 +02:00
|
|
|
{
|
|
|
|
/* Yes.. defer socket descriptor operations to net_vfcntl() */
|
|
|
|
|
2013-09-28 22:47:49 +02:00
|
|
|
ret = net_vfcntl(fd, cmd, ap);
|
2009-07-19 00:59:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* No.. this descriptor number is out of range */
|
|
|
|
|
|
|
|
ret = EBADF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(ap);
|
2016-12-09 20:49:36 +01:00
|
|
|
leave_cancellation_point();
|
2009-07-19 00:59:44 +02:00
|
|
|
return ret;
|
|
|
|
}
|