net/accept: alloc the accept fd after accept success

Change-Id: Ib174b510e5be359197db575a9b7b5aceea97665d
Signed-off-by: chao.an <anchao@xiaomi.com>
This commit is contained in:
chao.an
2021-06-09 21:28:25 +08:00
committed by anchao
parent 2152a35662
commit 28363bf9a8
+16 -10
View File
@@ -32,6 +32,7 @@
#include <assert.h>
#include <debug.h>
#include <fcntl.h>
#include <string.h>
#include <nuttx/cancelpt.h>
#include <nuttx/fs/fs.h>
@@ -136,6 +137,8 @@ int psock_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
DEBUGASSERT(psock->s_sockif != NULL && psock->s_sockif->si_accept != NULL);
memset(newsock, 0, sizeof(*newsock));
net_lock();
ret = psock->s_sockif->si_accept(psock, addr, addrlen, newsock);
if (ret < 0)
@@ -228,7 +231,8 @@ errout_with_lock:
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 socket *pnewsock;
FAR struct socket newsock;
FAR struct file *filep;
int newfd;
int errcode;
@@ -259,29 +263,31 @@ 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;
}
/* Allocate a socket descriptor for the new connection now (so that it
* cannot fail later)
*/
newfd = sockfd_allocate(&newsock, O_RDWR);
newfd = sockfd_allocate(&pnewsock, O_RDWR);
if (newfd < 0)
{
errcode = ENFILE;
goto errout;
}
ret = psock_accept(psock, addr, addrlen, newsock);
if (ret < 0)
{
errcode = -ret;
goto errout_with_socket;
}
memcpy(pnewsock, &newsock, sizeof(newsock));
leave_cancellation_point();
return newfd;
errout_with_socket:
nx_close(newfd);
psock_close(&newsock);
errout:
leave_cancellation_point();