diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2009-06-15 18:58:22 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2009-06-15 18:58:22 +0000 |
commit | f2675bf333e0378c842717a94a13c73dabb76a52 (patch) | |
tree | 6a460f3749d40743ca8d1d1dfb95481342860110 /nuttx/fs | |
parent | 6ee68a773a24586f6e6cfaccdf7dce064d213e88 (diff) | |
download | px4-nuttx-f2675bf333e0378c842717a94a13c73dabb76a52.tar.gz px4-nuttx-f2675bf333e0378c842717a94a13c73dabb76a52.tar.bz2 px4-nuttx-f2675bf333e0378c842717a94a13c73dabb76a52.zip |
dup() and dup2() support for socket descriptors
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1884 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/fs')
-rw-r--r-- | nuttx/fs/Makefile | 11 | ||||
-rw-r--r-- | nuttx/fs/fs_close.c | 2 | ||||
-rw-r--r-- | nuttx/fs/fs_dup.c | 109 | ||||
-rw-r--r-- | nuttx/fs/fs_dup2.c | 108 | ||||
-rw-r--r-- | nuttx/fs/fs_filedup.c | 124 | ||||
-rw-r--r-- | nuttx/fs/fs_filedup2.c | 121 |
6 files changed, 398 insertions, 77 deletions
diff --git a/nuttx/fs/Makefile b/nuttx/fs/Makefile index 1fce0e423..30073db9e 100644 --- a/nuttx/fs/Makefile +++ b/nuttx/fs/Makefile @@ -45,11 +45,12 @@ CSRCS += fs_close.c fs_write.c fs_ioctl.c fs_poll.c fs_select.c endif 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_mmap.c fs_opendir.c fs_closedir.c \ - fs_stat.c fs_readdir.c fs_readdirr.c fs_seekdir.c fs_telldir.c \ - fs_rewinddir.c fs_files.c fs_inode.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 + fs_select.c fs_lseek.c fs_dup.c fs_filedup.c fs_dup2.c fs_filedup2.c \ + fs_mmap.c fs_opendir.c fs_closedir.c fs_stat.c fs_readdir.c fs_readdirr.c \ + fs_seekdir.c fs_telldir.c fs_rewinddir.c fs_files.c fs_inode.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 \ fs_findblockdriver.c fs_openblockdriver.c fs_closeblockdriver.c ifneq ($(CONFIG_DISABLE_MOUNTPOINT),y) diff --git a/nuttx/fs/fs_close.c b/nuttx/fs/fs_close.c index 4970c459d..a60bc592d 100644 --- a/nuttx/fs/fs_close.c +++ b/nuttx/fs/fs_close.c @@ -1,7 +1,7 @@ /**************************************************************************** * fs/fs_close.c * - * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Redistribution and use in source and binary forms, with or without diff --git a/nuttx/fs/fs_dup.c b/nuttx/fs/fs_dup.c index 8f8b332b1..20b7fbf27 100644 --- a/nuttx/fs/fs_dup.c +++ b/nuttx/fs/fs_dup.c @@ -1,7 +1,7 @@ /**************************************************************************** * fs/fs_dup.c * - * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Redistribution and use in source and binary forms, with or without @@ -43,16 +43,21 @@ #include <unistd.h> #include <sched.h> #include <errno.h> + +#include <nuttx/fs.h> #include "fs_internal.h" +/* This logic in this applies only when both socket and file descriptors are + * in that case, this function descriminates which type of dup is being + * performed. + */ + +#if CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 + /**************************************************************************** * Definitions ****************************************************************************/ -#define DUP_ISOPEN(fd, list) \ - ((unsigned int)fd < CONFIG_NFILE_DESCRIPTORS && \ - list->fl_files[fd].f_inode != NULL) - /**************************************************************************** * Private Functions ****************************************************************************/ @@ -61,82 +66,44 @@ * Global Functions ****************************************************************************/ +/**************************************************************************** + * Name: dup + * + * Description: + * Clone a file or socket descriptor to an arbitray descriptor number + * + ****************************************************************************/ + int dup(int fildes) { - FAR struct filelist *list; - int fildes2; + /* Check the range of the descriptor to see if we got a file or a socket + * descriptor. */ - /* Get the thread-specific file list */ - - list = sched_getfiles(); - if (!list) + if ((unsigned int)fildes >= CONFIG_NFILE_DESCRIPTORS) { - *get_errno_ptr() = EMFILE; - return ERROR; - } - - /* Verify that fildes is a valid, open file descriptor */ + /* Not a vailid file descriptor. Did we get a valid socket descriptor? */ - if (!DUP_ISOPEN(fildes, list)) - { - *get_errno_ptr() = EBADF; - return ERROR; - } + if ((unsigned int)fildes < (CONFIG_NFILE_DESCRIPTORS+CONFIG_NSOCKET_DESCRIPTORS)) + { + /* Yes.. dup the socket descriptor */ - /* Increment the reference count on the contained inode */ + return net_dup(fildes); + } + else + { + /* No.. then it is a bad descriptor number */ - inode_addref(list->fl_files[fildes].f_inode); - - /* Then allocate a new file descriptor for the inode */ - - fildes2 = files_allocate(list->fl_files[fildes].f_inode, - list->fl_files[fildes].f_oflags, - list->fl_files[fildes].f_pos); - if (fildes2 < 0) - { - *get_errno_ptr() = EMFILE; - inode_release(list->fl_files[fildes].f_inode); - return ERROR; + errno = EBADF; + return ERROR; + } } - return fildes2; -} - -int dup2(int fildes1, int fildes2) -{ - FAR struct filelist *list; - - /* Get the thread-specific file list */ - - list = sched_getfiles(); - if (!list) + else { - *get_errno_ptr() = EMFILE; - return ERROR; - } + /* Its a valid file descriptor.. dup the file descriptor */ - /* Verify that fildes is a valid, open file descriptor */ - - if (!DUP_ISOPEN(fildes1, list)) - { - *get_errno_ptr() = EBADF; - return ERROR; + return file_dup(fildes); } - - /* Handle a special case */ - - if (fildes1 == fildes2) - { - return fildes1; - } - - /* Verify fildes2 */ - - if ((unsigned int)fildes2 >= CONFIG_NFILE_DESCRIPTORS) - { - *get_errno_ptr() = EBADF; - return ERROR; - } - - return files_dup(&list->fl_files[fildes1], &list->fl_files[fildes2]); } +#endif /* CONFIG_NFILE_DESCRIPTORS > 0 ... */ + diff --git a/nuttx/fs/fs_dup2.c b/nuttx/fs/fs_dup2.c new file mode 100644 index 000000000..546c024dc --- /dev/null +++ b/nuttx/fs/fs_dup2.c @@ -0,0 +1,108 @@ +/**************************************************************************** + * fs/fs_dup2.c + * + * Copyright (C) 2007-2009 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 <sys/types.h> +#include <unistd.h> +#include <sched.h> +#include <errno.h> +#include "fs_internal.h" + +/* This logic in this applies only when both socket and file descriptors are + * in that case, this function descriminates which type of dup2 is being + * performed. + */ + +#if CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Global Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: dup2 + * + * Description: + * Clone a file descriptor or socket descriptor to a specific descriptor + * number + * + ****************************************************************************/ + +int dup2(int fildes1, int fildes2) +{ + /* Check the range of the descriptor to see if we got a file or a socket + * descriptor. */ + + if ((unsigned int)fildes1 >= CONFIG_NFILE_DESCRIPTORS) + { + /* Not a vailid file descriptor. Did we get a valid socket descriptor? */ + + if ((unsigned int)fildes1 < (CONFIG_NFILE_DESCRIPTORS+CONFIG_NSOCKET_DESCRIPTORS)) + { + /* Yes.. dup the socket descriptor */ + + return net_dup2(fildes1, fildes2); + } + else + { + /* No.. then it is a bad descriptor number */ + + errno = EBADF; + return ERROR; + } + } + else + { + /* Its a valid file descriptor.. dup the file descriptor */ + + return file_dup2(fildes1, fildes2); + } +} + +#endif /* CONFIG_NFILE_DESCRIPTORS > 0 ... */ + diff --git a/nuttx/fs/fs_filedup.c b/nuttx/fs/fs_filedup.c new file mode 100644 index 000000000..71f625877 --- /dev/null +++ b/nuttx/fs/fs_filedup.c @@ -0,0 +1,124 @@ +/**************************************************************************** + * fs/fs_filedup.c + * + * Copyright (C) 2007-2009 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 <sys/types.h> +#include <unistd.h> +#include <sched.h> +#include <errno.h> + +#include <nuttx/fs.h> +#include "fs_internal.h" + +#if CONFIG_NFILE_DESCRIPTORS > 0 + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +#define DUP_ISOPEN(fd, list) \ + ((unsigned int)fd < CONFIG_NFILE_DESCRIPTORS && \ + list->fl_files[fd].f_inode != NULL) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Global Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: file_dup OR dup + * + * Description: + * Clone a file descriptor to an arbitray descriptor number. If socket + * descriptors are implemented, then this is called by dup() for the case + * of file descriptors. If socket descriptors are not implemented, then + * this function IS dup(). + * + ****************************************************************************/ + +#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 +int file_dup(int fildes) +#else +int dup(int fildes) +#endif +{ + FAR struct filelist *list; + int fildes2; + + /* Get the thread-specific file list */ + + list = sched_getfiles(); + if (!list) + { + errno = EMFILE; + return ERROR; + } + + /* Verify that fildes is a valid, open file descriptor */ + + if (!DUP_ISOPEN(fildes, list)) + { + errno = EBADF; + return ERROR; + } + + /* Increment the reference count on the contained inode */ + + inode_addref(list->fl_files[fildes].f_inode); + + /* Then allocate a new file descriptor for the inode */ + + fildes2 = files_allocate(list->fl_files[fildes].f_inode, + list->fl_files[fildes].f_oflags, + list->fl_files[fildes].f_pos); + if (fildes2 < 0) + { + errno = EMFILE; + inode_release(list->fl_files[fildes].f_inode); + return ERROR; + } + return fildes2; +} + +#endif /* CONFIG_NFILE_DESCRIPTORS > 0 */ + diff --git a/nuttx/fs/fs_filedup2.c b/nuttx/fs/fs_filedup2.c new file mode 100644 index 000000000..e1c2462d8 --- /dev/null +++ b/nuttx/fs/fs_filedup2.c @@ -0,0 +1,121 @@ +/**************************************************************************** + * fs/fs_filedup2.c + * + * Copyright (C) 2007-2009 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 <sys/types.h> +#include <unistd.h> +#include <sched.h> +#include <errno.h> +#include "fs_internal.h" + +#if CONFIG_NFILE_DESCRIPTORS > 0 + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +#define DUP_ISOPEN(fd, list) \ + ((unsigned int)fd < CONFIG_NFILE_DESCRIPTORS && \ + list->fl_files[fd].f_inode != NULL) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Global Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: file_dup2 + * + * Description: + * Clone a file descriptor or socket descriptor to a specific descriptor + * number. If socket descriptors are implemented, then this is called by + * dup2() for the case of file descriptors. If socket descriptors are not + * implemented, then this function IS dup2(). + * + ****************************************************************************/ + +#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 +int file_dup2(int fildes1, int fildes2) +#else +int dup2(int fildes1, int fildes2) +#endif +{ + FAR struct filelist *list; + + /* Get the thread-specific file list */ + + list = sched_getfiles(); + if (!list) + { + errno = EMFILE; + return ERROR; + } + + /* Verify that fildes is a valid, open file descriptor */ + + if (!DUP_ISOPEN(fildes1, list)) + { + errno = EBADF; + return ERROR; + } + + /* Handle a special case */ + + if (fildes1 == fildes2) + { + return fildes1; + } + + /* Verify fildes2 */ + + if ((unsigned int)fildes2 >= CONFIG_NFILE_DESCRIPTORS) + { + errno = EBADF; + return ERROR; + } + + return files_dup(&list->fl_files[fildes1], &list->fl_files[fildes2]); +} + +#endif /* CONFIG_NFILE_DESCRIPTORS > 0 */ + |