localsocket:add net_lock to protect connection

Signed-off-by: wangchen <wangchen41@xiaomi.com>
This commit is contained in:
wangchen 2024-06-05 15:29:04 +08:00 committed by Alan Carvalho de Assis
parent eb0055fd4a
commit 2195270ed5
5 changed files with 25 additions and 6 deletions

View File

@ -64,11 +64,15 @@ int psock_local_bind(FAR struct socket *psock,
/* Check if local address is already in use */ /* Check if local address is already in use */
net_lock();
if (local_findconn(conn, unaddr) != NULL) if (local_findconn(conn, unaddr) != NULL)
{ {
net_unlock();
return -EADDRINUSE; return -EADDRINUSE;
} }
net_unlock();
/* Save the address family */ /* Save the address family */
conn->lc_instance_id = -1; conn->lc_instance_id = -1;

View File

@ -135,6 +135,9 @@ FAR struct local_conn_s *local_peerconn(FAR struct local_conn_s *conn)
* This is normally something done by the implementation of the socket() * This is normally something done by the implementation of the socket()
* API * API
* *
* Assumptions:
* This function must be called with the network locked.
*
****************************************************************************/ ****************************************************************************/
FAR struct local_conn_s *local_alloc(void) FAR struct local_conn_s *local_alloc(void)
@ -170,9 +173,7 @@ FAR struct local_conn_s *local_alloc(void)
/* Add the connection structure to the list of listeners */ /* Add the connection structure to the list of listeners */
net_lock();
dq_addlast(&conn->lc_conn.node, &g_local_connections); dq_addlast(&conn->lc_conn.node, &g_local_connections);
net_unlock();
} }
return conn; return conn;
@ -186,6 +187,9 @@ FAR struct local_conn_s *local_alloc(void)
* connection in LISTEN. In that case, this function will create * connection in LISTEN. In that case, this function will create
* a new connection and initialize it. * a new connection and initialize it.
* *
* Assumptions:
* This function must be called with the network locked.
*
****************************************************************************/ ****************************************************************************/
int local_alloc_accept(FAR struct local_conn_s *server, int local_alloc_accept(FAR struct local_conn_s *server,
@ -262,6 +266,9 @@ err:
* Free a packet Unix domain connection structure that is no longer in use. * Free a packet Unix domain connection structure that is no longer in use.
* This should be done by the implementation of close(). * This should be done by the implementation of close().
* *
* Assumptions:
* This function must be called with the network locked.
*
****************************************************************************/ ****************************************************************************/
void local_free(FAR struct local_conn_s *conn) void local_free(FAR struct local_conn_s *conn)
@ -274,7 +281,6 @@ void local_free(FAR struct local_conn_s *conn)
/* Remove the server from the list of listeners. */ /* Remove the server from the list of listeners. */
net_lock();
dq_rem(&conn->lc_conn.node, &g_local_connections); dq_rem(&conn->lc_conn.node, &g_local_connections);
if (local_peerconn(conn) && conn->lc_peer) if (local_peerconn(conn) && conn->lc_peer)
@ -283,8 +289,6 @@ void local_free(FAR struct local_conn_s *conn)
conn->lc_peer = NULL; conn->lc_peer = NULL;
} }
net_unlock();
/* Make sure that the read-only FIFO is closed */ /* Make sure that the read-only FIFO is closed */
if (conn->lc_infile.f_inode != NULL) if (conn->lc_infile.f_inode != NULL)

View File

@ -109,7 +109,9 @@ static int inline local_stream_connect(FAR struct local_conn_s *client,
DEBUGASSERT(client->lc_outfile.f_inode != NULL); DEBUGASSERT(client->lc_outfile.f_inode != NULL);
net_lock();
ret = local_alloc_accept(server, client, &conn); ret = local_alloc_accept(server, client, &conn);
net_unlock();
if (ret < 0) if (ret < 0)
{ {
nerr("ERROR: Failed to alloc accept conn %s: %d\n", nerr("ERROR: Failed to alloc accept conn %s: %d\n",
@ -150,7 +152,9 @@ static int inline local_stream_connect(FAR struct local_conn_s *client,
return ret; return ret;
errout_with_conn: errout_with_conn:
net_lock();
local_free(conn); local_free(conn);
net_unlock();
errout_with_outfd: errout_with_outfd:
file_close(&client->lc_outfile); file_close(&client->lc_outfile);

View File

@ -306,12 +306,16 @@ static ssize_t local_sendto(FAR struct socket *psock,
return -EISCONN; return -EISCONN;
} }
net_lock();
if (local_findconn(conn, unaddr) == NULL) if (local_findconn(conn, unaddr) == NULL)
{ {
net_unlock();
nerr("ERROR: No such file or directory\n"); nerr("ERROR: No such file or directory\n");
return -ENOENT; return -ENOENT;
} }
net_unlock();
/* Make sure that dgram is sent safely */ /* Make sure that dgram is sent safely */
ret = nxmutex_lock(&conn->lc_sendlock); ret = nxmutex_lock(&conn->lc_sendlock);

View File

@ -128,7 +128,10 @@ static int local_sockif_alloc(FAR struct socket *psock)
{ {
/* Allocate the local connection structure */ /* Allocate the local connection structure */
FAR struct local_conn_s *conn = local_alloc(); FAR struct local_conn_s *conn;
net_lock();
conn = local_alloc();
net_unlock();
if (conn == NULL) if (conn == NULL)
{ {
/* Failed to reserve a connection structure */ /* Failed to reserve a connection structure */