mirror of
https://github.com/apache/nuttx.git
synced 2026-06-07 01:05:54 +08:00
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:
+16
-10
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user