From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54635) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1enozz-0006lq-Kh for guix-patches@gnu.org; Mon, 19 Feb 2018 12:13:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1enozu-0006im-Rp for guix-patches@gnu.org; Mon, 19 Feb 2018 12:13:07 -0500 Received: from debbugs.gnu.org ([208.118.235.43]:43625) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1enozu-0006if-LD for guix-patches@gnu.org; Mon, 19 Feb 2018 12:13:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1enozu-0004dy-Fw for guix-patches@gnu.org; Mon, 19 Feb 2018 12:13:02 -0500 Subject: [bug#30532] [PATCH] Shepherd: Terminate all services upon SIGTERM or SIGHUP Resent-Message-ID: Received: from eggs.gnu.org ([2001:4830:134:3::10]:54123) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1enoyt-0005nZ-CP for guix-patches@gnu.org; Mon, 19 Feb 2018 12:12:01 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1enoyp-0006GF-UK for guix-patches@gnu.org; Mon, 19 Feb 2018 12:11:59 -0500 Received: from mail-wr0-x230.google.com ([2a00:1450:400c:c0c::230]:40615) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1enoyp-0006FU-HF for guix-patches@gnu.org; Mon, 19 Feb 2018 12:11:55 -0500 Received: by mail-wr0-x230.google.com with SMTP id o76so10374327wrb.7 for ; Mon, 19 Feb 2018 09:11:55 -0800 (PST) Received: from pidgey (host75-122-dynamic.46-79-r.retail.telecomitalia.it. [79.46.122.75]) by smtp.gmail.com with ESMTPSA id s2sm10612624wmf.0.2018.02.19.09.11.52 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 19 Feb 2018 09:11:53 -0800 (PST) From: Carlo Zancanaro Date: Tue, 20 Feb 2018 04:11:45 +1100 Message-ID: <87sh9wnbu6.fsf@zancanaro.id.au> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="==-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+kyle=kyleam.com@gnu.org Sender: "Guix-patches" To: 30532@debbugs.gnu.org --==-=-= Content-Type: multipart/mixed; boundary="=-=-=" --=-=-= Content-Type: text/plain; format=flowed 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 --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: inline; filename=0001-Terminate-all-services-upon-SIGTERM-or-SIGHUP.patch Content-Transfer-Encoding: quoted-printable From=20aabb9c6b1b52189d20339531de0b8b96bcace69f 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/sigterm.sh, tests/sighup.sh: New files. * Makefile.am (TESTS): Add tests/sigterm.sh and tests/sighup.sh. =2D-- 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 =2D-- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,7 @@ # Makefile.am -- How to build and install the Shepherd. # Copyright =C2=A9 2002, 2003 Wolfgang J=C3=A4hrling # Copyright =C2=A9 2013, 2014, 2015, 2016, 2018 Ludovic Court=C3=A8s +# Copyright =C2=A9 2018 Carlo Zancanaro # # This file is part of the GNU Shepherd. # @@ -188,7 +189,9 @@ TESTS =3D \ tests/no-home.sh \ tests/pid-file.sh \ tests/status-sexp.sh \ =2D tests/sigint.sh + tests/sigint.sh \ + tests/sigterm.sh \ + tests/sighup.sh =20 TEST_EXTENSIONS =3D .sh EXTRA_DIST +=3D $(TESTS) diff --git a/modules/shepherd.scm b/modules/shepherd.scm index 5334657..650ba63 100644 =2D-- 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=C3=A8s ;; Copyright (C) 2002, 2003 Wolfgang J=C3=A4hrling +;; Copyright (C) 2018 Carlo Zancanaro ;; ;; This file is part of the GNU Shepherd. ;; @@ -182,6 +183,16 @@ (lambda _ (stop root-service))) =20 + ;; 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 connecti= on ;; prematurely. (sigaction SIGPIPE SIG_IGN) diff --git a/tests/sighup.sh b/tests/sighup.sh new file mode 100644 index 0000000..e9ca84b =2D-- /dev/null +++ b/tests/sighup.sh @@ -0,0 +1,58 @@ +# GNU Shepherd --- Make sure SIGHUP is correctly handled. +# Copyright =C2=A9 2014, 2016 Ludovic Court=C3=A8s +# Copyright =C2=A9 2018 Carlo Zancanaro +# +# 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 . + +shepherd --version +herd --version + +socket=3D"t-socket-$$" +conf=3D"t-conf-$$" +log=3D"t-log-$$" +stamp=3D"t-stamp-$$" +pid=3D"t-pid-$$" + +herd=3D"herd -s $socket" + +trap "rm -f $socket $conf $stamp $log; + test -f $pid && kill \`cat $pid\` || true; rm -f $pid" EXIT + +cat > "$conf"< + #: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=3D"$pid" --log=3D"$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 =2D-- /dev/null +++ b/tests/sigterm.sh @@ -0,0 +1,58 @@ +# GNU Shepherd --- Make sure SIGTERM is correctly handled. +# Copyright =C2=A9 2014, 2016 Ludovic Court=C3=A8s +# Copyright =C2=A9 2018 Carlo Zancanaro +# +# 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 . + +shepherd --version +herd --version + +socket=3D"t-socket-$$" +conf=3D"t-conf-$$" +log=3D"t-log-$$" +stamp=3D"t-stamp-$$" +pid=3D"t-pid-$$" + +herd=3D"herd -s $socket" + +trap "rm -f $socket $conf $stamp $log; + test -f $pid && kill \`cat $pid\` || true; rm -f $pid" EXIT + +cat > "$conf"< + #: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=3D"$pid" --log=3D"$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" =2D-=20 2.16.1 --=-=-=-- --==-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEE1lpncq7JnOkt+LaeqdyPv9awIbwFAlqLBVEACgkQqdyPv9aw IbxxpA/+O5zb2L/rpSnrGK7IMNAYeo2cOoA/TjKDu574BUfxqlpIkuSjRwvPFmVE 1pQNtpeZx0uWAEbFYtjtI8xsQlilQasK4L6AUYGpEQzGZetLPQzTyQaqH6rlzASH yr9Fjv6/qpbyTGGkhTfGMz+witLCefjg+L30m0jViIXxjBERIFW6OjEjgofY35OL wJBpB8NXcVbDem/h9dpdMzVTse5WSvmDHNQGTYwforYO45llmiLb8uItll6Ma8E6 oAG+GYDJPbDyXRMSMWLuQ1TPlQ42/s/SV6ZKjzax6xQ3eUIrUi53hZFex4VqlXqj EAdu8sNPb6BBVlew4XGrYFcmtwvj3s7y1nCTN2IuNFXb87PKjoOdnJfDdlyQR4+n EVul+qowBFqA5+AyVcCh/BFUDcrVvVTZrZUJBEBQjSgglNlQC8Wun39ulhIeNKb5 q8qfFedmT5H5emlij6ruSaFSU009Q3FSCDH4fg5eezjcncXDXIoZ/XLR5gtdEewa s9zHnqamTAIAvtVtcEmqjtg7iru3Qovknf0+VXrAoqE1Bn9Uv01HEaQv0uvGks4P 2aW1InSSu7qd1N60+udQdWcxlYzTpqJsn0dIPW0dENR2z3JnH5bIMXF8j270NGpe Zvy0ndXu0W3I/jRW+TAh0u3oDwOB+vbnsEylh9uEwC8dp3VUY9I= =LnSF -----END PGP SIGNATURE----- --==-=-=--