Move core of fdopen() logic from lib/stdio to fs/

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3464 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2011-04-04 23:02:00 +00:00
parent cf3d515a6b
commit 985929a8c3
9 changed files with 301 additions and 134 deletions

View File

@ -47,8 +47,8 @@ else
CSRCS += fs_open.c fs_close.c fs_read.c fs_write.c fs_ioctl.c \ CSRCS += fs_open.c fs_close.c fs_read.c fs_write.c fs_ioctl.c \
fs_poll.c fs_select.c fs_lseek.c fs_dup.c fs_filedup.c \ fs_poll.c fs_select.c fs_lseek.c fs_dup.c fs_filedup.c \
fs_dup2.c fs_fcntl.c fs_filedup2.c fs_mmap.c fs_opendir.c \ fs_dup2.c fs_fcntl.c fs_filedup2.c fs_mmap.c fs_opendir.c \
fs_closedir.c fs_stat.c fs_readdir.c fs_seekdir.c \ fs_closedir.c fs_stat.c fs_readdir.c fs_seekdir.c fs_rewinddir.c \
fs_rewinddir.c fs_files.c fs_inode.c fs_inodefind.c fs_inodereserve.c \ fs_files.c fs_inode.c fs_fdopen.c fs_inodefind.c fs_inodereserve.c \
fs_statfs.c fs_inoderemove.c fs_registerdriver.c fs_unregisterdriver.c \ fs_statfs.c fs_inoderemove.c fs_registerdriver.c fs_unregisterdriver.c \
fs_inodeaddref.c fs_inoderelease.c fs_inodeaddref.c fs_inoderelease.c
CSRCS += fs_registerblockdriver.c fs_unregisterblockdriver.c \ CSRCS += fs_registerblockdriver.c fs_unregisterblockdriver.c \

184
fs/fs_fdopen.c Normal file
View File

@ -0,0 +1,184 @@
/****************************************************************************
* fs/fs_fdopen.c
*
* Copyright (C) 2007-2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <semaphore.h>
#include <fcntl.h>
#include <errno.h>
#include <nuttx/fs.h>
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: fs_fdopen
****************************************************************************/
FAR struct file_struct *fs_fdopen(int fd, int oflags, FAR _TCB *tcb)
{
FAR struct filelist *flist;
FAR struct streamlist *slist;
FAR struct inode *inode;
FAR FILE *stream;
int err = OK;
int ret;
int i;
/* Check input parameters */
if (fd < 0)
{
err = EBADF;
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)
{
tcb = sched_self();
}
/* Get the file and stream list from the TCB */
flist = tcb->filelist;
slist = tcb->streams;
/* Get the inode associated with the file descriptor. This should
* normally be the case if fd >= 0. But not in the case where the
* called attempts to explictly stdin with fdopen(0) but stdin has
* been closed.
*/
inode = flist->fl_files[fd].f_inode;
if (!inode)
{
err = ENOENT;
goto errout;
}
/* 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)
{
err = EACCES;
goto errout;
}
/* Find an unallocated FILE structure */
ret = sem_wait(&slist->sl_sem);
if (ret != OK)
{
goto errout_with_errno;
}
for (i = 0 ; i < CONFIG_NFILE_STREAMS; i++)
{
stream = &slist->sl_streams[i];
if (stream->fs_filedes < 0)
{
/* Zero the structure */
#if CONFIG_STDIO_BUFFER_SIZE > 0
memset(stream, 0, sizeof(FILE));
#elif CONFIG_NUNGET_CHARS > 0
stream->fs_nungotten = 0;
#endif
#if CONFIG_STDIO_BUFFER_SIZE > 0
/* Initialize the semaphore the manages access to the buffer */
(void)sem_init(&stream->fs_sem, 0, 1);
/* Allocate the IO buffer */
stream->fs_bufstart = malloc(CONFIG_STDIO_BUFFER_SIZE);
if (!stream)
{
err = ENOMEM;
goto errout_with_sem;
}
/* Set up pointers */
stream->fs_bufend = &stream->fs_bufstart[CONFIG_STDIO_BUFFER_SIZE];
stream->fs_bufpos = stream->fs_bufstart;
stream->fs_bufpos = stream->fs_bufstart;
stream->fs_bufread = stream->fs_bufstart;
#endif
/* Save the file description and open flags. Setting the
* file descriptor locks this stream.
*/
stream->fs_filedes = fd;
stream->fs_oflags = oflags;
sem_post(&slist->sl_sem);
return stream;
}
}
#if CONFIG_STDIO_BUFFER_SIZE > 0
errout_with_sem:
#endif
sem_post(&slist->sl_sem);
errout:
set_errno(err);
errout_with_errno:
return NULL;
}

View File

@ -346,13 +346,13 @@ EXTERN int files_addreflist(FAR struct filelist *list);
EXTERN int files_releaselist(FAR struct filelist *list); EXTERN int files_releaselist(FAR struct filelist *list);
EXTERN int files_dup(FAR struct file *filep1, FAR struct file *filep2); EXTERN int files_dup(FAR struct file *filep1, FAR struct file *filep2);
/* fs_filedup.c **************************************************************/ /* fs_filedup.c *************************************************************/
/* Dupicate a file descriptor using any value greater than or equal to minfd */ /* Dupicate a file descriptor using any value greater than or equal to minfd */
EXTERN int file_dup(int fd, int minfd); EXTERN int file_dup(int fd, int minfd);
/* fs_filedup2.c *************************************************************/ /* fs_filedup2.c ************************************************************/
/* This alternative naming is used when dup could operate on both file and /* This alternative naming is used when dup could operate on both file and
* socket descritors to avoid drawing unused socket support into the link. * socket descritors to avoid drawing unused socket support into the link.
@ -364,25 +364,23 @@ EXTERN int file_dup2(int fd1, int fd2);
# define file_dup2(fd1, fd2) dup2(fd1, fd2) # define file_dup2(fd1, fd2) dup2(fd1, fd2)
#endif #endif
/* fs_openblockdriver.c ******************************************************/ /* fs_openblockdriver.c *****************************************************/
EXTERN int open_blockdriver(FAR const char *pathname, int mountflags, EXTERN int open_blockdriver(FAR const char *pathname, int mountflags,
FAR struct inode **ppinode); FAR struct inode **ppinode);
/* fs_closeblockdriver.c *****************************************************/ /* fs_closeblockdriver.c ****************************************************/
EXTERN int close_blockdriver(FAR struct inode *inode); EXTERN int close_blockdriver(FAR struct inode *inode);
#endif #endif
/* lib_fopen.c **************************************************************/ /* fs_fdopen.c **************************************************************/
/* Used by the OS to clone stdin, stdout, stderr */ /* Used by the OS to clone stdin, stdout, stderr */
#if CONFIG_NFILE_STREAMS > 0 #if CONFIG_NFILE_STREAMS > 0
EXTERN FAR struct file_struct *lib_fdopen(int fd, typedef struct _TCB _TCB;
const char *mode, EXTERN FAR struct file_struct *fs_fdopen(int fd, int oflags, FAR _TCB *tcb);
FAR struct filelist *flist,
FAR struct streamlist *slist);
#endif #endif
/* lib_fflush.c *************************************************************/ /* lib_fflush.c *************************************************************/

View File

@ -311,6 +311,10 @@ extern "C" {
#define EXTERN extern #define EXTERN extern
#endif #endif
/* TCB helpers */
EXTERN FAR _TCB *sched_self(void);
/* File system helpers */ /* File system helpers */
#if CONFIG_NFILE_DESCRIPTORS > 0 #if CONFIG_NFILE_DESCRIPTORS > 0

View File

@ -71,12 +71,14 @@ static int lib_mode2oflags(FAR const char *mode)
if (*(mode + 1) == '+') if (*(mode + 1) == '+')
{ {
/* Open for read/write access */ /* Open for read/write access */
oflags |= O_RDWR; oflags |= O_RDWR;
mode++; mode++;
} }
else else
{ {
/* Open for read access */ /* Open for read access */
oflags |= O_RDOK; oflags |= O_RDOK;
} }
break; break;
@ -87,12 +89,14 @@ static int lib_mode2oflags(FAR const char *mode)
if (*(mode + 1) == '+') if (*(mode + 1) == '+')
{ {
/* Open for write read/access, truncating any existing file */ /* Open for write read/access, truncating any existing file */
oflags |= O_RDWR|O_CREAT|O_TRUNC; oflags |= O_RDWR|O_CREAT|O_TRUNC;
mode++; mode++;
} }
else else
{ {
/* Open for write access, truncating any existing file */ /* Open for write access, truncating any existing file */
oflags |= O_WROK|O_CREAT|O_TRUNC; oflags |= O_WROK|O_CREAT|O_TRUNC;
} }
break; break;
@ -103,12 +107,14 @@ static int lib_mode2oflags(FAR const char *mode)
if (*(mode + 1) == '+') if (*(mode + 1) == '+')
{ {
/* Read from the beginning of the file; write to the end */ /* Read from the beginning of the file; write to the end */
oflags |= O_RDWR|O_CREAT; oflags |= O_RDWR|O_CREAT;
mode++; mode++;
} }
else else
{ {
/* Write to the end of the file */ /* Write to the end of the file */
oflags |= O_WROK|O_CREAT; oflags |= O_WROK|O_CREAT;
} }
break; break;
@ -129,120 +135,13 @@ static int lib_mode2oflags(FAR const char *mode)
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
/****************************************************************************
* Name: lib_fdopen
****************************************************************************/
FAR struct file_struct *lib_fdopen(int fd, FAR const char *mode,
FAR struct filelist *flist,
FAR struct streamlist *slist)
{
FAR struct inode *inode;
FAR FILE *stream;
int oflags = lib_mode2oflags(mode);
int err = OK;
int i;
/* Check input parameters */
if (fd < 0 || !flist || !slist)
{
err = EBADF;
goto errout;
}
/* Get the inode associated with the file descriptor. This should
* normally be the case if fd >= 0. But not in the case where the
* called attempts to explictly stdin with fdopen(0) but stdin has
* been closed.
*/
inode = flist->fl_files[fd].f_inode;
if (!inode)
{
err = ENOENT;
goto errout;
}
/* 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)
{
err = EACCES;
goto errout;
}
/* Find an unallocated FILE structure */
stream_semtake(slist);
for (i = 0 ; i < CONFIG_NFILE_STREAMS; i++)
{
stream = &slist->sl_streams[i];
if (stream->fs_filedes < 0)
{
/* Zero the structure */
#if CONFIG_STDIO_BUFFER_SIZE > 0
memset(stream, 0, sizeof(FILE));
#elif CONFIG_NUNGET_CHARS > 0
stream->fs_nungotten = 0;
#endif
#if CONFIG_STDIO_BUFFER_SIZE > 0
/* Initialize the semaphore the manages access to the buffer */
(void)sem_init(&stream->fs_sem, 0, 1);
/* Allocate the IO buffer */
stream->fs_bufstart = malloc(CONFIG_STDIO_BUFFER_SIZE);
if (!stream)
{
err = ENOMEM;
goto errout_with_sem;
}
/* Set up pointers */
stream->fs_bufend = &stream->fs_bufstart[CONFIG_STDIO_BUFFER_SIZE];
stream->fs_bufpos = stream->fs_bufstart;
stream->fs_bufpos = stream->fs_bufstart;
stream->fs_bufread = stream->fs_bufstart;
#endif
/* Save the file description and open flags. Setting the
* file descriptor locks this stream.
*/
stream->fs_filedes = fd;
stream->fs_oflags = oflags;
stream_semgive(slist);
return stream;
}
}
#if CONFIG_STDIO_BUFFER_SIZE > 0
errout_with_sem:
#endif
stream_semgive(slist);
errout:
set_errno(err);
return NULL;
}
/**************************************************************************** /****************************************************************************
* Name: fdopen * Name: fdopen
****************************************************************************/ ****************************************************************************/
FAR FILE *fdopen(int fd, FAR const char *mode) FAR FILE *fdopen(int fd, FAR const char *mode)
{ {
FAR struct filelist *flist = sched_getfiles(); return fs_fdopen(fd, lib_mode2oflags(mode), NULL);
FAR struct streamlist *slist = sched_getstreams();
return lib_fdopen(fd, mode, flist, slist);
} }
/**************************************************************************** /****************************************************************************
@ -251,7 +150,7 @@ FAR FILE *fdopen(int fd, FAR const char *mode)
FAR FILE *fopen(FAR const char *path, FAR const char *mode) FAR FILE *fopen(FAR const char *path, FAR const char *mode)
{ {
FAR FILE *ret = NULL;; FAR FILE *ret = NULL;
int oflags; int oflags;
int fd; int fd;
@ -267,10 +166,7 @@ FAR FILE *fopen(FAR const char *path, FAR const char *mode)
if (fd >= 0) if (fd >= 0)
{ {
FAR struct filelist *flist = sched_getfiles(); ret = fs_fdopen(fd, oflags, NULL);
FAR struct streamlist *slist = sched_getstreams();
ret = lib_fdopen(fd, mode, flist, slist);
if (!ret) if (!ret)
{ {
/* Don't forget to close the file descriptor if any other /* Don't forget to close the file descriptor if any other

View File

@ -53,7 +53,7 @@ TSK_SRCS = task_create.c task_init.c task_setup.c task_activate.c \
SCHED_SRCS = sched_setparam.c sched_setpriority.c sched_getparam.c \ SCHED_SRCS = sched_setparam.c sched_setpriority.c sched_getparam.c \
sched_setscheduler.c sched_getscheduler.c \ sched_setscheduler.c sched_getscheduler.c \
sched_yield.c sched_rrgetinterval.c sched_foreach.c \ sched_yield.c sched_rrgetinterval.c sched_foreach.c \
sched_lock.c sched_unlock.c sched_lockcount.c sched_lock.c sched_unlock.c sched_lockcount.c sched_self.c
ifeq ($(CONFIG_PRIORITY_INHERITANCE),y) ifeq ($(CONFIG_PRIORITY_INHERITANCE),y)
SCHED_SRCS += sched_reprioritize.c SCHED_SRCS += sched_reprioritize.c
endif endif

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* sched/ched_gettcb.c * sched/sched_gettcb.c
* *
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. * Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without

83
sched/sched_self.c Normal file
View File

@ -0,0 +1,83 @@
/****************************************************************************
* sched/sched_self.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <sys/types.h>
#include <sched.h>
#include "os_internal.h"
/****************************************************************************
* Definitions
****************************************************************************/
/****************************************************************************
* Private Type Declarations
****************************************************************************/
/****************************************************************************
* Global Variables
****************************************************************************/
/****************************************************************************
* Private Variables
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: sched_self
*
* Description:
* Return the current threads TCB. Basically, this function just wraps the
* head of the ready-to-run list and manages access to the TCB from outside
* of the sched/ sub-directory.
*
****************************************************************************/
FAR _TCB *sched_self(void)
{
return (FAR _TCB*)g_readytorun.head;
}

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* sched_setupstreams.c * sched_setupstreams.c
* *
* Copyright (C) 2007-2008, 2010 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2008, 2010-2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -40,6 +40,8 @@
#include <nuttx/config.h> #include <nuttx/config.h>
#include <sched.h> #include <sched.h>
#include <fcntl.h>
#include <nuttx/fs.h> #include <nuttx/fs.h>
#include <nuttx/net.h> #include <nuttx/net.h>
#include <nuttx/lib.h> #include <nuttx/lib.h>
@ -65,14 +67,14 @@ int sched_setupstreams(FAR _TCB *tcb)
* The following logic depends on the fact that the library * The following logic depends on the fact that the library
* layer will allocate FILEs in order. * layer will allocate FILEs in order.
* *
* fd = 0 is stdin * fd = 0 is stdin (read-only)
* fd = 1 is stdout * fd = 1 is stdout (write-only, append)
* fd = 2 is stderr * fd = 2 is stderr (write-only, append)
*/ */
(void)lib_fdopen(0, "r", tcb->filelist, tcb->streams); (void)fs_fdopen(0, O_RDONLY, tcb);
(void)lib_fdopen(1, "w", tcb->filelist, tcb->streams); (void)fs_fdopen(1, O_WROK|O_CREAT, tcb);
(void)lib_fdopen(2, "w", tcb->filelist, tcb->streams); (void)fs_fdopen(2, O_WROK|O_CREAT, tcb);
} }
return OK; return OK;