diff options
-rw-r--r-- | include/server.h | 6 | ||||
-rw-r--r-- | libseat/backend/seatd.c | 27 | ||||
-rw-r--r-- | seatd/seatd.c | 14 | ||||
-rw-r--r-- | seatd/server.c | 19 |
4 files changed, 33 insertions, 33 deletions
diff --git a/include/server.h b/include/server.h index 03bd9ca..f280993 100644 --- a/include/server.h +++ b/include/server.h @@ -4,8 +4,8 @@ #include <stdbool.h> #include "list.h" +#include "poller.h" -struct poller; struct client; struct server { @@ -15,8 +15,8 @@ struct server { struct list seats; }; -struct server *server_create(void); -void server_destroy(struct server *server); +int server_init(struct server *server); +void server_finish(struct server *server); struct seat *server_get_seat(struct server *server, const char *seat_name); diff --git a/libseat/backend/seatd.c b/libseat/backend/seatd.c index 1b1523e..3edecfc 100644 --- a/libseat/backend/seatd.c +++ b/libseat/backend/seatd.c @@ -530,23 +530,28 @@ static struct libseat *builtin_open_seat(struct libseat_seat_listener *listener, return NULL; } else if (pid == 0) { int fd = fds[0]; - struct server *server = server_create(); - if (server == NULL) { - close(fd); - exit(1); + int res = 0; + struct server server = {0}; + if (server_init(&server) == -1) { + res = 1; + goto error; } - if (server_add_client(server, fd) == -1) { - exit(1); + if (server_add_client(&server, fd) == -1) { + res = 1; + goto server_error; } set_deathsig(SIGTERM); - while (server->running) { - if (poller_poll(server->poller) == -1) { - exit(1); + while (server.running) { + if (poller_poll(&server.poller) == -1) { + res = 1; + goto server_error; } } - server_destroy(server); + server_error: + server_finish(&server); + error: close(fd); - exit(0); + exit(res); } else { int fd = fds[1]; return _open_seat(listener, data, fd); diff --git a/seatd/seatd.c b/seatd/seatd.c index 0c83253..d0ff7e1 100644 --- a/seatd/seatd.c +++ b/seatd/seatd.c @@ -29,8 +29,8 @@ int main(int argc, char *argv[]) { } libseat_log_init(level); - struct server *server = server_create(); - if (server == NULL) { + struct server server = {0}; + if (server_init(&server) == -1) { log_errorf("server_create failed: %s", strerror(errno)); return 1; } @@ -39,22 +39,22 @@ int main(int argc, char *argv[]) { path = "/run/seatd.sock"; } - if (server_listen(server, path) == -1) { + if (server_listen(&server, path) == -1) { log_errorf("server_listen failed: %s", strerror(errno)); - server_destroy(server); + server_finish(&server); return 1; } log_info("seatd started"); - while (server->running) { - if (poller_poll(&server->poller) == -1) { + while (server.running) { + if (poller_poll(&server.poller) == -1) { log_errorf("poller failed: %s", strerror(errno)); return 1; } } - server_destroy(server); + server_finish(&server); unlink(path); return 0; } diff --git a/seatd/server.c b/seatd/server.c index 017aa39..354d6a5 100644 --- a/seatd/server.c +++ b/seatd/server.c @@ -26,11 +26,7 @@ static int server_handle_vt_acq(int signal, void *data); 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 server *server = calloc(1, sizeof(struct server)); - if (server == NULL) { - return NULL; - } +int server_init(struct server *server) { poller_init(&server->poller); list_init(&server->seats); @@ -39,8 +35,8 @@ struct server *server_create(void) { 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; + server_finish(server); + return -1; } char *vtenv = getenv("SEATD_VTBOUND"); @@ -48,16 +44,16 @@ struct server *server_create(void) { // TODO: create more seats: struct seat *seat = seat_create("seat0", vtenv == NULL || strcmp(vtenv, "1") == 0); if (seat == NULL) { - server_destroy(server); - return NULL; + server_finish(server); + return -1; } list_add(&server->seats, seat); server->running = true; - return server; + return 0; } -void server_destroy(struct server *server) { +void server_finish(struct server *server) { assert(server); for (size_t idx = 0; idx < server->seats.length; idx++) { struct seat *seat = server->seats.items[idx]; @@ -65,7 +61,6 @@ void server_destroy(struct server *server) { } list_free(&server->seats); poller_finish(&server->poller); - free(server); } struct seat *server_get_seat(struct server *server, const char *seat_name) { |