Subject: [PATCH 5/6] service: use RECONFIGURE-FDS for redirecting FDs 0-2.

From: ulfvonbelow <[email-masked]>
Date: Fri, 18 Aug 2023 15:22:38 -0500

There are currently some corner cases in how EXTRA-PORTS works due to it not
managing FDs 0, 1, and 2.  Specifically, if one were to include a port in
EXTRA-PORTS with FD 0, 1, or 2, it would *not* be preserved, and would instead
represent the file that EXEC-COMMAND assigned to that file descriptor.  To
avoid this, it's necessary to call RECONFIGURE-FDS *before* redirecting the
input, but this could clobber LOG-PORT or INPUT-PORT, so it would become
necessary to include LOG-PORT and INPUT-PORT in the call to RECONFIGURE-FDS,
then do the redirection using the new FD assignment, then close them.  This
complication can be avoided if we simply let RECONFIGURE-FDS itself do the
redirection.  This also solves other edge cases, like if LOG-PORT has fileno 0
or 1 (previously passing a LOG-PORT of (current-output-port) would cause an
error, as the underlying file descriptor would be closed before dup2 was
called to copy it), or if INPUT-PORT has fileno 0.

To solve this, we have RECONFIGURE-FDS start the range it copies into at 0
instead of 3.  We then explicitly pass the desired standard I/O FDs / ports at
the front of the list passed to RECONFIGURE-FDS.

We also use O_CLOEXEC for opening /dev/null and the log file so that the file
descriptors they are originally opened on don't hang around.

* modules/shepherd/service.scm (exec-command): use RECONFIGURE-FDS for
  redirecting FDs 0, 1, and 2.
---
 modules/shepherd/service.scm | 62 +++++++++++++++++-------------------
 1 file changed, 30 insertions(+), 32 deletions(-)

diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm
index e816cd1..3008e31 100644
--- a/modules/shepherd/service.scm
+++ b/modules/shepherd/service.scm
@@ -1561,38 +1561,36 @@ false."
      (chdir directory)
      (environ environment-variables)
 
-     ;; Redirect stdin.
-     (catch-system-error (close-fdes 0))
-     ;; Make sure file descriptor zero is used, so we don't end up reusing
-     ;; it for something unrelated, which can confuse some packages.
-     (dup2 (if input-port
-               (fileno input-port)
-               (open-fdes "/dev/null" O_RDONLY))
-           0)
-
-     (when (or log-port log-file)
-       (catch #t
-         (lambda ()
-           ;; Redirect stdout and stderr to use LOG-FILE.
-           (catch-system-error (close-fdes 1))
-           (catch-system-error (close-fdes 2))
-           (dup2 (if log-file
-                     (open-fdes log-file (logior O_CREAT O_WRONLY O_APPEND)
-                                #o640)
-                     (fileno log-port))
-                 1)
-           (dup2 1 2))
-
-         (lambda (key . args)
-           (when log-file
-             (format (current-error-port)
-                     "failed to open log-file ~s:~%" log-file))
-           (print-exception (current-error-port) #f key args)
-           (primitive-exit 1))))
-
-     ;; Make EXTRA-PORTS available starting from file descriptor 3.
-     ;; This clears their FD_CLOEXEC flag.
-     (reconfigure-fds extra-ports 3)
+     (let* ( ;; Make sure file descriptor zero is used, so we don't end up reusing
+            ;; it for something unrelated, which can confuse some packages.
+            (stdin (or input-port (open-fdes "/dev/null"
+                                             (logior O_RDONLY
+                                                     O_CLOEXEC))))
+            (stdout (catch #t
+                      (lambda ()
+                        (or log-port
+                            (and log-file
+                                 (open-fdes log-file
+                                            (logior O_CREAT O_WRONLY O_APPEND
+                                                    O_CLOEXEC)
+                                            #o640))
+                            1))
+                      (lambda (key . args)
+                        (when log-file
+                          (format (current-error-port)
+                                  "failed to open log-file ~s:~%" log-file))
+                        (print-exception (current-error-port) #f key args)
+                        (primitive-exit 1))))
+            (stderr (if (or log-port log-file)
+                        stdout
+                        2))
+            (all-fds (+ 3 (length extra-ports))))
+       ;; Make EXTRA-PORTS available starting from file descriptor 3.
+       ;; This clears their FD_CLOEXEC flag.
+       (reconfigure-fds (cons* stdin
+                               stdout
+                               stderr
+                               extra-ports)))
 
      ;; setgid must be done *before* setuid, otherwise the user will
      ;; likely no longer have permissions to setgid.
-- 
2.40.1