Don't exit from the cleanup function

This commit is contained in:
Igor Socec 2016-11-27 23:52:18 +01:00
parent 36f969abb7
commit 8725d1c1f1

View file

@ -13,12 +13,16 @@
#define CONFILE "moxanix.cfg" #define CONFILE "moxanix.cfg"
/* Global variables used throughout the application. */ /* ========================================================================== */
/* global resources */
server_t server; /* main server */ server_t server; /* main server */
client_t client; /* connected client */ client_t client; /* connected client */
client_t new_client; /* reserved for a new client request */ client_t new_client; /* reserved for a new client request */
tty_t tty_dev; /* connected tty device */ tty_t tty_dev; /* connected tty device */
/* ========================================================================== */
/* Prints the help message. */ /* Prints the help message. */
static void usage() static void usage()
{ {
@ -29,11 +33,13 @@ static void usage()
fprintf(stdout, "\n"); fprintf(stdout, "\n");
} }
/* Performs cleanup and exit. */ /* Performs resource cleanup. */
void cleanup(int exit_code) void cleanup()
{ {
// TODO: maybe pthread_kill() should be used for threads and cleanup there fprintf(stderr, "[%s] performing cleanup\n", NAME);
fprintf(stderr, "[%s] cleanup and exit with %d\n", NAME, exit_code);
// TODO: maybe pthread_kill() should be used for thread cleanup?
/* close the client */ /* close the client */
if (client.socket != -1) if (client.socket != -1)
{ {
@ -46,8 +52,6 @@ void cleanup(int exit_code)
} }
/* close the server */ /* close the server */
server_close(&server); server_close(&server);
/* exit */
exit(exit_code);
} }
/* Handles received quit signals, use it for all quit signals of interest. */ /* Handles received quit signals, use it for all quit signals of interest. */
@ -55,7 +59,7 @@ void quit_handler(int signum)
{ {
/* perform cleanup and exit with 0 */ /* perform cleanup and exit with 0 */
fprintf(stderr, "[%s] received signal %d\n", NAME, signum); fprintf(stderr, "[%s] received signal %d\n", NAME, signum);
cleanup(0); exit(0);
} }
/* Parse handler function, used to configure serial port */ /* Parse handler function, used to configure serial port */
@ -194,10 +198,8 @@ int main(int argc, char *argv[])
/* start thread function (in this thread) that handles client data */ /* start thread function (in this thread) that handles client data */
thread_client_data(&r); thread_client_data(&r);
/* unexpected break from client data loop */ /* unexpected break from client data loop, cleanup and exit with -1 */
fprintf(stderr, "[%s] unexpected condition\n", NAME); fprintf(stderr, "[%s] unexpected condition\n", NAME);
/* cleanup and exit with -1 */ cleanup();
cleanup(-1);
return -1; return -1;
} }