guix-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[shepherd] 09/10: shepherd: Upon startup, mark preexisting file descript


From: Ludovic Courtès
Subject: [shepherd] 09/10: shepherd: Upon startup, mark preexisting file descriptors as FD_CLOEXEC.
Date: Wed, 7 Sep 2022 17:19:11 -0400 (EDT)

civodul pushed a commit to branch master
in repository shepherd.

commit 9cb7342b63ae49fe2e7eb6ad77b0d23d5e2a2a4d
Author: Ludovic Courtès <ludo@gnu.org>
AuthorDate: Wed Sep 7 23:03:20 2022 +0200

    shepherd: Upon startup, mark preexisting file descriptors as FD_CLOEXEC.
    
    * modules/shepherd.scm (mark-as-close-on-exec): New procedure.
    (run-daemon): Call it.
---
 modules/shepherd.scm | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/modules/shepherd.scm b/modules/shepherd.scm
index a6acd93..91f3318 100644
--- a/modules/shepherd.scm
+++ b/modules/shepherd.scm
@@ -151,8 +151,28 @@ already ~a threads running, disabling 'signalfd' support")
     ((signal-handler signal))))
 
 
+(define (mark-as-close-on-exec)
+  "Mark all the open file descriptors as close-on-exec."
+  (define max-fd
+    (max-file-descriptors))
+
+  (let loop ((fd 3))
+    (when (< fd max-fd)
+      (catch-system-error
+       (let ((flags (fcntl fd F_GETFD)))
+         (when (zero? (logand flags FD_CLOEXEC))
+           (fcntl fd F_SETFD (logior FD_CLOEXEC flags)))))
+      (loop (+ fd 1)))))
+
 (define* (run-daemon #:key (config-file (default-config-file)) persistency
                      socket-file pid-file signal-port poll-services?)
+  ;; We might have file descriptors inherited from our parent, as well as file
+  ;; descriptors wrongfully opened by Guile or Fibers (see
+  ;; <https://bugs.gnu.org/57567> and
+  ;; 
<https://github.com/wingo/fibers/commit/1f834cb81126dea2fd47d3d7ebb2d21f798a3c8b>);
+  ;; mark them all as FD_CLOEXEC so child processes do not inherit them.
+  (mark-as-close-on-exec)
+
   ;; This _must_ succeed.  (We could also put the `catch' around
   ;; `main', but it is often useful to get the backtrace, and
   ;; `caught-error' does not do this yet.)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]