1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
# GNU Shepherd --- Ensure replacing services works properly
# Copyright © 2014, 2016, 2023-2025 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 <https://www.gnu.org/licenses/>.
shepherd --version
herd --version
socket="t-socket-$$"
conf="t-conf-$$"
rconf="t-rconf-$$"
log="t-log-$$"
stamp="t-stamp-$$"
pid="t-pid-$$"
herd="herd -s $socket"
trap "cat $log || true; rm -f $socket $conf $rconf $stamp $log;
test -f $pid && kill \`cat $pid\` || true; rm -f $pid" EXIT
cat > "$conf"<<EOF
(use-modules (srfi srfi-26))
(register-services
(list (service
'(test)
#:start (let ((started? #f))
(lambda ()
(let ((first-time? (not started?)))
(unless first-time?
(error "Already started once!\n"))
(set! started? #t)
first-time?)))
#:actions (actions
(say-hello (lambda _
(call-with-output-file "$stamp"
(lambda (port)
(display "Hello" port))))))
#:respawn? #f)))
EOF
rm -f "$pid" "$stamp" "$socket"
shepherd -I -s "$socket" -c "$conf" --pid="$pid" --log="$log" &
while ! test -f "$pid"; do sleep 0.5 ; done
$herd start test
$herd say-hello test
test -f "$stamp"
$herd status test | grep -v "Replacement pending"
cat > "$rconf"<<EOF
(register-services
(list (service
'(test)
#:start (lambda _
(display "The replacement is starting.\n")
(fork+exec-command '("sleep" "300")))
#:stop (make-kill-destructor)
#:actions (actions
(say-goodbye (lambda _
(call-with-output-file "$stamp"
(lambda (port)
(display "Goodbye" port))))))
#:respawn? #f)))
EOF
$herd load root "$rconf"
$herd status test | grep "Replacement pending"
$herd say-hello test
test "`cat $stamp`" = "Hello"
$herd restart test
$herd status test | grep running
grep "The replacement is starting" "$log"
$herd say-hello test && false # this action should have vanished
$herd say-goodbye test # this one is new
test "`cat $stamp`" = "Goodbye"
# Make sure the "disabled" flag is preserved when replacing.
$herd stop test
$herd disable test
$herd load root "$rconf"
$herd status test | grep disabled
# Restarting the service from here used to trigger a bug:
# <https://issues.guix.gnu.org/67839>.
$herd enable test
$herd start test
child_pid="$($herd status test | grep PID \
| sed '-es/.*PID: \([0-9]\+\)$/\1/g')"
kill -0 "$child_pid"
$herd stop test
if kill -0 "$child_pid"; then false; else true; fi
|