• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  * fs/vfs/fs_dup.c
3  *
4  *   Copyright (C) 2007-2009, 2017 Gregory Nutt. All rights reserved.
5  *   Author: Gregory Nutt <gnutt@nuttx.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name NuttX nor the names of its contributors may be
18  *    used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  ****************************************************************************/
35 
36 /****************************************************************************
37  * Included Files
38  ****************************************************************************/
39 
40 #include "vfs_config.h"
41 
42 #include "errno.h"
43 #include "unistd.h"
44 #include "sched.h"
45 
46 #include "vnode.h"
47 #if defined(LOSCFG_NET_LWIP_SACK)
48 #include "lwip/sockets.h"
49 #endif
50 
51 /****************************************************************************
52  * Public Functions
53  ****************************************************************************/
54 
55 /****************************************************************************
56  * Name: dup
57  *
58  * Description:
59  *   Clone a file or socket descriptor to an arbitray descriptor number
60  *
61  ****************************************************************************/
62 
dup(int fd)63 int dup(int fd)
64 {
65   int ret = OK;
66 
67   /* Check the range of the descriptor to see if we got a file or a socket
68    * descriptor.
69    */
70 
71 #if CONFIG_NFILE_DESCRIPTORS > 0
72   if ((unsigned int)fd < CONFIG_NFILE_DESCRIPTORS)
73     {
74       /* 0,1,2 fd is not opened in system, no need to dup them, return fd directly */
75 
76       if ((fd >= STDIN_FILENO) && (fd <= STDERR_FILENO))
77         {
78           return fd;
79         }
80 
81       /* Its a valid file descriptor.. dup the file descriptor using any
82        * other file descriptor.  fd_dupfd() sets the errno value in the
83        * event of any failures.
84        */
85 
86       ret = fs_dupfd(fd, 3); /* 3: file start fd */
87     }
88   else
89 #endif
90     {
91       /* Not a valid file descriptor.  Did we get a valid socket descriptor? */
92 #if defined(LOSCFG_NET_LWIP_SACK)
93       if ((unsigned int)fd < (unsigned int)(CONFIG_NFILE_DESCRIPTORS+CONFIG_NSOCKET_DESCRIPTORS))
94         {
95           /* Yes.. dup the socket descriptor.  The errno value is not set. */
96 
97           set_errno(EBADF); /* When net_dupsd is still closed,errno should set */
98           ret = VFS_ERROR;
99 
100         }
101       else
102 #endif
103         {
104           /* No.. then it is a bad descriptor number */
105 
106           set_errno(EBADF);
107           ret = VFS_ERROR;
108         }
109     }
110 
111   return ret;
112 }
113