guix-commits
[Top][All Lists]
Advanced

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

[shepherd] 01/05: services: 'read-pid-file' optionally ensures it read a


From: Ludovic Courtès
Subject: [shepherd] 01/05: services: 'read-pid-file' optionally ensures it read a valid PID.
Date: Wed, 8 May 2019 10:24:36 -0400 (EDT)

civodul pushed a commit to branch master
in repository shepherd.

commit 1ebea0a6e4c6ff11212eda348072acf9c379e7b2
Author: Ludovic Courtès <address@hidden>
Date:   Wed May 8 15:40:58 2019 +0200

    services: 'read-pid-file' optionally ensures it read a valid PID.
    
    * modules/shepherd/service.scm (read-pid-file): Add #:validate-pid?
    parameter and honor it.
    (make-forkexec-constructor): Pass #:validate-pid? #t.
---
 modules/shepherd/service.scm | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm
index b937609..9e031a4 100644
--- a/modules/shepherd/service.scm
+++ b/modules/shepherd/service.scm
@@ -709,10 +709,16 @@ results."
 set when starting a service."
   (environ))
 
-(define* (read-pid-file file #:key (max-delay 5))
+(define* (read-pid-file file #:key (max-delay 5)
+                        (validate-pid? #f))
   "Wait for MAX-DELAY seconds for FILE to show up, and read its content as a
 number.  Return #f if FILE was not created or does not contain a number;
-otherwise return the number that was read (a PID)."
+otherwise return the number that was read (a PID).
+
+When VALIDATE-PID? is true, succeed if and only if the number that was read is
+the PID of an existing process in the current PID namespace.  This test cannot
+be used if FILE might contain a PID from another PID namespace--i.e., the
+daemon writing FILE is running in a separate PID namespace."
   (define start (current-time))
 
   (let loop ()
@@ -736,11 +742,13 @@ otherwise return the number that was read (a PID)."
            (try-again))
           ((? integer? pid)
            ;; It's possible, though unlikely, that PID is not a valid PID, for
-           ;; instance because writes to FILE did not complete.  However, we
-           ;; don't do (kill pid 0) because if the process lives in a separate
-           ;; PID namespace, then PID is probably invalid in our own
-           ;; namespace.
-           pid)))
+           ;; instance because writes to FILE did not complete.  When
+           ;; VALIDATE-PID? is true, check that PID is valid in the current
+           ;; PID namespace.
+           (if (or (not validate-pid?)
+                   (catch-system-error (kill pid 0) #t))
+               pid
+               (try-again)))))
       (lambda args
         (let ((errno (system-error-errno args)))
           (if (= ENOENT errno)
@@ -931,7 +939,8 @@ start."
                                         environment-variables)))
             (if pid-file
                 (match (read-pid-file pid-file
-                                      #:max-delay pid-file-timeout)
+                                      #:max-delay pid-file-timeout
+                                      #:validate-pid? #t)
                   (#f
                    (catch-system-error (kill pid SIGTERM))
                    #f)



reply via email to

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