diff options
author | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2024-06-19 18:31:01 +0200 |
---|---|---|
committer | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2024-06-19 18:50:31 +0200 |
commit | 6490c179e7f789a9efb876fa0bdfc195dc374dc5 (patch) | |
tree | bf59dba04da15e40f6489650aef589a04cafd7a3 /src | |
parent | 49948b4cc0f73d02a8932c525690a35e8efb6ac5 (diff) |
handle signals
Signed-off-by: Lizzy Fleckenstein <lizzy@vlhl.dev>
Diffstat (limited to 'src')
-rw-r--r-- | src/client.c | 8 | ||||
-rw-r--r-- | src/server.c | 8 | ||||
-rw-r--r-- | src/sig.c | 23 |
3 files changed, 37 insertions, 2 deletions
diff --git a/src/client.c b/src/client.c index 0dabbbf..3bf3649 100644 --- a/src/client.c +++ b/src/client.c @@ -9,6 +9,7 @@ #include "ser.h" #include "net.h" #include "ticker.h" +#include "sig.h" typedef struct { peer conn; @@ -59,6 +60,8 @@ bool handle_pkt(client *c, str pkt) int main() { + signal_setup(); + client c = {0}; c.name = str_clone(S("kitten")); @@ -94,11 +97,14 @@ int main() if (poll(fds, 2, ticker_timeout(&t)) < 0) { switch (errno) { - case EINTR: continue; + case EINTR: break; default: perror("poll"); continue; } } + if (signal_stop) + client_exit(&c, EXIT_SUCCESS); + if (fds[0].revents) { // this is cursed // maybe dont do this idk diff --git a/src/server.c b/src/server.c index 621acae..5355620 100644 --- a/src/server.c +++ b/src/server.c @@ -19,6 +19,7 @@ #include "net.h" #include "str.h" #include "ser.h" +#include "sig.h" typedef enum { MAP_VALLEY, @@ -318,6 +319,8 @@ bool handle_pkt(str pkt, player *p, game *g) int main() { + signal_setup(); + game g = {0}; g.accept_fd = -1; @@ -364,11 +367,14 @@ int main() if (poll(fds, g.players.len + 1, -1) < 0) { switch (errno) { - case EINTR: continue; + case EINTR: break; default: perror("poll"); continue; } } + if (signal_stop) + game_exit(&g, EXIT_SUCCESS); + for (size_t i = 0; i < g.players.len; i++) { player *p = &g.players.data[i]; diff --git a/src/sig.c b/src/sig.c new file mode 100644 index 0000000..9830f20 --- /dev/null +++ b/src/sig.c @@ -0,0 +1,23 @@ +// SPDX-FileCopyrightText: 2024 Lizzy Fleckenstein <lizzy@vlhl.dev> +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +#include <signal.h> +#include <stddef.h> +#include "sig.h" + +bool signal_stop = false; + +static void handle_signal(int sig) +{ + (void) sig; + signal_stop = true; +} + +void signal_setup() +{ + struct sigaction sa; + sa.sa_handler = &handle_signal; + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); +} |