Skip to content

Commit 86fb853

Browse files
src: refactoring current implementation (#566)
* src: refactoring current implementation + Remove dead code + Add NULL variables and failed processes detection + Assign delimiters to const char type + Handle multithread cancellation Signed-off-by: LUU QUANG MINH <[email protected]> --------- Signed-off-by: LUU QUANG MINH <[email protected]> Co-authored-by: LUU QUANG MINH <[email protected]> Co-authored-by: lvklevankhanh <[email protected]>
1 parent d7868aa commit 86fb853

22 files changed

+200
-146
lines changed

src/console/dlt-control-common.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,17 @@ int dlt_control_deinit(void)
663663
close(g_client.receiver.fd);
664664
g_client.receiver.fd = -1;
665665
}
666-
/* Waiting for thread to complete */
667-
pthread_join(daemon_connect_thread, NULL);
666+
667+
/* Stopping the listener thread */
668+
if (pthread_cancel(daemon_connect_thread)) {
669+
pr_error("Unable to cancel the thread with ERRNO=%s\n", strerror(errno));
670+
}
671+
else {
672+
if (pthread_join(daemon_connect_thread, NULL)) {
673+
pr_error("Unable to join the thread with ERRNO=%s\n", strerror(errno));
674+
}
675+
}
676+
668677
/* Closing the socket */
669678
return dlt_client_cleanup(&g_client, get_verbosity());
670679
}

src/console/dlt-convert.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,10 @@ void empty_dir(const char *dir)
134134
char tmp_filename[FILENAME_SIZE] = { 0 };
135135
uint32_t i;
136136

137-
if (dir == NULL)
137+
if (dir == NULL) {
138138
fprintf(stderr, "ERROR: %s: invalid arguments\n", __FUNCTION__);
139+
return;
140+
}
139141

140142
if (stat(dir, &st) == 0) {
141143
if (S_ISDIR(st.st_mode)) {

src/console/dlt-passive-node-ctrl.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,6 @@ static int dlt_passive_node_analyze_response(char *answer,
197197
DltServicePassiveNodeConnectionInfo *info =
198198
(DltServicePassiveNodeConnectionInfo *)(payload);
199199

200-
if (info == NULL) {
201-
fprintf(stderr, "Received response is NULL\n");
202-
return -1;
203-
}
204-
205200
dlt_print_passive_node_status(info);
206201
}
207202
}

src/console/dlt-sortbytimestamp.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -160,31 +160,31 @@ void write_messages(int ohandle, DltFile *file,
160160
if ((0 == i % 1001) || (i == message_count - 1))
161161
verbose(2, "Writing message %d\r", i);
162162

163-
if (dlt_file_message(file, timestamps[i].num, 0) < DLT_RETURN_OK)
164-
continue;
165-
iov[0].iov_base = file->msg.headerbuffer;
166-
iov[0].iov_len = file->msg.headersize;
167-
iov[1].iov_base = file->msg.databuffer;
168-
iov[1].iov_len = file->msg.datasize;
169-
170-
bytes_written = writev(ohandle, iov, 2);
171-
last_errno = errno;
172-
173-
if (0 > bytes_written) {
174-
printf("%s: returned an error [%s]!\n",
175-
__func__,
176-
strerror(last_errno));
177-
if (ohandle > 0) {
178-
close(ohandle);
179-
ohandle = -1;
163+
if (timestamps != NULL) {
164+
if (dlt_file_message(file, timestamps[i].num, 0) == DLT_RETURN_OK) {
165+
iov[0].iov_base = file->msg.headerbuffer;
166+
iov[0].iov_len = file->msg.headersize;
167+
iov[1].iov_base = file->msg.databuffer;
168+
iov[1].iov_len = file->msg.datasize;
169+
170+
bytes_written = writev(ohandle, iov, 2);
171+
last_errno = errno;
172+
173+
if (0 > bytes_written) {
174+
printf("%s: returned an error [%s]!\n",
175+
__func__,
176+
strerror(last_errno));
177+
if (ohandle > 0) {
178+
close(ohandle);
179+
ohandle = -1;
180+
}
181+
free(timestamps);
182+
timestamps = NULL;
183+
184+
dlt_file_free(file, 0);
185+
exit (-1);
186+
}
180187
}
181-
if (timestamps) {
182-
free(timestamps);
183-
timestamps = NULL;
184-
}
185-
186-
dlt_file_free(file, 0);
187-
exit (-1);
188188
}
189189
}
190190

src/console/logstorage/dlt-logstorage-ctrl.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,8 @@ static int parse_args(int argc, char *argv[])
458458
set_default_path(optarg);
459459
break;
460460
case 't':
461-
set_timeout((int) strtol(optarg, NULL, 10));
461+
if (optarg != NULL)
462+
set_timeout((int) strtol(optarg, NULL, 10));
462463
break;
463464
case 'S':
464465
{
@@ -483,15 +484,16 @@ static int parse_args(int argc, char *argv[])
483484
break;
484485
case 'p':
485486

486-
if (strlen(optarg) >= DLT_MOUNT_PATH_MAX) {
487+
if ((optarg != NULL) && (strlen(optarg) >= DLT_MOUNT_PATH_MAX)) {
487488
pr_error("Mount path '%s' too long\n", optarg);
488489
return -1;
489490
}
490491

491492
set_default_path(optarg);
492493
break;
493494
case 'c':
494-
set_default_event_type(strtol(optarg, NULL, 10));
495+
if (optarg != NULL)
496+
set_default_event_type(strtol(optarg, NULL, 10));
495497
break;
496498
case 'v':
497499
set_verbosity(1);

src/daemon/dlt-daemon.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,7 +2079,10 @@ void dlt_daemon_local_cleanup(DltDaemon *daemon, DltDaemonLocal *daemon_local, i
20792079
unlink(daemon_local->flags.daemonFifoName);
20802080
#else /* DLT_DAEMON_USE_UNIX_SOCKET_IPC */
20812081
/* Try to delete existing pipe, ignore result of unlink() */
2082-
unlink(daemon_local->flags.appSockPath);
2082+
if (unlink(daemon_local->flags.appSockPath) != 0) {
2083+
dlt_vlog(LOG_WARNING, "%s: unlink() failed: %s\n",
2084+
__func__, strerror(errno));
2085+
}
20832086
#endif
20842087

20852088
#ifdef DLT_SHM_ENABLE
@@ -2101,7 +2104,10 @@ void dlt_daemon_local_cleanup(DltDaemon *daemon, DltDaemonLocal *daemon_local, i
21012104
if (daemon->ECUVersionString != NULL)
21022105
free(daemon->ECUVersionString);
21032106

2104-
unlink(daemon_local->flags.ctrlSockPath);
2107+
if (unlink(daemon_local->flags.ctrlSockPath) != 0) {
2108+
dlt_vlog(LOG_WARNING, "%s: unlink() failed: %s\n",
2109+
__func__, strerror(errno));
2110+
}
21052111

21062112
/* free IP list */
21072113
free(daemon_local->flags.ipNodes);
@@ -2858,8 +2864,13 @@ int dlt_daemon_process_user_messages(DltDaemon *daemon,
28582864
break;
28592865

28602866
/* Set new start offset */
2861-
if (offset > 0)
2862-
dlt_receiver_remove(receiver, offset);
2867+
if (offset > 0) {
2868+
if (dlt_receiver_remove(receiver, offset) == -1) {
2869+
dlt_log(LOG_WARNING,
2870+
"Can't remove offset from receiver\n");
2871+
return -1;
2872+
}
2873+
}
28632874

28642875
if (userheader->message >= DLT_USER_MESSAGE_NOT_SUPPORTED)
28652876
func = dlt_daemon_process_user_message_not_sup;

src/daemon/dlt_daemon_client.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,8 +2419,8 @@ void dlt_daemon_control_service_logstorage(int sock,
24192419
DltMessage *msg,
24202420
int verbose)
24212421
{
2422-
DltServiceOfflineLogstorage *req;
2423-
int ret;
2422+
DltServiceOfflineLogstorage *req = NULL;
2423+
int ret = 0;
24242424
unsigned int connection_type = 0;
24252425
DltLogStorage *device = NULL;
24262426
int device_index = -1;
@@ -2774,22 +2774,6 @@ void dlt_daemon_control_passive_node_connect_status(int sock,
27742774

27752775
con = &daemon_local->pGateway.connections[i];
27762776

2777-
if (con == NULL) {
2778-
dlt_log(LOG_CRIT, "Passive node connection structure is NULL\n");
2779-
dlt_daemon_control_service_response(
2780-
sock,
2781-
daemon,
2782-
daemon_local,
2783-
DLT_SERVICE_ID_PASSIVE_NODE_CONNECTION_STATUS,
2784-
DLT_SERVICE_RESPONSE_ERROR,
2785-
verbose);
2786-
2787-
/* free message */
2788-
dlt_message_free(&msg, verbose);
2789-
2790-
return;
2791-
}
2792-
27932777
resp->connection_status[i] = con->status;
27942778
memcpy(&resp->node_id[i * DLT_ID_SIZE], con->ecuid, DLT_ID_SIZE);
27952779
}

src/daemon/dlt_daemon_common.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ DltDaemonContext *dlt_daemon_context_add(DltDaemon *daemon,
931931
return (DltDaemonContext *)NULL;
932932

933933
if (user_list->contexts == NULL) {
934-
user_list->contexts = (DltDaemonContext *)malloc(sizeof(DltDaemonContext) * DLT_DAEMON_CONTEXT_ALLOC_SIZE);
934+
user_list->contexts = (DltDaemonContext *)calloc(1, sizeof(DltDaemonContext) * DLT_DAEMON_CONTEXT_ALLOC_SIZE);
935935

936936
if (user_list->contexts == NULL)
937937
return (DltDaemonContext *)NULL;
@@ -953,7 +953,7 @@ DltDaemonContext *dlt_daemon_context_add(DltDaemon *daemon,
953953
if ((user_list->num_contexts % DLT_DAEMON_CONTEXT_ALLOC_SIZE) == 0) {
954954
/* allocate memory for context in steps of DLT_DAEMON_CONTEXT_ALLOC_SIZE, e.g 100 */
955955
old = user_list->contexts;
956-
user_list->contexts = (DltDaemonContext *)malloc((size_t) sizeof(DltDaemonContext) *
956+
user_list->contexts = (DltDaemonContext *)calloc(1, (size_t) sizeof(DltDaemonContext) *
957957
((user_list->num_contexts /
958958
DLT_DAEMON_CONTEXT_ALLOC_SIZE) + 1) *
959959
DLT_DAEMON_CONTEXT_ALLOC_SIZE);
@@ -1635,15 +1635,21 @@ void dlt_daemon_control_reset_to_factory_default(DltDaemon *daemon,
16351635
if (fd != NULL) {
16361636
/* Close and delete file */
16371637
fclose(fd);
1638-
unlink(filename);
1638+
if (unlink(filename) != 0) {
1639+
dlt_vlog(LOG_WARNING, "%s: unlink() failed: %s\n",
1640+
__func__, strerror(errno));
1641+
}
16391642
}
16401643

16411644
fd = fopen(filename1, "r");
16421645

16431646
if (fd != NULL) {
16441647
/* Close and delete file */
16451648
fclose(fd);
1646-
unlink(filename1);
1649+
if (unlink(filename1) != 0) {
1650+
dlt_vlog(LOG_WARNING, "%s: unlink() failed: %s\n",
1651+
__func__, strerror(errno));
1652+
}
16471653
}
16481654

16491655
daemon->default_log_level = (int8_t) InitialContextLogLevel;

src/daemon/dlt_daemon_socket.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,11 @@ int dlt_daemon_socket_get_send_qeue_max_size(int sock)
202202
{
203203
int n = 0;
204204
socklen_t m = sizeof(n);
205-
getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void *)&n, &m);
205+
if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void *)&n, &m) < 0) {
206+
dlt_vlog(LOG_ERR,
207+
"%s: socket get failed!\n", __func__);
208+
return -errno;
209+
}
206210

207211
return n;
208212
}
@@ -228,4 +232,3 @@ int dlt_daemon_socket_sendreliable(int sock, void *data_buffer, int message_size
228232

229233
return DLT_DAEMON_ERROR_OK;
230234
}
231-

src/daemon/dlt_daemon_unix_socket.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ int dlt_daemon_unix_socket_open(int *sock, char *sock_path, int type, int mask)
145145
addr.sun_family = AF_UNIX;
146146
memcpy(addr.sun_path, sock_path, sizeof(addr.sun_path));
147147

148-
unlink(sock_path);
148+
if (unlink(sock_path) != 0) {
149+
dlt_vlog(LOG_WARNING, "%s: unlink() failed: %s\n",
150+
__func__, strerror(errno));
151+
}
149152

150153
/* set appropriate access permissions */
151154
old_mask = umask(mask);

0 commit comments

Comments
 (0)