aboutsummaryrefslogtreecommitdiff
path: root/src/connection/fork.lisp
blob: 593d4683001b89ab12f0125dbed0e529a06e604d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
;;; Consfigurator -- Lisp declarative configuration management system

;;; Copyright (C) 2021  Sean Whitton <spwhitton@spwhitton.name>

;;; This file is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3, or (at your option)
;;; any later version.

;;; This file is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.

;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(in-package :consfigurator.connection.fork)
(named-readtables:in-readtable :consfigurator)

(defclass fork-connection (local-connection) ())

(defgeneric post-fork (connection)
  (:documentation
   "Code to execute after forking/reinvoking but before calling CONTINUE-DEPLOY*.
Must not start up any threads."))

(defmethod continue-connection ((connection fork-connection) remaining)
  (multiple-value-bind (out err exit)
      (eval-in-grandchild `(post-fork ,connection)
                          `(continue-deploy* ,connection ',remaining))
    (when-let ((lines (lines out)))
      (inform t lines))
    (exit-code-to-retval
     exit
     :on-failure (failed-change
                  "~&Fork connection child failed; stderr was ~%~%~A" err))))


;;;; Dumping and then immediately reinvoking Lisp

(defclass init-hooks-connection (fork-connection) ()
  (:documentation "On SBCL, call POST-FORK using SB-EXT:*INIT-HOOKS*.

The primary purpose of this connection type is to obtain a truly
single-threaded context for the execution of POST-FORK."))

#+(and sbcl sb-thread)
(eval-when (:compile-toplevel :load-toplevel :execute)
  ;; UIOP:VERSION< cannot handle Debian-patched SBCL version numbers, so we
  ;; split it up ourselves.
  (destructuring-bind (major minor patch . rest)
      (mapcar (lambda (s) (parse-integer s :junk-allowed t))
              (split-string (lisp-implementation-version) :separator '(#\.)))
    (declare (ignore rest))
    (unless (or (> major 2)
                (and (= major 2)
                     (or (> minor 1) (and (= minor 1) (> patch 7)))))
      (pushnew 'older-sbcl *features*))))

#+sbcl
(defmethod continue-connection ((connection init-hooks-connection) remaining)
  (multiple-value-bind (out err exit)
      (eval-in-reinvoked
       `(push
         (lambda ()
           (handler-bind
               ((serious-condition
                  (lambda (c)
                    (trivial-backtrace:print-backtrace c :output *error-output*)
                    (uiop:quit 1))))
             ;; Handle the finaliser thread in older SBCL, before the change in
             ;; 2.1.8 to call *INIT-HOOKS* before starting system threads.
             #+consfigurator.connection.fork::older-sbcl
             (sb-int:with-system-mutex (sb-thread::*make-thread-lock*)
               (sb-impl::finalizer-thread-stop))
             (post-fork ,connection)
             #+consfigurator.connection.fork::older-sbcl
             (sb-impl::finalizer-thread-start)))
         sb-ext:*init-hooks*)
       `(continue-deploy* ,connection ',remaining))
    (when-let ((lines (lines out)))
      (inform t lines))
    (exit-code-to-retval
     exit
     :on-failure (failed-change
                  "~&Reinvoked Lisp image failed; stderr was ~%~%~A" err))))