From 6659fec29dd78ffc6eb2c85eb01c264f28c79038 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Sat, 9 May 2020 21:50:04 +0200 Subject: nicely exit on SIGINT and SIGTERM --- hittpd.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/hittpd.c b/hittpd.c index 231da84..95fc11e 100644 --- a/hittpd.c +++ b/hittpd.c @@ -869,6 +869,15 @@ read_client(int i) } } +sig_atomic_t stop; + +void +do_stop(int sig) +{ + (void)sig; + stop = 1; +} + int main(int argc, char *argv[]) { @@ -897,12 +906,19 @@ main(int argc, char *argv[]) if (argc > optind) wwwroot = argv[optind]; + struct sigaction pipe_act = { .sa_handler = SIG_IGN }; + sigemptyset(&pipe_act.sa_mask); + sigaction(SIGPIPE, &pipe_act, 0); + + struct sigaction act = { .sa_handler = do_stop }; + sigemptyset(&act.sa_mask); + sigaction(SIGINT, &act, 0); + sigaction(SIGTERM, &act, 0); + int i, maxi, listenfd, sockfd; int nready; int r = 0; - signal(SIGPIPE, SIG_IGN); - if (uds) { struct sockaddr_un addr = { 0 }; addr.sun_family = AF_UNIX; @@ -971,9 +987,18 @@ main(int argc, char *argv[]) maxi = 0; /* max index into client[] array */ - while (1) { + while (!stop) { nready = poll(client, maxi + 1, maxi ? TIMEOUT*1000 : -1); + if (nready < 0) { + if (errno == EINTR) { + continue; // and stop maybe + } else { + perror("poll"); + exit(111); + } + } + time_t now = time(0); if (nready == 0) { -- cgit 1.4.1