diff --git a/src/client.c b/src/client.c index 6be858d..b48e624 100644 --- a/src/client.c +++ b/src/client.c @@ -26,6 +26,10 @@ char *client_addr_str, *client_addr_str_ptr, *server_addr_str, *server_addr_str_ptr, *log_base_prefix, *log_req_prefix; +void client_terminate() { + // TODO prevent processing of further requests in connection +} + int client_websocket_handler() { // TODO implement client_websocket_handler return 0; @@ -43,7 +47,10 @@ int client_connection_handler(int client) { sprintf(buf1, "Hello World!\nYour address is: %s\n", client_addr_str); send(client, buf1, strlen(buf1), 0); - int len = recv(client, &buf1, sizeof(buf1), 0); + int len = -1; + while (len == -1) { + len = recv(client, &buf1, sizeof(buf1), 0); + } sprintf(buf2, "Thank you, %.*s!\nGood bye!\n", len, buf1); send(client, buf2, strlen(buf2), 0); @@ -59,6 +66,9 @@ int client_handler(int client, long client_num, struct sockaddr_in6 *client_addr char *color_table[] = {"\x1B[31m", "\x1B[32m", "\x1B[33m", "\x1B[34m", "\x1B[35m", "\x1B[36m"}; + signal(SIGINT, client_terminate); + signal(SIGTERM, client_terminate); + client_addr_str_ptr = malloc(INET6_ADDRSTRLEN); inet_ntop(client_addr->sin6_family, (void *) &client_addr->sin6_addr, client_addr_str_ptr, INET6_ADDRSTRLEN); if (strncmp(client_addr_str_ptr, "::ffff:", 7) == 0) { diff --git a/src/necronda-server.c b/src/necronda-server.c index 8b3f0c4..faa6958 100644 --- a/src/necronda-server.c +++ b/src/necronda-server.c @@ -52,8 +52,8 @@ void terminate() { fprintf(stderr, "\nTerminating gracefully...\n"); active = 0; - signal(SIGTERM, destroy); signal(SIGINT, destroy); + signal(SIGTERM, destroy); for (int i = 0; i < NUM_SOCKETS; i++) { shutdown(SOCKETS[i], SHUT_RDWR); @@ -62,6 +62,19 @@ void terminate() { int status = 0; int ret; + for (int i = 0; i < MAX_CHILDREN; i++) { + if (CHILDREN[i] != 0) { + ret = waitpid(CHILDREN[i], &status, WNOHANG); + if (ret < 0) { + fprintf(stderr, ERR_STR "Unable to wait for child process (PID %i): %s" CLR_STR "\n", CHILDREN[i], strerror(errno)); + } else if (ret == CHILDREN[i]) { + CHILDREN[i] = 0; + } else { + kill(CHILDREN[i], SIGTERM); + } + } + } + for (int i = 0; i < MAX_CHILDREN; i++) { if (CHILDREN[i] != 0) { ret = waitpid(CHILDREN[i], &status, 0);