Subject: [PATCH shepherd]: Update required guile version, and remove some hacks

From: Carlo Zancanaro <[email-masked]>
Date: Mon, 26 Mar 2018 22:55:03 +1100

These patches bring the Shepherd's Guile dependencies in line with 
Guix, and remove some hacks that were required for old Guile 
problems.

I'm not very familiar with autotools, but I think I got the 
configure incantation right (I stole it from Guix).

From 8c812534137a5dc17dd8073706983c451d26f2db Mon Sep 17 00:00:00 2001
From: Carlo Zancanaro <[email-masked]>
Date: Mon, 26 Mar 2018 14:44:18 +1100
Subject: [PATCH 1/3] Update Guile dependency to 2.0.13 or later

* README (Requirements): Change 2.x to 2.0.13 or later.
* configure.ac: Check for 2.0.13 or later if Guile 2.0 is detected.
---
 README       | 7 ++++---
 configure.ac | 4 ++++
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/README b/README
index 88613aa..1237e2c 100644
--- a/README
+++ b/README
@@ -16,9 +16,10 @@ daemon-managing daemon.
 ** Requirements
 
 This program requires Guile (the GNU Ubiquitous Intelligent Language
-for Extension), version 2.x.  It uses GOOPS, but as GOOPS is part of
-Guile, a normal Guile installation is sufficient.  It also uses
-readline, though it does not really depend on it.
+for Extension), version 2.0.13 or later (including 2.2.x).  It uses
+GOOPS, but as GOOPS is part of Guile, a normal Guile installation is
+sufficient.  It also uses readline, though it does not really depend
+on it.
 
 GNU Make is required to build the Shepherd.
 
diff --git a/configure.ac b/configure.ac
index d768885..9d8c2aa 100644
--- a/configure.ac
+++ b/configure.ac
@@ -27,6 +27,10 @@ GUILE_PKG([2.2 2.0])
 dnl Checks for programs.
 GUILE_PROGS
 
+if test "x$GUILE_EFFECTIVE_VERSION" = "x2.0"; then
+  PKG_CHECK_MODULES([GUILE], [guile-2.0 >= 2.0.13])
+fi
+
 guilemoduledir="${datarootdir}/guile/site/$GUILE_EFFECTIVE_VERSION"
 guileobjectdir="${libdir}/guile/$GUILE_EFFECTIVE_VERSION/site-ccache"
 AC_SUBST([guilemoduledir])
-- 
2.16.2

From e11708aba0fbafd4c83273ee1fa5147e54d1c80e Mon Sep 17 00:00:00 2001
From: Carlo Zancanaro <[email-masked]>
Date: Mon, 26 Mar 2018 14:49:18 +1100
Subject: [PATCH 2/3] Remove EINTR-safe, and all references to it.

* modules/shepherd/support.scm (EINTR-safe): Remove procedure and its export.
* modules/shepherd/service.scm (system*, system*): Remove now-unnecessary
  procedures.
  (waitpid*): Remove references to EINTR-safe.
* modules/shepherd.scm (main): Remove references to EINTR-safe.
---
 modules/shepherd.scm         |  7 +------
 modules/shepherd/service.scm | 35 +++++++++++++----------------------
 modules/shepherd/support.scm | 14 --------------
 3 files changed, 14 insertions(+), 42 deletions(-)

diff --git a/modules/shepherd.scm b/modules/shepherd.scm
index fede338..5d97598 100644
--- a/modules/shepherd.scm
+++ b/modules/shepherd.scm
@@ -241,12 +241,7 @@
           ;; Get commands from the standard input port.
           (process-textual-commands (current-input-port))
           ;; Process the data arriving at a socket.
-          (let ((sock   (open-server-socket socket-file))
-
-                ;; With Guile <= 2.0.9, we can get a system-error exception for
-                ;; EINTR, which happens anytime we receive a signal, such as
-                ;; SIGCHLD.  Thus, wrap the 'accept' call.
-                (accept (EINTR-safe accept)))
+          (let ((sock   (open-server-socket socket-file)))
 
             ;; Possibly write out our PID, which means we're ready to accept
             ;; connections.  XXX: What if we daemonized already?
diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm
index 7b062a1..93d3779 100644
--- a/modules/shepherd/service.scm
+++ b/modules/shepherd/service.scm
@@ -590,13 +590,6 @@ results."
                (apply action service the-action args))
              which-services))))
 
-;; EINTR-safe versions of 'system' and 'system*'.
-
-(define system*
-  (EINTR-safe (@ (guile) system*)))
-
-(define system
-  (EINTR-safe (@ (guile) system)))
 
 
 
@@ -981,21 +974,19 @@ returned in unspecified."
   (hashq-ref %services name '()))
 
 (define waitpid*
-  (let ((waitpid (EINTR-safe waitpid)))
-    (lambda (what flags)
-      "Like 'waitpid', but EINTR-safe, and return (0 . _) when there's no
-child left."
-      (catch 'system-error
-        (lambda ()
-          (waitpid what flags))
-        (lambda args
-          ;; Did we get ECHILD or something?  If we did, that's a problem,
-          ;; because this procedure is supposed to be called only upon
-          ;; SIGCHLD.
-          (let ((errno (system-error-errno args)))
-            (local-output "warning: 'waitpid' ~a failed unexpectedly: ~a"
-                          what (strerror errno))
-            '(0 . #f)))))))
+  (lambda (what flags)
+    "Like 'waitpid', and return (0 . _) when there's no child left."
+    (catch 'system-error
+      (lambda ()
+        (waitpid what flags))
+      (lambda args
+        ;; Did we get ECHILD or something?  If we did, that's a problem,
+        ;; because this procedure is supposed to be called only upon
+        ;; SIGCHLD.
+        (let ((errno (system-error-errno args)))
+          (local-output "warning: 'waitpid' ~a failed unexpectedly: ~a"
+                        what (strerror errno))
+          '(0 . #f))))))
 
 (define (handle-SIGCHLD signum)
   "Handle SIGCHLD, possibly by respawning the service that just died, or
diff --git a/modules/shepherd/support.scm b/modules/shepherd/support.scm
index 380866e..9f02719 100644
--- a/modules/shepherd/support.scm
+++ b/modules/shepherd/support.scm
@@ -30,7 +30,6 @@
 
             catch-system-error
             with-system-error-handling
-            EINTR-safe
             with-atomic-file-output
             mkdir-p
             with-directory-excursion
@@ -127,19 +126,6 @@ turned into user error messages."
    (lambda ()
      body ...)))
 
-(define (EINTR-safe proc)
-  "Wrap PROC so that if a 'system-error' exception with EINTR is raised (that
-was possible up to Guile 2.0.9 included) the call to PROC is restarted."
-  (lambda args
-    (let loop ()
-      (catch 'system-error
-        (lambda ()
-          (apply proc args))
-        (lambda args
-          (if (= EINTR (system-error-errno args))
-              (loop)
-              (apply throw args)))))))
-
 (define (with-atomic-file-output file proc)       ;copied from Guix
   "Call PROC with an output port for the file that is going to replace FILE.
 Upon success, FILE is atomically replaced by what has been written to the
-- 
2.16.2

From 63bc9339d88d8f1bd8a9b366774ce8e33d76dd00 Mon Sep 17 00:00:00 2001
From: Carlo Zancanaro <[email-masked]>
Date: Mon, 26 Mar 2018 14:55:32 +1100
Subject: [PATCH 3/3] Remove SIGALRM hack.

* modules/shepherd.scm (main): Remove SIGALRM hack for guile <= 2.0.9.
---
 modules/shepherd.scm | 9 ---------
 1 file changed, 9 deletions(-)

diff --git a/modules/shepherd.scm b/modules/shepherd.scm
index 5d97598..69fd69d 100644
--- a/modules/shepherd.scm
+++ b/modules/shepherd.scm
@@ -207,15 +207,6 @@
                (apply format #f (gettext (cadr args)) (caddr args))
                (quit 1))))
 
-      (when (provided? 'threads)
-        ;; XXX: This terrible hack allows us to make sure that signal handlers
-        ;; get a chance to run in a timely fashion.  Without it, after an EINTR,
-        ;; we could restart the accept(2) call below before the corresponding
-        ;; async has been queued.  See the thread at
-        ;; <https://lists.gnu.org/archive/html/guile-devel/2013-07/msg00004.html>.
-        (sigaction SIGALRM (lambda _ (alarm 1)))
-        (alarm 1))
-
       ;; Stop everything when we get SIGINT.  When running as PID 1, that means
       ;; rebooting; this is what happens when pressing ctrl-alt-del, see
       ;; ctrlaltdel(8).
-- 
2.16.2