You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2021/06/11 10:00:27 UTC

[incubator-nuttx] branch master updated (762ae45 -> b6bf9cf)

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git.


    from 762ae45  Author: Michal Lyszczek: update licenses to Apache
     new 0d8e5b6  net/sock: move the psock calloc out of sockfd_allocate
     new b6bf9cf  net/accept: alloc the accept fd after accept success

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 fs/socket/socket.c      | 46 ++++++++++++++++++++++++----------------------
 include/nuttx/net/net.h |  4 ++--
 net/socket/accept.c     | 33 ++++++++++++++++++++++-----------
 3 files changed, 48 insertions(+), 35 deletions(-)

[incubator-nuttx] 02/02: net/accept: alloc the accept fd after accept success

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit b6bf9cf44b417fd8b10c88159b603ee5230118a6
Author: chao.an <an...@xiaomi.com>
AuthorDate: Fri Jun 11 12:24:37 2021 +0800

    net/accept: alloc the accept fd after accept success
    
    Signed-off-by: chao.an <an...@xiaomi.com>
---
 net/socket/accept.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/net/socket/accept.c b/net/socket/accept.c
index 08107d9..2431285 100644
--- a/net/socket/accept.c
+++ b/net/socket/accept.c
@@ -267,6 +267,13 @@ int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
       goto errout;
     }
 
+  ret = psock_accept(psock, addr, addrlen, newsock);
+  if (ret < 0)
+    {
+      errcode = -ret;
+      goto errout_with_alloc;
+    }
+
   /* Allocate a socket descriptor for the new connection now (so that it
    * cannot fail later)
    */
@@ -275,21 +282,14 @@ int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
   if (newfd < 0)
     {
       errcode = ENFILE;
-      goto errout_with_alloc;
-    }
-
-  ret = psock_accept(psock, addr, addrlen, newsock);
-  if (ret < 0)
-    {
-      errcode = -ret;
-      goto errout_with_socket;
+      goto errout_with_psock;
     }
 
   leave_cancellation_point();
   return newfd;
 
-errout_with_socket:
-  nx_close(newfd);
+errout_with_psock:
+  psock_close(newsock);
 
 errout_with_alloc:
   kmm_free(newsock);

[incubator-nuttx] 01/02: net/sock: move the psock calloc out of sockfd_allocate

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 0d8e5b66fde3022a96479904734f01b1a32df7dd
Author: chao.an <an...@xiaomi.com>
AuthorDate: Fri Jun 11 12:16:24 2021 +0800

    net/sock: move the psock calloc out of sockfd_allocate
    
    Signed-off-by: chao.an <an...@xiaomi.com>
---
 fs/socket/socket.c      | 46 ++++++++++++++++++++++++----------------------
 include/nuttx/net/net.h |  4 ++--
 net/socket/accept.c     | 17 ++++++++++++++---
 3 files changed, 40 insertions(+), 27 deletions(-)

diff --git a/fs/socket/socket.c b/fs/socket/socket.c
index ad923ca..69718cb 100644
--- a/fs/socket/socket.c
+++ b/fs/socket/socket.c
@@ -150,7 +150,7 @@ static int sock_file_poll(FAR struct file *filep, FAR struct pollfd *fds,
  *   Allocate a socket descriptor
  *
  * Input Parameters:
- *   psock    A double pointer to socket structure to be allocated.
+ *   psock    A pointer to socket structure.
  *   oflags   Open mode flags.
  *
  * Returned Value:
@@ -159,24 +159,16 @@ static int sock_file_poll(FAR struct file *filep, FAR struct pollfd *fds,
  *
  ****************************************************************************/
 
-int sockfd_allocate(FAR struct socket **psock, int oflags)
+int sockfd_allocate(FAR struct socket *psock, int oflags)
 {
   int sockfd;
 
-  *psock = kmm_zalloc(sizeof(**psock));
-  if (*psock == NULL)
+  sockfd = files_allocate(&g_sock_inode, oflags, 0, psock, 0);
+  if (sockfd >= 0)
     {
-      return -ENOMEM;
-    }
-
-  sockfd = files_allocate(&g_sock_inode, oflags, 0, *psock, 0);
-  if (sockfd < 0)
-    {
-      kmm_free(*psock);
+      inode_addref(&g_sock_inode);
     }
 
-  inode_addref(&g_sock_inode);
-
   return sockfd;
 }
 
@@ -261,13 +253,10 @@ int socket(int domain, int type, int protocol)
       oflags |= O_CLOEXEC;
     }
 
-  /* Allocate a socket descriptor */
-
-  sockfd = sockfd_allocate(&psock, oflags);
-  if (sockfd < 0)
+  psock = kmm_zalloc(sizeof(*psock));
+  if (psock == NULL)
     {
-      nerr("ERROR: Failed to allocate a socket descriptor\n");
-      ret = sockfd;
+      ret = -ENOMEM;
       goto errout;
     }
 
@@ -277,13 +266,26 @@ int socket(int domain, int type, int protocol)
   if (ret < 0)
     {
       nerr("ERROR: psock_socket() failed: %d\n", ret);
-      goto errout_with_sockfd;
+      goto errout_with_alloc;
+    }
+
+  /* Allocate a socket descriptor */
+
+  sockfd = sockfd_allocate(psock, oflags);
+  if (sockfd < 0)
+    {
+      nerr("ERROR: Failed to allocate a socket descriptor\n");
+      ret = sockfd;
+      goto errout_with_psock;
     }
 
   return sockfd;
 
-errout_with_sockfd:
-  nx_close(sockfd);
+errout_with_psock:
+  psock_close(psock);
+
+errout_with_alloc:
+  kmm_free(psock);
 
 errout:
   set_errno(-ret);
diff --git a/include/nuttx/net/net.h b/include/nuttx/net/net.h
index 21c50df..cf7ba01 100644
--- a/include/nuttx/net/net.h
+++ b/include/nuttx/net/net.h
@@ -496,7 +496,7 @@ FAR struct iob_s *net_ioballoc(bool throttled, enum iob_user_e consumerid);
  *   Allocate a socket descriptor
  *
  * Input Parameters:
- *   psock    A double pointer to socket structure to be allocated.
+ *   psock    A pointer to socket structure.
  *   oflags   Open mode flags.
  *
  * Returned Value:
@@ -505,7 +505,7 @@ FAR struct iob_s *net_ioballoc(bool throttled, enum iob_user_e consumerid);
  *
  ****************************************************************************/
 
-int sockfd_allocate(FAR struct socket **psock, int oflags);
+int sockfd_allocate(FAR struct socket *psock, int oflags);
 
 /****************************************************************************
  * Name: sockfd_socket
diff --git a/net/socket/accept.c b/net/socket/accept.c
index 772a273..08107d9 100644
--- a/net/socket/accept.c
+++ b/net/socket/accept.c
@@ -35,6 +35,7 @@
 
 #include <nuttx/cancelpt.h>
 #include <nuttx/fs/fs.h>
+#include <nuttx/kmalloc.h>
 #include <arch/irq.h>
 
 #include "socket/socket.h"
@@ -230,8 +231,8 @@ int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
   FAR struct socket *psock = sockfd_socket(sockfd);
   FAR struct socket *newsock;
   FAR struct file *filep;
-  int newfd;
   int errcode;
+  int newfd;
   int ret;
 
   /* accept() is a cancellation point */
@@ -259,15 +260,22 @@ int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
       goto errout;
     }
 
+  newsock = kmm_zalloc(sizeof(*newsock));
+  if (newsock == NULL)
+    {
+      errcode = ENOMEM;
+      goto errout;
+    }
+
   /* Allocate a socket descriptor for the new connection now (so that it
    * cannot fail later)
    */
 
-  newfd = sockfd_allocate(&newsock, O_RDWR);
+  newfd = sockfd_allocate(newsock, O_RDWR);
   if (newfd < 0)
     {
       errcode = ENFILE;
-      goto errout;
+      goto errout_with_alloc;
     }
 
   ret = psock_accept(psock, addr, addrlen, newsock);
@@ -283,6 +291,9 @@ int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
 errout_with_socket:
   nx_close(newfd);
 
+errout_with_alloc:
+  kmm_free(newsock);
+
 errout:
   leave_cancellation_point();