guix-commits
[Top][All Lists]
Advanced

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

[shepherd] 04/04: shepherd: Factorize list of handled signals.


From: Ludovic Courtès
Subject: [shepherd] 04/04: shepherd: Factorize list of handled signals.
Date: Tue, 2 Jun 2020 17:36:49 -0400 (EDT)

civodul pushed a commit to branch master
in repository shepherd.

commit 38e358962e9673c1ca2fc66f4e5d4fb135cadf11
Author: Ludovic Courtès <ludo@gnu.org>
AuthorDate: Tue Jun 2 23:29:10 2020 +0200

    shepherd: Factorize list of handled signals.
    
    Suggested by Mathieu Othacehe <othacehe@gnu.org>.
    
    * modules/shepherd/service.scm (%precious-signals): Export.
    * modules/shepherd.scm (handle-SIGINT): Turn into a thunk.
    (signal-handler): New procedure.
    (handle-signal-port): Replace 'cond' by a call to 'signal-handler'.
    (main): Pass %PRECIOUS-SIGNALS to 'maybe-signal-port'.
    Replace three explicit calls to 'sigaction' by 'for-each' +
    'signal-handler' calls.
---
 modules/shepherd.scm         | 49 +++++++++++++++++++++-----------------------
 modules/shepherd/service.scm |  1 +
 2 files changed, 24 insertions(+), 26 deletions(-)

diff --git a/modules/shepherd.scm b/modules/shepherd.scm
index 6786dea..9f80f62 100644
--- a/modules/shepherd.scm
+++ b/modules/shepherd.scm
@@ -93,25 +93,29 @@ already ~a threads running, disabling 'signalfd' support")
           #f
           (apply throw args)))))
 
-(define (handle-signal-port port)
-  "Read from PORT, a signalfd port, and handle the signal accordingly."
-  (let ((signal (consume-signalfd-siginfo port)))
-    (cond ((= signal SIGCHLD)
-           (handle-SIGCHLD))
-          ((= signal SIGINT)
-           (handle-SIGINT))
-          ((memv signal (list SIGTERM SIGHUP))
-           (stop root-service))
-          (else
-           #f))))
-
-(define (handle-SIGINT . _)
+(define (handle-SIGINT)
   "Handle SIGINT by stopping the Shepherd, which means rebooting if we're PID 
1."
   (catch 'quit
     (lambda ()
       (stop root-service))
     quit-exception-handler))
 
+(define (signal-handler signal)
+  "Return the signal handler for SIGNAL."
+  (cond ((= signal SIGCHLD)
+         (lambda _ (handle-SIGCHLD)))
+        ((= signal SIGINT)
+         (lambda _ (handle-SIGINT)))
+        ((memv signal (list SIGTERM SIGHUP))
+         (lambda _ (stop root-service)))
+        (else
+         (const #f))))
+
+(define (handle-signal-port port)
+  "Read from PORT, a signalfd port, and handle the signal accordingly."
+  (let ((signal (consume-signalfd-siginfo port)))
+    ((signal-handler signal))))
+
 
 ;; Main program.
 (define (main . args)
@@ -138,7 +142,7 @@ already ~a threads running, disabling 'signalfd' support")
     ;; Attempt to create a "signal port" via 'signalfd'.  This must be called
     ;; before the 'sigaction' procedure is called, because 'sigaction' spawns
     ;; the signal thread.
-    (maybe-signal-port (list SIGCHLD SIGINT SIGTERM SIGHUP)))
+    (maybe-signal-port %precious-signals))
 
   (initialize-cli)
 
@@ -276,18 +280,11 @@ already ~a threads running, disabling 'signalfd' support")
         (false-if-exception
          (dynamic-link (string-append %pkglibdir "/crash-handler"))))
 
-      ;; Stop everything when we get SIGINT.
-      (sigaction SIGINT handle-SIGINT)
-
-      ;; Stop everything when we get SIGTERM.
-      (sigaction SIGTERM
-        (lambda _
-          (stop root-service)))
-
-      ;; Stop everything when we get SIGHUP.
-      (sigaction SIGHUP
-        (lambda _
-          (stop root-service)))
+      ;; Install signal handlers for everything but SIGCHLD, which is taken
+      ;; care of in (shepherd services).
+      (for-each (lambda (signal)
+                  (sigaction signal (signal-handler signal)))
+                (delete SIGCHLD %precious-signals))
 
       ;; This _must_ succeed.  (We could also put the `catch' around
       ;; `main', but it is often useful to get the backtrace, and
diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm
index 1bc77b1..347b8cc 100644
--- a/modules/shepherd/service.scm
+++ b/modules/shepherd/service.scm
@@ -68,6 +68,7 @@
             lookup-services
             respawn-service
             handle-SIGCHLD
+            %precious-signals
             register-services
             provided-by
             required-by



reply via email to

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