Close all connections on exit
This commit is contained in:
@ -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 <openssl/evp.h>
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <signal.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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)
|
||||
|
@ -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);
|
||||
|
@ -14,7 +14,6 @@
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#include <poll.h>
|
||||
|
||||
|
||||
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;
|
||||
|
28
src/server.c
28
src/server.c
@ -24,7 +24,6 @@
|
||||
#include <sys/socket.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <poll.h>
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/types.h>
|
||||
@ -34,7 +33,6 @@
|
||||
#include <openssl/conf.h>
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -11,8 +11,6 @@
|
||||
|
||||
#include "worker/func.h"
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
#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
|
||||
|
@ -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);
|
||||
|
||||
|
Reference in New Issue
Block a user