diff --git a/src/cache_handler.c b/src/cache_handler.c index ff5906d..98b1e1e 100644 --- a/src/cache_handler.c +++ b/src/cache_handler.c @@ -6,7 +6,6 @@ * @date 2020-12-19 */ -#include "server.h" #include "logger.h" #include "cache_handler.h" #include "lib/utils.h" @@ -20,7 +19,9 @@ #include #include #include +#include #include +#include #define CACHE_BUF_SIZE 16 @@ -28,6 +29,7 @@ static magic_t magic; static pthread_t thread; static sem_t sem_free, sem_used, sem_lock; +volatile sig_atomic_t alive = 1; typedef struct { int rd; @@ -203,7 +205,7 @@ static void cache_process_entry(cache_entry_t *entry) { static void *cache_thread(void *arg) { logger_set_name("cache"); - while (server_alive) { + while (alive) { pthread_testcancel(); if (sem_wait(&sem_used) != 0) { if (errno == EINTR) { @@ -280,6 +282,11 @@ int cache_init(void) { return 0; } +void cache_stop(void) { + alive = 0; + pthread_kill(thread, SIGUSR1); +} + int cache_join(void) { return pthread_join(thread, NULL); } diff --git a/src/cache_handler.h b/src/cache_handler.h index 1489607..8d5e0a8 100644 --- a/src/cache_handler.h +++ b/src/cache_handler.h @@ -35,6 +35,8 @@ typedef struct { int cache_init(void); +void cache_stop(void); + int cache_join(void); void cache_mark_dirty(cache_t *cache, const char *filename); diff --git a/src/lib/proxy.c b/src/lib/proxy.c index f07c20e..f45cea1 100644 --- a/src/lib/proxy.c +++ b/src/lib/proxy.c @@ -46,6 +46,22 @@ void proxy_unload(void) { free(proxies); } +void proxy_close_all(void) { + int n = 0; + for (int i = 0; i < CONFIG_MAX_HOST_CONFIG; i++) { + host_config_t *hc = &config.hosts[i]; + if (hc->type == CONFIG_TYPE_UNSET) break; + if (hc->type != CONFIG_TYPE_REVERSE_PROXY) continue; + n++; + } + + proxy_ctx_t *ptr = proxies; + for (int i = 0; i < MAX_PROXY_CNX_PER_HOST * n; i++, ptr++) { + if (ptr->initialized) + proxy_close(ptr); + } +} + static proxy_ctx_t *proxy_get_by_conf(host_config_t *conf) { // TODO locking void *proxies int n = 0; @@ -225,6 +241,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu *proxy_ptr = proxy_get_by_conf(conf); proxy_ctx_t *proxy = *proxy_ptr; + proxy->client = NULL; proxy->in_use = 1; if (proxy->initialized && sock_has_pending(&proxy->proxy) == 0) diff --git a/src/lib/proxy.h b/src/lib/proxy.h index 1eec7d5..0ad35c3 100644 --- a/src/lib/proxy.h +++ b/src/lib/proxy.h @@ -25,12 +25,15 @@ typedef struct { unsigned char initialized:1, in_use:1; sock proxy; char *host; + void *client; } proxy_ctx_t; int proxy_preload(void); void proxy_unload(void); +void proxy_close_all(void); + int proxy_request_header(http_req *req, sock *sock); int proxy_response_header(http_req *req, http_res *res, host_config_t *conf); diff --git a/src/lib/sock.c b/src/lib/sock.c index bd6124a..3b864dd 100644 --- a/src/lib/sock.c +++ b/src/lib/sock.c @@ -14,7 +14,6 @@ #include #include #include -#include int sock_enc_error(sock *s) { @@ -142,7 +141,7 @@ long sock_splice_chunked(sock *dst, sock *src, void *buf, unsigned long buf_len) int sock_close(sock *s) { int e = errno; - if ((int) s->enc && s->ssl != NULL) { + if (s->enc && s->ssl != NULL) { if (s->_last_ret >= 0) SSL_shutdown(s->ssl); SSL_free(s->ssl); s->ssl = NULL; diff --git a/src/server.c b/src/server.c index f2395cd..49a531c 100644 --- a/src/server.c +++ b/src/server.c @@ -24,7 +24,6 @@ #include #include #include -#include #include #include #include @@ -34,7 +33,6 @@ #include -volatile sig_atomic_t server_alive = 1; const char *config_file; static int sockets[NUM_SOCKETS]; @@ -124,25 +122,26 @@ static void terminate_gracefully(int sig) { fprintf(stderr, "\n"); notice("Terminating gracefully..."); - server_alive = 0; struct sigaction act = {0}; act.sa_handler = terminate_forcefully; sigaction(SIGINT, &act, NULL); sigaction(SIGTERM, &act, NULL); - // TODO close client connections - // TODO close proxy connections - - workers_stop(); - workers_destroy(); - for (int i = 0; i < NUM_SOCKETS; i++) { close(sockets[i]); } - notice("Goodbye"); - geoip_free(); - exit(0); + cache_stop(); + workers_stop(); + workers_destroy(); + + for (int i = 0; i < list_size(clients); i++) { + tcp_close(clients[i]); + } + proxy_close_all(); + logger_set_prefix(""); + + async_stop(); } static void nothing(int sig) {} @@ -304,12 +303,13 @@ int main(int argc, char *const argv[]) { async_thread(); - warning("Async thread finished"); - notice("Goodbye?"); + notice("Goodbye!"); // cleanup + list_free(clients); geoip_free(); proxy_unload(); + cache_join(); async_free(); return 0; } diff --git a/src/server.h b/src/server.h index fe7a104..e9fcc80 100644 --- a/src/server.h +++ b/src/server.h @@ -11,8 +11,6 @@ #include "worker/func.h" -#include - #define NUM_SOCKETS 2 #define LISTEN_BACKLOG 16 #define REQ_PER_CONNECTION 200 @@ -23,8 +21,6 @@ #define CNX_HANDLER_WORKERS 8 #define REQ_HANDLER_WORKERS 16 -extern volatile sig_atomic_t server_alive; - void server_free_client(client_ctx_t *ctx); #endif //SESIMOS_SERVER_H diff --git a/src/worker/proxy_handler.c b/src/worker/proxy_handler.c index f245045..9dd9774 100644 --- a/src/worker/proxy_handler.c +++ b/src/worker/proxy_handler.c @@ -52,6 +52,7 @@ static int proxy_handler_1(client_ctx_t *ctx) { http_remove_header_field(&res->hdr, "Server", HTTP_REMOVE_ALL); ctx->use_proxy = proxy_init(&ctx->proxy, &ctx->req, res, status, ctx->conf, &ctx->socket, &ctx->custom_status, ctx->err_msg) == 0; + ctx->proxy->client = ctx; if (res->status->code == 101) { const char *connection = http_get_header_field(&res->hdr, "Connection"); @@ -147,6 +148,13 @@ static int proxy_handler_2(client_ctx_t *ctx) { } void proxy_close(proxy_ctx_t *ctx) { + client_ctx_t *cctx = ctx->client; + if (cctx) { + logger_set_prefix("[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, cctx->req_host, CLR_STR, cctx->log_prefix); + } else { + logger_set_prefix(""); + } + info(BLUE_STR "Closing proxy connection"); sock_close(&ctx->proxy);