diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-06-02 15:05:48 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-06-02 15:05:48 +0000 |
commit | 971830b005e76311a0950d26f6862997c892e76a (patch) | |
tree | 599f441faca90309ec3f48698ba5491b0a7017c7 /nuttx/fs | |
parent | 6f39c643ac7af0880f49f7673307053db11218e9 (diff) | |
download | px4-nuttx-971830b005e76311a0950d26f6862997c892e76a.tar.gz px4-nuttx-971830b005e76311a0950d26f6862997c892e76a.tar.bz2 px4-nuttx-971830b005e76311a0950d26f6862997c892e76a.zip |
Add support so that fdopen() may be used on socket descriptors
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3659 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/fs')
-rw-r--r-- | nuttx/fs/Makefile | 21 | ||||
-rw-r--r-- | nuttx/fs/fs_fdopen.c | 119 |
2 files changed, 117 insertions, 23 deletions
diff --git a/nuttx/fs/Makefile b/nuttx/fs/Makefile index 45ff2db79..f42ebcaaf 100644 --- a/nuttx/fs/Makefile +++ b/nuttx/fs/Makefile @@ -39,11 +39,28 @@ ASRCS = AOBJS = $(ASRCS:.S=$(OBJEXT)) CSRCS = + +# If there are no file descriptors configured, then a small part of the +# logic in this directory may still apply to socket descriptors + ifeq ($(CONFIG_NFILE_DESCRIPTORS),0) ifneq ($(CONFIG_NSOCKET_DESCRIPTORS),0) + +# Socket descriptor support + CSRCS += fs_close.c fs_read.c fs_write.c fs_ioctl.c fs_poll.c fs_select.c endif + +# Support for network access using streams + +ifneq ($(CONFIG_NFILE_STREAMS),0) +CSRCS += fs_fdopen.c +endif + else + +# Common file/socket descriptor support + 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_opendir.c fs_closedir.c \ @@ -56,10 +73,14 @@ CSRCS += fs_registerblockdriver.c fs_unregisterblockdriver.c \ include mmap/Make.defs +# Stream support + ifneq ($(CONFIG_NFILE_STREAMS),0) CSRCS += fs_fdopen.c endif +# Additional files required is mount-able file systems are supported + ifneq ($(CONFIG_DISABLE_MOUNTPOINT),y) CSRCS += fs_mount.c fs_umount.c fs_fsync.c fs_unlink.c fs_rename.c \ fs_mkdir.c fs_rmdir.c diff --git a/nuttx/fs/fs_fdopen.c b/nuttx/fs/fs_fdopen.c index 9445e97fa..329e4dd84 100644 --- a/nuttx/fs/fs_fdopen.c +++ b/nuttx/fs/fs_fdopen.c @@ -47,24 +47,79 @@ #include <nuttx/kmalloc.h> #include <nuttx/fs.h> +#include <nuttx/net.h> /**************************************************************************** * Private Functions ****************************************************************************/ /**************************************************************************** + * Name: fs_checkfd + * + * Description: + * Check if the file descriptor is valid for the provided TCB and if it + * supports the requested access. + * + ****************************************************************************/ + +#if CONFIG_NFILE_DESCRIPTORS > 0 +static inline int fs_checkfd(FAR _TCB *tcb, int fd, int oflags) +{ + FAR struct filelist *flist; + FAR struct inode *inode; + + /* Get the file list from the TCB */ + + flist = tcb->filelist; + + /* 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) + { + /* 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; +} +#endif + +/**************************************************************************** * Public Functions ****************************************************************************/ /**************************************************************************** * Name: fs_fdopen + * + * Description: + * This function does the core operations for fopen and 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; @@ -88,37 +143,55 @@ FAR struct file_struct *fs_fdopen(int fd, int oflags, FAR _TCB *tcb) tcb = sched_self(); } - /* Get the file and stream list from the TCB */ + /* Verify that this is a valid file/socket descriptor and that the + * requested access can be support. + * + * Is this fd in the range of valid file descriptors? Socket descriptors + * lie in a different range. + */ + +#if CONFIG_NFILE_DESCRIPTORS > 0 + if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS) +#endif + { + /* No.. If networking is enabled then this might be a socket + * descriptor. + */ - flist = tcb->filelist; - slist = tcb->streams; +#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 + ret = net_checksd; +#else + /* No networking... it is just a bad descriptor */ - /* 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 = EBADF; + return ERROR; +#endif + } + + /* The descriptor is in a valid range to file descriptor... do the read */ + +#if CONFIG_NFILE_DESCRIPTORS > 0 + else { - err = ENOENT; - goto errout; + ret = fs_checkfd(tcb, fd, oflags); } +#endif - /* 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. - */ + /* Do we have a good descriptor of some sort? */ - if (inode_checkflags(inode, oflags) != OK) + if (ret < 0) { - err = EACCES; + /* No... return the reported error */ + + err = -ret; goto errout; } - /* Find an unallocated FILE structure */ + /* Get the stream list from the TCB */ + + slist = tcb->streams; + + /* Find an unallocated FILE structure in the stream list */ ret = sem_wait(&slist->sl_sem); if (ret != OK) |