2008-07-31 02:28:24 +02:00
|
|
|
/****************************************************************************
|
2014-09-28 19:06:21 +02:00
|
|
|
* fs/inode/fs_files.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-03-30 01:36:19 +02: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
|
|
|
*
|
2020-03-30 01:36:19 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-03-30 01:36:19 +02: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
|
|
|
*
|
2008-07-31 02:28:24 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-07-31 02:28:24 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2008-07-31 02:28:24 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-15 01:18:32 +01:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2024-06-25 05:28:11 +02:00
|
|
|
#include <execinfo.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
2021-03-16 13:13:02 +01:00
|
|
|
#include <fcntl.h>
|
2024-06-25 05:28:11 +02:00
|
|
|
#include <debug.h>
|
2024-05-21 06:57:57 +02:00
|
|
|
#include <stdio.h>
|
2011-04-03 22:41:49 +02:00
|
|
|
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2021-02-23 11:04:13 +01:00
|
|
|
#include <nuttx/cancelpt.h>
|
2024-06-25 05:28:11 +02:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2023-02-06 15:56:05 +01:00
|
|
|
#include <nuttx/sched.h>
|
2023-11-13 13:59:42 +01:00
|
|
|
#include <nuttx/spawn.h>
|
2009-12-15 01:18:32 +01:00
|
|
|
|
2023-05-08 01:37:45 +02:00
|
|
|
#ifdef CONFIG_FDSAN
|
|
|
|
# include <android/fdsan.h>
|
|
|
|
#endif
|
|
|
|
|
2023-06-05 09:43:05 +02:00
|
|
|
#ifdef CONFIG_FDCHECK
|
|
|
|
# include <nuttx/fdcheck.h>
|
|
|
|
#endif
|
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-07-31 02:28:24 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Private Functions
|
2008-07-31 02:28:24 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2023-11-08 10:23:44 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: files_fget_by_index
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct file *files_fget_by_index(FAR struct filelist *list,
|
|
|
|
int l1, int l2)
|
|
|
|
{
|
|
|
|
FAR struct file *filep;
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
flags = spin_lock_irqsave(&list->fl_lock);
|
|
|
|
|
|
|
|
filep = &list->fl_files[l1][l2];
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&list->fl_lock, flags);
|
|
|
|
|
|
|
|
return filep;
|
|
|
|
}
|
|
|
|
|
2021-03-16 13:13:02 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: files_extend
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int files_extend(FAR struct filelist *list, size_t row)
|
|
|
|
{
|
2023-11-08 10:23:44 +01:00
|
|
|
FAR struct file **files;
|
|
|
|
uint8_t orig_rows;
|
|
|
|
FAR void *tmp;
|
|
|
|
int flags;
|
2021-03-16 13:13:02 +01:00
|
|
|
int i;
|
2023-11-08 10:23:44 +01:00
|
|
|
int j;
|
2021-03-16 13:13:02 +01:00
|
|
|
|
|
|
|
if (row <= list->fl_rows)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-12 14:04:07 +01:00
|
|
|
if (files_countlist(list) > OPEN_MAX)
|
2021-09-02 08:46:31 +02:00
|
|
|
{
|
|
|
|
return -EMFILE;
|
|
|
|
}
|
|
|
|
|
2023-11-08 10:23:44 +01:00
|
|
|
orig_rows = list->fl_rows;
|
|
|
|
|
|
|
|
files = kmm_malloc(sizeof(FAR struct file *) * row);
|
|
|
|
DEBUGASSERT(files);
|
|
|
|
if (files == NULL)
|
2021-03-16 13:13:02 +01:00
|
|
|
{
|
|
|
|
return -ENFILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = list->fl_rows;
|
|
|
|
do
|
|
|
|
{
|
2023-11-08 10:23:44 +01:00
|
|
|
files[i] = kmm_zalloc(sizeof(struct file) *
|
|
|
|
CONFIG_NFILE_DESCRIPTORS_PER_BLOCK);
|
|
|
|
if (files[i] == NULL)
|
2021-03-16 13:13:02 +01:00
|
|
|
{
|
|
|
|
while (--i >= list->fl_rows)
|
|
|
|
{
|
2023-11-08 10:23:44 +01:00
|
|
|
kmm_free(files[i]);
|
2021-03-16 13:13:02 +01:00
|
|
|
}
|
|
|
|
|
2023-11-08 10:23:44 +01:00
|
|
|
kmm_free(files);
|
2021-03-16 13:13:02 +01:00
|
|
|
return -ENFILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (++i < row);
|
|
|
|
|
2023-11-08 10:23:44 +01:00
|
|
|
flags = spin_lock_irqsave(&list->fl_lock);
|
2022-02-18 13:28:04 +01:00
|
|
|
|
2023-11-08 10:23:44 +01:00
|
|
|
/* To avoid race condition, if the file list is updated by other threads
|
|
|
|
* and list rows is greater or equal than temp list,
|
|
|
|
* release the obsolete buffers
|
2022-02-18 13:28:04 +01:00
|
|
|
*/
|
|
|
|
|
2023-11-08 10:23:44 +01:00
|
|
|
if (orig_rows != list->fl_rows && list->fl_rows >= row)
|
|
|
|
{
|
|
|
|
spin_unlock_irqrestore(&list->fl_lock, flags);
|
|
|
|
|
|
|
|
for (j = orig_rows; j < i; j++)
|
|
|
|
{
|
|
|
|
kmm_free(files[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
kmm_free(files);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2023-11-14 16:58:25 +01:00
|
|
|
if (list->fl_files != NULL)
|
|
|
|
{
|
|
|
|
memcpy(files, list->fl_files,
|
|
|
|
list->fl_rows * sizeof(FAR struct file *));
|
|
|
|
}
|
2023-11-08 10:23:44 +01:00
|
|
|
|
|
|
|
tmp = list->fl_files;
|
|
|
|
list->fl_files = files;
|
|
|
|
list->fl_rows = row;
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&list->fl_lock, flags);
|
|
|
|
|
2024-03-06 08:03:13 +01:00
|
|
|
if (tmp != NULL && tmp != &list->fl_prefile)
|
2023-11-14 16:58:25 +01:00
|
|
|
{
|
|
|
|
kmm_free(tmp);
|
|
|
|
}
|
2023-11-08 10:23:44 +01:00
|
|
|
|
|
|
|
return OK;
|
2021-03-16 13:13:02 +01:00
|
|
|
}
|
|
|
|
|
2023-02-06 15:56:05 +01:00
|
|
|
static void task_fssync(FAR struct tcb_s *tcb, FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct filelist *list;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
list = &tcb->group->tg_filelist;
|
|
|
|
|
|
|
|
for (i = 0; i < list->fl_rows; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < CONFIG_NFILE_DESCRIPTORS_PER_BLOCK; j++)
|
|
|
|
{
|
|
|
|
FAR struct file *filep;
|
|
|
|
|
2023-11-08 10:23:44 +01:00
|
|
|
filep = files_fget_by_index(list, i, j);
|
|
|
|
if (filep->f_inode != NULL)
|
2023-02-06 15:56:05 +01:00
|
|
|
{
|
|
|
|
file_fsync(filep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-22 10:53:58 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nx_dup3_from_tcb
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* nx_dup3_from_tcb() is similar to the standard 'dup3' interface
|
|
|
|
* except that is not a cancellation point and it does not modify the
|
|
|
|
* errno variable.
|
|
|
|
*
|
|
|
|
* nx_dup3_from_tcb() is an internal NuttX interface and should not be
|
|
|
|
* called from applications.
|
|
|
|
*
|
|
|
|
* Clone a file descriptor to a specific descriptor number and
|
|
|
|
* specific flags.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* fd2 is returned on success; a negated errno value is return on
|
|
|
|
* any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nx_dup3_from_tcb(FAR struct tcb_s *tcb, int fd1, int fd2,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
FAR struct filelist *list;
|
2023-12-20 11:39:40 +01:00
|
|
|
FAR struct file *filep;
|
|
|
|
#ifdef CONFIG_FDCHECK
|
|
|
|
uint8_t f_tag_fdcheck;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_FDSAN
|
|
|
|
uint64_t f_tag_fdsan;
|
|
|
|
#endif
|
2023-11-12 14:04:07 +01:00
|
|
|
int count;
|
2023-10-22 10:53:58 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (fd1 == fd2)
|
|
|
|
{
|
|
|
|
return fd1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_FDCHECK
|
|
|
|
fd1 = fdcheck_restore(fd1);
|
|
|
|
fd2 = fdcheck_restore(fd2);
|
|
|
|
#endif
|
|
|
|
|
2023-12-13 16:09:22 +01:00
|
|
|
/* Get the file descriptor list. It should not be NULL in this context. */
|
2023-10-22 10:53:58 +02:00
|
|
|
|
2023-12-13 16:09:22 +01:00
|
|
|
list = nxsched_get_files_from_tcb(tcb);
|
2023-11-12 14:04:07 +01:00
|
|
|
count = files_countlist(list);
|
|
|
|
|
2023-12-13 16:09:22 +01:00
|
|
|
if (fd1 < 0 || fd1 >= count || fd2 < 0)
|
2023-10-22 10:53:58 +02:00
|
|
|
{
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
2023-11-12 14:04:07 +01:00
|
|
|
if (fd2 >= count)
|
2023-10-22 10:53:58 +02:00
|
|
|
{
|
|
|
|
ret = files_extend(list, fd2 / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 11:39:40 +01:00
|
|
|
filep = files_fget(list, fd2);
|
|
|
|
if (filep == NULL)
|
|
|
|
{
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_FDSAN
|
|
|
|
f_tag_fdsan = filep->f_tag_fdsan;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_FDCHECK
|
|
|
|
f_tag_fdcheck = filep->f_tag_fdcheck;
|
|
|
|
#endif
|
|
|
|
|
2023-10-22 10:53:58 +02:00
|
|
|
/* Perform the dup3 operation */
|
|
|
|
|
2023-12-20 11:39:40 +01:00
|
|
|
ret = file_dup3(files_fget(list, fd1), filep, flags);
|
2023-12-13 16:09:22 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2023-10-22 10:53:58 +02:00
|
|
|
|
2023-12-20 11:39:40 +01:00
|
|
|
#ifdef CONFIG_FDSAN
|
|
|
|
filep->f_tag_fdsan = f_tag_fdsan;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_FDCHECK
|
|
|
|
filep->f_tag_fdcheck = f_tag_fdcheck;
|
|
|
|
#endif
|
|
|
|
|
2023-10-22 10:53:58 +02:00
|
|
|
#ifdef CONFIG_FDCHECK
|
2023-12-13 16:09:22 +01:00
|
|
|
return fdcheck_protect(fd2);
|
2023-10-22 10:53:58 +02:00
|
|
|
#else
|
2023-12-13 16:09:22 +01:00
|
|
|
return fd2;
|
2023-10-22 10:53:58 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-07-31 02:28:24 +02:00
|
|
|
/****************************************************************************
|
2008-08-01 02:22:09 +02:00
|
|
|
* Public Functions
|
2008-07-31 02:28:24 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-08-01 02:22:09 +02:00
|
|
|
/****************************************************************************
|
2013-01-26 21:17:29 +01:00
|
|
|
* Name: files_initlist
|
2008-08-01 02:22:09 +02:00
|
|
|
*
|
2013-01-26 21:17:29 +01:00
|
|
|
* Description: Initializes the list of files for a new task
|
2008-08-01 02:22:09 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
2012-02-01 00:39:12 +01:00
|
|
|
|
2013-01-26 21:17:29 +01:00
|
|
|
void files_initlist(FAR struct filelist *list)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2024-03-06 08:03:13 +01:00
|
|
|
/* The first row will reuse pre-allocated files, which will avoid
|
|
|
|
* unnecessary allocator accesses during file initialization.
|
|
|
|
*/
|
|
|
|
|
|
|
|
list->fl_rows = 1;
|
|
|
|
list->fl_files = &list->fl_prefile;
|
|
|
|
list->fl_prefile = list->fl_prefiles;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2024-06-25 05:28:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: files_dumplist
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Dump the list of files.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void files_dumplist(FAR struct filelist *list)
|
|
|
|
{
|
|
|
|
int count = files_countlist(list);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
syslog(LOG_INFO, "%-4s%-4s%-8s%-5s%-10s%-14s"
|
|
|
|
#if CONFIG_FS_BACKTRACE > 0
|
|
|
|
" BACKTRACE"
|
|
|
|
#endif
|
|
|
|
"\n",
|
|
|
|
"PID", "FD", "FLAGS", "TYPE", "POS", "PATH"
|
|
|
|
);
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
FAR struct file *filep = files_fget(list, i);
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
|
|
|
#if CONFIG_FS_BACKTRACE > 0
|
|
|
|
char buf[BACKTRACE_BUFFER_SIZE(CONFIG_FS_BACKTRACE)];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Is there an inode associated with the file descriptor? */
|
|
|
|
|
|
|
|
if (filep == NULL || filep->f_inode == NULL)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_ioctl(filep, FIOC_FILEPATH, path) < 0)
|
|
|
|
{
|
|
|
|
path[0] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CONFIG_FS_BACKTRACE > 0
|
|
|
|
backtrace_format(buf, sizeof(buf), filep->f_backtrace,
|
|
|
|
CONFIG_FS_BACKTRACE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
syslog(LOG_INFO, "%-4d%-4d%-8d%-5x%-10ld%-14s"
|
|
|
|
#if CONFIG_FS_BACKTRACE > 0
|
|
|
|
" %s"
|
|
|
|
#endif
|
|
|
|
"\n", getpid(), i, filep->f_oflags,
|
|
|
|
INODE_GET_TYPE(filep->f_inode),
|
|
|
|
(long)filep->f_pos, path
|
|
|
|
#if CONFIG_FS_BACKTRACE > 0
|
|
|
|
, buf
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-01 02:22:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: files_releaselist
|
|
|
|
*
|
2012-08-03 21:11:11 +02:00
|
|
|
* Description:
|
|
|
|
* Release a reference to the file list
|
2008-08-01 02:22:09 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
2009-09-16 22:25:16 +02:00
|
|
|
|
2013-01-26 21:17:29 +01:00
|
|
|
void files_releaselist(FAR struct filelist *list)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-01-26 21:17:29 +01:00
|
|
|
int i;
|
2021-03-16 13:13:02 +01:00
|
|
|
int j;
|
2012-07-14 23:05:40 +02:00
|
|
|
|
2013-01-26 21:17:29 +01:00
|
|
|
DEBUGASSERT(list);
|
2012-07-14 23:05:40 +02:00
|
|
|
|
2013-01-26 21:17:29 +01:00
|
|
|
/* Close each file descriptor .. Normally, you would need take the list
|
2022-09-06 08:18:45 +02:00
|
|
|
* mutex, but it is safe to ignore the mutex in this context
|
2020-03-30 01:36:19 +02:00
|
|
|
* because there should not be any references in this context.
|
2013-01-26 21:17:29 +01:00
|
|
|
*/
|
2012-07-14 23:05:40 +02:00
|
|
|
|
2021-03-16 13:13:02 +01:00
|
|
|
for (i = list->fl_rows - 1; i >= 0; i--)
|
2013-01-26 21:17:29 +01:00
|
|
|
{
|
2021-03-16 13:13:02 +01:00
|
|
|
for (j = CONFIG_NFILE_DESCRIPTORS_PER_BLOCK - 1; j >= 0; j--)
|
|
|
|
{
|
|
|
|
file_close(&list->fl_files[i][j]);
|
|
|
|
}
|
|
|
|
|
2024-03-06 08:03:13 +01:00
|
|
|
if (i != 0)
|
|
|
|
{
|
|
|
|
kmm_free(list->fl_files[i]);
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2012-07-14 23:05:40 +02:00
|
|
|
|
2024-03-06 08:03:13 +01:00
|
|
|
if (list->fl_files != &list->fl_prefile)
|
|
|
|
{
|
|
|
|
kmm_free(list->fl_files);
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2023-11-12 14:04:07 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: files_countlist
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Given a file descriptor, return the corresponding instance of struct
|
|
|
|
* file.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* fd - The file descriptor
|
|
|
|
* filep - The location to return the struct file instance
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int files_countlist(FAR struct filelist *list)
|
|
|
|
{
|
|
|
|
return list->fl_rows * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK;
|
|
|
|
}
|
|
|
|
|
2023-12-04 08:34:48 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: files_fget
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get the instance of struct file from file list by file descriptor.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* list - The list of files for a task.
|
|
|
|
* fd - A valid descriptor between 0 and files_countlist(list).
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Pointer to file structure of list[fd].
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct file *files_fget(FAR struct filelist *list, int fd)
|
|
|
|
{
|
|
|
|
return files_fget_by_index(list, fd / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK,
|
|
|
|
fd % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK);
|
|
|
|
}
|
|
|
|
|
2008-08-01 02:22:09 +02:00
|
|
|
/****************************************************************************
|
2023-04-26 05:44:08 +02:00
|
|
|
* Name: file_allocate_from_tcb
|
2008-08-01 02:22:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2020-03-30 01:36:19 +02:00
|
|
|
* Allocate a struct files instance and associate it with an inode
|
2022-10-26 22:36:13 +02:00
|
|
|
* instance.
|
2008-08-01 02:22:09 +02:00
|
|
|
*
|
2022-10-26 11:45:19 +02:00
|
|
|
* Returned Value:
|
2022-10-26 22:36:13 +02:00
|
|
|
* Returns the file descriptor == index into the files array on success;
|
|
|
|
* a negated errno value is returned on any failure.
|
2022-10-26 11:45:19 +02:00
|
|
|
*
|
2008-08-01 02:22:09 +02:00
|
|
|
****************************************************************************/
|
2009-07-19 02:14:46 +02:00
|
|
|
|
2023-04-26 05:44:08 +02:00
|
|
|
int file_allocate_from_tcb(FAR struct tcb_s *tcb, FAR struct inode *inode,
|
|
|
|
int oflags, off_t pos, FAR void *priv, int minfd,
|
|
|
|
bool addref)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2023-12-07 09:46:01 +01:00
|
|
|
int i = minfd / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK;
|
|
|
|
int j = minfd % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK;
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR struct filelist *list;
|
2023-11-08 10:23:44 +01:00
|
|
|
FAR struct file *filep;
|
2023-12-07 09:46:01 +01:00
|
|
|
irqstate_t flags;
|
2020-03-30 01:36:19 +02:00
|
|
|
int ret;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-04-14 18:14:38 +02:00
|
|
|
/* Get the file descriptor list. It should not be NULL in this context. */
|
|
|
|
|
2023-04-26 05:44:08 +02:00
|
|
|
list = nxsched_get_files_from_tcb(tcb);
|
2020-03-30 01:36:19 +02:00
|
|
|
|
2023-12-07 09:46:01 +01:00
|
|
|
/* Find free file */
|
|
|
|
|
|
|
|
flags = spin_lock_irqsave(&list->fl_lock);
|
2021-03-16 13:13:02 +01:00
|
|
|
|
2023-12-07 09:46:01 +01:00
|
|
|
for (; ; i++, j = 0)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2023-12-07 09:46:01 +01:00
|
|
|
if (i >= list->fl_rows)
|
2007-02-27 22:17:21 +01:00
|
|
|
{
|
2023-12-07 09:46:01 +01:00
|
|
|
spin_unlock_irqrestore(&list->fl_lock, flags);
|
2021-03-16 13:13:02 +01:00
|
|
|
|
2023-12-07 09:46:01 +01:00
|
|
|
ret = files_extend(list, i + 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags = spin_lock_irqsave(&list->fl_lock);
|
|
|
|
}
|
2021-03-16 13:13:02 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2023-12-07 09:46:01 +01:00
|
|
|
filep = &list->fl_files[i][j];
|
2023-11-08 10:23:44 +01:00
|
|
|
if (filep->f_inode == NULL)
|
2021-03-16 13:13:02 +01:00
|
|
|
{
|
2023-12-07 09:46:01 +01:00
|
|
|
filep->f_oflags = oflags;
|
|
|
|
filep->f_pos = pos;
|
|
|
|
filep->f_inode = inode;
|
|
|
|
filep->f_priv = priv;
|
|
|
|
|
2023-11-08 10:23:44 +01:00
|
|
|
goto found;
|
2021-03-16 13:13:02 +01:00
|
|
|
}
|
2007-02-27 22:17:21 +01:00
|
|
|
}
|
2021-03-16 13:13:02 +01:00
|
|
|
while (++j < CONFIG_NFILE_DESCRIPTORS_PER_BLOCK);
|
|
|
|
}
|
|
|
|
|
2023-11-08 10:23:44 +01:00
|
|
|
found:
|
2023-12-07 09:46:01 +01:00
|
|
|
spin_unlock_irqrestore(&list->fl_lock, flags);
|
2022-10-26 12:15:16 +02:00
|
|
|
|
|
|
|
if (addref)
|
|
|
|
{
|
|
|
|
inode_addref(inode);
|
|
|
|
}
|
|
|
|
|
2024-05-21 06:57:57 +02:00
|
|
|
FS_ADD_BACKTRACE(filep);
|
|
|
|
|
2023-06-05 09:43:05 +02:00
|
|
|
#ifdef CONFIG_FDCHECK
|
2023-11-08 10:23:44 +01:00
|
|
|
return fdcheck_protect(i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + j);
|
2023-06-05 09:43:05 +02:00
|
|
|
#else
|
2023-11-08 10:23:44 +01:00
|
|
|
return i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + j;
|
2023-06-05 09:43:05 +02:00
|
|
|
#endif
|
2021-03-16 13:13:02 +01:00
|
|
|
}
|
|
|
|
|
2023-04-26 05:44:08 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: file_allocate
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a struct files instance and associate it with an inode
|
|
|
|
* instance.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Returns the file descriptor == index into the files array on success;
|
|
|
|
* a negated errno value is returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int file_allocate(FAR struct inode *inode, int oflags, off_t pos,
|
|
|
|
FAR void *priv, int minfd, bool addref)
|
|
|
|
{
|
|
|
|
return file_allocate_from_tcb(nxsched_self(), inode, oflags,
|
|
|
|
pos, priv, minfd, addref);
|
|
|
|
}
|
|
|
|
|
2024-05-21 06:57:57 +02:00
|
|
|
FAR char *file_dump_backtrace(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
#if CONFIG_FS_BACKTRACE > 0
|
2024-06-05 04:49:17 +02:00
|
|
|
backtrace_format(buffer, len, filep->f_backtrace, CONFIG_FS_BACKTRACE);
|
2024-05-21 06:57:57 +02:00
|
|
|
#else
|
|
|
|
buffer[0] = '\0';
|
|
|
|
#endif
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2021-03-16 13:13:02 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: files_duplist
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Duplicate parent task's file descriptors.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-11-13 13:59:42 +01:00
|
|
|
int files_duplist(FAR struct filelist *plist, FAR struct filelist *clist,
|
|
|
|
FAR const posix_spawn_file_actions_t *actions,
|
|
|
|
bool cloexec)
|
2021-03-16 13:13:02 +01:00
|
|
|
{
|
2023-11-13 13:59:42 +01:00
|
|
|
bool fcloexec;
|
2021-03-16 13:13:02 +01:00
|
|
|
int ret;
|
2023-11-13 13:59:42 +01:00
|
|
|
int fd;
|
2021-03-16 13:13:02 +01:00
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
for (i = 0; i < plist->fl_rows; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < CONFIG_NFILE_DESCRIPTORS_PER_BLOCK; j++)
|
|
|
|
{
|
|
|
|
FAR struct file *filep;
|
2023-11-13 13:59:42 +01:00
|
|
|
|
|
|
|
fd = i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + j;
|
2022-03-01 07:28:15 +01:00
|
|
|
#ifdef CONFIG_FDCLONE_STDIO
|
2021-03-16 13:13:02 +01:00
|
|
|
|
2022-03-01 07:28:15 +01:00
|
|
|
/* Determine how many file descriptors to clone. If
|
|
|
|
* CONFIG_FDCLONE_DISABLE is set, no file descriptors will be
|
|
|
|
* cloned. If CONFIG_FDCLONE_STDIO is set, only the first
|
|
|
|
* three descriptors (stdin, stdout, and stderr) will be
|
|
|
|
* cloned. Otherwise all file descriptors will be cloned.
|
|
|
|
*/
|
|
|
|
|
2023-11-13 13:59:42 +01:00
|
|
|
if (fd >= 3)
|
2021-03-16 13:13:02 +01:00
|
|
|
{
|
2023-11-08 10:23:44 +01:00
|
|
|
return OK;
|
2021-03-16 13:13:02 +01:00
|
|
|
}
|
2022-03-01 07:28:15 +01:00
|
|
|
#endif
|
2021-03-16 13:13:02 +01:00
|
|
|
|
2023-11-08 10:23:44 +01:00
|
|
|
filep = files_fget_by_index(plist, i, j);
|
|
|
|
if (filep->f_inode == NULL)
|
2021-03-16 13:13:02 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-11-13 13:59:42 +01:00
|
|
|
fcloexec = (cloexec && (filep->f_oflags & O_CLOEXEC));
|
|
|
|
|
|
|
|
/* Skip file dup if file action is unnecessary to duplicate */
|
|
|
|
|
|
|
|
if (actions != NULL)
|
|
|
|
{
|
2023-11-22 10:04:43 +01:00
|
|
|
#ifdef CONFIG_FDCHECK
|
|
|
|
fd = fdcheck_protect(fd);
|
|
|
|
#endif
|
2023-11-13 13:59:42 +01:00
|
|
|
if (!spawn_file_is_duplicateable(actions, fd, fcloexec))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fcloexec)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-16 13:13:02 +01:00
|
|
|
ret = files_extend(clist, i + 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-11-08 10:23:44 +01:00
|
|
|
return ret;
|
2021-03-16 13:13:02 +01:00
|
|
|
}
|
|
|
|
|
2023-10-22 11:06:46 +02:00
|
|
|
/* Yes... duplicate it for the child, include O_CLOEXEC flag. */
|
2021-03-16 13:13:02 +01:00
|
|
|
|
2023-11-13 13:59:42 +01:00
|
|
|
ret = file_dup2(filep, files_fget_by_index(clist, i, j));
|
2021-03-16 13:13:02 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-11-08 10:23:44 +01:00
|
|
|
return ret;
|
2021-03-16 13:13:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-08 10:23:44 +01:00
|
|
|
return OK;
|
2021-03-16 13:13:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fs_getfilep
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Given a file descriptor, return the corresponding instance of struct
|
|
|
|
* file.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* fd - The file descriptor
|
|
|
|
* filep - The location to return the struct file instance
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int fs_getfilep(int fd, FAR struct file **filep)
|
|
|
|
{
|
|
|
|
FAR struct filelist *list;
|
|
|
|
|
2023-06-05 09:43:05 +02:00
|
|
|
#ifdef CONFIG_FDCHECK
|
|
|
|
fd = fdcheck_restore(fd);
|
|
|
|
#endif
|
|
|
|
|
2022-10-15 19:48:35 +02:00
|
|
|
*filep = NULL;
|
2021-03-16 13:13:02 +01:00
|
|
|
|
|
|
|
list = nxsched_get_files();
|
|
|
|
|
|
|
|
/* The file list can be NULL under two cases: (1) One is an obscure
|
|
|
|
* cornercase: When memory management debug output is enabled. Then
|
|
|
|
* there may be attempts to write to stdout from malloc before the group
|
|
|
|
* data has been allocated. The other other is (2) if this is a kernel
|
|
|
|
* thread. Kernel threads have no allocated file descriptors.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
{
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
2023-11-12 14:04:07 +01:00
|
|
|
if (fd < 0 || fd >= files_countlist(list))
|
2021-03-16 13:13:02 +01:00
|
|
|
{
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The descriptor is in a valid range to file descriptor... Get the
|
|
|
|
* thread-specific file list.
|
|
|
|
*/
|
|
|
|
|
2023-11-08 10:23:44 +01:00
|
|
|
*filep = files_fget(list, fd);
|
2022-04-19 08:45:55 +02:00
|
|
|
|
|
|
|
/* if f_inode is NULL, fd was closed */
|
|
|
|
|
2023-11-08 10:23:44 +01:00
|
|
|
if ((*filep)->f_inode == NULL)
|
2021-03-16 13:13:02 +01:00
|
|
|
{
|
2022-10-15 19:48:35 +02:00
|
|
|
*filep = NULL;
|
2023-11-08 10:23:44 +01:00
|
|
|
return -EBADF;
|
2021-03-16 13:13:02 +01:00
|
|
|
}
|
|
|
|
|
2023-11-08 10:23:44 +01:00
|
|
|
return OK;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2021-01-02 17:53:46 +01:00
|
|
|
/****************************************************************************
|
2023-04-26 05:44:08 +02:00
|
|
|
* Name: nx_dup2_from_tcb
|
2021-01-02 17:53:46 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2023-04-26 05:44:08 +02:00
|
|
|
* nx_dup2_from_tcb() is similar to the standard 'dup2' interface
|
|
|
|
* except that is not a cancellation point and it does not modify the
|
|
|
|
* errno variable.
|
2021-02-23 11:04:13 +01:00
|
|
|
*
|
2023-04-26 05:44:08 +02:00
|
|
|
* nx_dup2_from_tcb() is an internal NuttX interface and should not be
|
|
|
|
* called from applications.
|
2021-02-23 11:04:13 +01:00
|
|
|
*
|
2021-01-02 17:53:46 +01:00
|
|
|
* Clone a file descriptor to a specific descriptor number.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* fd2 is returned on success; a negated errno value is return on
|
|
|
|
* any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-26 05:44:08 +02:00
|
|
|
int nx_dup2_from_tcb(FAR struct tcb_s *tcb, int fd1, int fd2)
|
2021-01-02 17:53:46 +01:00
|
|
|
{
|
2023-10-22 10:53:58 +02:00
|
|
|
return nx_dup3_from_tcb(tcb, fd1, fd2, 0);
|
2021-01-02 17:53:46 +01:00
|
|
|
}
|
|
|
|
|
2023-04-26 05:44:08 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nx_dup2
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* nx_dup2() is similar to the standard 'dup2' interface except that is
|
|
|
|
* not a cancellation point and it does not modify the errno variable.
|
|
|
|
*
|
|
|
|
* nx_dup2() is an internal NuttX interface and should not be called from
|
|
|
|
* applications.
|
|
|
|
*
|
|
|
|
* Clone a file descriptor to a specific descriptor number.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* fd2 is returned on success; a negated errno value is return on
|
|
|
|
* any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nx_dup2(int fd1, int fd2)
|
|
|
|
{
|
|
|
|
return nx_dup2_from_tcb(nxsched_self(), fd1, fd2);
|
|
|
|
}
|
|
|
|
|
2008-08-01 03:59:11 +02:00
|
|
|
/****************************************************************************
|
2021-02-23 11:04:13 +01:00
|
|
|
* Name: dup2
|
2008-08-01 03:59:11 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2021-02-23 11:04:13 +01:00
|
|
|
* Clone a file descriptor or socket descriptor to a specific descriptor
|
|
|
|
* number
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int dup2(int fd1, int fd2)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = nx_dup2(fd1, fd2);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-10-22 10:53:58 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dup3
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Clone a file descriptor or socket descriptor to a specific descriptor
|
|
|
|
* number and specific flags.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int dup3(int fd1, int fd2, int flags)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = nx_dup3_from_tcb(nxsched_self(), fd1, fd2, flags);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
/****************************************************************************
|
2023-04-26 05:44:08 +02:00
|
|
|
* Name: nx_close_from_tcb
|
2021-02-23 11:04:13 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2023-04-26 05:44:08 +02:00
|
|
|
* nx_close_from_tcb() is similar to the standard 'close' interface
|
|
|
|
* except that is not a cancellation point and it does not modify the
|
|
|
|
* errno variable.
|
2021-02-23 11:04:13 +01:00
|
|
|
*
|
2023-04-26 05:44:08 +02:00
|
|
|
* nx_close_from_tcb() is an internal NuttX interface and should not
|
|
|
|
* be called from applications.
|
2021-02-23 11:04:13 +01:00
|
|
|
*
|
2008-08-01 03:59:11 +02:00
|
|
|
* Close an inode (if open)
|
|
|
|
*
|
2021-02-23 11:04:13 +01:00
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; A negated errno value is returned on
|
|
|
|
* on any failure.
|
|
|
|
*
|
2020-02-23 09:50:23 +01:00
|
|
|
* Assumptions:
|
2022-09-06 08:18:45 +02:00
|
|
|
* Caller holds the list mutex because the file descriptor will be
|
2020-03-30 01:36:19 +02:00
|
|
|
* freed.
|
2008-08-01 03:59:11 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
2012-02-01 00:39:12 +01:00
|
|
|
|
2023-04-26 05:44:08 +02:00
|
|
|
int nx_close_from_tcb(FAR struct tcb_s *tcb, int fd)
|
2008-08-01 03:59:11 +02:00
|
|
|
{
|
2021-06-24 16:06:29 +02:00
|
|
|
FAR struct file *filep;
|
2023-04-26 05:44:08 +02:00
|
|
|
FAR struct filelist *list;
|
2008-08-01 04:17:32 +02:00
|
|
|
|
2023-06-05 09:43:05 +02:00
|
|
|
#ifdef CONFIG_FDCHECK
|
|
|
|
fd = fdcheck_restore(fd);
|
|
|
|
#endif
|
|
|
|
|
2023-04-26 05:44:08 +02:00
|
|
|
list = nxsched_get_files_from_tcb(tcb);
|
2008-08-01 04:17:32 +02:00
|
|
|
|
2021-03-16 13:13:02 +01:00
|
|
|
/* Perform the protected close operation */
|
2008-08-01 04:17:32 +02:00
|
|
|
|
2023-11-12 14:04:07 +01:00
|
|
|
if (fd < 0 || fd >= files_countlist(list))
|
2016-05-26 17:41:50 +02:00
|
|
|
{
|
2023-11-08 10:23:44 +01:00
|
|
|
return -EBADF;
|
2016-05-26 17:41:50 +02:00
|
|
|
}
|
2008-08-01 04:17:32 +02:00
|
|
|
|
2023-11-08 10:23:44 +01:00
|
|
|
filep = files_fget(list, fd);
|
|
|
|
|
2021-03-16 13:13:02 +01:00
|
|
|
/* If the file was properly opened, there should be an inode assigned */
|
2008-08-01 04:17:32 +02:00
|
|
|
|
2023-11-08 10:23:44 +01:00
|
|
|
if (filep->f_inode == NULL)
|
2020-03-30 01:36:19 +02:00
|
|
|
{
|
2021-03-16 13:13:02 +01:00
|
|
|
return -EBADF;
|
2020-03-30 01:36:19 +02:00
|
|
|
}
|
|
|
|
|
2023-12-07 10:09:08 +01:00
|
|
|
return file_close(filep);
|
2008-08-01 03:59:11 +02:00
|
|
|
}
|
|
|
|
|
2023-04-26 05:44:08 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nx_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* nx_close() is similar to the standard 'close' interface except that is
|
|
|
|
* not a cancellation point and it does not modify the errno variable.
|
|
|
|
*
|
|
|
|
* nx_close() is an internal NuttX interface and should not be called from
|
|
|
|
* applications.
|
|
|
|
*
|
|
|
|
* Close an inode (if open)
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; A negated errno value is returned on
|
|
|
|
* on any failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Caller holds the list mutex because the file descriptor will be
|
|
|
|
* freed.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nx_close(int fd)
|
|
|
|
{
|
|
|
|
return nx_close_from_tcb(nxsched_self(), fd);
|
|
|
|
}
|
|
|
|
|
2008-08-01 02:22:09 +02:00
|
|
|
/****************************************************************************
|
2021-02-23 11:04:13 +01:00
|
|
|
* Name: close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* close() closes a file descriptor, so that it no longer refers to any
|
|
|
|
* file and may be reused. Any record locks (see fcntl(2)) held on the file
|
|
|
|
* it was associated with, and owned by the process, are removed
|
|
|
|
* (regardless of the file descriptor that was used to obtain the lock).
|
|
|
|
*
|
|
|
|
* If fd is the last copy of a particular file descriptor the resources
|
|
|
|
* associated with it are freed; if the descriptor was the last reference
|
|
|
|
* to a file which has been removed using unlink(2) the file is deleted.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* fd file descriptor to close
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; -1 on error with errno set appropriately.
|
2008-08-01 03:59:11 +02:00
|
|
|
*
|
2020-02-23 09:50:23 +01:00
|
|
|
* Assumptions:
|
2008-08-01 03:59:11 +02:00
|
|
|
*
|
2008-08-01 02:22:09 +02:00
|
|
|
****************************************************************************/
|
2012-02-01 00:39:12 +01:00
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
int close(int fd)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2020-03-30 01:36:19 +02:00
|
|
|
int ret;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2023-05-08 01:37:45 +02:00
|
|
|
#ifdef CONFIG_FDSAN
|
|
|
|
android_fdsan_exchange_owner_tag(fd, 0, 0);
|
|
|
|
#endif
|
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
/* close() is a cancellation point */
|
|
|
|
|
|
|
|
enter_cancellation_point();
|
2013-09-28 22:47:49 +02:00
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
ret = nx_close(fd);
|
|
|
|
if (ret < 0)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2021-02-23 11:04:13 +01:00
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2021-02-23 11:04:13 +01:00
|
|
|
|
|
|
|
leave_cancellation_point();
|
|
|
|
return ret;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2023-02-06 15:56:05 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sync
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* sync() causes all pending modifications to filesystem metadata and
|
|
|
|
* cached file data to be written to the underlying filesystems.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void sync(void)
|
|
|
|
{
|
|
|
|
nxsched_foreach(task_fssync, NULL);
|
|
|
|
}
|