diff --git a/cmarketcap.c b/cmarketcap.c index 26a2867..b43c01c 100644 --- a/cmarketcap.c +++ b/cmarketcap.c @@ -15,7 +15,7 @@ pthread_mutex_t shift_column_locker = PTHREAD_MUTEX_INITIALIZER; #define LOCK_SHIFT_COLUMN_LOCKER pthread_mutex_lock(&shift_column_locker) #define UNLOCK_SHIFT_COLUMN_LOCKER pthread_mutex_unlock(&shift_column_locker) -static size_t number_of_coins = 0; +struct global_data_handle global_data_handle; /* @brief coin_history column map */ char *col_names[] = { @@ -102,6 +102,7 @@ int main(int argc, char *argv[]) pthread_mutex_init(&sql_db_access, NULL); pthread_mutex_init(&shift_column_locker, NULL); + global_data_handle.httpd_sockfd = 0; prog_name = *argv; diff --git a/cmarketcap.h b/cmarketcap.h index 6ec3b38..7082e90 100644 --- a/cmarketcap.h +++ b/cmarketcap.h @@ -3,7 +3,12 @@ #include +struct global_data_handle { + int httpd_sockfd; +}; + extern char *prog_name; extern pthread_mutex_t shift_column_locker; +extern struct global_data_handle global_data_handle; #endif diff --git a/httpd.c b/httpd.c index ad419af..4d12446 100644 --- a/httpd.c +++ b/httpd.c @@ -385,7 +385,6 @@ int write_to_client(int sockfd) /* @brief needs openned database */ int __cb_main_thread(sqlite3 *db) { - int httpd_sockfd; int httpd_port; int sockfd; @@ -401,8 +400,8 @@ int __cb_main_thread(sqlite3 *db) httpd_port = 1040; /* init server socket (ipv4, tcp) */ - httpd_sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (httpd_sockfd < 0) { + global_data_handle.httpd_sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (global_data_handle.httpd_sockfd < 0) { CM_ERROR("httpd socket failed to create\n"); exit(1); } @@ -412,24 +411,24 @@ int __cb_main_thread(sqlite3 *db) httpd_sockaddr.sin_port = htons(httpd_port); httpd_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY); - if (bind(httpd_sockfd, (struct sockaddr *)&httpd_sockaddr, + if (bind(global_data_handle.httpd_sockfd, (struct sockaddr *)&httpd_sockaddr, sizeof(httpd_sockaddr)) == -1) { - close(httpd_sockfd); + close(global_data_handle.httpd_sockfd); CM_ERROR("bind failed\n"); exit(1); } - if (listen(httpd_sockfd, CLIENT_MAX) == -1) { - close(httpd_sockfd); + if (listen(global_data_handle.httpd_sockfd, CLIENT_MAX) == -1) { + close(global_data_handle.httpd_sockfd); CM_ERROR("listen failed\n"); exit(1); } - /* at this stage 'httpd_sockfd', 'httpd_sockaddr', 'httpd_port', 'clilen' + /* at this stage 'global_data_handle.httpd_sockfd', 'httpd_sockaddr', 'httpd_port', 'clilen' * settled up */ while (1) { struct __cb_args *cb_arg = NULL; - sockfd = accept(httpd_sockfd, (struct sockaddr *)&client_sockaddr, &clilen); + sockfd = accept(global_data_handle.httpd_sockfd, (struct sockaddr *)&client_sockaddr, &clilen); if (sockfd == -1) { CM_ERROR("Connection accept error\n"); continue; @@ -450,7 +449,7 @@ int __cb_main_thread(sqlite3 *db) } } - close(httpd_sockfd); + close(global_data_handle.httpd_sockfd); //pthread_exit(NULL); return 0; diff --git a/signal_handler.c b/signal_handler.c index edafce0..f73405f 100644 --- a/signal_handler.c +++ b/signal_handler.c @@ -4,6 +4,7 @@ #include // alarm() #include "signal_handler.h" +#include "cmarketcap.h" void handle_signal(int signal) { @@ -20,6 +21,7 @@ void handle_signal(int signal) break; case SIGINT: printf("Caught SIGINT, exiting now\n"); + close(global_data_handle.httpd_sockfd); exit(0); default: fprintf(stderr, "Caught wrong signal: %d\n", signal);