Subject: Re: [bug#30532] [PATCH] Shepherd: Terminate all services upon SIGTERM or SIGHUP

From: Carlo Zancanaro <[email-masked]>
Date: Wed, 28 Feb 2018 08:29:50 +1100

Hey Ludo,

On Tue, Feb 27 2018, Ludovic Courtès wrote:
>> It's also worth noting that I had to run `gettextize` to get 
>> Shepherd
>> to build on current master, but I haven't included those 
>> changes in
>> this commit (because I don't know what should be committed and 
>> what
>> shouldn't).
>
> Right, I’m not sure what’s wrong with that.  I’ve committed the 
> same
> files under po/ as I did for Guix:
>
> --8<---------------cut 
> here---------------start------------->8---
> $ git ls-files po/
> po/Makevars
> po/POTFILES.in
> --8<---------------cut 
> here---------------end--------------->8---

If you clone shepherd from the repository, can you build it? 
Comparing the guix repository to the shepherd one I can't easily 
work out what needs to be done for this, but hopefully you can at 
least reproduce my problem.

> I have one request: since the three tests differ only in the 
> signal
> name, could you make it a single test file and have a loop like:
>
>   for signal in SIGTERM SIGHUP SIGINT

Updated patch is attached.

Carlo

From 717456edd92ba753daf5dd40e2f8d51eab3e7a73 Mon Sep 17 00:00:00 2001
From: Carlo Zancanaro <[email-masked]>
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 <[email-masked]>
 # Copyright © 2013, 2014, 2015, 2016, 2018 Ludovic Courtès <[email-masked]>
+# Copyright © 2018 Carlo Zancanaro <[email-masked]>
 #
 # 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 <[email-masked]>
 ;; Copyright (C) 2002, 2003 Wolfgang Jährling <[email-masked]>
+;; Copyright (C) 2018 Carlo Zancanaro <[email-masked]>
 ;;
 ;; 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 <[email-masked]>
+# Copyright © 2018 Carlo Zancanaro <[email-masked]>
 #
 # 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