From e0311dff404859be4c3c3b2f9e1ee5a74995f5ea Mon Sep 17 00:00:00 2001 From: Tobias Girstmair Date: Mon, 20 May 2024 17:40:21 +0200 Subject: [PATCH] implement handler for the usual terminating signals right now, we just send /QUIT and cleanly shut down the socket. in the future, this allows us to do something to the socket on SIGUSR{1,2}, for example. --- ircpipe.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/ircpipe.c b/ircpipe.c index 89b2b01..40db358 100644 --- a/ircpipe.c +++ b/ircpipe.c @@ -1,7 +1,8 @@ -#define _POSIX_C_SOURCE 200112L /* getopt(>=2), strtok_r(*), getaddrinfo(>=200112L), clock_gettime(>=199309L) */ +#define _POSIX_C_SOURCE 200112L /* getopt(>=2), strtok_r(*), getaddrinfo(>=200112L), clock_gettime(>=199309L), sigaction(*) */ #include #include #include +#include #include #include #include @@ -312,6 +313,18 @@ void irc_cleanup(const sock_t sock) { close(sock.fd); } +sock_t sock; + +void irc_sighandler(int signum) { + switch (signum) { + case SIGHUP: + case SIGINT: + case SIGTERM: + irc_cleanup(sock); + exit(signum); + } +} + int main(int argc, char **argv) { char *host = NULL; char *port = NULL; @@ -322,8 +335,8 @@ int main(int argc, char **argv) { char *ca_file = NULL; int pass_type = NO_PASSWD; - sock_t sock; int rv; + struct sigaction act; int opt; opterr = 0; @@ -368,6 +381,12 @@ int main(int argc, char **argv) { exit(1); } + memset(&act, 0, sizeof act); + act.sa_handler = irc_sighandler; + sigaction(SIGHUP, &act, NULL); + sigaction(SIGINT, &act, NULL); + sigaction(SIGTERM, &act, NULL); + sock = irc_connect(host, port, tls, ca_file); sock.fd OR_DIE; irc_setup(sock, 1, nick, pass, pass_type, chan); rv = irc_poll(sock, 0, 1); -- 2.39.3