[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.)
- [shepherd] branch master updated (5c3a618 -> 978e5b4), Ludovic Courtès, 2022/09/07
- [shepherd] 04/10: shepherd: Mark client connection sockets as SOCK_NONBLOCK., Ludovic Courtès, 2022/09/07
- [shepherd] 07/10: service: Mark systemd listening sockets as SOCK_CLOEXEC., Ludovic Courtès, 2022/09/07
- [shepherd] 01/10: doc: Update inetd service example., Ludovic Courtès, 2022/09/07
- [shepherd] 09/10: shepherd: Upon startup, mark preexisting file descriptors as FD_CLOEXEC.,
Ludovic Courtès <=
- [shepherd] 10/10: shepherd: Add test ensuring proper use of close-on-exec., Ludovic Courtès, 2022/09/07
- [shepherd] 02/10: shepherd: Open listening socket as SOCK_NONBLOCK., Ludovic Courtès, 2022/09/07
- [shepherd] 03/10: shepherd: Mark client connection sockets as SOCK_CLOEXEC., Ludovic Courtès, 2022/09/07
- [shepherd] 05/10: system: Add 'pipe2' bindings., Ludovic Courtès, 2022/09/07
- [shepherd] 06/10: service: Mark service logging pipe as O_CLOEXEC., Ludovic Courtès, 2022/09/07
- [shepherd] 08/10: service: Mark inetd connection sockets as SOCK_CLOEXEC., Ludovic Courtès, 2022/09/07