From db942182224dfc0accad94897dd2122b128eef07 Mon Sep 17 00:00:00 2001 From: Jelle Licht Date: Thu, 7 Sep 2017 00:52:49 +0200 Subject: [PATCH] Lazily register SIGCHLD hander on first call to 'fork+exec-command'. * modules/shepherd.scm (main): Move unconditional top-level call to 'sigaction' to... * modules/shepherd/service.scm (fork+exec-command): here. Use new variable. (%sigchld-handler-installed?): New variable. --- modules/shepherd.scm | 3 --- modules/shepherd/service.scm | 7 +++++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/modules/shepherd.scm b/modules/shepherd.scm index f7c169d..7ea6493 100644 --- a/modules/shepherd.scm +++ b/modules/shepherd.scm @@ -142,9 +142,6 @@ ;; Start the 'root' service. (start root-service) - ;; Install the SIGCHLD handler. - (sigaction SIGCHLD respawn-service SA_NOCLDSTOP) - ;; 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.) diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm index 72fbc3d..b2d8bc5 100644 --- a/modules/shepherd/service.scm +++ b/modules/shepherd/service.scm @@ -100,6 +100,9 @@ condition->sexp)) +;; Keep track of lazy initialization of SIGCHLD handler +(define %sigchld-handler-installed? #f) + ;; Type of service actions. (define-record-type (make-action name proc doc) @@ -787,6 +790,10 @@ false." (default-environment-variables))) "Spawn a process that executed COMMAND as per 'exec-command', and return its PID." + ;; Install the SIGCHLD handler if this is the first fork+exec-command call + (unless %sigchld-handler-installed? + (sigaction SIGCHLD respawn-service SA_NOCLDSTOP) + (set! %sigchld-handler-installed? #t)) (let ((pid (primitive-fork))) (if (zero? pid) (exec-command command -- 2.14.1