Subject: [bug#76262] [PATCH 2/3] services: Use ‘spawn-command’ instead of ‘fork’ + ‘waitpid’.

From: Ludovic Courtès <[email-masked]>
Date: Thu, 13 Feb 2025 12:45:47 +0100

This is more concise and more robust: these ‘waitpid’ calls would
compete with those made by shepherd’s event loop upon SIGCHLD, and they
could hang forever.

* gnu/services/databases.scm (postgresql-role-shepherd-service): Use
‘spawn-command’ instead of ‘fork+exec-command’ followed by ‘waitpid’.
* gnu/services/networking.scm (dhcp-client-shepherd-service): Change
‘start’ to use ‘spawn-command’ instead of ‘fork+exec-command’ and
* gnu/services/web.scm (patchwork-django-admin-gexp): Use
‘spawn-command’ instead of ‘primitive-fork’ + ‘waitpid’.

Change-Id: I449290bfa46f8600e6ccdb5a6da990ad0cb7948c
---
 gnu/services/databases.scm  | 16 +++++++++-------
 gnu/services/networking.scm | 26 +++++++++++++-------------
 gnu/services/web.scm        | 31 +++++++++----------------------
 3 files changed, 31 insertions(+), 42 deletions(-)

diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm
index e8a4acc996d..6530c6f0a12 100644
--- a/gnu/services/databases.scm
+++ b/gnu/services/databases.scm
@@ -1,6 +1,6 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2015 David Thompson <[email-masked]>
-;;; Copyright © 2015-2016, 2022-2023 Ludovic Courtès <[email-masked]>
+;;; Copyright © 2015-2016, 2022-2023, 2025 Ludovic Courtès <[email-masked]>
 ;;; Copyright © 2016 Leo Famulari <[email-masked]>
 ;;; Copyright © 2017 Christopher Baines <[email-masked]>
 ;;; Copyright © 2018 Clément Lassieur <[email-masked]>
@@ -448,12 +448,14 @@ (define (postgresql-role-shepherd-service config)
            (one-shot? #t)
            (start
             #~(lambda args
-                (let ((pid (fork+exec-command
-                            #$(postgresql-create-roles config)
-                            #:user "postgres"
-                            #:group "postgres"
-                            #:log-file #$log)))
-                  (zero? (cdr (waitpid pid))))))
+                (zero? (spawn-command
+                        #$(postgresql-create-roles config)
+                        #:user "postgres"
+                        #:group "postgres"
+                        ;; XXX: As of Shepherd 1.0.2, #:log-file is not
+                        ;; supported.
+                        ;; #:log-file #$log
+                        ))))
            (documentation "Create PostgreSQL roles.")))))
 
 (define postgresql-role-service-type
diff --git a/gnu/services/networking.scm b/gnu/services/networking.scm
index af28bd0626b..53f383f6f3d 100644
--- a/gnu/services/networking.scm
+++ b/gnu/services/networking.scm
@@ -1,5 +1,5 @@
 ;;; GNU Guix --- Functional package management for GNU
-;;; Copyright © 2013-2024 Ludovic Courtès <[email-masked]>
+;;; Copyright © 2013-2025 Ludovic Courtès <[email-masked]>
 ;;; Copyright © 2015 Mark H Weaver <[email-masked]>
 ;;; Copyright © 2016, 2018, 2020 Efraim Flashner <[email-masked]>
 ;;; Copyright © 2016 John Darrington <[email-masked]>
@@ -389,18 +389,18 @@ (define dhcp-client-shepherd-service
                                  '()))
 
                            (false-if-exception (delete-file #$pid-file))
-                           (let ((pid (fork+exec-command
-                                       ;; By default dhclient uses a
-                                       ;; pre-standardization implementation of
-                                       ;; DDNS, which is incompatable with
-                                       ;; non-ISC DHCP servers; thus, pass '-I'.
-                                       ;; <https://kb.isc.org/docs/aa-01091>.
-                                       `(,dhclient "-nw" "-I"
-                                                   #$(string-append "-" version)
-                                                   "-pf" ,#$pid-file
-                                                   ,@config-file-args
-                                                   ,@ifaces))))
-                             (and (zero? (cdr (waitpid pid)))
+                           (let ((status (spawn-command
+                                          ;; By default dhclient uses a
+                                          ;; pre-standardization implementation of
+                                          ;; DDNS, which is incompatable with
+                                          ;; non-ISC DHCP servers; thus, pass '-I'.
+                                          ;; <https://kb.isc.org/docs/aa-01091>.
+                                          `(,dhclient "-nw" "-I"
+                                                      #$(string-append "-" version)
+                                                      "-pf" ,#$pid-file
+                                                      ,@config-file-args
+                                                      ,@ifaces))))
+                             (and (zero? status)
                                   (read-pid-file #$pid-file)))))
                 (stop #~(make-kill-destructor)))))))
     (package
diff --git a/gnu/services/web.scm b/gnu/services/web.scm
index f5de5997acb..d42ef09c3c0 100644
--- a/gnu/services/web.scm
+++ b/gnu/services/web.scm
@@ -1,6 +1,6 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2015 David Thompson <[email-masked]>
-;;; Copyright © 2015-2023 Ludovic Courtès <[email-masked]>
+;;; Copyright © 2015-2023, 2025 Ludovic Courtès <[email-masked]>
 ;;; Copyright © 2016 Nikita <[email-masked]>
 ;;; Copyright © 2016, 2017, 2018 Julien Lepiller <[email-masked]>
 ;;; Copyright © 2017, 2018, 2019 Christopher Baines <[email-masked]>
@@ -1902,27 +1902,14 @@ (define (patchwork-httpd-configuration patchwork-configuration)
 
 (define (patchwork-django-admin-gexp patchwork settings-module)
   #~(lambda command
-      (let ((pid (primitive-fork))
-            (user (getpwnam "httpd")))
-        (if (eq? pid 0)
-            (dynamic-wind
-              (const #t)
-              (lambda ()
-                (setgid (passwd:gid user))
-                (setuid (passwd:uid user))
-
-                (setenv "DJANGO_SETTINGS_MODULE" "guix.patchwork.settings")
-                (setenv "PYTHONPATH" #$settings-module)
-                (primitive-exit
-                 (if (zero?
-                      (apply system*
-                             #$(file-append patchwork "/bin/patchwork-admin")
-                             command))
-                     0
-                     1)))
-              (lambda ()
-                (primitive-exit 1)))
-            (zero? (cdr (waitpid pid)))))))
+      (zero? (spawn-command
+              `(#$(file-append patchwork "/bin/patchwork-admin")
+                ,command)
+              #:user "httpd"
+              #:group "httpd"
+              #:environment-variables
+              `("DJANGO_SETTINGS_MODULE=guix.patchwork.settings"
+                ,(string-append "PYTHONPATH=" #$settings-module))))))
 
 (define (patchwork-django-admin-action patchwork settings-module)
   (shepherd-action
-- 
2.48.1