Skip to content

Commit bfe6ffb

Browse files
committed
Improve remote shell loop error handling
1 parent 270c784 commit bfe6ffb

2 files changed

Lines changed: 30 additions & 18 deletions

File tree

‎bundles/shell/remote_shell/src/connection_listener.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ celix_status_t connectionListener_create(remote_shell_pt remoteShell, int port,
8383
celix_status_t connectionListener_start(connection_listener_pt instance) {
8484
celix_status_t status = CELIX_SUCCESS;
8585
celixThreadMutex_lock(&instance->mutex);
86-
celixThread_create(&instance->thread, NULL, connection_listener_thread, instance);
86+
status = celixThread_create(&instance->thread, NULL, connection_listener_thread, instance);
8787
celixThreadMutex_unlock(&instance->mutex);
8888
return status;
8989
}
@@ -138,7 +138,11 @@ static void* connection_listener_thread(void *data) {
138138
char portStr[10];
139139
snprintf(&portStr[0], 10, "%d", instance->port);
140140

141-
getaddrinfo(NULL, portStr, &hints, &result);
141+
int gaiResult = getaddrinfo(NULL, portStr, &hints, &result);
142+
if (gaiResult != 0) {
143+
celix_logHelper_log(*instance->loghelper, CELIX_LOG_LEVEL_ERROR, "getaddrinfo failed: %s", gai_strerror(gaiResult));
144+
return NULL;
145+
}
142146

143147
for (rp = result; rp != NULL && status == CELIX_BUNDLE_EXCEPTION; rp = rp->ai_next) {
144148

‎bundles/shell/remote_shell/src/remote_shell.c‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -175,24 +175,32 @@ void *remoteShell_connection_run(void *data) {
175175
char buff[COMMAND_BUFF_SIZE];
176176

177177
len = recv(fd, buff, COMMAND_BUFF_SIZE - 1, 0);
178-
if (len < COMMAND_BUFF_SIZE) {
179-
celix_status_t commandStatus = CELIX_SUCCESS;
180-
buff[len] = '\0';
181-
182-
commandStatus = remoteShell_connection_execute(connection, buff);
183-
184-
if (commandStatus == CELIX_SUCCESS) {
185-
remoteShell_connection_print(connection, RS_PROMPT);
186-
} else if (commandStatus == CELIX_FILE_IO_EXCEPTION) {
187-
//exit command
188-
break;
189-
} else { //error
190-
remoteShell_connection_print(connection, RS_ERROR);
191-
remoteShell_connection_print(connection, RS_PROMPT);
178+
if (len > 0) {
179+
if (len < COMMAND_BUFF_SIZE) {
180+
celix_status_t commandStatus = CELIX_SUCCESS;
181+
buff[len] = '\0';
182+
183+
commandStatus = remoteShell_connection_execute(connection, buff);
184+
185+
if (commandStatus == CELIX_SUCCESS) {
186+
remoteShell_connection_print(connection, RS_PROMPT);
187+
} else if (commandStatus == CELIX_FILE_IO_EXCEPTION) {
188+
//exit command
189+
break;
190+
} else { //error
191+
remoteShell_connection_print(connection, RS_ERROR);
192+
remoteShell_connection_print(connection, RS_PROMPT);
193+
}
194+
195+
} else {
196+
celix_logHelper_log(*connection->parent->loghelper, CELIX_LOG_LEVEL_ERROR, "REMOTE_SHELL: Error while retrieving data");
192197
}
193-
198+
} else if (len == 0) {
199+
celix_logHelper_log(*connection->parent->loghelper, CELIX_LOG_LEVEL_INFO, "REMOTE_SHELL: Connection closed by peer");
200+
break;
194201
} else {
195-
celix_logHelper_log(*connection->parent->loghelper, CELIX_LOG_LEVEL_ERROR, "REMOTE_SHELL: Error while retrieving data");
202+
celix_logHelper_log(*connection->parent->loghelper, CELIX_LOG_LEVEL_ERROR, "REMOTE_SHELL: recv() failed: %s", strerror(errno));
203+
break;
196204
}
197205
}
198206
}

0 commit comments

Comments
 (0)