From b43c128d8a175a9a123eb7b1af465fb3747a5393 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Sun, 4 Mar 2018 07:46:13 +1100 Subject: [PATCH 2/3] Handle forked process SIGCHLD signals * Makefile.am (TESTS): Add tests/forking-service.sh. * modules/shepherd.scm: Set the child subreaper attribute of main shepherd process (as long as we're not pid 1). * modules/shepherd/service.scm (root-service)[daemonize]: Set the child subreaper attribute of newly forked shepherd process. --- Makefile.am | 1 + modules/shepherd.scm | 7 +++++++ modules/shepherd/service.scm | 4 +++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Makefile.am b/Makefile.am index eafa308..8dad006 100644 --- a/Makefile.am +++ b/Makefile.am @@ -190,6 +190,7 @@ TESTS = \ tests/no-home.sh \ tests/pid-file.sh \ tests/status-sexp.sh \ + tests/forking-service.sh \ tests/signals.sh TEST_EXTENSIONS = .sh diff --git a/modules/shepherd.scm b/modules/shepherd.scm index df5420f..faa1e47 100644 --- a/modules/shepherd.scm +++ b/modules/shepherd.scm @@ -51,6 +51,13 @@ (define (main . args) (initialize-cli) + (when (not (= 1 (getpid))) + ;; Register for orphaned processes to be reparented onto us when their + ;; original parent dies. This lets us handle SIGCHLD from daemon processes + ;; that would otherwise have been reparented under pid 1. This is + ;; unnecessary when we are pid 1. + (catch-system-error (prctl PR_SET_CHILD_SUBREAPER 1))) + (let ((config-file #f) (socket-file default-socket-file) (pid-file #f) diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm index 2224932..b6394f2 100644 --- a/modules/shepherd/service.scm +++ b/modules/shepherd/service.scm @@ -1274,7 +1274,9 @@ we want to receive these signals." (local-output "Running as PID 1, so not daemonizing.")) (else (if (zero? (primitive-fork)) - #t + (begin + (catch-system-error (prctl PR_SET_CHILD_SUBREAPER 1)) + #t) (primitive-exit 0)))))) (persistency "Safe the current state of running and non-running services. -- 2.16.1