From 6747f4bc21cb7595ceec1a08527a450900217751 Mon Sep 17 00:00:00 2001 From: Lassi Marttala Date: Mon, 7 May 2012 15:36:31 +0200 Subject: [D4099]: Integrated fix from Alexander Wenzel. This will check for duplicate file handles, and clean them up if found. Signed-off-by: Christian Muck --- src/daemon/dlt-daemon.c | 8 ++++++- src/daemon/dlt_daemon_common.c | 51 +++++++++++++++++++++++++++++++++++++++++- src/daemon/dlt_daemon_common.h | 16 +++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) mode change 100755 => 100644 src/daemon/dlt-daemon.c mode change 100755 => 100644 src/daemon/dlt_daemon_common.c mode change 100755 => 100644 src/daemon/dlt_daemon_common.h diff --git a/src/daemon/dlt-daemon.c b/src/daemon/dlt-daemon.c old mode 100755 new mode 100644 index 04612bb..69e4072 --- a/src/daemon/dlt-daemon.c +++ b/src/daemon/dlt-daemon.c @@ -210,7 +210,7 @@ int option_file_parser(DltDaemonLocal *daemon_local) daemon_local->flags.offlineTraceMaxSize = 0; daemon_local->flags.loggingMode = 0; daemon_local->flags.loggingLevel = 6; - strncpy(daemon_local->flags.loggingFilename,"/tmp/dlt.log",sizeof(daemon_local->flags.loggingFilename)); + strncpy(daemon_local->flags.loggingFilename, DLT_USER_DIR "/dlt.log",sizeof(daemon_local->flags.loggingFilename)); /* open configuration file */ if(daemon_local->flags.cvalue[0]) @@ -974,6 +974,12 @@ int dlt_daemon_process_client_connect(DltDaemon *daemon, DltDaemonLocal *daemon_ dlt_log(LOG_ERR, "accept() failed!\n"); return -1 ; } + + /* check if file file descriptor was already used, and make it invalid if it is reused */ + /* This prevents sending messages to wrong file descriptor */ + dlt_daemon_applications_invalidate_fd(daemon,in_sock,verbose); + dlt_daemon_contexts_invalidate_fd(daemon,in_sock,verbose); + //sprintf("str,"Client Connection from %s\n", inet_ntoa(cli.sin_addr)); //dlt_log(str); FD_SET(in_sock, &(daemon_local->master)); /* add to master set */ diff --git a/src/daemon/dlt_daemon_common.c b/src/daemon/dlt_daemon_common.c old mode 100755 new mode 100644 index 99bbabe..3bf940e --- a/src/daemon/dlt_daemon_common.c +++ b/src/daemon/dlt_daemon_common.c @@ -208,6 +208,28 @@ int dlt_daemon_free(DltDaemon *daemon,int verbose) return 0; } +int dlt_daemon_applications_invalidate_fd(DltDaemon *daemon,int fd,int verbose) +{ + int i; + + PRINT_FUNCTION_VERBOSE(verbose); + + if (daemon==0) + { + return -1; + } + + for (i=0; inum_applications; i++) + { + if (daemon->applications[i].user_handle==fd) + { + daemon->applications[i].user_handle = 0; + } + } + + return 0; +} + int dlt_daemon_applications_clear(DltDaemon *daemon,int verbose) { int i; @@ -349,6 +371,11 @@ DltDaemonApplication* dlt_daemon_application_add(DltDaemon *daemon,char *apid,pi dlt_log(LOG_ERR, str); } /* if */ + /* check if file file descriptor was already used, and make it invalid if it is reused */ + /* This prevents sending messages to wrong file descriptor */ + dlt_daemon_applications_invalidate_fd(daemon,dlt_user_handle,verbose); + dlt_daemon_contexts_invalidate_fd(daemon,dlt_user_handle,verbose); + application->user_handle = dlt_user_handle; } @@ -600,7 +627,7 @@ DltDaemonContext* dlt_daemon_context_add(DltDaemon *daemon,char *apid,char *ctid if (context->context_description) { free(context->context_description); - context->context_description=0; // opt mb: moved inside the if clause (very minor opt) + context->context_description=0; } if (description) @@ -707,6 +734,28 @@ DltDaemonContext* dlt_daemon_context_find(DltDaemon *daemon,char *apid,char *cti return (DltDaemonContext*)bsearch(&context,daemon->contexts,daemon->num_contexts,sizeof(DltDaemonContext),dlt_daemon_cmp_apid_ctid); } +int dlt_daemon_contexts_invalidate_fd(DltDaemon *daemon,int fd,int verbose) +{ + int i; + + PRINT_FUNCTION_VERBOSE(verbose); + + if (daemon==0) + { + return -1; + } + + for (i=0; inum_contexts; i++) + { + if (daemon->contexts[i].user_handle==fd) + { + daemon->contexts[i].user_handle = 0; + } + } + + return 0; +} + int dlt_daemon_contexts_clear(DltDaemon *daemon,int verbose) { int i; diff --git a/src/daemon/dlt_daemon_common.h b/src/daemon/dlt_daemon_common.h old mode 100755 new mode 100644 index 9bbe3c7..48c8868 --- a/src/daemon/dlt_daemon_common.h +++ b/src/daemon/dlt_daemon_common.h @@ -203,6 +203,14 @@ int dlt_daemon_applications_load(DltDaemon *daemon,const char *filename, int ver * @return negative value if there was an error */ int dlt_daemon_applications_save(DltDaemon *daemon,const char *filename, int verbose); +/** + * Invalidate all applications fd, if fd is reused + * @param daemon pointer to dlt daemon structure + * @param fd file descriptor + * @param verbose if set to true verbose information is printed out. + * @return negative value if there was an error + */ +int dlt_daemon_applications_invalidate_fd(DltDaemon *daemon,int fd,int verbose); /** * Clear all applications in internal application management * @param daemon pointer to dlt daemon structure @@ -242,6 +250,14 @@ int dlt_daemon_context_del(DltDaemon *daemon, DltDaemonContext* context, int ver * @return Pointer to context, null pointer on error or not found */ DltDaemonContext* dlt_daemon_context_find(DltDaemon *daemon,char *apid,char *ctid,int verbose); +/** + * Invalidate all contexts fd, if fd is reused + * @param daemon pointer to dlt daemon structure + * @param fd file descriptor + * @param verbose if set to true verbose information is printed out. + * @return negative value if there was an error + */ +int dlt_daemon_contexts_invalidate_fd(DltDaemon *daemon,int fd,int verbose); /** * Clear all contexts in internal context management * @param daemon pointer to dlt daemon structure -- cgit v1.2.1