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>
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-one-shot?
48 shepherd-service-respawn?
49 shepherd-service-start
51 shepherd-service-auto-start?
52 shepherd-service-modules
57 shepherd-action-documentation
58 shepherd-action-procedure
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 (one-shot? shepherd-service-one-shot? ;Boolean
154 (respawn? shepherd-service-respawn? ;Boolean
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>
161 (auto-start? shepherd-service-auto-start? ;Boolean
163 (modules shepherd-service-modules ;list of module names
164 (default %default-modules)))
166 (define-record-type* <shepherd-action>
167 shepherd-action make-shepherd-action
169 (name shepherd-action-name) ;symbol
170 (procedure shepherd-action-procedure) ;gexp
171 (documentation shepherd-action-documentation)) ;string
173 (define (shepherd-service-canonical-name service)
174 "Return the 'canonical name' of SERVICE."
175 (first (shepherd-service-provision service)))
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.
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
186 ;; The set of provisions (symbols). Bail out if a symbol is given more
188 (fold (lambda (service set)
189 (define (assert-unique symbol)
190 (when (set-contains? set symbol)
194 (format #f (G_ "service '~a' provided more than once")
197 (for-each assert-unique (shepherd-service-provision service))
198 (fold set-insert set (shepherd-service-provision service)))
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)
209 (format #f (G_ "service '~a' requires '~a', \
210 which is not provided by any service")
211 (match (shepherd-service-provision service)
215 (shepherd-service-requirement service)))
217 (for-each assert-satisfied-requirements services))
219 (define (shepherd-service-file-name service)
220 "Return the file name where the initialization code for SERVICE is to be
222 (let ((provisions (string-join (map symbol->string
223 (shepherd-service-provision service)))))
224 (string-append "shepherd-"
225 (string-map (match-lambda
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
237 (use-modules #$@(shepherd-service-modules service))
240 #:docstring '#$(shepherd-service-documentation service)
241 #:provides '#$(shepherd-service-provision service)
242 #:requires '#$(shepherd-service-requirement service)
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)
248 #:respawn? '#$(shepherd-service-respawn? service)
249 #:start #$(shepherd-service-start service)
250 #:stop #$(shepherd-service-stop service)
253 #$@(map (match-lambda
254 (($ <shepherd-action> name proc doc)
255 #~(#$name #$doc #$proc)))
256 (shepherd-service-actions service))))))))
258 (define (shepherd-configuration-file services)
259 "Return the shepherd configuration file for SERVICES."
260 (assert-valid-graph services)
262 (let ((files (map shepherd-service-file services)))
265 (use-modules (srfi srfi-34)
266 (system repl error-handling))
268 ;; Arrange to spawn a REPL if something goes wrong. This is better
269 ;; than a kernel panic.
270 (call-with-error-handling
272 (apply register-services (map primitive-load '#$files))
274 ;; guix-daemon 0.6 aborts if 'PATH' is undefined, so work around
276 (setenv "PATH" "/run/current-system/profile/bin")
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'~%"
289 '#$(append-map shepherd-service-provision
290 (filter shepherd-service-auto-start?
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))))))
303 (scheme-file "shepherd.conf" config)))
305 (define* (shepherd-service-lookup-procedure services
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 <>)
315 (provision service)))
319 (match (vhash-assq name services)
320 ((_ . service) service)
323 (define* (shepherd-service-back-edges services
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.
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))
336 (fold (lambda (service edges)
337 (fold (lambda (requirement edges)
338 (vhash-consq (provision->service requirement) service
341 (requirement service)))
346 (vhash-foldq* cons '() service edges)))
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))
356 (define lookup-target
357 (shepherd-service-lookup-procedure target
358 shepherd-service-provision))
361 (shepherd-service-lookup-procedure live
362 live-service-provision))
364 (define (running? service)
365 (and=> (lookup-live (shepherd-service-canonical-name service))
366 live-service-running))
368 (define live-service-dependents
369 (shepherd-service-back-edges live
370 #:provision live-service-provision
371 #:requirement live-service-requirement))
373 (define (obsolete? service)
374 (match (lookup-target (first (live-service-provision service)))
375 (#f (every obsolete? (live-service-dependents service)))
379 ;; Restart services that are currently running.
380 (filter running? target))
383 ;; Unload services that are no longer required.
384 (remove essential? (filter obsolete? live)))
386 (values to-unload to-restart))
388 ;;; shepherd.scm ends here