shepherd.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  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.
  11. ;;;
  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.
  16. ;;;
  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/>.
  19. (define-module (gnu services shepherd)
  20. #:use-module (guix ui)
  21. #:use-module (guix sets)
  22. #:use-module (guix gexp)
  23. #:use-module (guix store)
  24. #:use-module (guix monads)
  25. #:use-module (guix records)
  26. #:use-module (guix derivations) ;imported-modules, etc.
  27. #:use-module (gnu services)
  28. #:use-module (gnu services herd)
  29. #:use-module (gnu packages admin)
  30. #:use-module (ice-9 match)
  31. #:use-module (ice-9 vlist)
  32. #:use-module (srfi srfi-1)
  33. #:use-module (srfi srfi-26)
  34. #:use-module (srfi srfi-34)
  35. #:use-module (srfi srfi-35)
  36. #:export (shepherd-root-service-type
  37. %shepherd-root-service
  38. shepherd-service-type
  39. shepherd-service
  40. shepherd-service?
  41. shepherd-service-documentation
  42. shepherd-service-provision
  43. shepherd-service-canonical-name
  44. shepherd-service-requirement
  45. shepherd-service-respawn?
  46. shepherd-service-start
  47. shepherd-service-stop
  48. shepherd-service-auto-start?
  49. shepherd-service-modules
  50. %default-modules
  51. shepherd-service-file
  52. shepherd-service-lookup-procedure
  53. shepherd-service-back-edges
  54. shepherd-service-upgrade))
  55. ;;; Commentary:
  56. ;;;
  57. ;;; Instantiating system services as a shepherd configuration file.
  58. ;;;
  59. ;;; Code:
  60. (define (shepherd-boot-gexp services)
  61. (mlet %store-monad ((shepherd-conf (shepherd-configuration-file services)))
  62. (return #~(begin
  63. ;; Keep track of the booted system.
  64. (false-if-exception (delete-file "/run/booted-system"))
  65. (symlink (readlink "/run/current-system")
  66. "/run/booted-system")
  67. ;; Close any remaining open file descriptors to be on the safe
  68. ;; side. This must be the very last thing we do, because
  69. ;; Guile has internal FDs such as 'sleep_pipe' that need to be
  70. ;; alive.
  71. (let loop ((fd 3))
  72. (when (< fd 1024)
  73. (false-if-exception (close-fdes fd))
  74. (loop (+ 1 fd))))
  75. ;; Start shepherd.
  76. (execl #$(file-append shepherd "/bin/shepherd")
  77. "shepherd" "--config" #$shepherd-conf)))))
  78. (define shepherd-root-service-type
  79. (service-type
  80. (name 'shepherd-root)
  81. ;; Extending the root shepherd service (aka. PID 1) happens by
  82. ;; concatenating the list of services provided by the extensions.
  83. (compose concatenate)
  84. (extend append)
  85. (extensions (list (service-extension boot-service-type
  86. shepherd-boot-gexp)
  87. (service-extension profile-service-type
  88. (const (list shepherd)))))))
  89. (define %shepherd-root-service
  90. ;; The root shepherd service, aka. PID 1. Its parameter is a list of
  91. ;; <shepherd-service> objects.
  92. (service shepherd-root-service-type '()))
  93. (define-syntax-rule (shepherd-service-type service-name proc)
  94. "Return a <service-type> denoting a simple shepherd service--i.e., the type
  95. for a service that extends SHEPHERD-ROOT-SERVICE-TYPE and nothing else."
  96. (service-type
  97. (name service-name)
  98. (extensions
  99. (list (service-extension shepherd-root-service-type
  100. (compose list proc))))))
  101. (define %default-imported-modules
  102. ;; Default set of modules imported for a service's consumption.
  103. '((guix build utils)
  104. (guix build syscalls)))
  105. (define %default-modules
  106. ;; Default set of modules visible in a service's file.
  107. `((shepherd service)
  108. (oop goops)
  109. (guix build utils)
  110. (guix build syscalls)))
  111. (define-record-type* <shepherd-service>
  112. shepherd-service make-shepherd-service
  113. shepherd-service?
  114. (documentation shepherd-service-documentation ;string
  115. (default "[No documentation.]"))
  116. (provision shepherd-service-provision) ;list of symbols
  117. (requirement shepherd-service-requirement ;list of symbols
  118. (default '()))
  119. (respawn? shepherd-service-respawn? ;Boolean
  120. (default #t))
  121. (start shepherd-service-start) ;g-expression (procedure)
  122. (stop shepherd-service-stop ;g-expression (procedure)
  123. (default #~(const #f)))
  124. (auto-start? shepherd-service-auto-start? ;Boolean
  125. (default #t))
  126. (modules shepherd-service-modules ;list of module names
  127. (default %default-modules)))
  128. (define (shepherd-service-canonical-name service)
  129. "Return the 'canonical name' of SERVICE."
  130. (first (shepherd-service-provision service)))
  131. (define (assert-valid-graph services)
  132. "Raise an error if SERVICES does not define a valid shepherd service graph,
  133. for instance if a service requires a nonexistent service, or if more than one
  134. service uses a given name.
  135. These are constraints that shepherd's 'register-service' verifies but we'd
  136. better verify them here statically than wait until PID 1 halts with an
  137. assertion failure."
  138. (define provisions
  139. ;; The set of provisions (symbols). Bail out if a symbol is given more
  140. ;; than once.
  141. (fold (lambda (service set)
  142. (define (assert-unique symbol)
  143. (when (set-contains? set symbol)
  144. (raise (condition
  145. (&message
  146. (message
  147. (format #f (G_ "service '~a' provided more than once")
  148. symbol)))))))
  149. (for-each assert-unique (shepherd-service-provision service))
  150. (fold set-insert set (shepherd-service-provision service)))
  151. (setq 'shepherd)
  152. services))
  153. (define (assert-satisfied-requirements service)
  154. ;; Bail out if the requirements of SERVICE aren't satisfied.
  155. (for-each (lambda (requirement)
  156. (unless (set-contains? provisions requirement)
  157. (raise (condition
  158. (&message
  159. (message
  160. (format #f (G_ "service '~a' requires '~a', \
  161. which is not provided by any service")
  162. (match (shepherd-service-provision service)
  163. ((head . _) head)
  164. (_ service))
  165. requirement)))))))
  166. (shepherd-service-requirement service)))
  167. (for-each assert-satisfied-requirements services))
  168. (define (shepherd-service-file-name service)
  169. "Return the file name where the initialization code for SERVICE is to be
  170. stored."
  171. (let ((provisions (string-join (map symbol->string
  172. (shepherd-service-provision service)))))
  173. (string-append "shepherd-"
  174. (string-map (match-lambda
  175. (#\/ #\-)
  176. (#\ #\-)
  177. (chr chr))
  178. provisions)
  179. ".scm")))
  180. (define (shepherd-service-file service)
  181. "Return a file defining SERVICE."
  182. (gexp->file (shepherd-service-file-name service)
  183. (with-imported-modules %default-imported-modules
  184. #~(begin
  185. (use-modules #$@(shepherd-service-modules service))
  186. (make <service>
  187. #:docstring '#$(shepherd-service-documentation service)
  188. #:provides '#$(shepherd-service-provision service)
  189. #:requires '#$(shepherd-service-requirement service)
  190. #:respawn? '#$(shepherd-service-respawn? service)
  191. #:start #$(shepherd-service-start service)
  192. #:stop #$(shepherd-service-stop service))))))
  193. (define (shepherd-configuration-file services)
  194. "Return the shepherd configuration file for SERVICES."
  195. (assert-valid-graph services)
  196. (mlet %store-monad ((files (mapm %store-monad
  197. shepherd-service-file services)))
  198. (define config
  199. #~(begin
  200. (use-modules (srfi srfi-34)
  201. (system repl error-handling))
  202. ;; Arrange to spawn a REPL if something goes wrong. This is better
  203. ;; than a kernel panic.
  204. (call-with-error-handling
  205. (lambda ()
  206. (apply register-services (map primitive-load '#$files))
  207. ;; guix-daemon 0.6 aborts if 'PATH' is undefined, so work around
  208. ;; it.
  209. (setenv "PATH" "/run/current-system/profile/bin")
  210. (format #t "starting services...~%")
  211. (for-each (lambda (service)
  212. ;; In the Shepherd 0.3 the 'start' method can raise
  213. ;; '&action-runtime-error' if it fails, so protect
  214. ;; against it. (XXX: 'action-runtime-error?' is not
  215. ;; exported is 0.3, hence 'service-error?'.)
  216. (guard (c ((service-error? c)
  217. (format (current-error-port)
  218. "failed to start service '~a'~%"
  219. service)))
  220. (start service)))
  221. '#$(append-map shepherd-service-provision
  222. (filter shepherd-service-auto-start?
  223. services)))))))
  224. (gexp->file "shepherd.conf" config)))
  225. (define* (shepherd-service-lookup-procedure services
  226. #:optional
  227. (provision
  228. shepherd-service-provision))
  229. "Return a procedure that, when passed a symbol, return the item among
  230. SERVICES that provides this symbol. PROVISION must be a one-argument
  231. procedure that takes a service and returns the list of symbols it provides."
  232. (let ((services (fold (lambda (service result)
  233. (fold (cut vhash-consq <> service <>)
  234. result
  235. (provision service)))
  236. vlist-null
  237. services)))
  238. (lambda (name)
  239. (match (vhash-assq name services)
  240. ((_ . service) service)
  241. (#f #f)))))
  242. (define* (shepherd-service-back-edges services
  243. #:key
  244. (provision shepherd-service-provision)
  245. (requirement shepherd-service-requirement))
  246. "Return a procedure that, when given a <shepherd-service> from SERVICES,
  247. returns the list of <shepherd-service> that depend on it.
  248. Use PROVISION and REQUIREMENT as one-argument procedures that return the
  249. symbols provided/required by a service."
  250. (define provision->service
  251. (shepherd-service-lookup-procedure services provision))
  252. (define edges
  253. (fold (lambda (service edges)
  254. (fold (lambda (requirement edges)
  255. (vhash-consq (provision->service requirement) service
  256. edges))
  257. edges
  258. (requirement service)))
  259. vlist-null
  260. services))
  261. (lambda (service)
  262. (vhash-foldq* cons '() service edges)))
  263. (define (shepherd-service-upgrade live target)
  264. "Return two values: the subset of LIVE (a list of <live-service>) that needs
  265. to be unloaded, and the subset of TARGET (a list of <shepherd-service>) that
  266. needs to be loaded."
  267. (define (essential? service)
  268. (memq (first (live-service-provision service))
  269. '(root shepherd)))
  270. (define lookup-target
  271. (shepherd-service-lookup-procedure target
  272. shepherd-service-provision))
  273. (define lookup-live
  274. (shepherd-service-lookup-procedure live
  275. live-service-provision))
  276. (define (running? service)
  277. (and=> (lookup-live (shepherd-service-canonical-name service))
  278. live-service-running))
  279. (define (stopped service)
  280. (match (lookup-live (shepherd-service-canonical-name service))
  281. (#f #f)
  282. (service (and (not (live-service-running service))
  283. service))))
  284. (define live-service-dependents
  285. (shepherd-service-back-edges live
  286. #:provision live-service-provision
  287. #:requirement live-service-requirement))
  288. (define (obsolete? service)
  289. (match (lookup-target (first (live-service-provision service)))
  290. (#f (every obsolete? (live-service-dependents service)))
  291. (_ #f)))
  292. (define to-load
  293. ;; Only load services that are either new or currently stopped.
  294. (remove running? target))
  295. (define to-unload
  296. ;; Unload services that are (1) no longer required, or (2) are in TO-LOAD.
  297. (remove essential?
  298. (append (filter obsolete? live)
  299. (filter-map stopped to-load))))
  300. (values to-unload to-load))
  301. ;;; shepherd.scm ends here