civodul pushed a commit to branch main
in repository shepherd.

commit 3ab3930a30495391e5176355e524a8dc026bd060
Author: Ludovic Courtès <[email protected]>
AuthorDate: Sat Mar 22 19:46:29 2025 +0100

    service: ‘restart’ passes arguments to the service.
    
    * modules/shepherd/service.scm (perform-service-action): For ‘restart’,
    pass ARGS to SERVICE.
    * tests/restart.sh: Test it.
    * NEWS: Update.
    
    Reported-by: Maxim Cournoyer <[email protected]>
    Reported-by: Tomas Volf <[email protected]>
---
 NEWS                         | 14 ++++++++++++++
 modules/shepherd/service.scm |  8 ++++++--
 tests/restart.sh             | 24 +++++++++++++++++-------
 3 files changed, 37 insertions(+), 9 deletions(-)

diff --git a/NEWS b/NEWS
index 235b250..b91741c 100644
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,20 @@ Copyright © 2013-2014, 2016, 2018-2020, 2022-2025 Ludovic 
Courtès <[email protected]
 
 Please send Shepherd bug reports to [email protected].
 
+* Changes in 1.0.4
+
+** ‘restart’ action passes extra arguments to the service’s constructor
+   (<https://issues.guix.gnu.org/77109>)
+
+Until now, extra arguments passed to the ‘restart’ action would be ignored.
+Thus, “herd restart SERVICE a b c” would restart SERVICE but silently ignore
+the arguments “a b c”.  The behavior is now to pass those extra arguments to
+the ‘start’ method of the service.
+
+Incidentally, this also means that mistakenly running “herd restart SERVICE1
+SERVICE2” now results in an error because SERVICE2 is interpreted as an
+argument to SERVICE1 and not as a second service to restart.
+
 * Changes in 1.0.3
 
 ** ‘spawn-command’ now honors #:log-file
diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm
index 79d14cf..a9c152b 100644
--- a/modules/shepherd/service.scm
+++ b/modules/shepherd/service.scm
@@ -1112,11 +1112,15 @@ the action."
     ;; All actions which are handled here might be called even if the
     ;; service is not running, so they have to take this into account.
     (case the-action
-      ;; Restarting is done in the obvious way.
+      ;; Restarting is done in the obvious way.  ARGS is passed to SERVICE
+      ;; only.
       ((restart)
        (lambda (running . args)
          (let ((stopped-services (stop-service service)))
-           (for-each start-service
+           (for-each (lambda (restarting)
+                       (if (eq? restarting service)
+                           (apply start-service restarting args)
+                           (start-service restarting)))
                      (remove transient-service? stopped-services))
            #t)))
       ((status)
diff --git a/tests/restart.sh b/tests/restart.sh
index f2efaf4..44861ee 100644
--- a/tests/restart.sh
+++ b/tests/restart.sh
@@ -1,5 +1,5 @@
 # GNU Shepherd --- Test restarting services.
-# Copyright © 2013, 2014, 2016, 2023 Ludovic Courtès <[email protected]>
+# Copyright © 2013-2014, 2016, 2023, 2025 Ludovic Courtès <[email protected]>
 # Copyright © 2018 Carlo Zancanaro <[email protected]>
 #
 # This file is part of the GNU Shepherd.
@@ -35,18 +35,18 @@ cat > "$conf"<<EOF
 (register-services
  (list (service
         '(test1)
-        #:start (const #t)
-        #:stop  (const #t))
+        #:start (lambda () #t)
+        #:stop  (const #f))
        (service
         '(test2)
         #:requirement '(test1)
-        #:start (const #t)
-        #:stop  (const #t))
+        #:start (lambda* (#:optional (value #t)) value)
+        #:stop  (const #f))
        (service
         '(test3)
         #:requirement '(test2)
-        #:start (const #t)
-        #:stop  (const #t))))
+        #:start (lambda () #t)
+        #:stop  (const #f))))
 EOF
 
 rm -f "$pid"
@@ -75,3 +75,13 @@ $herd restart test1
 $herd status test1 | grep running
 $herd status test2 | grep running
 $herd status test3 | grep running
+
+# Passing too many arguments to 'start'.
+$herd restart test1 a b c d && false
+
+# Restarting with one argument: the argument must be passed to 'test2' but not
+# to its dependent 'test3'.
+$herd start test3
+$herd restart test2 MAGIC      # restart 'test2' and 'test3'
+$herd status test2 | grep MAGIC
+$herd status test3 | grep running

Reply via email to