21baacbaf7030450201c34380f430ef692cec52a
[jackhill/guix/guix.git] / gnu / services / shepherd.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
4 ;;; Copyright © 2018 Carlo Zancanaro <carlo@zancanaro.id.au>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (gnu services shepherd)
22 #:use-module (guix ui)
23 #:use-module (guix sets)
24 #:use-module (guix gexp)
25 #:use-module (guix store)
26 #:use-module (guix records)
27 #:use-module (guix derivations) ;imported-modules, etc.
28 #:use-module (gnu services)
29 #:use-module (gnu services herd)
30 #:use-module (gnu packages admin)
31 #:use-module (ice-9 match)
32 #:use-module (ice-9 vlist)
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-26)
35 #:use-module (srfi srfi-34)
36 #:use-module (srfi srfi-35)
37 #:export (shepherd-root-service-type
38 %shepherd-root-service
39 shepherd-service-type
40
41 shepherd-service
42 shepherd-service?
43 shepherd-service-documentation
44 shepherd-service-provision
45 shepherd-service-canonical-name
46 shepherd-service-requirement
47 shepherd-service-one-shot?
48 shepherd-service-respawn?
49 shepherd-service-start
50 shepherd-service-stop
51 shepherd-service-auto-start?
52 shepherd-service-modules
53
54 shepherd-action
55 shepherd-action?
56 shepherd-action-name
57 shepherd-action-documentation
58 shepherd-action-procedure
59
60 %default-modules
61
62 shepherd-service-file
63 %containerized-shepherd-service
64
65 shepherd-service-lookup-procedure
66 shepherd-service-back-edges
67 shepherd-service-upgrade))
68
69 ;;; Commentary:
70 ;;;
71 ;;; Instantiating system services as a shepherd configuration file.
72 ;;;
73 ;;; Code:
74
75
76 (define (shepherd-boot-gexp services)
77 #~(begin
78 ;; Keep track of the booted system.
79 (false-if-exception (delete-file "/run/booted-system"))
80 (symlink (readlink "/run/current-system")
81 "/run/booted-system")
82
83 ;; Close any remaining open file descriptors to be on the safe
84 ;; side. This must be the very last thing we do, because
85 ;; Guile has internal FDs such as 'sleep_pipe' that need to be
86 ;; alive.
87 (let loop ((fd 3))
88 (when (< fd 1024)
89 (false-if-exception (close-fdes fd))
90 (loop (+ 1 fd))))
91
92 ;; Start shepherd.
93 (execl #$(file-append shepherd "/bin/shepherd")
94 "shepherd" "--config"
95 #$(shepherd-configuration-file services))))
96
97 (define shepherd-root-service-type
98 (service-type
99 (name 'shepherd-root)
100 ;; Extending the root shepherd service (aka. PID 1) happens by
101 ;; concatenating the list of services provided by the extensions.
102 (compose concatenate)
103 (extend append)
104 (extensions (list (service-extension boot-service-type
105 shepherd-boot-gexp)
106 (service-extension profile-service-type
107 (const (list shepherd)))))))
108
109 (define %shepherd-root-service
110 ;; The root shepherd service, aka. PID 1. Its parameter is a list of
111 ;; <shepherd-service> objects.
112 (service shepherd-root-service-type '()))
113
114 (define-syntax shepherd-service-type
115 (syntax-rules ()
116 "Return a <service-type> denoting a simple shepherd service--i.e., the type
117 for a service that extends SHEPHERD-ROOT-SERVICE-TYPE and nothing else. When
118 DEFAULT is given, use it as the service's default value."
119 ((_ service-name proc default)
120 (service-type
121 (name service-name)
122 (extensions
123 (list (service-extension shepherd-root-service-type
124 (compose list proc))))
125 (default-value default)))
126 ((_ service-name proc)
127 (service-type
128 (name service-name)
129 (extensions
130 (list (service-extension shepherd-root-service-type
131 (compose list proc))))))))
132
133 (define %default-imported-modules
134 ;; Default set of modules imported for a service's consumption.
135 '((guix build utils)
136 (guix build syscalls)))
137
138 (define %default-modules
139 ;; Default set of modules visible in a service's file.
140 `((shepherd service)
141 (oop goops)
142 (guix build utils)
143 (guix build syscalls)))
144
145 (define-record-type* <shepherd-service>
146 shepherd-service make-shepherd-service
147 shepherd-service?
148 (documentation shepherd-service-documentation ;string
149 (default "[No documentation.]"))
150 (provision shepherd-service-provision) ;list of symbols
151 (requirement shepherd-service-requirement ;list of symbols
152 (default '()))
153 (one-shot? shepherd-service-one-shot? ;Boolean
154 (default #f))
155 (respawn? shepherd-service-respawn? ;Boolean
156 (default #t))
157 (start shepherd-service-start) ;g-expression (procedure)
158 (stop shepherd-service-stop ;g-expression (procedure)
159 (default #~(const #f)))
160 (actions shepherd-service-actions ;list of <shepherd-action>
161 (default '()))
162 (auto-start? shepherd-service-auto-start? ;Boolean
163 (default #t))
164 (modules shepherd-service-modules ;list of module names
165 (default %default-modules)))
166
167 (define-record-type* <shepherd-action>
168 shepherd-action make-shepherd-action
169 shepherd-action?
170 (name shepherd-action-name) ;symbol
171 (procedure shepherd-action-procedure) ;gexp
172 (documentation shepherd-action-documentation)) ;string
173
174 (define (shepherd-service-canonical-name service)
175 "Return the 'canonical name' of SERVICE."
176 (first (shepherd-service-provision service)))
177
178 (define (assert-valid-graph services)
179 "Raise an error if SERVICES does not define a valid shepherd service graph,
180 for instance if a service requires a nonexistent service, or if more than one
181 service uses a given name.
182
183 These are constraints that shepherd's 'register-service' verifies but we'd
184 better verify them here statically than wait until PID 1 halts with an
185 assertion failure."
186 (define provisions
187 ;; The set of provisions (symbols). Bail out if a symbol is given more
188 ;; than once.
189 (fold (lambda (service set)
190 (define (assert-unique symbol)
191 (when (set-contains? set symbol)
192 (raise (condition
193 (&message
194 (message
195 (format #f (G_ "service '~a' provided more than once")
196 symbol)))))))
197
198 (for-each assert-unique (shepherd-service-provision service))
199 (fold set-insert set (shepherd-service-provision service)))
200 (setq 'shepherd)
201 services))
202
203 (define (assert-satisfied-requirements service)
204 ;; Bail out if the requirements of SERVICE aren't satisfied.
205 (for-each (lambda (requirement)
206 (unless (set-contains? provisions requirement)
207 (raise (condition
208 (&message
209 (message
210 (format #f (G_ "service '~a' requires '~a', \
211 which is not provided by any service")
212 (match (shepherd-service-provision service)
213 ((head . _) head)
214 (_ service))
215 requirement)))))))
216 (shepherd-service-requirement service)))
217
218 (for-each assert-satisfied-requirements services))
219
220 (define (shepherd-service-file-name service)
221 "Return the file name where the initialization code for SERVICE is to be
222 stored."
223 (let ((provisions (string-join (map symbol->string
224 (shepherd-service-provision service)))))
225 (string-append "shepherd-"
226 (string-map (match-lambda
227 (#\/ #\-)
228 (#\ #\-)
229 (chr chr))
230 provisions)
231 ".scm")))
232
233 (define (shepherd-service-file service)
234 "Return a file defining SERVICE."
235 (scheme-file (shepherd-service-file-name service)
236 (with-imported-modules %default-imported-modules
237 #~(begin
238 (use-modules #$@(shepherd-service-modules service))
239
240 (make <service>
241 #:docstring '#$(shepherd-service-documentation service)
242 #:provides '#$(shepherd-service-provision service)
243 #:requires '#$(shepherd-service-requirement service)
244
245 ;; The 'one-shot?' slot is new in Shepherd 0.6.0.
246 ;; Older versions ignore it.
247 #:one-shot? '#$(shepherd-service-one-shot? service)
248
249 #:respawn? '#$(shepherd-service-respawn? service)
250 #:start #$(shepherd-service-start service)
251 #:stop #$(shepherd-service-stop service)
252 #:actions
253 (make-actions
254 #$@(map (match-lambda
255 (($ <shepherd-action> name proc doc)
256 #~(#$name #$doc #$proc)))
257 (shepherd-service-actions service))))))))
258
259 (define (shepherd-configuration-file services)
260 "Return the shepherd configuration file for SERVICES."
261 (assert-valid-graph services)
262
263 (let ((files (map shepherd-service-file services)))
264 (define config
265 #~(begin
266 (use-modules (srfi srfi-34)
267 (system repl error-handling))
268
269 ;; Arrange to spawn a REPL if something goes wrong. This is better
270 ;; than a kernel panic.
271 (call-with-error-handling
272 (lambda ()
273 (apply register-services (map primitive-load '#$files))
274
275 ;; guix-daemon 0.6 aborts if 'PATH' is undefined, so work around
276 ;; it.
277 (setenv "PATH" "/run/current-system/profile/bin")
278
279 (format #t "starting services...~%")
280 (for-each (lambda (service)
281 ;; In the Shepherd 0.3 the 'start' method can raise
282 ;; '&action-runtime-error' if it fails, so protect
283 ;; against it. (XXX: 'action-runtime-error?' is not
284 ;; exported is 0.3, hence 'service-error?'.)
285 (guard (c ((service-error? c)
286 (format (current-error-port)
287 "failed to start service '~a'~%"
288 service)))
289 (start service)))
290 '#$(append-map shepherd-service-provision
291 (filter shepherd-service-auto-start?
292 services)))
293
294 ;; Hang up stdin. At this point, we assume that 'start' methods
295 ;; that required user interaction on the console (e.g.,
296 ;; 'cryptsetup open' invocations, post-fsck emergency REPL) have
297 ;; completed. User interaction becomes impossible after this
298 ;; call; this avoids situations where services wrongfully lead
299 ;; PID 1 to read from stdin (the console), which users may not
300 ;; have access to (see <https://bugs.gnu.org/23697>).
301 (redirect-port (open-input-file "/dev/null")
302 (current-input-port))))))
303
304 (scheme-file "shepherd.conf" config)))
305
306 (define* (shepherd-service-lookup-procedure services
307 #:optional
308 (provision
309 shepherd-service-provision))
310 "Return a procedure that, when passed a symbol, return the item among
311 SERVICES that provides this symbol. PROVISION must be a one-argument
312 procedure that takes a service and returns the list of symbols it provides."
313 (let ((services (fold (lambda (service result)
314 (fold (cut vhash-consq <> service <>)
315 result
316 (provision service)))
317 vlist-null
318 services)))
319 (lambda (name)
320 (match (vhash-assq name services)
321 ((_ . service) service)
322 (#f #f)))))
323
324 (define* (shepherd-service-back-edges services
325 #:key
326 (provision shepherd-service-provision)
327 (requirement shepherd-service-requirement))
328 "Return a procedure that, when given a <shepherd-service> from SERVICES,
329 returns the list of <shepherd-service> that depend on it.
330
331 Use PROVISION and REQUIREMENT as one-argument procedures that return the
332 symbols provided/required by a service."
333 (define provision->service
334 (shepherd-service-lookup-procedure services provision))
335
336 (define edges
337 (fold (lambda (service edges)
338 (fold (lambda (requirement edges)
339 (vhash-consq (provision->service requirement) service
340 edges))
341 edges
342 (requirement service)))
343 vlist-null
344 services))
345
346 (lambda (service)
347 (vhash-foldq* cons '() service edges)))
348
349 (define %containerized-shepherd-service
350 ;; XXX: This service works around a bug in the Shepherd 0.5.0: shepherd
351 ;; calls reboot(2) (via 'disable-reboot-on-ctrl-alt-del') when it starts,
352 ;; but in a container that fails with EINVAL. This was fixed in Shepherd
353 ;; commit 92e806bac1abaeeaf5d60f0ab50d1ae85ba6a62f.
354 (simple-service 'containerized-shepherd
355 shepherd-root-service-type
356 (list (shepherd-service
357 (provision '(containerized-shepherd))
358 (start #~(lambda ()
359 (set! (@@ (shepherd)
360 disable-reboot-on-ctrl-alt-del)
361 (const #t))
362 #t))))))
363
364 (define (shepherd-service-upgrade live target)
365 "Return two values: the subset of LIVE (a list of <live-service>) that needs
366 to be unloaded, and the subset of TARGET (a list of <shepherd-service>) that
367 need to be restarted to complete their upgrade."
368 (define (essential? service)
369 (memq (first (live-service-provision service))
370 '(root shepherd)))
371
372 (define lookup-target
373 (shepherd-service-lookup-procedure target
374 shepherd-service-provision))
375
376 (define lookup-live
377 (shepherd-service-lookup-procedure live
378 live-service-provision))
379
380 (define (running? service)
381 (and=> (lookup-live (shepherd-service-canonical-name service))
382 live-service-running))
383
384 (define live-service-dependents
385 (shepherd-service-back-edges live
386 #:provision live-service-provision
387 #:requirement live-service-requirement))
388
389 (define (obsolete? service)
390 (match (lookup-target (first (live-service-provision service)))
391 (#f (every obsolete? (live-service-dependents service)))
392 (_ #f)))
393
394 (define to-restart
395 ;; Restart services that are currently running.
396 (filter running? target))
397
398 (define to-unload
399 ;; Unload services that are no longer required.
400 (remove essential? (filter obsolete? live)))
401
402 (values to-unload to-restart))
403
404 ;;; shepherd.scm ends here