summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2020-05-09 21:50:04 +0200
committerLeah Neukirchen <leah@vuxu.org>2020-05-09 21:50:04 +0200
commit6659fec29dd78ffc6eb2c85eb01c264f28c79038 (patch)
tree265e7cc365521a29aef400c2b60fe0ddbc6ad4c2
parentd1ebc6ac96b95d982fb1c85044137e57a59861d9 (diff)
downloadhittpd-6659fec29dd78ffc6eb2c85eb01c264f28c79038.tar.gz
hittpd-6659fec29dd78ffc6eb2c85eb01c264f28c79038.tar.xz
hittpd-6659fec29dd78ffc6eb2c85eb01c264f28c79038.zip
nicely exit on SIGINT and SIGTERM
-rw-r--r--hittpd.c31
1 files 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) {