diff mbox series

[bug#65221,2/2] service: use PRESERVE-PORTS for redirecting FDs 0-2.

Message ID 20230811090615.3707-2-striness@tilde.club
State New
Headers show
Series Fix EXTRA-PORTS edge cases | expand

Commit Message

ulfvonbelow Aug. 11, 2023, 9:06 a.m. UTC
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 PRESERVE-PORTS *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 PRESERVE-PORTS,
then do the redirection using the new FD assignment, then close them.  This
complication can be avoided if we simply let PRESERVE-PORTS 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 modify PRESERVE-PORTS to allow both file descriptors and
ports, and to start the range it copies into at 0 instead of 3.  We then
modify EXEC-COMMAND to explicitly pass the desired standard I/O FDs / ports at
the front of the list it passes to PRESERVE-PORTS.

* modules/shepherd/service.scm (preserve-ports): Allow elements of EXTRA-PORTS
  to be either ports or file descriptors.  Start the range of FDs being
  duplicated into at 0 instead of 3.
  (exec-command): use PRESERVE-PORTS for redirecting FDs 0, 1, and 2.
---
 modules/shepherd/service.scm | 74 +++++++++++++++++-------------------
 1 file changed, 35 insertions(+), 39 deletions(-)
diff mbox series

Patch

diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm
index ffbd03c..5f735fe 100644
--- a/modules/shepherd/service.scm
+++ b/modules/shepherd/service.scm
@@ -1435,10 +1435,10 @@  FILE."
                      supplementary-groups)))
 
 (define (preserve-ports extra-ports)
-  "Duplicate the FDs (fd1 fd2 ... fdN) corresponding to the N ports in
-EXTRA-PORTS into the FD range (3 4 ... 3+N).  This will work regardless of the
+  "Duplicate the FDs (fd0 fd1 ... fdN) corresponding to the N+1 ports or FDs in
+EXTRA-PORTS into the FD range (0 1 ... N).  This will work regardless of the
 numeric values of fd1 ... fdN.  Any open file descriptors not in EXTRA-PORTS
-and numbered 3 or higher WILL be closed or marked FD_CLOEXEC."
+WILL be closed or marked FD_CLOEXEC."
   ;; We employ the following strategy: copy FDs as high as possible, in
   ;; descending order of FD, so as to avoid clobbering, then copy the high FDs
   ;; to low FDs, in the order specified in EXTRA-PORTS.  If more than half of
@@ -1449,8 +1449,9 @@  and numbered 3 or higher WILL be closed or marked FD_CLOEXEC."
   (let* ((max-fds-count (max-file-descriptors))
          (highest-fd (- max-fds-count 1))
          (extra-fds-count (length extra-ports))
-         (preserved-fds-count (+ 3 extra-fds-count))
-         (extra-fds (map fileno extra-ports))
+         (extra-fds (map (lambda (x)
+                           (if (port? x) (fileno x) x))
+                         extra-ports))
          (index+fd (map cons
                         (iota extra-fds-count)
                         extra-fds))
@@ -1470,15 +1471,15 @@  and numbered 3 or higher WILL be closed or marked FD_CLOEXEC."
     (for-each (match-lambda
                 ((by-fileno-index original-index . original-fd)
                  (dup2 (- highest-fd by-fileno-index)
-                       (+ 3 original-index))))
+                       original-index)))
               index2+fd)
     (for-each (lambda (fd)
                 (catch-system-error
                  (let ((flags (fcntl fd F_GETFD)))
                    (when (zero? (logand flags FD_CLOEXEC))
                      (fcntl fd F_SETFD (logior FD_CLOEXEC flags))))))
-              (iota (- max-fds-count preserved-fds-count)
-                    preserved-fds-count))))
+              (iota (- max-fds-count extra-fds-count)
+                    extra-fds-count))))
 
 (define* (exec-command command
                        #:key
@@ -1525,39 +1526,34 @@  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))))
-
-     ;; Close all the file descriptors except stdout, stderr, and EXTRA-PORTS.
+     ;; Close all the file descriptors except stdin, stdout, stderr, and
+     ;; EXTRA-PORTS.
      ;; Make EXTRA-PORTS available starting from file descriptor 3.
      ;; This clears their FD_CLOEXEC flag.
-     (preserve-ports extra-ports)
+     (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" O_RDONLY)))
+            (stdout (catch #t
+                      (lambda ()
+                        (or log-port
+                            (and log-file
+                                 (open-fdes log-file
+                                            (logior O_CREAT O_WRONLY O_APPEND)
+                                            #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)))
+       (preserve-ports (cons* stdin
+                              stdout
+                              stderr
+                              extra-ports)))
 
      ;; setgid must be done *before* setuid, otherwise the user will
      ;; likely no longer have permissions to setgid.