2011-04-05 01:02:00 +02:00
|
|
|
/****************************************************************************
|
2014-09-28 19:46:11 +02:00
|
|
|
* fs/vfs/fs_fdopen.c
|
2011-04-05 01:02:00 +02:00
|
|
|
*
|
2020-05-23 06:58:52 +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
|
2011-04-05 01:02:00 +02:00
|
|
|
*
|
2020-05-23 06:58:52 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-05 01:02:00 +02:00
|
|
|
*
|
2020-05-23 06:58:52 +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.
|
2011-04-05 01:02:00 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2011-04-05 01:02:00 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
2011-04-05 22:54:00 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2020-02-01 08:17:32 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2020-08-13 12:17:29 +02:00
|
|
|
#include <nuttx/lib/lib.h>
|
2022-12-21 14:13:14 +01:00
|
|
|
#include <nuttx/tls.h>
|
2011-04-05 01:02:00 +02:00
|
|
|
|
2020-05-04 15:32:03 +02:00
|
|
|
#include "inode/inode.h"
|
|
|
|
|
2011-04-05 01:02:00 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-06-02 17:05:48 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: fs_checkfd
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if the file descriptor is valid for the provided TCB and if it
|
|
|
|
* supports the requested access.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-02-04 19:46:28 +01:00
|
|
|
static inline int fs_checkfd(FAR struct tcb_s *tcb, int fd, int oflags)
|
2011-06-02 17:05:48 +02:00
|
|
|
{
|
2014-10-06 18:53:25 +02:00
|
|
|
FAR struct file *filep;
|
|
|
|
FAR struct inode *inode;
|
2017-10-11 16:39:19 +02:00
|
|
|
int ret;
|
2011-06-02 17:05:48 +02:00
|
|
|
|
2013-01-26 21:17:29 +01:00
|
|
|
DEBUGASSERT(tcb && tcb->group);
|
2011-06-02 17:05:48 +02:00
|
|
|
|
2014-10-06 18:53:25 +02:00
|
|
|
/* Get the file structure corresponding to the file descriptor. */
|
2013-01-26 21:17:29 +01:00
|
|
|
|
2017-10-11 16:39:19 +02:00
|
|
|
ret = fs_getfilep(fd, &filep);
|
|
|
|
if (ret < 0)
|
2014-10-06 18:53:25 +02:00
|
|
|
{
|
2017-10-11 16:39:19 +02:00
|
|
|
return ret;
|
2014-10-06 18:53:25 +02:00
|
|
|
}
|
2011-06-02 17:05:48 +02:00
|
|
|
|
|
|
|
/* Get the inode associated with the file descriptor. This should
|
|
|
|
* normally be the case if fd >= 0. But not in the case where the
|
2014-10-06 18:53:25 +02:00
|
|
|
* called attempts to explicitly stdin with fdopen(0) but stdin has
|
2011-06-02 17:05:48 +02:00
|
|
|
* been closed.
|
|
|
|
*/
|
2014-04-14 00:22:22 +02:00
|
|
|
|
2014-10-06 18:53:25 +02:00
|
|
|
inode = filep->f_inode;
|
2011-06-02 17:05:48 +02:00
|
|
|
if (!inode)
|
|
|
|
{
|
|
|
|
/* No inode -- descriptor does not correspond to an open file */
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that the inode supports the requested access. In
|
|
|
|
* the case of fdopen, we are not actually creating the file -- in
|
|
|
|
* particular w and w+ do not truncate the file and any files have
|
|
|
|
* already been created.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (inode_checkflags(inode, oflags) != OK)
|
|
|
|
{
|
|
|
|
/* Cannot support the requested access */
|
|
|
|
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Looks good to me */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-04-05 01:02:00 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fs_fdopen
|
2011-06-02 17:05:48 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function does the core operations for fopen and fdopen.
|
|
|
|
*
|
2011-04-05 01:02:00 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-23 06:58:52 +02:00
|
|
|
int fs_fdopen(int fd, int oflags, FAR struct tcb_s *tcb,
|
|
|
|
FAR struct file_struct **filep)
|
2011-04-05 01:02:00 +02:00
|
|
|
{
|
|
|
|
FAR struct streamlist *slist;
|
|
|
|
FAR FILE *stream;
|
2020-12-15 08:46:40 +01:00
|
|
|
int ret = OK;
|
2011-04-05 01:02:00 +02:00
|
|
|
|
|
|
|
/* Check input parameters */
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
2020-05-23 06:58:52 +02:00
|
|
|
ret = -EBADF;
|
2011-04-05 01:02:00 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A NULL TCB pointer means to use this threads TCB. This is a little
|
|
|
|
* hack the let's this function be called from user-space (via a syscall)
|
|
|
|
* without having access to the TCB.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!tcb)
|
|
|
|
{
|
2020-05-09 16:04:45 +02:00
|
|
|
tcb = nxsched_self();
|
2011-04-05 01:02:00 +02:00
|
|
|
}
|
2013-03-12 22:53:18 +01:00
|
|
|
|
2013-01-26 23:25:21 +01:00
|
|
|
DEBUGASSERT(tcb && tcb->group);
|
2011-04-05 01:02:00 +02:00
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
if (fd >= 3)
|
2011-04-05 01:02:00 +02:00
|
|
|
{
|
2011-06-02 17:05:48 +02:00
|
|
|
ret = fs_checkfd(tcb, fd, oflags);
|
2011-04-05 01:02:00 +02:00
|
|
|
}
|
|
|
|
|
2011-06-02 17:05:48 +02:00
|
|
|
/* Do we have a good descriptor of some sort? */
|
2011-04-05 01:02:00 +02:00
|
|
|
|
2011-06-02 17:05:48 +02:00
|
|
|
if (ret < 0)
|
2011-04-05 01:02:00 +02:00
|
|
|
{
|
2011-06-02 17:05:48 +02:00
|
|
|
/* No... return the reported error */
|
|
|
|
|
2011-04-05 01:02:00 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2020-11-09 06:32:59 +01:00
|
|
|
/* Get the stream list from the TCB */
|
|
|
|
|
2022-12-21 14:13:14 +01:00
|
|
|
slist = &tcb->group->tg_info->ta_streamlist;
|
2020-11-09 06:32:59 +01:00
|
|
|
|
2020-08-13 12:17:29 +02:00
|
|
|
/* Allocate FILE structure */
|
2011-06-02 17:05:48 +02:00
|
|
|
|
2020-11-09 06:32:59 +01:00
|
|
|
if (fd >= 3)
|
2020-08-13 12:17:29 +02:00
|
|
|
{
|
2020-11-09 06:32:59 +01:00
|
|
|
stream = group_zalloc(tcb->group, sizeof(FILE));
|
|
|
|
if (stream == NULL)
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add FILE structure to the stream list */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&slist->sl_lock);
|
2020-11-09 06:32:59 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
group_free(tcb->group, stream);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slist->sl_tail)
|
|
|
|
{
|
|
|
|
slist->sl_tail->fs_next = stream;
|
|
|
|
slist->sl_tail = stream;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
slist->sl_head = stream;
|
|
|
|
slist->sl_tail = stream;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&slist->sl_lock);
|
2020-11-09 06:32:59 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Initialize the mutex the manages access to the buffer */
|
2020-11-09 06:32:59 +01:00
|
|
|
|
2022-04-25 18:13:41 +02:00
|
|
|
nxrmutex_init(&stream->fs_lock);
|
2020-08-13 12:17:29 +02:00
|
|
|
}
|
2020-11-09 06:32:59 +01:00
|
|
|
else
|
2011-04-05 01:02:00 +02:00
|
|
|
{
|
2020-11-09 06:32:59 +01:00
|
|
|
stream = &slist->sl_std[fd];
|
2011-04-05 01:02:00 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 06:32:59 +01:00
|
|
|
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
|
2017-02-09 16:24:44 +01:00
|
|
|
#if CONFIG_STDIO_BUFFER_SIZE > 0
|
2020-08-13 12:17:29 +02:00
|
|
|
/* Set up pointers */
|
2011-04-05 01:02:00 +02:00
|
|
|
|
2020-11-09 06:32:59 +01:00
|
|
|
stream->fs_bufstart = stream->fs_buffer;
|
2020-08-13 12:17:29 +02:00
|
|
|
stream->fs_bufend = &stream->fs_bufstart[CONFIG_STDIO_BUFFER_SIZE];
|
|
|
|
stream->fs_bufpos = stream->fs_bufstart;
|
|
|
|
stream->fs_bufread = stream->fs_bufstart;
|
|
|
|
stream->fs_flags = __FS_FLAG_UBF; /* Fake setvbuf and fclose */
|
2017-02-08 18:28:24 +01:00
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
#ifdef CONFIG_STDIO_LINEBUFFER
|
2020-08-13 12:17:29 +02:00
|
|
|
/* Setup buffer flags */
|
2017-02-08 18:28:24 +01:00
|
|
|
|
2020-08-13 12:17:29 +02:00
|
|
|
stream->fs_flags |= __FS_FLAG_LBF; /* Line buffering */
|
2017-02-09 16:24:44 +01:00
|
|
|
|
|
|
|
#endif /* CONFIG_STDIO_LINEBUFFER */
|
|
|
|
#endif /* CONFIG_STDIO_BUFFER_SIZE > 0 */
|
2020-08-13 12:17:29 +02:00
|
|
|
#endif /* CONFIG_STDIO_DISABLE_BUFFERING */
|
2017-02-09 16:24:44 +01:00
|
|
|
|
2020-08-13 12:17:29 +02:00
|
|
|
/* Save the file description and open flags. Setting the
|
|
|
|
* file descriptor locks this stream.
|
|
|
|
*/
|
2011-04-05 01:02:00 +02:00
|
|
|
|
2020-08-13 12:17:29 +02:00
|
|
|
stream->fs_fd = fd;
|
|
|
|
stream->fs_oflags = oflags;
|
2011-04-05 01:02:00 +02:00
|
|
|
|
2020-08-13 12:17:29 +02:00
|
|
|
if (filep != NULL)
|
|
|
|
{
|
|
|
|
*filep = stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
2011-04-05 01:02:00 +02:00
|
|
|
errout:
|
2020-05-23 06:58:52 +02:00
|
|
|
if (filep != NULL)
|
|
|
|
{
|
|
|
|
*filep = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2011-04-05 01:02:00 +02:00
|
|
|
}
|