From c6be4affa0b817437e5c11ced065195459818be3 Mon Sep 17 00:00:00 2001 From: Vo Trung Chi Date: Wed, 15 May 2019 16:16:30 +0700 Subject: daemon: Loop for client fds In dlt_daemon_client_send_all_multiple(), while in the loop, the connection to client may be destroyed by recursive call to dlt_daemon_close_socket(). So in some scenario we still use the removed connection, which is unexpected behavior. Solution: loop on the fds array and find the client connection corresponds to. Signed-off-by: Vo Trung Chi --- src/daemon/dlt_daemon_client.c | 28 ++++++++++++++++------------ src/daemon/dlt_daemon_connection.c | 3 --- src/daemon/dlt_daemon_event_handler.c | 9 +++++---- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/daemon/dlt_daemon_client.c b/src/daemon/dlt_daemon_client.c index d8062d5..fd2bde4 100644 --- a/src/daemon/dlt_daemon_client.c +++ b/src/daemon/dlt_daemon_client.c @@ -64,6 +64,7 @@ #include "dlt_daemon_client.h" #include "dlt_daemon_connection.h" +#include "dlt_daemon_event_handler.h" #include "dlt_daemon_offline_logstorage.h" #include "dlt_gateway.h" @@ -109,27 +110,32 @@ static int dlt_daemon_client_send_all_multiple(DltDaemon *daemon, int size2, int verbose) { - int j, sent = 0; + int sent = 0; + unsigned int i = 0; + int ret = 0; DltConnection *temp = NULL; int type_mask = (DLT_CON_MASK_CLIENT_MSG_TCP | DLT_CON_MASK_CLIENT_MSG_SERIAL); + PRINT_FUNCTION_VERBOSE(verbose); + if ((daemon == NULL) || (daemon_local == NULL)) { dlt_vlog(LOG_ERR, "%s: Invalid parameters\n", __func__); return 0; } - temp = daemon_local->pEvent.connections; - temp = dlt_connection_get_next(temp, type_mask); + for (i = 0; i < daemon_local->pEvent.nfds; i++) + { + temp = dlt_event_handler_find_connection(&(daemon_local->pEvent), + daemon_local->pEvent.pfd[i].fd); + + if ((temp == NULL) || (temp->receiver == NULL) || + !((1 << temp->type) & type_mask)) { + dlt_vlog(LOG_DEBUG, "The connection not found or the connection type not TCP/Serial.\n"); + continue; + } - /* FIXME: the lock shall include the for loop as data - * can be affect between each iteration, but - * dlt_daemon_close_socket may call us too ... - */ - for (j = 0; ((j < daemon_local->client_connections) && (temp != NULL)); j++) { - int ret = 0; DLT_DAEMON_SEM_LOCK(); - DltConnection *next = dlt_connection_get_next(temp->next, type_mask); ret = dlt_connection_send_multiple(temp, data1, @@ -154,8 +160,6 @@ static int dlt_daemon_client_send_all_multiple(DltDaemon *daemon, * then do not store in ring buffer */ sent = 1; - - temp = next; } /* for */ return sent; diff --git a/src/daemon/dlt_daemon_connection.c b/src/daemon/dlt_daemon_connection.c index 456a075..b1a4080 100644 --- a/src/daemon/dlt_daemon_connection.c +++ b/src/daemon/dlt_daemon_connection.c @@ -340,9 +340,6 @@ void dlt_connection_destroy(DltConnection *to_destroy) to_destroy->id = 0; close(to_destroy->receiver->fd); dlt_connection_destroy_receiver(to_destroy); - /* connection pointer might be in poll queue and used even after destroying - * it. To make sure it is not used anymore, connection type is invalidated */ - to_destroy->type = DLT_CONNECTION_TYPE_MAX; free(to_destroy); } diff --git a/src/daemon/dlt_daemon_event_handler.c b/src/daemon/dlt_daemon_event_handler.c index 895a896..38aa022 100644 --- a/src/daemon/dlt_daemon_event_handler.c +++ b/src/daemon/dlt_daemon_event_handler.c @@ -285,14 +285,15 @@ int dlt_daemon_handle_event(DltEventHandler *pEvent, * * @return The found connection pointer, NULL otherwise. */ -DltConnection *dlt_event_handler_find_connection(DltEventHandler *ev, - int fd) +DltConnection *dlt_event_handler_find_connection(DltEventHandler *ev, int fd) { - DltConnection *temp = ev->connections; - while ((temp != NULL) && (temp->receiver->fd != fd)) + while (temp != NULL) { + if ((temp->receiver != NULL) && (temp->receiver->fd == fd)) + return temp; temp = temp->next; + } return temp; } -- cgit v1.2.1