From 5abd10c0c3c6a624b8815f1d01c8460e67d0c475 Mon Sep 17 00:00:00 2001 From: Mathieu Rene Date: Sat, 22 Aug 2009 19:26:58 +0000 Subject: [PATCH] refactor mod_event_socket so it uses switch_log functions to duplicate and free log nodes while it uses them internally (fix bad ptr for userdata on event socket listeners) git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@14598 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_log.h | 4 ++++ .../mod_event_socket/mod_event_socket.c | 21 +++++------------- src/switch_log.c | 22 ++++++++++++++++++- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/include/switch_log.h b/src/include/switch_log.h index 7f3793e9f8..58063b9836 100644 --- a/src/include/switch_log.h +++ b/src/include/switch_log.h @@ -143,6 +143,10 @@ SWITCH_DECLARE(switch_log_level_t) switch_log_str2level(_In_z_ const char *str); SWITCH_DECLARE(uint32_t) switch_log_str2mask(_In_z_ const char *str); #define switch_log_check_mask(_mask, _level) (_mask & (1 << _level)) + +SWITCH_DECLARE(switch_log_node_t*) switch_log_node_dup(const switch_log_node_t *node); +SWITCH_DECLARE(void) switch_log_node_free(switch_log_node_t **pnode); + ///\} SWITCH_END_EXTERN_C #endif diff --git a/src/mod/event_handlers/mod_event_socket/mod_event_socket.c b/src/mod/event_handlers/mod_event_socket/mod_event_socket.c index 6cc280df70..b0e0645310 100644 --- a/src/mod/event_handlers/mod_event_socket/mod_event_socket.c +++ b/src/mod/event_handlers/mod_event_socket/mod_event_socket.c @@ -144,12 +144,7 @@ static switch_status_t socket_logger(const switch_log_node_t *node, switch_log_l switch_mutex_lock(globals.listener_mutex); for (l = listen_list.listeners; l; l = l->next) { if (switch_test_flag(l, LFLAG_LOG) && l->level >= node->level) { - switch_log_node_t *dnode = malloc(sizeof(*node)); - - switch_assert(dnode); - *dnode = *node; - dnode->data = strdup(node->data); - switch_assert(dnode->data); + switch_log_node_t *dnode = switch_log_node_dup(node); if (switch_queue_trypush(l->log_queue, dnode) == SWITCH_STATUS_SUCCESS) { if (l->lost_logs) { @@ -163,8 +158,7 @@ static switch_status_t socket_logger(const switch_log_node_t *node, switch_log_l } } } else { - switch_safe_free(dnode->data); - switch_safe_free(dnode); + switch_log_node_free(&dnode); l->lost_logs++; } } @@ -182,8 +176,7 @@ static void flush_listener(listener_t *listener, switch_bool_t flush_log, switch while (switch_queue_trypop(listener->log_queue, &pop) == SWITCH_STATUS_SUCCESS) { switch_log_node_t *dnode = (switch_log_node_t *) pop; if (dnode) { - free(dnode->data); - free(dnode); + switch_log_node_free(&dnode); } } } @@ -889,8 +882,7 @@ SWITCH_STANDARD_API(event_sink_function) encode_buf ); free(encode_buf); - free(dnode->data); - free(dnode); + switch_log_node_free(&dnode); } stream->write_function(stream, "\n"); @@ -1168,10 +1160,9 @@ static switch_status_t read_packet(listener_t *listener, switch_event_t **event, switch_socket_send(listener->sock, buf, &len); len = strlen(dnode->data); switch_socket_send(listener->sock, dnode->data, &len); - - free(dnode->data); - free(dnode); } + + switch_log_node_free(&dnode); do_sleep = 0; } } diff --git a/src/switch_log.c b/src/switch_log.c index c872b171e0..6a8cacdb27 100644 --- a/src/switch_log.c +++ b/src/switch_log.c @@ -97,7 +97,26 @@ static switch_log_node_t* switch_log_node_alloc() return node; } -static void switch_log_node_free(switch_log_node_t **pnode) +SWITCH_DECLARE(switch_log_node_t*) switch_log_node_dup(const switch_log_node_t *node) +{ + switch_log_node_t *newnode = switch_log_node_alloc(); + + *newnode = *node; + + if (!switch_strlen_zero(node->data)) { + newnode->data = strdup(node->data); + switch_assert(node->data); + } + + if (!switch_strlen_zero(node->userdata)) { + newnode->userdata = strdup(node->userdata); + switch_assert(node->userdata); + } + + return newnode; +} + +SWITCH_DECLARE(void) switch_log_node_free(switch_log_node_t **pnode) { switch_log_node_t *node; @@ -284,6 +303,7 @@ static void *SWITCH_THREAD_FUNC log_thread(switch_thread_t *t, void *obj) switch_mutex_unlock(BINDLOCK); switch_log_node_free(&node); + } THREAD_RUNNING = 0;