From 9f9566c0eb5515d89c2324c71cc2ca4ea619f8de Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Fri, 31 Jan 2020 16:44:28 +0800 Subject: [PATCH] Rename net_clone/net_dupsd[2] to psock_dup2/net_dup[2] like vfs --- drivers/net/telnet.c | 4 +- fs/vfs/fs_dup.c | 2 +- fs/vfs/fs_dup2.c | 2 +- include/nuttx/net/net.h | 32 +++---- net/socket/Make.defs | 4 +- net/socket/{net_dupsd.c => net_dup.c} | 14 ++-- net/socket/{net_clone.c => net_dup2.c} | 71 ++++++++++++++-- net/socket/net_dupsd2.c | 111 ------------------------- net/socket/net_vfcntl.c | 2 +- net/socket/socket.h | 19 ----- sched/group/group_setuptaskfiles.c | 2 +- 11 files changed, 93 insertions(+), 170 deletions(-) rename net/socket/{net_dupsd.c => net_dup.c} (95%) rename net/socket/{net_clone.c => net_dup2.c} (77%) delete mode 100644 net/socket/net_dupsd2.c diff --git a/drivers/net/telnet.c b/drivers/net/telnet.c index e1bf037ba8..9d142fe7e2 100644 --- a/drivers/net/telnet.c +++ b/drivers/net/telnet.c @@ -1086,10 +1086,10 @@ static int telnet_session(FAR struct telnet_session_s *session) goto errout_with_dev; } - ret = net_clone(psock, &priv->td_psock); + ret = psock_dup2(psock, &priv->td_psock); if (ret < 0) { - nerr("ERROR: net_clone failed: %d\n", ret); + nerr("ERROR: psock_dup2 failed: %d\n", ret); goto errout_with_dev; } diff --git a/fs/vfs/fs_dup.c b/fs/vfs/fs_dup.c index 80f847f699..a92621adfb 100644 --- a/fs/vfs/fs_dup.c +++ b/fs/vfs/fs_dup.c @@ -84,7 +84,7 @@ int dup(int fd) { /* 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 #endif diff --git a/fs/vfs/fs_dup2.c b/fs/vfs/fs_dup2.c index 64a8762842..d7e6a908f9 100644 --- a/fs/vfs/fs_dup2.c +++ b/fs/vfs/fs_dup2.c @@ -82,7 +82,7 @@ int dup2(int fd1, int fd2) { /* Yes.. dup the socket descriptor. The errno value is not set. */ - ret = net_dupsd2(fd1, fd2); + ret = net_dup2(fd1, fd2); } else { diff --git a/include/nuttx/net/net.h b/include/nuttx/net/net.h index 7fc4154a02..3ad7afb174 100644 --- a/include/nuttx/net/net.h +++ b/include/nuttx/net/net.h @@ -1308,7 +1308,7 @@ struct pollfd; /* Forward reference -- see poll.h */ int net_poll(int sockfd, struct pollfd *fds, bool setup); /**************************************************************************** - * Name: psock_dupsd + * Name: psock_dup * * Description: * 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: * 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: * 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 @@ -1378,16 +1388,6 @@ struct stat; /* Forward reference. See sys/stat.h */ 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 * diff --git a/net/socket/Make.defs b/net/socket/Make.defs index a99c5a7e7d..47fb4d29eb 100644 --- a/net/socket/Make.defs +++ b/net/socket/Make.defs @@ -37,8 +37,8 @@ SOCK_CSRCS += bind.c connect.c getsockname.c getpeername.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 += net_dupsd2.c net_sockif.c net_clone.c net_poll.c net_vfcntl.c +SOCK_CSRCS += socket.c net_sockets.c net_close.c net_dup.c +SOCK_CSRCS += net_dup2.c net_sockif.c net_poll.c net_vfcntl.c SOCK_CSRCS += net_fstat.c # TCP/IP support diff --git a/net/socket/net_dupsd.c b/net/socket/net_dup.c similarity index 95% rename from net/socket/net_dupsd.c rename to net/socket/net_dup.c index 74616b0d5a..09fcbcaae0 100644 --- a/net/socket/net_dupsd.c +++ b/net/socket/net_dup.c @@ -1,5 +1,5 @@ /**************************************************************************** - * net/socket/net_dupsd.c + * net/socket/net_dup.c * * Copyright (C) 2009, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt @@ -51,7 +51,7 @@ ****************************************************************************/ /**************************************************************************** - * Name: psock_dupsd + * Name: psock_dup * * Description: * 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; int sockfd2; @@ -117,7 +117,7 @@ int psock_dupsd(FAR struct socket *psock, int minsd) /* Duplicate the socket state */ - ret = net_clone(psock, psock2); + ret = psock_dup2(psock, psock2); if (ret < 0) { goto errout_with_sockfd; @@ -135,7 +135,7 @@ errout: } /**************************************************************************** - * Name: net_dupsd + * Name: net_dup * * Description: * 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); } diff --git a/net/socket/net_clone.c b/net/socket/net_dup2.c similarity index 77% rename from net/socket/net_clone.c rename to net/socket/net_dup2.c index 93900c933a..50a3f70c4d 100644 --- a/net/socket/net_clone.c +++ b/net/socket/net_dup2.c @@ -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. * Author: Gregory Nutt @@ -41,28 +41,25 @@ #include #include +#include #include #include -#include #include -#include #include "inet/inet.h" #include "tcp/tcp.h" #include "socket/socket.h" -#ifdef CONFIG_NET - /**************************************************************************** * Public Functions ****************************************************************************/ /**************************************************************************** - * Name: net_clone + * Name: psock_dup2 * * 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: * 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; @@ -155,4 +152,60 @@ int net_clone(FAR struct socket *psock1, FAR struct socket *psock2) 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; +} diff --git a/net/socket/net_dupsd2.c b/net/socket/net_dupsd2.c deleted file mode 100644 index d8b1d636eb..0000000000 --- a/net/socket/net_dupsd2.c +++ /dev/null @@ -1,111 +0,0 @@ -/**************************************************************************** - * net/socket/net_dupsd2.c - * - * Copyright (C) 2009, 2011, 2017 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * 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 - -#include -#include -#include -#include - -#include - -#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; -} diff --git a/net/socket/net_vfcntl.c b/net/socket/net_vfcntl.c index c1a7a3cfcb..51592d7dbf 100644 --- a/net/socket/net_vfcntl.c +++ b/net/socket/net_vfcntl.c @@ -105,7 +105,7 @@ int psock_vfcntl(FAR struct socket *psock, int cmd, va_list ap) { /* 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; diff --git a/net/socket/socket.h b/net/socket/socket.h index a2452f2e6e..107e23e92e 100644 --- a/net/socket/socket.h +++ b/net/socket/socket.h @@ -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, 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 /* _NET_SOCKET_SOCKET_H */ diff --git a/sched/group/group_setuptaskfiles.c b/sched/group/group_setuptaskfiles.c index dae52b5589..b632c91343 100644 --- a/sched/group/group_setuptaskfiles.c +++ b/sched/group/group_setuptaskfiles.c @@ -174,7 +174,7 @@ static inline void sched_dupsockets(FAR struct task_tcb_s *tcb) { /* Yes... duplicate it for the child */ - net_clone(&parent[i], &child[i]); + psock_dup2(&parent[i], &child[i]); } } }