From: Simon Ruderich Date: Sun, 27 Feb 2011 13:36:12 +0000 (+0100) Subject: tlsproxy.c: Free resources when receiving SIGINT. X-Git-Tag: 0.1~40 X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=b2692bc5abacf503e4f67c447d52511cc2cd26a8;hp=1f5acb94b4dfa529d950099c7c1937340f81a274;p=tlsproxy%2Ftlsproxy.git tlsproxy.c: Free resources when receiving SIGINT. --- diff --git a/tlsproxy.c b/tlsproxy.c index 2d16465..157633b 100644 --- a/tlsproxy.c +++ b/tlsproxy.c @@ -30,6 +30,8 @@ #include /* strncmp() */ #include +/* sigaction() */ +#include /* poll() */ #include @@ -40,10 +42,16 @@ #define MAX_REQUEST_LINE 4096 +/* Server should shut down. Set by SIGINT handler. */ +static volatile int done; + /* Proxy hostname and port if specified on the command line. */ static char *use_proxy_host; static char *use_proxy_port; + +static void sigint_handler(int signal); + static void parse_arguments(int argc, char **argv); static void print_usage(const char *argv); @@ -66,6 +74,8 @@ int main(int argc, char **argv) { int client_socket, server_socket; struct sockaddr_in6 server_in; + struct sigaction action; + parse_arguments(argc, argv); port = atoi(argv[argc - 1]); @@ -75,6 +85,13 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } + /* Setup our SIGINT signal handler which allows a "normal" termination of + * the server. */ + sigemptyset(&action.sa_mask); + action.sa_handler = sigint_handler; + action.sa_flags = 0; + sigaction(SIGINT, &action, NULL); + server_socket = socket(PF_INET6, SOCK_STREAM, 0); if (-1 == server_socket) { perror("socket()"); @@ -114,18 +131,29 @@ int main(int argc, char **argv) { } #endif - for (;;) { + while (!done) { /* Accept new connection. */ client_socket = accept(server_socket, NULL, NULL); if (-1 == client_socket) { perror("accept()"); - return EXIT_FAILURE; + break; } handle_connection(client_socket); } - return EXIT_SUCCESS; + close(server_socket); + + free(use_proxy_host); + free(use_proxy_port); + + return EXIT_FAILURE; +} + +static void sigint_handler(int signal_number) { + (void)signal_number; + + done = 1; } static void parse_arguments(int argc, char **argv) {