diff --git a/src/async.c b/src/async.c new file mode 100644 index 0000000..4ad65b7 --- /dev/null +++ b/src/async.c @@ -0,0 +1,70 @@ + + +#include "logger.h" + +#include +#include +#include +#include + + +typedef struct { + int fd; + short events; + int flags; + void (*cb)(void *); + void *arg; + void (*err_cb)(void *); + void *err_arg; +} evt_listen_t; + +typedef struct { + int n; + evt_listen_t q[256]; +} listen_queue_t; + + +static listen_queue_t listen1; +static listen_queue_t listen2; +listen_queue_t *listen = &listen1; + +volatile sig_atomic_t alive = 1; + +int async(int fd, int events, int flags, void (*cb)(void *), void *arg, void (*err_cb)(void *), void *err_arg) { + return -1; +} + +void async_thread(void) { + + int num_fds = 0; + struct pollfd fds[256]; + + // main event loop + while (alive) { + // swap listen queue + listen_queue_t *l = listen; + listen = (listen == &listen1) ? &listen2 : &listen1; + + // fill fds with newly added + for (int i = 0; i < l->n; i++, num_fds++) { + fds[num_fds].fd = l->q[i].fd; + fds[num_fds].events = l->q[i].events; + } + + int ready_fds = poll(fds, num_fds, -1); + if (ready_fds < 0) { + if (errno == EINTR) { + // interrupt + continue; + } else { + // other error + critical("Unable to poll for events"); + return; + } + } + + for (int i = 0; i < num_fds; i++) { + // TODO + } + } +} diff --git a/src/async.h b/src/async.h new file mode 100644 index 0000000..136076b --- /dev/null +++ b/src/async.h @@ -0,0 +1,13 @@ + + +#ifndef SESIMOS_ASYNC_H +#define SESIMOS_ASYNC_H + +#define async_read(fd, cb, arg, err_cb, err_arg) async(fd, 0, 0, cb, arg, err, err_arg) +#define async_read_keep(fd, cb, arg, err_cb, err_arg) async(fd, 0, 0, cb, arg, err, err_arg) + +int async(int fd, int events, int flags, void (*cb)(void *), void *arg, void (*err_cb)(void *), void *err_arg); + +void async_thread(void); + +#endif //SESIMOS_ASYNC_H diff --git a/src/client.c b/src/client.c index a75c183..e99730c 100644 --- a/src/client.c +++ b/src/client.c @@ -34,6 +34,8 @@ volatile sig_atomic_t server_keep_alive = 1; struct timeval client_timeout = {.tv_sec = CLIENT_TIMEOUT, .tv_usec = 0}; +static const char *color_table[] = {"\x1B[31m", "\x1B[32m", "\x1B[33m", "\x1B[34m", "\x1B[35m", "\x1B[36m"}; + host_config *get_host_config(const char *host) { for (int i = 0; i < CONFIG_MAX_HOST_CONFIG; i++) { host_config *hc = &config->hosts[i]; @@ -814,8 +816,6 @@ int client_handler(sock *client, unsigned long client_num) { client_ctx_t ctx; char log_client_prefix[256], log_conn_prefix[512]; - char *color_table[] = {"\x1B[31m", "\x1B[32m", "\x1B[33m", "\x1B[34m", "\x1B[35m", "\x1B[36m"}; - signal(SIGINT, client_terminate); signal(SIGTERM, client_terminate); diff --git a/src/server.c b/src/server.c index 347bed0..886db8d 100644 --- a/src/server.c +++ b/src/server.c @@ -49,8 +49,10 @@ static int ssl_servername_cb(SSL *ssl, int *ad, void *arg) { return SSL_TLSEXT_ERR_OK; } -void destroy(int sig) { - critical("Terminating forcefully!"); +void terminate_forcefully(int sig) { + fprintf(stderr, "\n"); + notice("Terminating forcefully!"); + int status = 0; int ret; int kills = 0; @@ -71,20 +73,20 @@ void destroy(int sig) { } } if (kills > 0) { - critical("Killed %i child process(es)", kills); + notice("Killed %i child process(es)", kills); } cache_unload(); config_unload(); exit(2); } -void terminate(int sig) { +void terminate_gracefully(int sig) { fprintf(stderr, "\n"); notice("Terminating gracefully..."); - active = 0; - signal(SIGINT, destroy); - signal(SIGTERM, destroy); + active = 0; + signal(SIGINT, terminate_forcefully); + signal(SIGTERM, terminate_forcefully); for (int i = 0; i < NUM_SOCKETS; i++) { shutdown(sockets[i], SHUT_RDWR); @@ -231,8 +233,8 @@ int main(int argc, const char *argv[]) { return 1; } - signal(SIGINT, terminate); - signal(SIGTERM, terminate); + signal(SIGINT, terminate_gracefully); + signal(SIGTERM, terminate_gracefully); if ((ret = geoip_init(geoip_dir)) != 0) { if (ret == -1) { @@ -306,7 +308,7 @@ int main(int argc, const char *argv[]) { ready_sockets_num = poll(poll_fds, NUM_SOCKETS, 1000); if (ready_sockets_num < 0) { critical("Unable to poll sockets"); - terminate(0); + terminate_gracefully(0); return 1; }