From 717456edd92ba753daf5dd40e2f8d51eab3e7a73 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Tue, 20 Feb 2018 02:52:47 +1100 Subject: [PATCH] Terminate all services upon SIGTERM or SIGHUP * modules/shepherd.scm (main): Add SIGTERM and SIGHUP handlers which stop root-service. * tests/sigint.sh: Rename to... * tests/signals.sh: ... this, and add tests for SIGTERM and SIGUP. --- Makefile.am | 3 ++- modules/shepherd.scm | 11 +++++++++++ tests/{sigint.sh => signals.sh} | 23 ++++++++++++++--------- 3 files changed, 27 insertions(+), 10 deletions(-) rename tests/{sigint.sh => signals.sh} (72%) diff --git a/Makefile.am b/Makefile.am index a30b11d..1c394e1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,7 @@ # Makefile.am -- How to build and install the Shepherd. # Copyright © 2002, 2003 Wolfgang Jährling # Copyright © 2013, 2014, 2015, 2016, 2018 Ludovic Courtès +# Copyright © 2018 Carlo Zancanaro # # This file is part of the GNU Shepherd. # @@ -188,7 +189,7 @@ TESTS = \ tests/no-home.sh \ tests/pid-file.sh \ tests/status-sexp.sh \ - tests/sigint.sh + tests/signals.sh TEST_EXTENSIONS = .sh EXTRA_DIST += $(TESTS) diff --git a/modules/shepherd.scm b/modules/shepherd.scm index 5334657..650ba63 100644 --- a/modules/shepherd.scm +++ b/modules/shepherd.scm @@ -1,6 +1,7 @@ ;; shepherd.scm -- The daemon shepherd. ;; Copyright (C) 2013, 2014, 2016, 2018 Ludovic Courtès ;; Copyright (C) 2002, 2003 Wolfgang Jährling +;; Copyright (C) 2018 Carlo Zancanaro ;; ;; This file is part of the GNU Shepherd. ;; @@ -182,6 +183,16 @@ (lambda _ (stop root-service))) + ;; Stop everything when we get SIGTERM. + (sigaction SIGTERM + (lambda _ + (stop root-service))) + + ;; Stop everything when we get SIGHUP. + (sigaction SIGHUP + (lambda _ + (stop root-service))) + ;; Ignore SIGPIPE so that we don't die if a client closes the connection ;; prematurely. (sigaction SIGPIPE SIG_IGN) diff --git a/tests/sigint.sh b/tests/signals.sh similarity index 72% rename from tests/sigint.sh rename to tests/signals.sh index 7354848..acb254a 100644 --- a/tests/sigint.sh +++ b/tests/signals.sh @@ -1,5 +1,6 @@ -# GNU Shepherd --- Make sure SIGINT is correctly handled. +# GNU Shepherd --- Make sure SIGINT, SIGTERM, and SIGHUP are correctly handled. # Copyright © 2014, 2016 Ludovic Courtès +# Copyright © 2018 Carlo Zancanaro # # This file is part of the GNU Shepherd. # @@ -44,14 +45,18 @@ cat > "$conf"<