Rename net_clone/net_dupsd[2] to psock_dup2/net_dup[2] like vfs

This commit is contained in:
Xiang Xiao
2020-01-31 16:44:28 +08:00
committed by Gregory Nutt
parent 2def8035db
commit 9f9566c0eb
11 changed files with 93 additions and 170 deletions

View File

@@ -1086,10 +1086,10 @@ static int telnet_session(FAR struct telnet_session_s *session)
goto errout_with_dev; goto errout_with_dev;
} }
ret = net_clone(psock, &priv->td_psock); ret = psock_dup2(psock, &priv->td_psock);
if (ret < 0) if (ret < 0)
{ {
nerr("ERROR: net_clone failed: %d\n", ret); nerr("ERROR: psock_dup2 failed: %d\n", ret);
goto errout_with_dev; goto errout_with_dev;
} }

View File

@@ -84,7 +84,7 @@ int dup(int fd)
{ {
/* Yes.. dup the socket descriptor. The errno value is not set. */ /* Yes.. dup the socket descriptor. The errno value is not set. */
ret = net_dupsd(fd, CONFIG_NFILE_DESCRIPTORS); ret = net_dup(fd, CONFIG_NFILE_DESCRIPTORS);
} }
else else
#endif #endif

View File

@@ -82,7 +82,7 @@ int dup2(int fd1, int fd2)
{ {
/* Yes.. dup the socket descriptor. The errno value is not set. */ /* Yes.. dup the socket descriptor. The errno value is not set. */
ret = net_dupsd2(fd1, fd2); ret = net_dup2(fd1, fd2);
} }
else else
{ {

View File

@@ -1308,7 +1308,7 @@ struct pollfd; /* Forward reference -- see poll.h */
int net_poll(int sockfd, struct pollfd *fds, bool setup); int net_poll(int sockfd, struct pollfd *fds, bool setup);
/**************************************************************************** /****************************************************************************
* Name: psock_dupsd * Name: psock_dup
* *
* Description: * Description:
* Clone a socket descriptor to an arbitray descriptor number. If file * Clone a socket descriptor to an arbitray descriptor number. If file
@@ -1322,10 +1322,10 @@ int net_poll(int sockfd, struct pollfd *fds, bool setup);
* *
****************************************************************************/ ****************************************************************************/
int psock_dupsd(FAR struct socket *psock, int minsd); int psock_dup(FAR struct socket *psock, int minsd);
/**************************************************************************** /****************************************************************************
* Name: net_dupsd * Name: net_dup
* *
* Description: * Description:
* Clone a socket descriptor to an arbitray descriptor number. If file * Clone a socket descriptor to an arbitray descriptor number. If file
@@ -1339,10 +1339,20 @@ int psock_dupsd(FAR struct socket *psock, int minsd);
* *
****************************************************************************/ ****************************************************************************/
int net_dupsd(int sockfd, int minsd); int net_dup(int sockfd, int minsd);
/**************************************************************************** /****************************************************************************
* Name: net_dupsd2 * Name: psock_dup2
*
* Description:
* Performs the low level, common portion of net_dup() and net_dup2()
*
****************************************************************************/
int psock_dup2(FAR struct socket *psock1, FAR struct socket *psock2);
/****************************************************************************
* Name: net_dup2
* *
* Description: * Description:
* Clone a socket descriptor to an arbitray descriptor number. If file * Clone a socket descriptor to an arbitray descriptor number. If file
@@ -1356,7 +1366,7 @@ int net_dupsd(int sockfd, int minsd);
* *
****************************************************************************/ ****************************************************************************/
int net_dupsd2(int sockfd1, int sockfd2); int net_dup2(int sockfd1, int sockfd2);
/**************************************************************************** /****************************************************************************
* Name: net_fstat * Name: net_fstat
@@ -1378,16 +1388,6 @@ struct stat; /* Forward reference. See sys/stat.h */
int net_fstat(int sockfd, FAR struct stat *buf); int net_fstat(int sockfd, FAR struct stat *buf);
/****************************************************************************
* Name: net_clone
*
* Description:
* Performs the low level, common portion of net_dupsd() and net_dupsd2()
*
****************************************************************************/
int net_clone(FAR struct socket *psock1, FAR struct socket *psock2);
/**************************************************************************** /****************************************************************************
* Name: net_sendfile * Name: net_sendfile
* *

View File

@@ -37,8 +37,8 @@
SOCK_CSRCS += bind.c connect.c getsockname.c getpeername.c SOCK_CSRCS += bind.c connect.c getsockname.c getpeername.c
SOCK_CSRCS += recv.c recvfrom.c send.c sendto.c SOCK_CSRCS += recv.c recvfrom.c send.c sendto.c
SOCK_CSRCS += socket.c net_sockets.c net_close.c net_dupsd.c SOCK_CSRCS += socket.c net_sockets.c net_close.c net_dup.c
SOCK_CSRCS += net_dupsd2.c net_sockif.c net_clone.c net_poll.c net_vfcntl.c SOCK_CSRCS += net_dup2.c net_sockif.c net_poll.c net_vfcntl.c
SOCK_CSRCS += net_fstat.c SOCK_CSRCS += net_fstat.c
# TCP/IP support # TCP/IP support

View File

@@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* net/socket/net_dupsd.c * net/socket/net_dup.c
* *
* Copyright (C) 2009, 2017 Gregory Nutt. All rights reserved. * Copyright (C) 2009, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
@@ -51,7 +51,7 @@
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
* Name: psock_dupsd * Name: psock_dup
* *
* Description: * Description:
* Clone a socket descriptor to an arbitrary descriptor number. If file * Clone a socket descriptor to an arbitrary descriptor number. If file
@@ -65,7 +65,7 @@
* *
****************************************************************************/ ****************************************************************************/
int psock_dupsd(FAR struct socket *psock, int minsd) int psock_dup(FAR struct socket *psock, int minsd)
{ {
FAR struct socket *psock2; FAR struct socket *psock2;
int sockfd2; int sockfd2;
@@ -117,7 +117,7 @@ int psock_dupsd(FAR struct socket *psock, int minsd)
/* Duplicate the socket state */ /* Duplicate the socket state */
ret = net_clone(psock, psock2); ret = psock_dup2(psock, psock2);
if (ret < 0) if (ret < 0)
{ {
goto errout_with_sockfd; goto errout_with_sockfd;
@@ -135,7 +135,7 @@ errout:
} }
/**************************************************************************** /****************************************************************************
* Name: net_dupsd * Name: net_dup
* *
* Description: * Description:
* Clone a socket descriptor to an arbitrary descriptor number. If file * Clone a socket descriptor to an arbitrary descriptor number. If file
@@ -149,7 +149,7 @@ errout:
* *
****************************************************************************/ ****************************************************************************/
int net_dupsd(int sockfd, int minsd) int net_dup(int sockfd, int minsd)
{ {
return psock_dupsd(sockfd_socket(sockfd), minsd); return psock_dup(sockfd_socket(sockfd), minsd);
} }

View File

@@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* net/socket/net_clone.c * net/socket/net_dup2.c
* *
* Copyright (C) 2009, 2011-2015, 2017 Gregory Nutt. All rights reserved. * Copyright (C) 2009, 2011-2015, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
@@ -41,28 +41,25 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <string.h> #include <string.h>
#include <sched.h>
#include <errno.h> #include <errno.h>
#include <debug.h> #include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/net/net.h> #include <nuttx/net/net.h>
#include <nuttx/net/udp.h>
#include "inet/inet.h" #include "inet/inet.h"
#include "tcp/tcp.h" #include "tcp/tcp.h"
#include "socket/socket.h" #include "socket/socket.h"
#ifdef CONFIG_NET
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
* Name: net_clone * Name: psock_dup2
* *
* Description: * Description:
* Performs the low level, common portion of net_dupsd() and net_dupsd2() * Performs the low level, common portion of net_dup() and net_dup2()
* *
* Input Parameters: * Input Parameters:
* psock1 - The existing socket that is being cloned. * psock1 - The existing socket that is being cloned.
@@ -75,7 +72,7 @@
* *
****************************************************************************/ ****************************************************************************/
int net_clone(FAR struct socket *psock1, FAR struct socket *psock2) int psock_dup2(FAR struct socket *psock1, FAR struct socket *psock2)
{ {
int ret = OK; int ret = OK;
@@ -155,4 +152,60 @@ int net_clone(FAR struct socket *psock1, FAR struct socket *psock2)
return ret; return ret;
} }
#endif /* CONFIG_NET */ /****************************************************************************
* Name: net_dup2
*
* Description:
* Clone a socket descriptor to an arbitray descriptor number. If file
* descriptors are implemented, then this is called by dup2() for the case
* of socket file descriptors. If file descriptors are not implemented,
* then this function IS dup2().
*
* Returned Value:
* On success, returns the number of characters sent. On any error,
* a negated errno value is returned:.
*
****************************************************************************/
int net_dup2(int sockfd1, int sockfd2)
{
FAR struct socket *psock1;
FAR struct socket *psock2;
int ret;
/* Lock the scheduler throughout the following */
sched_lock();
/* Get the socket structures underly both descriptors */
psock1 = sockfd_socket(sockfd1);
psock2 = sockfd_socket(sockfd2);
/* Verify that the sockfd1 and sockfd2 both refer to valid socket
* descriptors and that sockfd2 corresponds to an allocated socket
*/
if (psock1 == NULL || psock2 == NULL || psock1->s_crefs <= 0)
{
ret = -EBADF;
goto errout;
}
/* If sockfd2 also valid, allocated socket, then we will have to
* close it!
*/
if (psock2->s_crefs > 0)
{
net_close(sockfd2);
}
/* Duplicate the socket state */
ret = psock_dup2(psock1, psock2);
errout:
sched_unlock();
return ret;
}

View File

@@ -1,111 +0,0 @@
/****************************************************************************
* net/socket/net_dupsd2.c
*
* Copyright (C) 2009, 2011, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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/socket.h>
#include <sched.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/net/net.h>
#include "socket/socket.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: net_dupsd2
*
* Description:
* Clone a socket descriptor to an arbitray descriptor number. If file
* descriptors are implemented, then this is called by dup2() for the case
* of socket file descriptors. If file descriptors are not implemented,
* then this function IS dup2().
*
* Returned Value:
* On success, returns the number of characters sent. On any error,
* a negated errno value is returned:.
*
****************************************************************************/
int net_dupsd2(int sockfd1, int sockfd2)
{
FAR struct socket *psock1;
FAR struct socket *psock2;
int ret;
/* Lock the scheduler throughout the following */
sched_lock();
/* Get the socket structures underly both descriptors */
psock1 = sockfd_socket(sockfd1);
psock2 = sockfd_socket(sockfd2);
/* Verify that the sockfd1 and sockfd2 both refer to valid socket
* descriptors and that sockfd2 corresponds to an allocated socket
*/
if (psock1 == NULL || psock2 == NULL || psock1->s_crefs <= 0)
{
ret = -EBADF;
goto errout;
}
/* If sockfd2 also valid, allocated socket, then we will have to
* close it!
*/
if (psock2->s_crefs > 0)
{
net_close(sockfd2);
}
/* Duplicate the socket state */
ret = net_clone(psock1, psock2);
errout:
sched_unlock();
return ret;
}

View File

@@ -105,7 +105,7 @@ int psock_vfcntl(FAR struct socket *psock, int cmd, va_list ap)
{ {
/* Does not set the errno value on failure */ /* Does not set the errno value on failure */
ret = psock_dupsd(psock, va_arg(ap, int)); ret = psock_dup(psock, va_arg(ap, int));
} }
break; break;

View File

@@ -336,24 +336,5 @@ int net_timeo(clock_t start_time, socktimeo_t timeo);
ssize_t psock_send(FAR struct socket *psock, FAR const void *buf, size_t len, ssize_t psock_send(FAR struct socket *psock, FAR const void *buf, size_t len,
int flags); int flags);
/****************************************************************************
* Name: net_clone
*
* Description:
* Performs the low level, common portion of net_dupsd() and net_dupsd2()
*
* Input Parameters:
* psock1 - The existing socket that is being cloned.
* psock2 - A reference to an uninitialized socket structure alloated by
* the caller.
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned on
* any failure.
*
****************************************************************************/
int net_clone(FAR struct socket *psock1, FAR struct socket *psock2);
#endif /* CONFIG_NET */ #endif /* CONFIG_NET */
#endif /* _NET_SOCKET_SOCKET_H */ #endif /* _NET_SOCKET_SOCKET_H */

View File

@@ -174,7 +174,7 @@ static inline void sched_dupsockets(FAR struct task_tcb_s *tcb)
{ {
/* Yes... duplicate it for the child */ /* Yes... duplicate it for the child */
net_clone(&parent[i], &child[i]); psock_dup2(&parent[i], &child[i]);
} }
} }
} }