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
|
|
|
*
|
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
|
2009-07-18 23:39:59 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-07-18 23:39:59 +02: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.
|
2009-07-18 23:39:59 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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>
|
2021-07-08 05:11:28 +02:00
|
|
|
#include <sys/ioctl.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>
|
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
|
|
|
/****************************************************************************
|
2021-01-04 04:33:16 +01:00
|
|
|
* Private Functions
|
2014-10-06 18:53:25 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2012-07-14 23:05:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: file_vfcntl
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-01-04 04:33:16 +01:00
|
|
|
static int file_vfcntl(FAR struct file *filep, int cmd, va_list ap)
|
2009-07-18 23:39:59 +02:00
|
|
|
{
|
2017-10-02 19:46:08 +02:00
|
|
|
int ret = -EINVAL;
|
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
|
|
|
{
|
2017-09-30 18:41:21 +02:00
|
|
|
return -EBADF;
|
2009-07-19 00:59:44 +02:00
|
|
|
}
|
|
|
|
|
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
|
2020-05-03 11:38:48 +02:00
|
|
|
* 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-18 23:39:59 +02:00
|
|
|
*/
|
|
|
|
|
2009-07-19 02:14:46 +02:00
|
|
|
{
|
2017-09-30 18:41:21 +02:00
|
|
|
/* Does not set the errno variable in the event of a failure */
|
|
|
|
|
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:
|
2020-05-03 11:38:48 +02:00
|
|
|
/* Get the file descriptor flags defined in <fcntl.h> that are
|
|
|
|
* associated with the file descriptor fd. File descriptor flags are
|
|
|
|
* associated with a single file descriptor and do not affect other
|
|
|
|
* file descriptors that refer to the same file.
|
2009-07-18 23:39:59 +02:00
|
|
|
*/
|
|
|
|
|
2020-02-03 15:21:54 +01:00
|
|
|
{
|
|
|
|
ret = filep->f_oflags & O_CLOEXEC ? FD_CLOEXEC : 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-07-18 23:39:59 +02:00
|
|
|
case F_SETFD:
|
2020-02-03 15:21:54 +01:00
|
|
|
/* Set the file descriptor flags defined in <fcntl.h>, that are
|
|
|
|
* associated with fd, to the third argument, arg, taken as type int.
|
|
|
|
* If the 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.
|
2009-07-18 23:39:59 +02:00
|
|
|
*/
|
|
|
|
|
2020-02-03 15:21:54 +01:00
|
|
|
{
|
|
|
|
int oflags = va_arg(ap, int);
|
|
|
|
|
|
|
|
if (oflags & ~FD_CLOEXEC)
|
|
|
|
{
|
|
|
|
ret = -ENOSYS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oflags & FD_CLOEXEC)
|
|
|
|
{
|
2022-03-04 04:53:57 +01:00
|
|
|
ret = file_ioctl(filep, FIOCLEX, NULL);
|
2020-02-03 15:21:54 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-04 04:53:57 +01:00
|
|
|
ret = file_ioctl(filep, FIONCLEX, NULL);
|
2020-02-03 15:21:54 +01:00
|
|
|
}
|
|
|
|
}
|
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:
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Get the file status flags and file access modes, defined in
|
|
|
|
* <fcntl.h>, for the file description associated with fd. The file
|
|
|
|
* access modes 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-18 23:39:59 +02:00
|
|
|
*/
|
|
|
|
|
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:
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Set the file status flags, defined in <fcntl.h>, for the file
|
|
|
|
* description associated with fd from the corresponding bits in
|
|
|
|
* the third argument, 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-18 23:39:59 +02:00
|
|
|
*/
|
|
|
|
|
2009-07-19 00:59:44 +02:00
|
|
|
{
|
2013-02-12 15:05:30 +01:00
|
|
|
int oflags = va_arg(ap, int);
|
2021-11-25 06:43:10 +01:00
|
|
|
int nonblock = !!(oflags & O_NONBLOCK);
|
2013-02-12 15:05:30 +01:00
|
|
|
|
2021-11-25 06:43:10 +01:00
|
|
|
ret = file_ioctl(filep, FIONBIO, &nonblock);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
oflags &= (FFCNTL & ~O_NONBLOCK);
|
|
|
|
filep->f_oflags &= ~(FFCNTL & ~O_NONBLOCK);
|
|
|
|
filep->f_oflags |= oflags;
|
2021-12-31 10:48:00 +01:00
|
|
|
|
|
|
|
if (filep->f_oflags & O_APPEND)
|
|
|
|
{
|
|
|
|
file_seek(filep, 0, SEEK_END);
|
|
|
|
}
|
2021-11-25 06:43:10 +01:00
|
|
|
}
|
2009-07-19 00:59:44 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-07-18 23:39:59 +02:00
|
|
|
case F_GETOWN:
|
2019-10-27 18:48:14 +01:00
|
|
|
/* If fd refers to a socket, get the process or process group ID
|
|
|
|
* specified 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 ID. If fd does
|
|
|
|
* not refer to a socket, the results are unspecified.
|
2009-07-18 23:39:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
case F_SETOWN:
|
2019-10-27 18:48:14 +01:00
|
|
|
/* If fd refers to a socket, set the process or process group ID
|
|
|
|
* specified to receive SIGURG signals when out-of-band data is
|
|
|
|
* available, using the value of the third argument, arg, taken as
|
|
|
|
* type int. Positive values indicate a process ID; negative values,
|
|
|
|
* other than -1, indicate a process group ID. If fd does not refer
|
|
|
|
* to a socket, the results are unspecified.
|
2009-07-18 23:39:59 +02:00
|
|
|
*/
|
|
|
|
|
2017-09-30 18:41:21 +02:00
|
|
|
ret = -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:
|
2019-10-27 18:48:14 +01:00
|
|
|
/* 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.
|
2009-07-18 23:39:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
case F_SETLK:
|
2019-10-27 18:48:14 +01:00
|
|
|
/* 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.
|
2009-07-18 23:39:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
case F_SETLKW:
|
2019-10-27 18:48:14 +01:00
|
|
|
/* 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.
|
2009-07-18 23:39:59 +02:00
|
|
|
*/
|
|
|
|
|
2017-09-30 18:41:21 +02:00
|
|
|
ret = -ENOSYS; /* Not implemented */
|
2012-07-14 23:05:40 +02:00
|
|
|
break;
|
2009-07-18 23:39:59 +02:00
|
|
|
|
2021-07-08 05:11:28 +02:00
|
|
|
case F_GETPATH:
|
|
|
|
/* Get the path of the file descriptor. The argument must be a buffer
|
|
|
|
* of size PATH_MAX or greater.
|
|
|
|
*/
|
|
|
|
|
|
|
|
{
|
|
|
|
ret = file_ioctl(filep, FIOC_FILEPATH, va_arg(ap, FAR char *));
|
|
|
|
}
|
|
|
|
|
2009-07-18 23:39:59 +02:00
|
|
|
default:
|
2012-07-14 23:05:40 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-07-18 23:39:59 +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
|
|
|
|
2021-01-04 04:33:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nx_vfcntl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nx_vfcntl(int fd, int cmd, va_list ap)
|
|
|
|
{
|
|
|
|
FAR struct file *filep;
|
|
|
|
int ret;
|
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
/* Get the file structure corresponding to the file descriptor. */
|
2021-01-04 04:33:16 +01:00
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
ret = fs_getfilep(fd, &filep);
|
|
|
|
if (ret >= 0)
|
2021-01-04 04:33:16 +01:00
|
|
|
{
|
2021-02-23 11:04:13 +01:00
|
|
|
DEBUGASSERT(filep != NULL);
|
2021-01-04 04:33:16 +01:00
|
|
|
|
2021-07-10 17:19:30 +02:00
|
|
|
/* Let file_vfcntl() do the real work. The errno is not set on
|
|
|
|
* failures.
|
|
|
|
*/
|
2021-01-04 04:33:16 +01:00
|
|
|
|
2021-07-10 17:19:30 +02:00
|
|
|
ret = file_vfcntl(filep, cmd, ap);
|
2021-01-04 04:33:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-08-26 21:31:18 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: file_fcntl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Similar to the standard fcntl function except that is accepts a struct
|
|
|
|
* struct file instance instead of a file descriptor.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* filep - Instance for struct file for the opened file.
|
|
|
|
* cmd - Identifies the operation to be performed. Command specific
|
|
|
|
* arguments may follow.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The nature of the return value depends on the command. Non-negative
|
|
|
|
* values indicate success. Failures are reported as negated errno
|
|
|
|
* values.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int file_fcntl(FAR struct file *filep, int cmd, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Setup to access the variable argument list */
|
|
|
|
|
|
|
|
va_start(ap, cmd);
|
|
|
|
|
|
|
|
/* Let file_vfcntl() do the real work. The errno is not set on
|
|
|
|
* failures.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = file_vfcntl(filep, cmd, ap);
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-07-14 23:05:40 +02:00
|
|
|
/****************************************************************************
|
2021-01-04 04:33:16 +01:00
|
|
|
* Name: nx_fcntl
|
2017-09-30 18:41:21 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2020-05-03 11:38:48 +02:00
|
|
|
* nx_fcntl() is similar to the standard 'fcntl' interface except that is
|
|
|
|
* not a cancellation point and it does not modify the errno variable.
|
2017-09-30 18:41:21 +02:00
|
|
|
*
|
2021-01-04 04:33:16 +01:00
|
|
|
* nx_fcntl() is an internal NuttX interface and should not be called
|
|
|
|
* from applications.
|
2017-09-30 18:41:21 +02: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 fcntl() for a list of appropriate
|
|
|
|
* errno values).
|
2017-09-30 18:41:21 +02:00
|
|
|
*
|
2012-07-14 23:05:40 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-03 11:38:48 +02:00
|
|
|
int nx_fcntl(int fd, int cmd, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Setup to access the variable argument list */
|
|
|
|
|
|
|
|
va_start(ap, cmd);
|
|
|
|
|
|
|
|
/* Let nx_vfcntl() do the real work. The errno is not set on
|
|
|
|
* failures.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = nx_vfcntl(fd, cmd, ap);
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fcntl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* fcntl() will perform the operation specified by 'cmd' on an open file.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* fd - File descriptor of the open file
|
|
|
|
* cmd - Identifies the operation to be performed. Command specific
|
|
|
|
* arguments may follow.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The returned value depends on the nature of the command but for all
|
|
|
|
* commands the return value of -1 (ERROR) indicates that an error has
|
|
|
|
* occurred and, in this case, the errno variable will be set
|
|
|
|
* appropriately
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int fcntl(int fd, int cmd, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* fcntl() is a cancellation point */
|
|
|
|
|
|
|
|
enter_cancellation_point();
|
|
|
|
|
|
|
|
/* Setup to access the variable argument list */
|
|
|
|
|
|
|
|
va_start(ap, cmd);
|
|
|
|
|
|
|
|
/* Let nx_vfcntl() do the real work. The errno is not set on
|
|
|
|
* failures.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = nx_vfcntl(fd, cmd, ap);
|
|
|
|
|
2009-07-19 00:59:44 +02:00
|
|
|
va_end(ap);
|
2017-09-30 18:41:21 +02:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
2016-12-09 20:49:36 +01:00
|
|
|
leave_cancellation_point();
|
2009-07-19 00:59:44 +02:00
|
|
|
return ret;
|
|
|
|
}
|