gnu: perl: Update home page.
[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
64 shepherd-service-lookup-procedure
65 shepherd-service-back-edges
66 shepherd-service-upgrade))
67
68 ;;; Commentary:
69 ;;;
70 ;;; Instantiating system services as a shepherd configuration file.
71 ;;;
72 ;;; Code:
73
74
75 (define (shepherd-boot-gexp services)
76 #~(begin
77 ;; Keep track of the booted system.
78 (false-if-exception (delete-file "/run/booted-system"))
79 (symlink (readlink "/run/current-system")
80 "/run/booted-system")
81
82 ;; Close any remaining open file descriptors to be on the safe
83 ;; side. This must be the very last thing we do, because
84 ;; Guile has internal FDs such as 'sleep_pipe' that need to be
85 ;; alive.
86 (let loop ((fd 3))
87 (when (< fd 1024)
88 (false-if-exception (close-fdes fd))
89 (loop (+ 1 fd))))
90
91 ;; Start shepherd.
92 (execl #$(file-append shepherd "/bin/shepherd")
93 "shepherd" "--config"
94 #$(shepherd-configuration-file services))))
95
96 (define shepherd-root-service-type
97 (service-type
98 (name 'shepherd-root)
99 ;; Extending the root shepherd service (aka. PID 1) happens by
100 ;; concatenating the list of services provided by the extensions.
101 (compose concatenate)
102 (extend append)
103 (extensions (list (service-extension boot-service-type
104 shepherd-boot-gexp)
105 (service-extension profile-service-type
106 (const (list shepherd)))))))
107
108 (define %shepherd-root-service
109 ;; The root shepherd service, aka. PID 1. Its parameter is a list of
110 ;; <shepherd-service> objects.
111 (service shepherd-root-service-type '()))
112
113 (define-syntax shepherd-service-type
114 (syntax-rules ()
115 "Return a <service-type> denoting a simple shepherd service--i.e., the type
116 for a service that extends SHEPHERD-ROOT-SERVICE-TYPE and nothing else. When
117 DEFAULT is given, use it as the service's default value."
118 ((_ service-name proc default)
119 (service-type
120 (name service-name)
121 (extensions
122 (list (service-extension shepherd-root-service-type
123 (compose list proc))))
124 (default-value default)))
125 ((_ service-name proc)
126 (service-type
127 (name service-name)
128 (extensions
129 (list (service-extension shepherd-root-service-type
130 (compose list proc))))))))
131
132 (define %default-imported-modules
133 ;; Default set of modules imported for a service's consumption.
134 '((guix build utils)
135 (guix build syscalls)))
136
137 (define %default-modules
138 ;; Default set of modules visible in a service's file.
139 `((shepherd service)
140 (oop goops)
141 (guix build utils)
142 (guix build syscalls)))
143
144 (define-record-type* <shepherd-service>
145 shepherd-service make-shepherd-service
146 shepherd-service?
147 (documentation shepherd-service-documentation ;string
148 (default "[No documentation.]"))
149 (provision shepherd-service-provision) ;list of symbols
150 (requirement shepherd-service-requirement ;list of symbols
151 (default '()))
152 (one-shot? shepherd-service-one-shot? ;Boolean
153 (default #f))
154 (respawn? shepherd-service-respawn? ;Boolean
155 (default #t))
156 (start shepherd-service-start) ;g-expression (procedure)
157 (stop shepherd-service-stop ;g-expression (procedure)
158 (default #~(const #f)))
159 (actions shepherd-service-actions ;list of <shepherd-action>
160 (default '()))
161 (auto-start? shepherd-service-auto-start? ;Boolean
162 (default #t))
163 (modules shepherd-service-modules ;list of module names
164 (default %default-modules)))
165
166 (define-record-type* <shepherd-action>
167 shepherd-action make-shepherd-action
168 shepherd-action?
169 (name shepherd-action-name) ;symbol
170 (procedure shepherd-action-procedure) ;gexp
171 (documentation shepherd-action-documentation)) ;string
172
173 (define (shepherd-service-canonical-name service)
174 "Return the 'canonical name' of SERVICE."
175 (first (shepherd-service-provision service)))
176
177 (define (assert-valid-graph services)
178 "Raise an error if SERVICES does not define a valid shepherd service graph,
179 for instance if a service requires a nonexistent service, or if more than one
180 service uses a given name.
181
182 These are constraints that shepherd's 'register-service' verifies but we'd
183 better verify them here statically than wait until PID 1 halts with an
184 assertion failure."
185 (define provisions
186 ;; The set of provisions (symbols). Bail out if a symbol is given more
187 ;; than once.
188 (fold (lambda (service set)
189 (define (assert-unique symbol)
190 (when (set-contains? set symbol)
191 (raise (condition
192 (&message
193 (message
194 (format #f (G_ "service '~a' provided more than once")
195 symbol)))))))
196
197 (for-each assert-unique (shepherd-service-provision service))
198 (fold set-insert set (shepherd-service-provision service)))
199 (setq 'shepherd)
200 services))
201
202 (define (assert-satisfied-requirements service)
203 ;; Bail out if the requirements of SERVICE aren't satisfied.
204 (for-each (lambda (requirement)
205 (unless (set-contains? provisions requirement)
206 (raise (condition
207 (&message
208 (message
209 (format #f (G_ "service '~a' requires '~a', \
210 which is not provided by any service")
211 (match (shepherd-service-provision service)
212 ((head . _) head)
213 (_ service))
214 requirement)))))))
215 (shepherd-service-requirement service)))
216
217 (for-each assert-satisfied-requirements services))
218
219 (define (shepherd-service-file-name service)
220 "Return the file name where the initialization code for SERVICE is to be
221 stored."
222 (let ((provisions (string-join (map symbol->string
223 (shepherd-service-provision service)))))
224 (string-append "shepherd-"
225 (string-map (match-lambda
226 (#\/ #\-)
227 (#\ #\-)
228 (chr chr))
229 provisions)
230 ".scm")))
231
232 (define (shepherd-service-file service)
233 "Return a file defining SERVICE."
234 (scheme-file (shepherd-service-file-name service)
235 (with-imported-modules %default-imported-modules
236 #~(begin
237 (use-modules #$@(shepherd-service-modules service))
238
239 (make <service>
240 #:docstring '#$(shepherd-service-documentation service)
241 #:provides '#$(shepherd-service-provision service)
242 #:requires '#$(shepherd-service-requirement service)
243
244 ;; The 'one-shot?' slot is new in Shepherd 0.6.0.
245 ;; Older versions ignore it.
246 #:one-shot? '#$(shepherd-service-one-shot? service)
247
248 #:respawn? '#$(shepherd-service-respawn? service)
249 #:start #$(shepherd-service-start service)
250 #:stop #$(shepherd-service-stop service)
251 #:actions
252 (make-actions
253 #$@(map (match-lambda
254 (($ <shepherd-action> name proc doc)
255 #~(#$name #$doc #$proc)))
256 (shepherd-service-actions service))))))))
257
258 (define (shepherd-configuration-file services)
259 "Return the shepherd configuration file for SERVICES."
260 (assert-valid-graph services)
261
262 (let ((files (map shepherd-service-file services)))
263 (define config
264 #~(begin
265 (use-modules (srfi srfi-34)
266 (system repl error-handling))
267
268 ;; Arrange to spawn a REPL if something goes wrong. This is better
269 ;; than a kernel panic.
270 (call-with-error-handling
271 (lambda ()
272 (apply register-services (map primitive-load '#$files))
273
274 ;; guix-daemon 0.6 aborts if 'PATH' is undefined, so work around
275 ;; it.
276 (setenv "PATH" "/run/current-system/profile/bin")
277
278 (format #t "starting services...~%")
279 (for-each (lambda (service)
280 ;; In the Shepherd 0.3 the 'start' method can raise
281 ;; '&action-runtime-error' if it fails, so protect
282 ;; against it. (XXX: 'action-runtime-error?' is not
283 ;; exported is 0.3, hence 'service-error?'.)
284 (guard (c ((service-error? c)
285 (format (current-error-port)
286 "failed to start service '~a'~%"
287 service)))
288 (start service)))
289 '#$(append-map shepherd-service-provision
290 (filter shepherd-service-auto-start?
291 services)))
292
293 ;; Hang up stdin. At this point, we assume that 'start' methods
294 ;; that required user interaction on the console (e.g.,
295 ;; 'cryptsetup open' invocations, post-fsck emergency REPL) have
296 ;; completed. User interaction becomes impossible after this
297 ;; call; this avoids situations where services wrongfully lead
298 ;; PID 1 to read from stdin (the console), which users may not
299 ;; have access to (see <https://bugs.gnu.org/23697>).
300 (redirect-port (open-input-file "/dev/null")
301 (current-input-port))))))
302
303 (scheme-file "shepherd.conf" config)))
304
305 (define* (shepherd-service-lookup-procedure services
306 #:optional
307 (provision
308 shepherd-service-provision))
309 "Return a procedure that, when passed a symbol, return the item among
310 SERVICES that provides this symbol. PROVISION must be a one-argument
311 procedure that takes a service and returns the list of symbols it provides."
312 (let ((services (fold (lambda (service result)
313 (fold (cut vhash-consq <> service <>)
314 result
315 (provision service)))
316 vlist-null
317 services)))
318 (lambda (name)
319 (match (vhash-assq name services)
320 ((_ . service) service)
321 (#f #f)))))
322
323 (define* (shepherd-service-back-edges services
324 #:key
325 (provision shepherd-service-provision)
326 (requirement shepherd-service-requirement))
327 "Return a procedure that, when given a <shepherd-service> from SERVICES,
328 returns the list of <shepherd-service> that depend on it.
329
330 Use PROVISION and REQUIREMENT as one-argument procedures that return the
331 symbols provided/required by a service."
332 (define provision->service
333 (shepherd-service-lookup-procedure services provision))
334
335 (define edges
336 (fold (lambda (service edges)
337 (fold (lambda (requirement edges)
338 (vhash-consq (provision->service requirement) service
339 edges))
340 edges
341 (requirement service)))
342 vlist-null
343 services))
344
345 (lambda (service)
346 (vhash-foldq* cons '() service edges)))
347
348 (define (shepherd-service-upgrade live target)
349 "Return two values: the subset of LIVE (a list of <live-service>) that needs
350 to be unloaded, and the subset of TARGET (a list of <shepherd-service>) that
351 need to be restarted to complete their upgrade."
352 (define (essential? service)
353 (memq (first (live-service-provision service))
354 '(root shepherd)))
355
356 (define lookup-target
357 (shepherd-service-lookup-procedure target
358 shepherd-service-provision))
359
360 (define lookup-live
361 (shepherd-service-lookup-procedure live
362 live-service-provision))
363
364 (define (running? service)
365 (and=> (lookup-live (shepherd-service-canonical-name service))
366 live-service-running))
367
368 (define live-service-dependents
369 (shepherd-service-back-edges live
370 #:provision live-service-provision
371 #:requirement live-service-requirement))
372
373 (define (obsolete? service)
374 (match (lookup-target (first (live-service-provision service)))
375 (#f (every obsolete? (live-service-dependents service)))
376 (_ #f)))
377
378 (define to-restart
379 ;; Restart services that are currently running.
380 (filter running? target))
381
382 (define to-unload
383 ;; Unload services that are no longer required.
384 (remove essential? (filter obsolete? live)))
385
386 (values to-unload to-restart))
387
388 ;;; shepherd.scm ends here