service: shepherd: Guard against exceptions raised by 'start'.
[jackhill/guix/guix.git] / gnu / services / shepherd.scm
CommitLineData
db4fdc04 1;;; GNU Guix --- Functional package management for GNU
c273d81b 2;;; Copyright © 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
db4fdc04
LC
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
0190c1c0 19(define-module (gnu services shepherd)
116244df
LC
20 #:use-module (guix ui)
21 #:use-module (guix sets)
b5f4e686 22 #:use-module (guix gexp)
e87f0591 23 #:use-module (guix store)
db4fdc04 24 #:use-module (guix monads)
0adfe95a 25 #:use-module (guix records)
e87f0591 26 #:use-module (guix derivations) ;imported-modules, etc.
db4fdc04 27 #:use-module (gnu services)
0adfe95a 28 #:use-module (gnu packages admin)
db4fdc04 29 #:use-module (ice-9 match)
80a67734 30 #:use-module (ice-9 vlist)
db4fdc04 31 #:use-module (srfi srfi-1)
80a67734 32 #:use-module (srfi srfi-26)
116244df
LC
33 #:use-module (srfi srfi-34)
34 #:use-module (srfi srfi-35)
d4053c71
AK
35 #:export (shepherd-root-service-type
36 %shepherd-root-service
37 shepherd-service-type
38
39 shepherd-service
40 shepherd-service?
41 shepherd-service-documentation
42 shepherd-service-provision
240b57f0 43 shepherd-service-canonical-name
d4053c71
AK
44 shepherd-service-requirement
45 shepherd-service-respawn?
46 shepherd-service-start
47 shepherd-service-stop
48 shepherd-service-auto-start?
49 shepherd-service-modules
50 shepherd-service-imported-modules
fae685b9
LC
51
52 %default-imported-modules
53 %default-modules
80a67734 54
240b57f0
LC
55 shepherd-service-file
56
d4053c71 57 shepherd-service-back-edges))
db4fdc04
LC
58
59;;; Commentary:
60;;;
fe1ad5f5 61;;; Instantiating system services as a shepherd configuration file.
db4fdc04
LC
62;;;
63;;; Code:
64
0adfe95a 65
d4053c71 66(define (shepherd-boot-gexp services)
fe1ad5f5 67 (mlet %store-monad ((shepherd-conf (shepherd-configuration-file services)))
0adfe95a
LC
68 (return #~(begin
69 ;; Keep track of the booted system.
70 (false-if-exception (delete-file "/run/booted-system"))
71 (symlink (readlink "/run/current-system")
72 "/run/booted-system")
73
74 ;; Close any remaining open file descriptors to be on the safe
75 ;; side. This must be the very last thing we do, because
76 ;; Guile has internal FDs such as 'sleep_pipe' that need to be
77 ;; alive.
78 (let loop ((fd 3))
79 (when (< fd 1024)
80 (false-if-exception (close-fdes fd))
81 (loop (+ 1 fd))))
82
34044d55
AK
83 ;; Start shepherd.
84 (execl (string-append #$shepherd "/bin/shepherd")
fe1ad5f5 85 "shepherd" "--config" #$shepherd-conf)))))
0adfe95a 86
d4053c71 87(define shepherd-root-service-type
0adfe95a 88 (service-type
d4053c71
AK
89 (name 'shepherd-root)
90 ;; Extending the root shepherd service (aka. PID 1) happens by
91 ;; concatenating the list of services provided by the extensions.
0adfe95a
LC
92 (compose concatenate)
93 (extend append)
d4053c71
AK
94 (extensions (list (service-extension boot-service-type
95 shepherd-boot-gexp)
c273d81b 96 (service-extension profile-service-type
34044d55 97 (const (list shepherd)))))))
0adfe95a 98
d4053c71
AK
99(define %shepherd-root-service
100 ;; The root shepherd service, aka. PID 1. Its parameter is a list of
101 ;; <shepherd-service> objects.
102 (service shepherd-root-service-type '()))
0adfe95a 103
d4053c71
AK
104(define-syntax-rule (shepherd-service-type service-name proc)
105 "Return a <service-type> denoting a simple shepherd service--i.e., the type
106for a service that extends SHEPHERD-ROOT-SERVICE-TYPE and nothing else."
0adfe95a 107 (service-type
00184239 108 (name service-name)
0adfe95a 109 (extensions
d4053c71 110 (list (service-extension shepherd-root-service-type
0adfe95a
LC
111 (compose list proc))))))
112
fae685b9
LC
113(define %default-imported-modules
114 ;; Default set of modules imported for a service's consumption.
115 '((guix build utils)
479b417b 116 (guix build syscalls)))
fae685b9
LC
117
118(define %default-modules
119 ;; Default set of modules visible in a service's file.
34044d55 120 `((shepherd service)
fae685b9 121 (oop goops)
fae685b9 122 (guix build utils)
479b417b 123 (guix build syscalls)))
fae685b9 124
d4053c71
AK
125(define-record-type* <shepherd-service>
126 shepherd-service make-shepherd-service
127 shepherd-service?
128 (documentation shepherd-service-documentation ;string
0adfe95a 129 (default "[No documentation.]"))
d4053c71
AK
130 (provision shepherd-service-provision) ;list of symbols
131 (requirement shepherd-service-requirement ;list of symbols
0adfe95a 132 (default '()))
d4053c71 133 (respawn? shepherd-service-respawn? ;Boolean
0adfe95a 134 (default #t))
d4053c71
AK
135 (start shepherd-service-start) ;g-expression (procedure)
136 (stop shepherd-service-stop ;g-expression (procedure)
0adfe95a 137 (default #~(const #f)))
d4053c71 138 (auto-start? shepherd-service-auto-start? ;Boolean
fae685b9 139 (default #t))
d4053c71 140 (modules shepherd-service-modules ;list of module names
fae685b9 141 (default %default-modules))
d4053c71 142 (imported-modules shepherd-service-imported-modules ;list of module names
fae685b9 143 (default %default-imported-modules)))
0adfe95a 144
240b57f0
LC
145(define (shepherd-service-canonical-name service)
146 "Return the 'canonical name' of SERVICE."
147 (first (shepherd-service-provision service)))
0adfe95a 148
2d2651e7 149(define (assert-valid-graph services)
d4053c71
AK
150 "Raise an error if SERVICES does not define a valid shepherd service graph,
151for instance if a service requires a nonexistent service, or if more than one
2d2651e7 152service uses a given name.
116244df 153
d4053c71
AK
154These are constraints that shepherd's 'register-service' verifies but we'd
155better verify them here statically than wait until PID 1 halts with an
156assertion failure."
2d2651e7
LC
157 (define provisions
158 ;; The set of provisions (symbols). Bail out if a symbol is given more
159 ;; than once.
160 (fold (lambda (service set)
161 (define (assert-unique symbol)
162 (when (set-contains? set symbol)
163 (raise (condition
164 (&message
165 (message
166 (format #f (_ "service '~a' provided more than once")
167 symbol)))))))
168
d4053c71
AK
169 (for-each assert-unique (shepherd-service-provision service))
170 (fold set-insert set (shepherd-service-provision service)))
171 (setq 'shepherd)
2d2651e7
LC
172 services))
173
174 (define (assert-satisfied-requirements service)
175 ;; Bail out if the requirements of SERVICE aren't satisfied.
176 (for-each (lambda (requirement)
177 (unless (set-contains? provisions requirement)
178 (raise (condition
179 (&message
180 (message
181 (format #f (_ "service '~a' requires '~a', \
182which is undefined")
d4053c71 183 (match (shepherd-service-provision service)
2d2651e7
LC
184 ((head . _) head)
185 (_ service))
186 requirement)))))))
d4053c71 187 (shepherd-service-requirement service)))
2d2651e7
LC
188
189 (for-each assert-satisfied-requirements services))
116244df 190
d4053c71 191(define (shepherd-service-file-name service)
fae685b9
LC
192 "Return the file name where the initialization code for SERVICE is to be
193stored."
194 (let ((provisions (string-join (map symbol->string
d4053c71
AK
195 (shepherd-service-provision service)))))
196 (string-append "shepherd-"
fae685b9
LC
197 (string-map (match-lambda
198 (#\/ #\-)
199 (chr chr))
200 provisions)
201 ".scm")))
202
d4053c71 203(define (shepherd-service-file service)
fae685b9 204 "Return a file defining SERVICE."
d4053c71 205 (gexp->file (shepherd-service-file-name service)
fae685b9 206 #~(begin
d4053c71 207 (use-modules #$@(shepherd-service-modules service))
fae685b9
LC
208
209 (make <service>
d4053c71
AK
210 #:docstring '#$(shepherd-service-documentation service)
211 #:provides '#$(shepherd-service-provision service)
212 #:requires '#$(shepherd-service-requirement service)
213 #:respawn? '#$(shepherd-service-respawn? service)
214 #:start #$(shepherd-service-start service)
215 #:stop #$(shepherd-service-stop service)))))
fae685b9 216
fe1ad5f5
AK
217(define (shepherd-configuration-file services)
218 "Return the shepherd configuration file for SERVICES."
23ed63a1 219 (define modules
fae685b9 220 (delete-duplicates
d4053c71 221 (append-map shepherd-service-imported-modules services)))
23ed63a1 222
2d2651e7 223 (assert-valid-graph services)
116244df 224
23ed63a1 225 (mlet %store-monad ((modules (imported-modules modules))
fae685b9 226 (compiled (compiled-modules modules))
d4053c71
AK
227 (files (mapm %store-monad
228 shepherd-service-file
229 services)))
23ed63a1
LC
230 (define config
231 #~(begin
232 (eval-when (expand load eval)
233 (set! %load-path (cons #$modules %load-path))
234 (set! %load-compiled-path
fae685b9
LC
235 (cons #$compiled %load-compiled-path)))
236
081bd3bd
LC
237 (use-modules (srfi srfi-34)
238 (system repl error-handling))
b9c7ed71
LC
239
240 ;; Arrange to spawn a REPL if loading one of FILES fails. This is
241 ;; better than a kernel panic.
242 (call-with-error-handling
243 (lambda ()
244 (apply register-services (map primitive-load '#$files))))
23ed63a1
LC
245
246 ;; guix-daemon 0.6 aborts if 'PATH' is undefined, so work around it.
b4140694 247 (setenv "PATH" "/run/current-system/profile/bin")
23ed63a1
LC
248
249 (format #t "starting services...~%")
081bd3bd
LC
250 (for-each (lambda (service)
251 ;; In the Shepherd 0.3 the 'start' method can raise
252 ;; '&action-runtime-error' if it fails, so protect
253 ;; against it. (XXX: 'action-runtime-error?' is not
254 ;; exported is 0.3, hence 'service-error?'.)
255 (guard (c ((service-error? c)
256 (format (current-error-port)
257 "failed to start service '~a'~%"
258 service)))
259 (start service)))
d4053c71
AK
260 '#$(append-map shepherd-service-provision
261 (filter shepherd-service-auto-start?
c5d735f7 262 services)))))
23ed63a1 263
fe1ad5f5 264 (gexp->file "shepherd.conf" config)))
db4fdc04 265
d4053c71
AK
266(define (shepherd-service-back-edges services)
267 "Return a procedure that, when given a <shepherd-service> from SERVICES,
268returns the list of <shepherd-service> that depend on it."
80a67734
LC
269 (define provision->service
270 (let ((services (fold (lambda (service result)
271 (fold (cut vhash-consq <> service <>)
272 result
d4053c71 273 (shepherd-service-provision service)))
80a67734
LC
274 vlist-null
275 services)))
276 (lambda (name)
277 (match (vhash-assq name services)
278 ((_ . service) service)
279 (#f #f)))))
280
281 (define edges
282 (fold (lambda (service edges)
283 (fold (lambda (requirement edges)
284 (vhash-consq (provision->service requirement) service
285 edges))
286 edges
d4053c71 287 (shepherd-service-requirement service)))
80a67734
LC
288 vlist-null
289 services))
290
291 (lambda (service)
292 (vhash-foldq* cons '() service edges)))
293
0190c1c0 294;;; shepherd.scm ends here