From: Carlo Zancanaro <carlo@zancanaro.id.au>
To: 30532@debbugs.gnu.org
Subject: [bug#30532] [PATCH] Shepherd: Terminate all services upon SIGTERM or SIGHUP
Date: Tue, 20 Feb 2018 04:11:45 +1100 [thread overview]
Message-ID: <87sh9wnbu6.fsf@zancanaro.id.au> (raw)
[-- Attachment #1.1: Type: text/plain, Size: 589 bytes --]
Hey,
I use Shepherd to manage my user session, and if I log out then
Shepherd leaves all my services running. This patch handles
SIGTERM and SIGHUP to prevent that.
I hope the guix-patches mailing list was the right place to send
it. From what I understand Shepherd development is done through
the guix lists, so it seemed to make the most sense to me.
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).
Carlo
[-- Attachment #1.2: 0001-Terminate-all-services-upon-SIGTERM-or-SIGHUP.patch --]
[-- Type: text/x-patch, Size: 6455 bytes --]
From aabb9c6b1b52189d20339531de0b8b96bcace69f Mon Sep 17 00:00:00 2001
From: Carlo Zancanaro <carlo@zancanaro.id.au>
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/sigterm.sh, tests/sighup.sh: New files.
* Makefile.am (TESTS): Add tests/sigterm.sh and tests/sighup.sh.
---
Makefile.am | 5 ++++-
modules/shepherd.scm | 11 ++++++++++
tests/sighup.sh | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++
tests/sigterm.sh | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 131 insertions(+), 1 deletion(-)
create mode 100644 tests/sighup.sh
create mode 100644 tests/sigterm.sh
diff --git a/Makefile.am b/Makefile.am
index a30b11d..021857d 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,9 @@ TESTS = \
tests/no-home.sh \
tests/pid-file.sh \
tests/status-sexp.sh \
- tests/sigint.sh
+ tests/sigint.sh \
+ tests/sigterm.sh \
+ tests/sighup.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/sighup.sh b/tests/sighup.sh
new file mode 100644
index 0000000..e9ca84b
--- /dev/null
+++ b/tests/sighup.sh
@@ -0,0 +1,58 @@
+# GNU Shepherd --- Make sure SIGHUP is 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.
+#
+# The GNU Shepherd is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or (at
+# your option) any later version.
+#
+# The GNU Shepherd is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with the GNU Shepherd. If not, see <http://www.gnu.org/licenses/>.
+
+shepherd --version
+herd --version
+
+socket="t-socket-$$"
+conf="t-conf-$$"
+log="t-log-$$"
+stamp="t-stamp-$$"
+pid="t-pid-$$"
+
+herd="herd -s $socket"
+
+trap "rm -f $socket $conf $stamp $log;
+ test -f $pid && kill \`cat $pid\` || true; rm -f $pid" EXIT
+
+cat > "$conf"<<EOF
+(use-modules (srfi srfi-26))
+(register-services
+ (make <service>
+ #:provides '(test)
+ #:start (const #t)
+ #:stop (lambda _
+ (call-with-output-file "$stamp"
+ (lambda (port)
+ (display "stopped" port))))
+ #:respawn? #f))
+ (start 'test)
+EOF
+
+rm -f "$pid" "$stamp"
+shepherd -I -s "$socket" -c "$conf" --pid="$pid" --log="$log" &
+
+while [ ! -f "$pid" ] ; do sleep 0.5 ; done
+
+# Send SIGTERM to shepherd.
+kill -HUP "`cat "$pid"`"
+while kill -0 "`cat "$pid"`" ; do sleep 0.5 ; done
+
+# Make sure the service's 'stop' method was called.
+test -f "$stamp"
diff --git a/tests/sigterm.sh b/tests/sigterm.sh
new file mode 100644
index 0000000..f6b66be
--- /dev/null
+++ b/tests/sigterm.sh
@@ -0,0 +1,58 @@
+# GNU Shepherd --- Make sure SIGTERM is 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.
+#
+# The GNU Shepherd is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or (at
+# your option) any later version.
+#
+# The GNU Shepherd is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with the GNU Shepherd. If not, see <http://www.gnu.org/licenses/>.
+
+shepherd --version
+herd --version
+
+socket="t-socket-$$"
+conf="t-conf-$$"
+log="t-log-$$"
+stamp="t-stamp-$$"
+pid="t-pid-$$"
+
+herd="herd -s $socket"
+
+trap "rm -f $socket $conf $stamp $log;
+ test -f $pid && kill \`cat $pid\` || true; rm -f $pid" EXIT
+
+cat > "$conf"<<EOF
+(use-modules (srfi srfi-26))
+(register-services
+ (make <service>
+ #:provides '(test)
+ #:start (const #t)
+ #:stop (lambda _
+ (call-with-output-file "$stamp"
+ (lambda (port)
+ (display "stopped" port))))
+ #:respawn? #f))
+ (start 'test)
+EOF
+
+rm -f "$pid" "$stamp"
+shepherd -I -s "$socket" -c "$conf" --pid="$pid" --log="$log" &
+
+while [ ! -f "$pid" ] ; do sleep 0.5 ; done
+
+# Send SIGTERM to shepherd.
+kill -TERM "`cat "$pid"`"
+while kill -0 "`cat "$pid"`" ; do sleep 0.5 ; done
+
+# Make sure the service's 'stop' method was called.
+test -f "$stamp"
--
2.16.1
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]
next reply other threads:[~2018-02-19 17:13 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-19 17:11 Carlo Zancanaro [this message]
2018-02-27 9:45 ` [bug#30532] [PATCH] Shepherd: Terminate all services upon SIGTERM or SIGHUP Ludovic Courtès
2018-02-27 17:22 ` Leo Famulari
2018-02-27 21:03 ` Ludovic Courtès
2018-02-27 21:19 ` Carlo Zancanaro
2018-02-27 21:30 ` Ludovic Courtès
2018-03-02 0:30 ` Leo Famulari
2018-03-02 7:25 ` Carlo Zancanaro
2018-02-27 21:29 ` Carlo Zancanaro
2018-03-01 9:55 ` Ludovic Courtès
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://guix.gnu.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87sh9wnbu6.fsf@zancanaro.id.au \
--to=carlo@zancanaro.id.au \
--cc=30532@debbugs.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/guix.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).