diff --git a/fs/Makefile b/fs/Makefile index 3926cac2e55..0810be4948e 100644 --- a/fs/Makefile +++ b/fs/Makefile @@ -47,8 +47,8 @@ else 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_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_rewinddir.c fs_files.c fs_inode.c fs_inodefind.c fs_inodereserve.c \ + fs_closedir.c fs_stat.c fs_readdir.c fs_seekdir.c fs_rewinddir.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_inodeaddref.c fs_inoderelease.c CSRCS += fs_registerblockdriver.c fs_unregisterblockdriver.c \ diff --git a/fs/fs_fdopen.c b/fs/fs_fdopen.c new file mode 100644 index 00000000000..d0a44f9e8ed --- /dev/null +++ b/fs/fs_fdopen.c @@ -0,0 +1,184 @@ +/**************************************************************************** + * fs/fs_fdopen.c + * + * Copyright (C) 2007-2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include +#include +#include +#include +#include +#include + +#include + +/**************************************************************************** + * 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; +} + diff --git a/include/nuttx/fs.h b/include/nuttx/fs.h index 02c40121e5f..46234751094 100644 --- a/include/nuttx/fs.h +++ b/include/nuttx/fs.h @@ -346,13 +346,13 @@ EXTERN int files_addreflist(FAR struct filelist *list); EXTERN int files_releaselist(FAR struct filelist *list); 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 */ 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 * 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) #endif -/* fs_openblockdriver.c ******************************************************/ +/* fs_openblockdriver.c *****************************************************/ EXTERN int open_blockdriver(FAR const char *pathname, int mountflags, FAR struct inode **ppinode); -/* fs_closeblockdriver.c *****************************************************/ +/* fs_closeblockdriver.c ****************************************************/ EXTERN int close_blockdriver(FAR struct inode *inode); #endif -/* lib_fopen.c **************************************************************/ +/* fs_fdopen.c **************************************************************/ /* Used by the OS to clone stdin, stdout, stderr */ #if CONFIG_NFILE_STREAMS > 0 -EXTERN FAR struct file_struct *lib_fdopen(int fd, - const char *mode, - FAR struct filelist *flist, - FAR struct streamlist *slist); +typedef struct _TCB _TCB; +EXTERN FAR struct file_struct *fs_fdopen(int fd, int oflags, FAR _TCB *tcb); #endif /* lib_fflush.c *************************************************************/ diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h index 70c8a746a86..1b5d28f82b9 100644 --- a/include/nuttx/sched.h +++ b/include/nuttx/sched.h @@ -311,6 +311,10 @@ extern "C" { #define EXTERN extern #endif +/* TCB helpers */ + +EXTERN FAR _TCB *sched_self(void); + /* File system helpers */ #if CONFIG_NFILE_DESCRIPTORS > 0 diff --git a/lib/stdio/lib_fopen.c b/lib/stdio/lib_fopen.c index 42ac19d1b30..4f841aefcf6 100644 --- a/lib/stdio/lib_fopen.c +++ b/lib/stdio/lib_fopen.c @@ -71,12 +71,14 @@ static int lib_mode2oflags(FAR const char *mode) if (*(mode + 1) == '+') { /* Open for read/write access */ + oflags |= O_RDWR; mode++; } else { /* Open for read access */ + oflags |= O_RDOK; } break; @@ -87,12 +89,14 @@ static int lib_mode2oflags(FAR const char *mode) if (*(mode + 1) == '+') { /* Open for write read/access, truncating any existing file */ + oflags |= O_RDWR|O_CREAT|O_TRUNC; mode++; } else { /* Open for write access, truncating any existing file */ + oflags |= O_WROK|O_CREAT|O_TRUNC; } break; @@ -103,12 +107,14 @@ static int lib_mode2oflags(FAR const char *mode) if (*(mode + 1) == '+') { /* Read from the beginning of the file; write to the end */ + oflags |= O_RDWR|O_CREAT; mode++; } else { /* Write to the end of the file */ + oflags |= O_WROK|O_CREAT; } break; @@ -129,120 +135,13 @@ static int lib_mode2oflags(FAR const char *mode) * 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 ****************************************************************************/ FAR FILE *fdopen(int fd, FAR const char *mode) { - FAR struct filelist *flist = sched_getfiles(); - FAR struct streamlist *slist = sched_getstreams(); - return lib_fdopen(fd, mode, flist, slist); + return fs_fdopen(fd, lib_mode2oflags(mode), NULL); } /**************************************************************************** @@ -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 *ret = NULL;; + FAR FILE *ret = NULL; int oflags; int fd; @@ -267,10 +166,7 @@ FAR FILE *fopen(FAR const char *path, FAR const char *mode) if (fd >= 0) { - FAR struct filelist *flist = sched_getfiles(); - FAR struct streamlist *slist = sched_getstreams(); - - ret = lib_fdopen(fd, mode, flist, slist); + ret = fs_fdopen(fd, oflags, NULL); if (!ret) { /* Don't forget to close the file descriptor if any other diff --git a/sched/Makefile b/sched/Makefile index 86e7c0b76e4..f5d2ce9dbf1 100644 --- a/sched/Makefile +++ b/sched/Makefile @@ -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_setscheduler.c sched_getscheduler.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) SCHED_SRCS += sched_reprioritize.c endif diff --git a/sched/sched_gettcb.c b/sched/sched_gettcb.c index a44e310e682..39933369f6b 100644 --- a/sched/sched_gettcb.c +++ b/sched/sched_gettcb.c @@ -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 * * Redistribution and use in source and binary forms, with or without diff --git a/sched/sched_self.c b/sched/sched_self.c new file mode 100644 index 00000000000..99a0f1995f3 --- /dev/null +++ b/sched/sched_self.c @@ -0,0 +1,83 @@ +/**************************************************************************** + * sched/sched_self.c + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 +#include +#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; +} + + diff --git a/sched/sched_setupstreams.c b/sched/sched_setupstreams.c index 28b824f263f..a30a9259dc3 100644 --- a/sched/sched_setupstreams.c +++ b/sched/sched_setupstreams.c @@ -1,7 +1,7 @@ /**************************************************************************** * 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 * * Redistribution and use in source and binary forms, with or without @@ -40,6 +40,8 @@ #include #include +#include + #include #include #include @@ -65,14 +67,14 @@ int sched_setupstreams(FAR _TCB *tcb) * The following logic depends on the fact that the library * layer will allocate FILEs in order. * - * fd = 0 is stdin - * fd = 1 is stdout - * fd = 2 is stderr + * fd = 0 is stdin (read-only) + * fd = 1 is stdout (write-only, append) + * fd = 2 is stderr (write-only, append) */ - (void)lib_fdopen(0, "r", tcb->filelist, tcb->streams); - (void)lib_fdopen(1, "w", tcb->filelist, tcb->streams); - (void)lib_fdopen(2, "w", tcb->filelist, tcb->streams); + (void)fs_fdopen(0, O_RDONLY, tcb); + (void)fs_fdopen(1, O_WROK|O_CREAT, tcb); + (void)fs_fdopen(2, O_WROK|O_CREAT, tcb); } return OK;