civodul pushed a commit to branch master in repository shepherd. commit b1395094b7937dd3d87756077855f4511dbd7d06 Author: Carlo Zancanaro <ca...@zancanaro.id.au> Date: Tue Feb 20 02:52:47 2018 +1100
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. Signed-off-by: Ludovic Courtès <l...@gnu.org> --- Makefile.am | 3 ++- modules/shepherd.scm | 11 +++++++++++ tests/{sigint.sh => signals.sh} | 23 ++++++++++++++--------- 3 files changed, 27 insertions(+), 10 deletions(-) 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 <wolfg...@pro-linux.de> # Copyright © 2013, 2014, 2015, 2016, 2018 Ludovic Courtès <l...@gnu.org> +# Copyright © 2018 Carlo Zancanaro <ca...@zancanaro.id.au> # # 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 <l...@gnu.org> ;; Copyright (C) 2002, 2003 Wolfgang Jährling <wolfg...@pro-linux.de> +;; Copyright (C) 2018 Carlo Zancanaro <ca...@zancanaro.id.au> ;; ;; 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 <l...@gnu.org> +# Copyright © 2018 Carlo Zancanaro <ca...@zancanaro.id.au> # # This file is part of the GNU Shepherd. # @@ -44,14 +45,18 @@ cat > "$conf"<<EOF (start 'test) EOF -rm -f "$pid" "$stamp" -shepherd -I -s "$socket" -c "$conf" --pid="$pid" --log="$log" & +for signal in INT TERM HUP; do -while [ ! -f "$pid" ] ; do sleep 0.5 ; done + rm -f "$pid" "$stamp" "$socket" + shepherd -I -s "$socket" -c "$conf" --pid="$pid" --log="$log" & -# Send SIGINT to shepherd. -kill -INT "`cat "$pid"`" -while kill -0 "`cat "$pid"`" ; do sleep 0.5 ; done + while [ ! -f "$pid" ] ; do sleep 0.5 ; done -# Make sure the service's 'stop' method was called. -test -f "$stamp" + # Send signal to shepherd. + kill -$signal "`cat "$pid"`" + while kill -0 "`cat "$pid"`" ; do sleep 0.5 ; done + + # Make sure the service's 'stop' method was called. + test -f "$stamp" + +done