From d68c5feaee9b294d071c0825f5600e316c728708 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Mon, 3 Aug 2020 01:21:51 +0200 Subject: poller: Inline the poller into the server struct --- include/poller.h | 12 ++++++++---- include/server.h | 2 +- seatd/poller.c | 16 ++++------------ seatd/seatd.c | 2 +- seatd/server.c | 26 +++++++++----------------- 5 files changed, 23 insertions(+), 35 deletions(-) diff --git a/include/poller.h b/include/poller.h index b203181..6b727c4 100644 --- a/include/poller.h +++ b/include/poller.h @@ -122,13 +122,17 @@ struct poller { * Creates a poller with the best available polling backend. This poller must * be torn down with poller_destroy when it is no longer needed. */ -struct poller *poller_create(void); +/** + * Initializes the poller. The poller must be torn down with poller_finish when + * it is no longer needed. + */ +void poller_init(struct poller *poller); /** - * Destroys the poller. This destroys all remaining event sources, tears down - * the poller and frees the structure. + * De-initializes the poller. This destroys all remaining event sources and + * tears down the poller. */ -int poller_destroy(struct poller *poller); +int poller_finish(struct poller *poller); /** * Create an fd event source with the provided initial parameters. This event diff --git a/include/server.h b/include/server.h index 11de2c5..03bd9ca 100644 --- a/include/server.h +++ b/include/server.h @@ -10,7 +10,7 @@ struct client; struct server { bool running; - struct poller *poller; + struct poller poller; struct list seats; }; diff --git a/seatd/poller.c b/seatd/poller.c index 43a57b4..f786d47 100644 --- a/seatd/poller.c +++ b/seatd/poller.c @@ -10,28 +10,20 @@ #include "list.h" #include "poller.h" +/* Used for signal handling */ struct poller *global_poller = NULL; -struct poller *poller_create(void) { - if (global_poller != NULL) { - errno = EEXIST; - return NULL; - } +void poller_init(struct poller *poller) { + assert(global_poller == NULL); - struct poller *poller = calloc(1, sizeof(struct poller)); - if (poller == NULL) { - errno = ENOMEM; - return NULL; - } list_init(&poller->fds); list_init(&poller->new_fds); list_init(&poller->signals); list_init(&poller->new_signals); global_poller = poller; - return poller; } -int poller_destroy(struct poller *poller) { +int poller_finish(struct poller *poller) { for (size_t idx = 0; idx < poller->fds.length; idx++) { struct event_source_fd *bpfd = poller->fds.items[idx]; free(bpfd); diff --git a/seatd/seatd.c b/seatd/seatd.c index c1cdccf..0c83253 100644 --- a/seatd/seatd.c +++ b/seatd/seatd.c @@ -48,7 +48,7 @@ int main(int argc, char *argv[]) { log_info("seatd started"); while (server->running) { - if (poller_poll(server->poller) == -1) { + if (poller_poll(&server->poller) == -1) { log_errorf("poller failed: %s", strerror(errno)); return 1; } diff --git a/seatd/server.c b/seatd/server.c index 725ebf7..017aa39 100644 --- a/seatd/server.c +++ b/seatd/server.c @@ -27,23 +27,18 @@ static int server_handle_vt_rel(int signal, void *data); static int server_handle_kill(int signal, void *data); struct server *server_create(void) { - struct poller *poller = poller_create(); - if (poller == NULL) { - log_error("could not create poller"); - return NULL; - } struct server *server = calloc(1, sizeof(struct server)); if (server == NULL) { return NULL; } - server->poller = poller; + poller_init(&server->poller); list_init(&server->seats); - if (poller_add_signal(poller, SIGUSR1, server_handle_vt_rel, server) == NULL || - poller_add_signal(poller, SIGUSR2, server_handle_vt_acq, server) == NULL || - poller_add_signal(poller, SIGINT, server_handle_kill, server) == NULL || - poller_add_signal(poller, SIGTERM, server_handle_kill, server) == NULL) { + if (poller_add_signal(&server->poller, SIGUSR1, server_handle_vt_rel, server) == NULL || + poller_add_signal(&server->poller, SIGUSR2, server_handle_vt_acq, server) == NULL || + poller_add_signal(&server->poller, SIGINT, server_handle_kill, server) == NULL || + poller_add_signal(&server->poller, SIGTERM, server_handle_kill, server) == NULL) { server_destroy(server); return NULL; } @@ -69,10 +64,7 @@ void server_destroy(struct server *server) { seat_destroy(seat); } list_free(&server->seats); - if (server->poller != NULL) { - poller_destroy(server->poller); - server->poller = NULL; - } + poller_finish(&server->poller); free(server); } @@ -152,7 +144,7 @@ static int server_handle_connection(int fd, uint32_t mask, void *data) { } struct client *client = client_create(server, new_fd); - client->event_source = poller_add_fd(server->poller, new_fd, EVENT_READABLE, + client->event_source = poller_add_fd(&server->poller, new_fd, EVENT_READABLE, client_handle_connection, client); if (client->event_source == NULL) { client_destroy(client); @@ -174,7 +166,7 @@ int server_add_client(struct server *server, int fd) { struct client *client = client_create(server, fd); client->event_source = - poller_add_fd(server->poller, fd, EVENT_READABLE, client_handle_connection, client); + poller_add_fd(&server->poller, fd, EVENT_READABLE, client_handle_connection, client); if (client->event_source == NULL) { client_destroy(client); log_errorf("could not add client socket to poller: %s", strerror(errno)); @@ -222,7 +214,7 @@ int server_listen(struct server *server, const char *path) { } else { log_errorf("could not get video group: %s", strerror(errno)); } - if (poller_add_fd(server->poller, fd, EVENT_READABLE, server_handle_connection, server) == + if (poller_add_fd(&server->poller, fd, EVENT_READABLE, server_handle_connection, server) == NULL) { log_errorf("could not add socket to poller: %s", strerror(errno)); close(fd); -- cgit v1.2.3