123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- /****************************************************************************
- * fs/inode/fs_files.c
- *
- * Copyright (C) 2007-2009, 2011-2013, 2016 Gregory Nutt. All rights reserved.
- * Author: Gregory Nutt <gnutt@nuttx.org>
- *
- * 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>
- #include <sys/types.h>
- #include <string.h>
- #include <semaphore.h>
- #include <assert.h>
- #include <sched.h>
- #include <errno.h>
- #include <nuttx/fs/fs.h>
- #include <nuttx/kmalloc.h>
- #include "inode/inode.h"
- /****************************************************************************
- * Private Functions
- ****************************************************************************/
- /****************************************************************************
- * Name: _files_semtake
- ****************************************************************************/
- static void _files_semtake(FAR struct filelist *list)
- {
- /* Take the semaphore (perhaps waiting) */
- while (sem_wait(&list->fl_sem) != 0)
- {
- /* The only case that an error should occur here is if
- * the wait was awakened by a signal.
- */
- ASSERT(get_errno() == EINTR);
- }
- }
- /****************************************************************************
- * Name: _files_semgive
- ****************************************************************************/
- #define _files_semgive(list) sem_post(&list->fl_sem)
- /****************************************************************************
- * Name: _files_close
- *
- * Description:
- * Close an inode (if open)
- *
- * Assumuptions:
- * Caller holds the list semaphore because the file descriptor will be freed.
- *
- ****************************************************************************/
- static int _files_close(FAR struct file *filep)
- {
- struct inode *inode = filep->f_inode;
- int ret = OK;
- /* Check if the struct file is open (i.e., assigned an inode) */
- if (inode)
- {
- /* Close the file, driver, or mountpoint. */
- if (inode->u.i_ops && inode->u.i_ops->close)
- {
- /* Perform the close operation */
- ret = inode->u.i_ops->close(filep);
- }
- /* And release the inode */
- inode_release(inode);
- /* Release the file descriptor */
- filep->f_oflags = 0;
- filep->f_pos = 0;
- filep->f_inode = NULL;
- }
- return ret;
- }
- /****************************************************************************
- * Public Functions
- ****************************************************************************/
- /****************************************************************************
- * Name: files_initialize
- *
- * Description:
- * This is called from the FS initialization logic to configure the files.
- *
- ****************************************************************************/
- void files_initialize(void)
- {
- }
- /****************************************************************************
- * Name: files_initlist
- *
- * Description: Initializes the list of files for a new task
- *
- ****************************************************************************/
- void files_initlist(FAR struct filelist *list)
- {
- DEBUGASSERT(list);
- /* Initialize the list access mutex */
- (void)sem_init(&list->fl_sem, 0, 1);
- }
- /****************************************************************************
- * Name: files_releaselist
- *
- * Description:
- * Release a reference to the file list
- *
- ****************************************************************************/
- void files_releaselist(FAR struct filelist *list)
- {
- int i;
- DEBUGASSERT(list);
- /* Close each file descriptor .. Normally, you would need take the list
- * semaphore, but it is safe to ignore the semaphore in this context because
- * there should not be any references in this context.
- */
- for (i = 0; i < CONFIG_NFILE_DESCRIPTORS; i++)
- {
- (void)_files_close(&list->fl_files[i]);
- }
- /* Destroy the semaphore */
- (void)sem_destroy(&list->fl_sem);
- }
- /****************************************************************************
- * Name: file_dup2
- *
- * Description:
- * Assign an inode to a specific files structure. This is the heart of
- * dup2.
- *
- ****************************************************************************/
- int file_dup2(FAR struct file *filep1, FAR struct file *filep2)
- {
- FAR struct filelist *list;
- FAR struct inode *inode;
- int errcode;
- int ret;
- if (!filep1 || !filep1->f_inode || !filep2)
- {
- errcode = EBADF;
- goto errout;
- }
- list = sched_getfiles();
- /* 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)
- {
- _files_semtake(list);
- }
- /* If there is already an inode contained in the new file structure,
- * close the file and release the inode.
- */
- ret = _files_close(filep2);
- if (ret < 0)
- {
- /* An error occurred while closing the driver */
- goto errout_with_ret;
- }
- /* Increment the reference count on the contained inode */
- inode = filep1->f_inode;
- inode_addref(inode);
- /* Then clone the file structure */
- filep2->f_oflags = filep1->f_oflags;
- filep2->f_pos = filep1->f_pos;
- filep2->f_inode = inode;
- /* Call the open method on the file, driver, mountpoint so that it
- * can maintain the correct open counts.
- */
- if (inode->u.i_ops && inode->u.i_ops->open)
- {
- #ifndef CONFIG_DISABLE_MOUNTPOINT
- if (INODE_IS_MOUNTPT(inode))
- {
- /* Dup the open file on the in the new file structure */
- ret = inode->u.i_mops->dup(filep1, filep2);
- }
- else
- #endif
- {
- /* (Re-)open the pseudo file or device driver */
- ret = inode->u.i_ops->open(filep2);
- }
- /* Handle open failures */
- if (ret < 0)
- {
- goto errout_with_inode;
- }
- }
- if (list != NULL)
- {
- _files_semgive(list);
- }
- return OK;
- /* Handler various error conditions */
- errout_with_inode:
- inode_release(filep2->f_inode);
- filep2->f_oflags = 0;
- filep2->f_pos = 0;
- filep2->f_inode = NULL;
- errout_with_ret:
- errcode = -ret;
- if (list != NULL)
- {
- _files_semgive(list);
- }
- errout:
- set_errno(errcode);
- return ERROR;
- }
- /****************************************************************************
- * Name: files_allocate
- *
- * Description:
- * Allocate a struct files instance and associate it with an inode instance.
- * Returns the file descriptor == index into the files array.
- *
- ****************************************************************************/
- int files_allocate(FAR struct inode *inode, int oflags, off_t pos, int minfd)
- {
- FAR struct filelist *list;
- int i;
- /* Get the file descriptor list. It should not be NULL in this context. */
- list = sched_getfiles();
- DEBUGASSERT(list != NULL);
- _files_semtake(list);
- for (i = minfd; i < CONFIG_NFILE_DESCRIPTORS; i++)
- {
- if (!list->fl_files[i].f_inode)
- {
- list->fl_files[i].f_oflags = oflags;
- list->fl_files[i].f_pos = pos;
- list->fl_files[i].f_inode = inode;
- list->fl_files[i].f_priv = NULL;
- _files_semgive(list);
- return i;
- }
- }
- _files_semgive(list);
- return ERROR;
- }
- /****************************************************************************
- * Name: files_close
- *
- * Description:
- * Close an inode (if open)
- *
- * Assumuptions:
- * Caller holds the list semaphore because the file descriptor will be freed.
- *
- ****************************************************************************/
- int files_close(int fd)
- {
- FAR struct filelist *list;
- int ret;
- /* Get the thread-specific file list. It should never be NULL in this
- * context.
- */
- list = sched_getfiles();
- DEBUGASSERT(list != NULL);
- /* If the file was properly opened, there should be an inode assigned */
- if (fd < 0 || fd >= CONFIG_NFILE_DESCRIPTORS || !list->fl_files[fd].f_inode)
- {
- return -EBADF;
- }
- /* Perform the protected close operation */
- _files_semtake(list);
- ret = _files_close(&list->fl_files[fd]);
- _files_semgive(list);
- return ret;
- }
- /****************************************************************************
- * Name: files_release
- *
- * Assumuptions:
- * Similar to files_close(). Called only from open() logic on error
- * conditions.
- *
- ****************************************************************************/
- void files_release(int fd)
- {
- FAR struct filelist *list;
- list = sched_getfiles();
- DEBUGASSERT(list);
- if (fd >= 0 && fd < CONFIG_NFILE_DESCRIPTORS)
- {
- _files_semtake(list);
- list->fl_files[fd].f_oflags = 0;
- list->fl_files[fd].f_pos = 0;
- list->fl_files[fd].f_inode = NULL;
- _files_semgive(list);
- }
- }
|