From e2943ff4cf2d7d2da4000e2ca35663a7c83675d7 Mon Sep 17 00:00:00 2001 From: Simon Brandner Date: Mon, 28 Jan 2013 12:14:06 +0100 Subject: Semaphores and Pointer passing insteasd by value and otehr coverity issue fixes Signed-off-by: Alexander Wenzel --- src/daemon/dlt-daemon.c | 14 +++++++++----- src/daemon/dlt_daemon_common.c | 13 ++++++++++--- src/lib/dlt_user.c | 5 ++++- src/shared/dlt_common.c | 15 +++++++++++---- src/shared/dlt_offline_trace.c | 23 +++++++++++++++-------- src/system/dlt-system-logfile.c | 18 +++++++++--------- src/system/dlt-system-process-handling.c | 10 +++++++++- src/system/dlt-system-processes.c | 16 ++++++++-------- src/tests/dlt-test-client.c | 2 +- 9 files changed, 76 insertions(+), 40 deletions(-) diff --git a/src/daemon/dlt-daemon.c b/src/daemon/dlt-daemon.c index ea9e43d..20cba66 100644 --- a/src/daemon/dlt-daemon.c +++ b/src/daemon/dlt-daemon.c @@ -1106,10 +1106,13 @@ void dlt_daemon_daemonize(int verbose) /* Open standard descriptors stdin, stdout, stderr */ i=open("/dev/null",O_RDWR); /* open stdin */ - if(dup(i) < 0) - dlt_log(LOG_ERR, "Failed to direct stdout to /dev/null.\n");/* stdout */ - if(dup(i) < 0) - dlt_log(LOG_ERR, "Failed to direct stderr to /dev/null.\n"); /* stderr */ + if (-1 < i) + { + if(dup(i) < 0) + dlt_log(LOG_ERR, "Failed to direct stdout to /dev/null.\n");/* stdout */ + if(dup(i) < 0) + dlt_log(LOG_ERR, "Failed to direct stderr to /dev/null.\n"); /* stderr */ + } /* Set umask */ umask(DLT_DAEMON_UMASK); @@ -1225,6 +1228,7 @@ int dlt_daemon_process_client_messages(DltDaemon *daemon, DltDaemonLocal *daemon if (dlt_receiver_receive_socket(&(daemon_local->receiverSock))<=0) { close(daemon_local->receiverSock.fd); + daemon_local->receiverSock.fd = 0; FD_CLR(daemon_local->receiverSock.fd, &(daemon_local->master)); if (daemon_local->client_connections) @@ -1252,7 +1256,7 @@ int dlt_daemon_process_client_messages(DltDaemon *daemon, DltDaemonLocal *daemon while (dlt_message_read(&(daemon_local->msg),(uint8_t*)daemon_local->receiverSock.buf,daemon_local->receiverSock.bytesRcvd,daemon_local->flags.nflag,daemon_local->flags.vflag)==0) { /* Check for control message */ - if (DLT_MSG_IS_CONTROL_REQUEST(&(daemon_local->msg))) + if ( 0 != daemon_local->receiverSock.fd && DLT_MSG_IS_CONTROL_REQUEST(&(daemon_local->msg)) ) { dlt_daemon_control_process_control(daemon_local->receiverSock.fd, daemon, &(daemon_local->msg), daemon_local->flags.vflag); } diff --git a/src/daemon/dlt_daemon_common.c b/src/daemon/dlt_daemon_common.c index 12af44e..1b34f21 100644 --- a/src/daemon/dlt_daemon_common.c +++ b/src/daemon/dlt_daemon_common.c @@ -2523,13 +2523,20 @@ void dlt_daemon_control_message_time(int sock, DltDaemon *daemon, int verbose) /* Optional: Send serial header, if requested */ if (daemon->sendserialheader) { - send(sock, dltSerialHeader,sizeof(dltSerialHeader),0); + if ( -1 == send(sock, dltSerialHeader,sizeof(dltSerialHeader),0) ) + dlt_log(LOG_ERR,"dlt_daemon_control_message_time: Failed to send dltSerialHeader"); + } /* Send data */ - send(sock, msg.headerbuffer+sizeof(DltStorageHeader),msg.headersize-sizeof(DltStorageHeader),0); + if (-1 == send(sock, msg.headerbuffer+sizeof(DltStorageHeader),msg.headersize-sizeof(DltStorageHeader),0)) + dlt_log(LOG_ERR,"dlt_daemon_control_message_time: Failed to send DltStorageHeader"); + if(msg.datasize > 0) - send(sock, msg.databuffer,msg.datasize,0); + { + if (-1 == send(sock, msg.databuffer,msg.datasize,0) ) + dlt_log(LOG_ERR,"dlt_daemon_control_message_time: Failed to send databuffer"); + } DLT_DAEMON_SEM_FREE(); } diff --git a/src/lib/dlt_user.c b/src/lib/dlt_user.c index 0728fb1..407154f 100644 --- a/src/lib/dlt_user.c +++ b/src/lib/dlt_user.c @@ -519,6 +519,7 @@ int dlt_free(void) /* Ignore return value */ dlt_buffer_free_dynamic(&(dlt_user.startup_buffer)); + DLT_SEM_LOCK(); if (dlt_user.dlt_ll_ts) { for (i=0;ihandle,file->file_position,SEEK_SET); + if (fseek(file->handle,file->file_position,SEEK_SET) < 0) + return -1; } } @@ -1711,7 +1712,12 @@ int dlt_file_read(DltFile *file,int verbose) } /* set to end of last succesful read message, because of conflicting calls to dlt_file_read and dlt_file_message */ - fseek(file->handle,file->file_position,SEEK_SET); + if ( fseek(file->handle,file->file_position,SEEK_SET) < 0 ) + { + sprintf(str,"Seek failed to file_position %ld \n",file->file_position); + dlt_log(LOG_ERR, str); + return -1; + } /* get file position at start of DLT message */ if (verbose) @@ -1823,8 +1829,9 @@ int dlt_file_read_raw(DltFile *file,int resync, int verbose) file->index = ptr; } - /* set to end of last succesful read message, because of conflicting calls to dlt_file_read and dlt_file_message */ - fseek(file->handle,file->file_position,SEEK_SET); + /* set to end of last successful read message, because of conflicting calls to dlt_file_read and dlt_file_message */ + if (fseek(file->handle,file->file_position,SEEK_SET) < 0) + return -1; /* get file position at start of DLT message */ if (verbose) diff --git a/src/shared/dlt_offline_trace.c b/src/shared/dlt_offline_trace.c index 72b31ad..80c328a 100644 --- a/src/shared/dlt_offline_trace.c +++ b/src/shared/dlt_offline_trace.c @@ -101,8 +101,10 @@ unsigned long dlt_offline_trace_get_total_size(DltOfflineTrace *trace) { while ((dp=readdir(dir)) != NULL) { if(strstr(dp->d_name,".dlt")) { sprintf(filename,"%s/%s",trace->directory,dp->d_name); - stat(filename,&status); - size += status.st_size; + if ( 0 == stat(filename,&status) ) + size += status.st_size; + else + printf("Offline trace file %s cannot be stat-ed",filename); } } closedir(dir); @@ -127,12 +129,17 @@ int dlt_offline_trace_delete_oldest_file(DltOfflineTrace *trace) { while ((dp=readdir(dir)) != NULL) { if(strstr(dp->d_name,".dlt")) { sprintf(filename,"%s/%s",trace->directory,dp->d_name); - stat(filename,&status); - if(time_oldest == 0 || status.st_mtime < time_oldest) { - time_oldest = status.st_mtime; - size_oldest = status.st_size; - strcpy(filename_oldest,filename); - } + if (0 == stat(filename,&status)) + { + if(time_oldest == 0 || status.st_mtime < time_oldest) { + time_oldest = status.st_mtime; + size_oldest = status.st_size; + strcpy(filename_oldest,filename); + } + } + else + printf("Old offline trace file %s cannot be stat-ed",filename); + } } closedir(dir); diff --git a/src/system/dlt-system-logfile.c b/src/system/dlt-system-logfile.c index 50964f2..1354b83 100644 --- a/src/system/dlt-system-logfile.c +++ b/src/system/dlt-system-logfile.c @@ -56,7 +56,7 @@ DLT_IMPORT_CONTEXT(dltsystem); extern DltSystemThreads threads; DltContext logfileContext[DLT_SYSTEM_LOG_FILE_MAX]; -void send_file(LogFileOptions fileopt, int n) +void send_file(LogFileOptions const *fileopt, int n) { DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("dlt-system-logfile, sending file.")); @@ -66,7 +66,7 @@ void send_file(LogFileOptions fileopt, int n) int bytes; int seq = 1; - pFile = fopen(fileopt.Filename[n],"r"); + pFile = fopen((*fileopt).Filename[n],"r"); if(pFile != NULL) { @@ -91,19 +91,19 @@ void send_file(LogFileOptions fileopt, int n) { DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("dlt-system-logfile, failed to open file."), - DLT_STRING(fileopt.Filename[n])); + DLT_STRING((*fileopt).Filename[n])); } } -void register_contexts(LogFileOptions fileopts) +void register_contexts(LogFileOptions const *fileopts) { DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("dlt-system-logfile, registering file contexts.")); int i; - for(i = 0;i < fileopts.Count;i++) + for(i = 0;i < (*fileopts).Count;i++) { - DLT_REGISTER_CONTEXT(logfileContext[i], fileopts.ContextId[i], - fileopts.Filename[i]); + DLT_REGISTER_CONTEXT(logfileContext[i], (*fileopts).ContextId[i], + (*fileopts).Filename[i]); } } @@ -113,7 +113,7 @@ void logfile_thread(void *v_conf) DLT_STRING("dlt-system-logfile, in thread.")); DltSystemConfiguration *conf = (DltSystemConfiguration *) v_conf; - register_contexts(conf->LogFile); + register_contexts(&(conf->LogFile)); int logfile_delays[DLT_SYSTEM_LOG_FILE_MAX]; int i; @@ -130,7 +130,7 @@ void logfile_thread(void *v_conf) if(logfile_delays[i] <= 0) { - send_file(conf->LogFile, i); + send_file(&(conf->LogFile), i); logfile_delays[i] = conf->LogFile.TimeDelay[i]; if(conf->LogFile.Mode[i] == SEND_MODE_ONCE) conf->LogFile.Mode[i] = SEND_MODE_OFF; diff --git a/src/system/dlt-system-process-handling.c b/src/system/dlt-system-process-handling.c index 3ad7908..49c20e7 100644 --- a/src/system/dlt-system-process-handling.c +++ b/src/system/dlt-system-process-handling.c @@ -81,9 +81,16 @@ int daemonize() close(i); int fd = open("/dev/null",O_RDWR); + if(fd < 0) + { + return -1; + } - if(fd < 0 || dup(fd) < 0 || dup(fd) < 0) + if(dup(fd) < 0 || dup(fd) < 0) + { + close(fd); return -1; + } /** * Ignore signals related to child processes and @@ -93,6 +100,7 @@ int daemonize() signal(SIGTSTP, SIG_IGN); signal(SIGTTOU, SIG_IGN); signal(SIGTTIN, SIG_IGN); + close(fd); return 0; } diff --git a/src/system/dlt-system-processes.c b/src/system/dlt-system-processes.c index ee37bfc..495c784 100644 --- a/src/system/dlt-system-processes.c +++ b/src/system/dlt-system-processes.c @@ -63,7 +63,7 @@ extern DltSystemThreads threads; DLT_IMPORT_CONTEXT(dltsystem) DLT_DECLARE_CONTEXT(procContext) -void send_process(LogProcessOptions popts, int n) +void send_process(LogProcessOptions const *popts, int n) { DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("dlt-system-processes, send process info.")); @@ -90,11 +90,11 @@ void send_process(LogProcessOptions popts, int n) bytes = fread(buffer, 1, sizeof(buffer)-1, pFile); fclose(pFile); } - if((strcmp(popts.Name[n], "*")==0) || - (strcmp(buffer, popts.Name[n])==0)) + if((strcmp((*popts).Name[n], "*")==0) || + (strcmp(buffer, (*popts).Name[n])==0)) { found = 1; - sprintf(filename, "/proc/%s/%s", dp->d_name,popts.Filename[n]); + sprintf(filename, "/proc/%s/%s", dp->d_name,(*popts).Filename[n]); pFile = fopen(filename, "r"); if(pFile != NULL) { @@ -104,10 +104,10 @@ void send_process(LogProcessOptions popts, int n) if(bytes>0) { buffer[bytes] = 0; - DLT_LOG(procContext, DLT_LOG_INFO, DLT_INT(atoi(dp->d_name)), DLT_STRING(popts.Filename[n]), DLT_STRING(buffer)); + DLT_LOG(procContext, DLT_LOG_INFO, DLT_INT(atoi(dp->d_name)), DLT_STRING((*popts).Filename[n]), DLT_STRING(buffer)); } } - if(strcmp(popts.Name[n], "*") !=0) + if(strcmp((*popts).Name[n], "*") !=0) break; } } @@ -121,7 +121,7 @@ void send_process(LogProcessOptions popts, int n) } if(!found) { - DLT_LOG(procContext, DLT_LOG_INFO, DLT_STRING("Process"), DLT_STRING(popts.Name[n]),DLT_STRING("not running!")); + DLT_LOG(procContext, DLT_LOG_INFO, DLT_STRING("Process"), DLT_STRING((*popts).Name[n]),DLT_STRING("not running!")); } } @@ -148,7 +148,7 @@ void logprocess_thread(void *v_conf) if(process_delays[i] <= 0) { - send_process(conf->LogProcesses, i); + send_process(&(conf->LogProcesses), i); process_delays[i] = conf->LogProcesses.TimeDelay[i]; if(conf->LogProcesses.Mode[i] == SEND_MODE_ONCE) conf->LogProcesses.Mode[i] = SEND_MODE_OFF; diff --git a/src/tests/dlt-test-client.c b/src/tests/dlt-test-client.c index 3fb98cd..3855000 100755 --- a/src/tests/dlt-test-client.c +++ b/src/tests/dlt-test-client.c @@ -673,7 +673,7 @@ int dlt_testclient_message_callback(DltMessage *message, void *data) /* Get length */ DLT_MSG_READ_VALUE(length_tmp,ptr,datalength,uint16_t); length=DLT_ENDIAN_GET_16(message->standardheader->htyp, length_tmp); - if ((length==datalength) && (length=10)) + if ((length==datalength) && (10 == length)) { dltdata->test_counter_macro[1]++; } -- cgit v1.2.1