1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu services herd)
21 #:use-module (srfi srfi-1)
22 #:use-module (srfi srfi-9)
23 #:use-module (srfi srfi-11)
24 #:use-module (srfi srfi-34)
25 #:use-module (srfi srfi-35)
26 #:use-module (ice-9 match)
27 #:export (%shepherd-socket-file
30 service-not-found-error?
31 service-not-found-error-service
32 action-not-found-error?
33 action-not-found-error-service
34 action-not-found-error-action
35 action-exception-error?
36 action-exception-error-service
37 action-exception-error-action
38 action-exception-error-key
39 action-exception-error-arguments
40 unknown-shepherd-error?
41 unknown-shepherd-error-sexp
45 live-service-provision
46 live-service-requirement
48 live-service-canonical-name
61 ;;; This module provides an interface to the GNU Shepherd, similar to the
62 ;;; 'herd' command. Essentially it implements a subset of the (shepherd comm)
63 ;;; module, but focusing only on the parts relevant to 'guix system
68 (define %shepherd-socket-file
69 (make-parameter "/var/run/shepherd/socket"))
71 (define* (open-connection #:optional (file (%shepherd-socket-file)))
72 "Open a connection to the daemon, using the Unix-domain socket at FILE, and
74 ;; The protocol is sexp-based and UTF-8-encoded.
75 (with-fluids ((%default-port-encoding "UTF-8"))
76 (let ((sock (socket PF_UNIX SOCK_STREAM 0))
77 (address (make-socket-address PF_UNIX file)))
80 (connect sock address)
81 (setvbuf sock 'block 1024)
85 (apply throw args))))))
87 (define-syntax-rule (with-shepherd connection body ...)
88 "Evaluate BODY... with CONNECTION bound to an open socket to PID 1."
89 (let ((connection (open-connection)))
95 (close-port connection)))))
97 (define-condition-type &shepherd-error &error
100 (define-condition-type &service-not-found-error &shepherd-error
101 service-not-found-error?
102 (service service-not-found-error-service))
104 (define-condition-type &action-not-found-error &shepherd-error
105 action-not-found-error?
106 (service action-not-found-error-service)
107 (action action-not-found-error-action))
109 (define-condition-type &action-exception-error &shepherd-error
110 action-exception-error?
111 (service action-exception-error-service)
112 (action action-exception-error-action)
113 (key action-exception-error-key)
114 (args action-exception-error-arguments))
116 (define-condition-type &unknown-shepherd-error &shepherd-error
117 unknown-shepherd-error?
118 (sexp unknown-shepherd-error-sexp))
120 (define (raise-shepherd-error error)
121 "Raise an error condition corresponding to ERROR, an sexp received by a
122 shepherd client in reply to COMMAND, a command object. Return #t if ERROR
123 does not denote an error."
125 (('error ('version 0 x ...) 'service-not-found service)
126 (raise (condition (&service-not-found-error
127 (service service)))))
128 (('error ('version 0 x ...) 'action-not-found action service)
129 (raise (condition (&action-not-found-error
132 (('error ('version 0 x ...) 'action-exception action service
134 (raise (condition (&action-exception-error
137 (key key) (args args)))))
139 (raise (condition (&unknown-shepherd-error (sexp error)))))
143 (define (display-message message)
144 (format (current-error-port) "shepherd: ~a~%" message))
146 (define* (invoke-action service action arguments cont)
147 "Invoke ACTION on SERVICE with ARGUMENTS. On success, call CONT with the
148 list of results (one result per instance with the name SERVICE). Otherwise
151 (write `(shepherd-command (version 0)
154 (arguments ,arguments)
155 (directory ,(getcwd)))
160 (('reply ('version 0 _ ...) ('result result) ('error #f)
161 ('messages messages))
162 (for-each display-message messages)
164 (('reply ('version 0 x ...) ('result y) ('error error)
165 ('messages messages))
166 (for-each display-message messages)
167 (raise-shepherd-error error)
173 (define-syntax-rule (with-shepherd-action service (action args ...)
175 "Invoke ACTION on SERVICE with the given ARGS, and evaluate BODY with RESULT
176 bound to the action's result."
177 (invoke-action service action (list args ...)
178 (lambda (result) body ...)))
180 (define-syntax alist-let*
182 "Bind the given KEYs in EXP to the corresponding items in ALIST. ALIST
183 is assumed to be a list of two-element tuples rather than a traditional list
185 ((_ alist (key ...) exp ...)
186 (let ((key (and=> (assoc-ref alist 'key) car)) ...)
189 ;; Information about live Shepherd services.
190 (define-record-type <live-service>
191 (live-service provision requirement running)
193 (provision live-service-provision) ;list of symbols
194 (requirement live-service-requirement) ;list of symbols
195 (running live-service-running)) ;#f | object
197 (define (live-service-canonical-name service)
198 "Return the 'canonical name' of SERVICE."
199 (first (live-service-provision service)))
201 (define (current-services)
202 "Return the list of currently defined Shepherd services, represented as
203 <live-service> objects. Return #f if the list of services could not be
205 (with-shepherd-action 'root ('status) results
206 ;; We get a list of results, one for each service with the name 'root'.
207 ;; In practice there's only one such service though.
211 ((('service ('version 0 _ ...) _ ...) ...)
212 (map (lambda (service)
213 (alist-let* service (provides requires running)
214 (live-service provides requires running)))
219 (define (unload-service service)
220 "Unload SERVICE, a symbol name; return #t on success."
221 (with-shepherd-action 'root ('unload (symbol->string service)) result
224 (define (%load-file file)
225 "Load FILE in the Shepherd."
226 (with-shepherd-action 'root ('load file) result
229 (define (eval-there exp)
230 "Eval EXP in the Shepherd."
231 (with-shepherd-action 'root ('eval (object->string exp)) result
234 (define (load-services files)
235 "Load and register the services from FILES, where FILES contain code that
236 returns a shepherd <service> object."
237 (eval-there `(register-services
238 ,@(map (lambda (file)
239 `(primitive-load ,file))
242 (define (load-services/safe files)
243 "This is like 'load-services', but make sure only the subset of FILES that
244 can be safely reloaded is actually reloaded.
246 This is done to accommodate the Shepherd < 0.15.0 where services lacked the
247 'replacement' slot, and where 'register-services' would throw an exception
248 when passed a service with an already-registered name."
249 (eval-there `(let* ((services (map primitive-load ',files))
250 (slots (map slot-definition-name
251 (class-slots <service>)))
252 (can-replace? (memq 'replacement slots)))
253 (define (registered? service)
254 (not (null? (lookup-services (canonical-name service)))))
256 (apply register-services
259 (remove registered? services))))))
261 (define* (start-service name #:optional (arguments '()))
262 (invoke-action name 'start arguments
266 (define (stop-service name)
267 (with-shepherd-action name ('stop) result
271 ;; eval: (put 'alist-let* 'scheme-indent-function 2)
272 ;; eval: (put 'with-shepherd 'scheme-indent-function 1)
273 ;; eval: (put 'with-shepherd-action 'scheme-indent-function 3)
276 ;;; herd.scm ends here