1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2018 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
4 ;;; Copyright © 2018 Carlo Zancanaro <carlo@zancanaro.id.au>
6 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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
43 shepherd-service-documentation
44 shepherd-service-provision
45 shepherd-service-canonical-name
46 shepherd-service-requirement
47 shepherd-service-respawn?
48 shepherd-service-start
50 shepherd-service-auto-start?
51 shepherd-service-modules
56 shepherd-action-documentation
57 shepherd-action-procedure
62 %containerized-shepherd-service
64 shepherd-service-lookup-procedure
65 shepherd-service-back-edges
66 shepherd-service-upgrade))
70 ;;; Instantiating system services as a shepherd configuration file.
75 (define (shepherd-boot-gexp services)
77 ;; Keep track of the booted system.
78 (false-if-exception (delete-file "/run/booted-system"))
79 (symlink (readlink "/run/current-system")
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
88 (false-if-exception (close-fdes fd))
92 (execl #$(file-append shepherd "/bin/shepherd")
94 #$(shepherd-configuration-file services))))
96 (define shepherd-root-service-type
99 ;; Extending the root shepherd service (aka. PID 1) happens by
100 ;; concatenating the list of services provided by the extensions.
101 (compose concatenate)
103 (extensions (list (service-extension boot-service-type
105 (service-extension profile-service-type
106 (const (list shepherd)))))))
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 '()))
113 (define-syntax shepherd-service-type
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)
122 (list (service-extension shepherd-root-service-type
123 (compose list proc))))
124 (default-value default)))
125 ((_ service-name proc)
129 (list (service-extension shepherd-root-service-type
130 (compose list proc))))))))
132 (define %default-imported-modules
133 ;; Default set of modules imported for a service's consumption.
135 (guix build syscalls)))
137 (define %default-modules
138 ;; Default set of modules visible in a service's file.
142 (guix build syscalls)))
144 (define-record-type* <shepherd-service>
145 shepherd-service make-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
152 (respawn? shepherd-service-respawn? ;Boolean
154 (start shepherd-service-start) ;g-expression (procedure)
155 (stop shepherd-service-stop ;g-expression (procedure)
156 (default #~(const #f)))
157 (actions shepherd-service-actions ;list of <shepherd-action>
159 (auto-start? shepherd-service-auto-start? ;Boolean
161 (modules shepherd-service-modules ;list of module names
162 (default %default-modules)))
164 (define-record-type* <shepherd-action>
165 shepherd-action make-shepherd-action
167 (name shepherd-action-name) ;symbol
168 (procedure shepherd-action-procedure) ;gexp
169 (documentation shepherd-action-documentation)) ;string
171 (define (shepherd-service-canonical-name service)
172 "Return the 'canonical name' of SERVICE."
173 (first (shepherd-service-provision service)))
175 (define (assert-valid-graph services)
176 "Raise an error if SERVICES does not define a valid shepherd service graph,
177 for instance if a service requires a nonexistent service, or if more than one
178 service uses a given name.
180 These are constraints that shepherd's 'register-service' verifies but we'd
181 better verify them here statically than wait until PID 1 halts with an
184 ;; The set of provisions (symbols). Bail out if a symbol is given more
186 (fold (lambda (service set)
187 (define (assert-unique symbol)
188 (when (set-contains? set symbol)
192 (format #f (G_ "service '~a' provided more than once")
195 (for-each assert-unique (shepherd-service-provision service))
196 (fold set-insert set (shepherd-service-provision service)))
200 (define (assert-satisfied-requirements service)
201 ;; Bail out if the requirements of SERVICE aren't satisfied.
202 (for-each (lambda (requirement)
203 (unless (set-contains? provisions requirement)
207 (format #f (G_ "service '~a' requires '~a', \
208 which is not provided by any service")
209 (match (shepherd-service-provision service)
213 (shepherd-service-requirement service)))
215 (for-each assert-satisfied-requirements services))
217 (define (shepherd-service-file-name service)
218 "Return the file name where the initialization code for SERVICE is to be
220 (let ((provisions (string-join (map symbol->string
221 (shepherd-service-provision service)))))
222 (string-append "shepherd-"
223 (string-map (match-lambda
230 (define (shepherd-service-file service)
231 "Return a file defining SERVICE."
232 (scheme-file (shepherd-service-file-name service)
233 (with-imported-modules %default-imported-modules
235 (use-modules #$@(shepherd-service-modules service))
238 #:docstring '#$(shepherd-service-documentation service)
239 #:provides '#$(shepherd-service-provision service)
240 #:requires '#$(shepherd-service-requirement service)
241 #:respawn? '#$(shepherd-service-respawn? service)
242 #:start #$(shepherd-service-start service)
243 #:stop #$(shepherd-service-stop service)
246 #$@(map (match-lambda
247 (($ <shepherd-action> name proc doc)
248 #~(#$name #$doc #$proc)))
249 (shepherd-service-actions service))))))))
251 (define (shepherd-configuration-file services)
252 "Return the shepherd configuration file for SERVICES."
253 (assert-valid-graph services)
255 (let ((files (map shepherd-service-file services)))
258 (use-modules (srfi srfi-34)
259 (system repl error-handling))
261 ;; Arrange to spawn a REPL if something goes wrong. This is better
262 ;; than a kernel panic.
263 (call-with-error-handling
265 (apply register-services (map primitive-load '#$files))
267 ;; guix-daemon 0.6 aborts if 'PATH' is undefined, so work around
269 (setenv "PATH" "/run/current-system/profile/bin")
271 (format #t "starting services...~%")
272 (for-each (lambda (service)
273 ;; In the Shepherd 0.3 the 'start' method can raise
274 ;; '&action-runtime-error' if it fails, so protect
275 ;; against it. (XXX: 'action-runtime-error?' is not
276 ;; exported is 0.3, hence 'service-error?'.)
277 (guard (c ((service-error? c)
278 (format (current-error-port)
279 "failed to start service '~a'~%"
282 '#$(append-map shepherd-service-provision
283 (filter shepherd-service-auto-start?
286 (scheme-file "shepherd.conf" config)))
288 (define* (shepherd-service-lookup-procedure services
291 shepherd-service-provision))
292 "Return a procedure that, when passed a symbol, return the item among
293 SERVICES that provides this symbol. PROVISION must be a one-argument
294 procedure that takes a service and returns the list of symbols it provides."
295 (let ((services (fold (lambda (service result)
296 (fold (cut vhash-consq <> service <>)
298 (provision service)))
302 (match (vhash-assq name services)
303 ((_ . service) service)
306 (define* (shepherd-service-back-edges services
308 (provision shepherd-service-provision)
309 (requirement shepherd-service-requirement))
310 "Return a procedure that, when given a <shepherd-service> from SERVICES,
311 returns the list of <shepherd-service> that depend on it.
313 Use PROVISION and REQUIREMENT as one-argument procedures that return the
314 symbols provided/required by a service."
315 (define provision->service
316 (shepherd-service-lookup-procedure services provision))
319 (fold (lambda (service edges)
320 (fold (lambda (requirement edges)
321 (vhash-consq (provision->service requirement) service
324 (requirement service)))
329 (vhash-foldq* cons '() service edges)))
331 (define %containerized-shepherd-service
332 ;; XXX: This service works around a bug in the Shepherd 0.5.0: shepherd
333 ;; calls reboot(2) (via 'disable-reboot-on-ctrl-alt-del') when it starts,
334 ;; but in a container that fails with EINVAL. This was fixed in Shepherd
335 ;; commit 92e806bac1abaeeaf5d60f0ab50d1ae85ba6a62f.
336 (simple-service 'containerized-shepherd
337 shepherd-root-service-type
338 (list (shepherd-service
339 (provision '(containerized-shepherd))
342 disable-reboot-on-ctrl-alt-del)
346 (define (shepherd-service-upgrade live target)
347 "Return two values: the subset of LIVE (a list of <live-service>) that needs
348 to be unloaded, and the subset of TARGET (a list of <shepherd-service>) that
349 need to be restarted to complete their upgrade."
350 (define (essential? service)
351 (memq (first (live-service-provision service))
354 (define lookup-target
355 (shepherd-service-lookup-procedure target
356 shepherd-service-provision))
359 (shepherd-service-lookup-procedure live
360 live-service-provision))
362 (define (running? service)
363 (and=> (lookup-live (shepherd-service-canonical-name service))
364 live-service-running))
366 (define live-service-dependents
367 (shepherd-service-back-edges live
368 #:provision live-service-provision
369 #:requirement live-service-requirement))
371 (define (obsolete? service)
372 (match (lookup-target (first (live-service-provision service)))
373 (#f (every obsolete? (live-service-dependents service)))
377 ;; Restart services that are currently running.
378 (filter running? target))
381 ;; Unload services that are no longer required.
382 (remove essential? (filter obsolete? live)))
384 (values to-unload to-restart))
386 ;;; shepherd.scm ends here