From debbugs-submit-bounces@debbugs.gnu.org Mon Feb 19 12:12:24 2018 Received: (at submit) by debbugs.gnu.org; 19 Feb 2018 17:12:24 +0000 Received: from localhost ([127.0.0.1]:51522 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1enoz9-0004cw-J8 for submit@debbugs.gnu.org; Mon, 19 Feb 2018 12:12:24 -0500 Received: from eggs.gnu.org ([208.118.235.92]:34949) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1enoz2-0004cb-7d for submit@debbugs.gnu.org; Mon, 19 Feb 2018 12:12:13 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1enoyv-0006Ib-HP for submit@debbugs.gnu.org; Mon, 19 Feb 2018 12:12:02 -0500 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_50,FREEMAIL_FROM, T_DKIM_INVALID autolearn=disabled version=3.3.2 Received: from lists.gnu.org ([2001:4830:134:3::11]:59047) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1enoyv-0006IS-BP for submit@debbugs.gnu.org; Mon, 19 Feb 2018 12:12:01 -0500 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) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:user-agent:from:to:subject:date:message-id:mime-version; bh=vnPAXbsuINmhZSUlcknFqN47YyMt3ytZaCEYSQL8Ono=; b=RLMcZrzIifsVFKrEXgBWn+pXPmvFVMmxs5EGsiTZkm/Su5YtL0w2ySP8Q24oUvydew EtZRDIpn2PfwN9j5Y0Ck+iFMQI4nhrJ3TrOlpxvkcYXxOD8+edfbMRIorUcnvJr/AlO2 ymNPL9zFSMsC+7FXycKZxnEU8VuYTcUXf3ObVeqBrP6TJJ1sWHh+SPdkNO6Oyi9IhaCm 2bD4+qAY6C1roTgEUSD1s56C1cbVZZsTzxZXLskDstiYx6SfVvDFDa0/BA+XGHcEzl6n i/x8t/YuljmKJ+PZY/pwizf6gwqLB5s/pROAzlxRg5U02QkfvcVZKcCdAPb0I3tbpWwG Ua7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:user-agent:from:to:subject:date :message-id:mime-version; bh=vnPAXbsuINmhZSUlcknFqN47YyMt3ytZaCEYSQL8Ono=; b=dI5hmgeunzRGEshVEXYl2i7fT0V0GOUFfsEU2m1pI09TfxeAeBpz0MchzZT3Pz/UUD v62fp54Sb1NAVIZJV+xyRXo39gXR7IcHXvPkPR3cY//Y3oeiqzitgxfXeeFMvkvOAU+r 1lLT5RT29aTylGuBfnqLmC9xCJXtXAgUr+R0BodfpdBR1trC3REx94OPjkrlpEnkIOI+ qxzHDa2RL50bLdBrz6zk/45dzpA6eQtrx/GMVmT0BGru2xYZo8JQ7+3Pqt+zF+5j0cxW he1895P1wecNVlYcD3E/2nIcwZk6DkI93Ya4h0QqOWGff67uUzZ8fQCY9T28v1VwKKyf THEA== X-Gm-Message-State: APf1xPC4lT6o1HuFJA2N2XAibN/GbqxuWHtzd74zUwUDDsCuTbeMifLG E0JwTsvlS0LcB9pt/0P3PxabJ17g X-Google-Smtp-Source: AH8x226IU9pzRiw3jkmxxyr7mY0SJgePKt71OypUGzXcWKRmwEVx5oZllWzDaUI/D2z7LFrJfmxqVg== X-Received: by 10.223.197.139 with SMTP id m11mr14981746wrg.0.1519060313974; Mon, 19 Feb 2018 09:11:53 -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) User-agent: mu4e 1.0; emacs 25.3.1 From: Carlo Zancanaro To: guix-patches@gnu.org Subject: [PATCH] Shepherd: Terminate all services upon SIGTERM or SIGHUP 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" X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 2001:4830:134:3::11 X-Spam-Score: -3.5 (---) X-Debbugs-Envelope-To: submit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: 1.5 (+) X-Spam-Report: Spam detection software, running on the system "debbugs.gnu.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: 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. [...] Content analysis details: (1.5 points, 10.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.2 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail domains are different 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (carlozancanaro[at]gmail.com) 1.0 SPF_SOFTFAIL SPF: sender does not match SPF record (softfail) 0.2 FREEMAIL_FORGED_FROMDOMAIN 2nd level domains in From and EnvelopeFrom freemail headers are different 0.0 T_DKIM_INVALID DKIM-Signature header exists but is not valid --==-=-= 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----- --==-=-=--