diff --git a/net/local/local_bind.c b/net/local/local_bind.c index 627b6eee5c..10c7220ed7 100644 --- a/net/local/local_bind.c +++ b/net/local/local_bind.c @@ -64,11 +64,15 @@ int psock_local_bind(FAR struct socket *psock, /* Check if local address is already in use */ + net_lock(); if (local_findconn(conn, unaddr) != NULL) { + net_unlock(); return -EADDRINUSE; } + net_unlock(); + /* Save the address family */ conn->lc_instance_id = -1; diff --git a/net/local/local_conn.c b/net/local/local_conn.c index acb6f80fe0..d1403a3a1d 100644 --- a/net/local/local_conn.c +++ b/net/local/local_conn.c @@ -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() * API * + * Assumptions: + * This function must be called with the network locked. + * ****************************************************************************/ 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 */ - net_lock(); dq_addlast(&conn->lc_conn.node, &g_local_connections); - net_unlock(); } return conn; @@ -186,6 +187,9 @@ FAR struct local_conn_s *local_alloc(void) * connection in LISTEN. In that case, this function will create * 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, @@ -262,6 +266,9 @@ err: * Free a packet Unix domain connection structure that is no longer in use. * 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) @@ -274,7 +281,6 @@ void local_free(FAR struct local_conn_s *conn) /* Remove the server from the list of listeners. */ - net_lock(); dq_rem(&conn->lc_conn.node, &g_local_connections); if (local_peerconn(conn) && conn->lc_peer) @@ -283,8 +289,6 @@ void local_free(FAR struct local_conn_s *conn) conn->lc_peer = NULL; } - net_unlock(); - /* Make sure that the read-only FIFO is closed */ if (conn->lc_infile.f_inode != NULL) diff --git a/net/local/local_connect.c b/net/local/local_connect.c index 6ae84213d7..b768a2bc92 100644 --- a/net/local/local_connect.c +++ b/net/local/local_connect.c @@ -109,7 +109,9 @@ static int inline local_stream_connect(FAR struct local_conn_s *client, DEBUGASSERT(client->lc_outfile.f_inode != NULL); + net_lock(); ret = local_alloc_accept(server, client, &conn); + net_unlock(); if (ret < 0) { 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; errout_with_conn: + net_lock(); local_free(conn); + net_unlock(); errout_with_outfd: file_close(&client->lc_outfile); diff --git a/net/local/local_sendmsg.c b/net/local/local_sendmsg.c index a06b925322..64f14457bb 100644 --- a/net/local/local_sendmsg.c +++ b/net/local/local_sendmsg.c @@ -306,12 +306,16 @@ static ssize_t local_sendto(FAR struct socket *psock, return -EISCONN; } + net_lock(); if (local_findconn(conn, unaddr) == NULL) { + net_unlock(); nerr("ERROR: No such file or directory\n"); return -ENOENT; } + net_unlock(); + /* Make sure that dgram is sent safely */ ret = nxmutex_lock(&conn->lc_sendlock); diff --git a/net/local/local_sockif.c b/net/local/local_sockif.c index ec1649c0e5..47f16c87b5 100644 --- a/net/local/local_sockif.c +++ b/net/local/local_sockif.c @@ -128,7 +128,10 @@ static int local_sockif_alloc(FAR struct socket *psock) { /* 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) { /* Failed to reserve a connection structure */