From 717456edd92ba753daf5dd40e2f8d51eab3e7a73 Mon Sep 17 00:00:00 2001
* 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%)
Toggle diff (94 lines)
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 <wolfgang@pro-linux.de>
# Copyright © 2013, 2014, 2015, 2016, 2018 Ludovic Courtès <ludo@gnu.org>
+# Copyright © 2018 Carlo Zancanaro <carlo@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 <ludo@gnu.org>
;; Copyright (C) 2002, 2003 Wolfgang Jährling <wolfgang@pro-linux.de>
+;; Copyright (C) 2018 Carlo Zancanaro <carlo@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 <ludo@gnu.org>
+# Copyright © 2018 Carlo Zancanaro <carlo@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
--
2.16.1