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