herd.scm 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  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 herd)
  20. #:use-module (srfi srfi-1)
  21. #:use-module (srfi srfi-9)
  22. #:use-module (srfi srfi-11)
  23. #:use-module (srfi srfi-34)
  24. #:use-module (srfi srfi-35)
  25. #:use-module (ice-9 match)
  26. #:export (%shepherd-socket-file
  27. shepherd-error?
  28. service-not-found-error?
  29. service-not-found-error-service
  30. action-not-found-error?
  31. action-not-found-error-service
  32. action-not-found-error-action
  33. action-exception-error?
  34. action-exception-error-service
  35. action-exception-error-action
  36. action-exception-error-key
  37. action-exception-error-arguments
  38. unknown-shepherd-error?
  39. unknown-shepherd-error-sexp
  40. live-service?
  41. live-service-provision
  42. live-service-requirement
  43. live-service-running
  44. with-shepherd-action
  45. current-services
  46. unload-services
  47. unload-service
  48. load-services
  49. load-services/safe
  50. start-service
  51. stop-service))
  52. ;;; Commentary:
  53. ;;;
  54. ;;; This module provides an interface to the GNU Shepherd, similar to the
  55. ;;; 'herd' command. Essentially it implements a subset of the (shepherd comm)
  56. ;;; module, but focusing only on the parts relevant to 'guix system
  57. ;;; reconfigure'.
  58. ;;;
  59. ;;; Code:
  60. (define %shepherd-socket-file
  61. (make-parameter "/var/run/shepherd/socket"))
  62. (define* (open-connection #:optional (file (%shepherd-socket-file)))
  63. "Open a connection to the daemon, using the Unix-domain socket at FILE, and
  64. return the socket."
  65. ;; The protocol is sexp-based and UTF-8-encoded.
  66. (with-fluids ((%default-port-encoding "UTF-8"))
  67. (let ((sock (socket PF_UNIX SOCK_STREAM 0))
  68. (address (make-socket-address PF_UNIX file)))
  69. (catch 'system-error
  70. (lambda ()
  71. (connect sock address)
  72. (setvbuf sock _IOFBF 1024)
  73. sock)
  74. (lambda args
  75. (close-port sock)
  76. (apply throw args))))))
  77. (define-syntax-rule (with-shepherd connection body ...)
  78. "Evaluate BODY... with CONNECTION bound to an open socket to PID 1."
  79. (let ((connection (open-connection)))
  80. (dynamic-wind
  81. (const #t)
  82. (lambda ()
  83. body ...)
  84. (lambda ()
  85. (close-port connection)))))
  86. (define-condition-type &shepherd-error &error
  87. shepherd-error?)
  88. (define-condition-type &service-not-found-error &shepherd-error
  89. service-not-found-error?
  90. (service service-not-found-error-service))
  91. (define-condition-type &action-not-found-error &shepherd-error
  92. action-not-found-error?
  93. (service action-not-found-error-service)
  94. (action action-not-found-error-action))
  95. (define-condition-type &action-exception-error &shepherd-error
  96. action-exception-error?
  97. (service action-exception-error-service)
  98. (action action-exception-error-action)
  99. (key action-exception-error-key)
  100. (args action-exception-error-arguments))
  101. (define-condition-type &unknown-shepherd-error &shepherd-error
  102. unknown-shepherd-error?
  103. (sexp unknown-shepherd-error-sexp))
  104. (define (raise-shepherd-error error)
  105. "Raise an error condition corresponding to ERROR, an sexp received by a
  106. shepherd client in reply to COMMAND, a command object. Return #t if ERROR
  107. does not denote an error."
  108. (match error
  109. (('error ('version 0 x ...) 'service-not-found service)
  110. (raise (condition (&service-not-found-error
  111. (service service)))))
  112. (('error ('version 0 x ...) 'action-not-found action service)
  113. (raise (condition (&action-not-found-error
  114. (service service)
  115. (action action)))))
  116. (('error ('version 0 x ...) 'action-exception action service
  117. key (args ...))
  118. (raise (condition (&action-exception-error
  119. (service service)
  120. (action action)
  121. (key key) (args args)))))
  122. (('error . _)
  123. (raise (condition (&unknown-shepherd-error (sexp error)))))
  124. (#f ;not an error
  125. #t)))
  126. (define (display-message message)
  127. (format (current-error-port) "shepherd: ~a~%" message))
  128. (define* (invoke-action service action arguments cont)
  129. "Invoke ACTION on SERVICE with ARGUMENTS. On success, call CONT with the
  130. list of results (one result per instance with the name SERVICE). Otherwise
  131. return #f."
  132. (with-shepherd sock
  133. (write `(shepherd-command (version 0)
  134. (action ,action)
  135. (service ,service)
  136. (arguments ,arguments)
  137. (directory ,(getcwd)))
  138. sock)
  139. (force-output sock)
  140. (match (read sock)
  141. (('reply ('version 0 _ ...) ('result result) ('error #f)
  142. ('messages messages))
  143. (for-each display-message messages)
  144. (cont result))
  145. (('reply ('version 0 x ...) ('result y) ('error error)
  146. ('messages messages))
  147. (for-each display-message messages)
  148. (raise-shepherd-error error)
  149. #f)
  150. (x
  151. ;; invalid reply
  152. #f))))
  153. (define-syntax-rule (with-shepherd-action service (action args ...)
  154. result body ...)
  155. "Invoke ACTION on SERVICE with the given ARGS, and evaluate BODY with RESULT
  156. bound to the action's result."
  157. (invoke-action service action (list args ...)
  158. (lambda (result) body ...)))
  159. (define-syntax alist-let*
  160. (syntax-rules ()
  161. "Bind the given KEYs in EXP to the corresponding items in ALIST. ALIST
  162. is assumed to be a list of two-element tuples rather than a traditional list
  163. of pairs."
  164. ((_ alist (key ...) exp ...)
  165. (let ((key (and=> (assoc-ref alist 'key) car)) ...)
  166. exp ...))))
  167. ;; Information about live Shepherd services.
  168. (define-record-type <live-service>
  169. (live-service provision requirement running)
  170. live-service?
  171. (provision live-service-provision) ;list of symbols
  172. (requirement live-service-requirement) ;list of symbols
  173. (running live-service-running)) ;#f | object
  174. (define (current-services)
  175. "Return the list of currently defined Shepherd services, represented as
  176. <live-service> objects. Return #f if the list of services could not be
  177. obtained."
  178. (with-shepherd-action 'root ('status) results
  179. ;; We get a list of results, one for each service with the name 'root'.
  180. ;; In practice there's only one such service though.
  181. (match results
  182. ((services _ ...)
  183. (match services
  184. ((('service ('version 0 _ ...) _ ...) ...)
  185. (map (lambda (service)
  186. (alist-let* service (provides requires running)
  187. (live-service provides requires running)))
  188. services))
  189. (x
  190. #f))))))
  191. (define (unload-service service)
  192. "Unload SERVICE, a symbol name; return #t on success."
  193. (with-shepherd-action 'root ('unload (symbol->string service)) result
  194. (first result)))
  195. (define (%load-file file)
  196. "Load FILE in the Shepherd."
  197. (with-shepherd-action 'root ('load file) result
  198. (first result)))
  199. (define (eval-there exp)
  200. "Eval EXP in the Shepherd."
  201. (with-shepherd-action 'root ('eval (object->string exp)) result
  202. (first result)))
  203. (define (load-services files)
  204. "Load and register the services from FILES, where FILES contain code that
  205. returns a shepherd <service> object."
  206. (eval-there `(register-services
  207. ,@(map (lambda (file)
  208. `(primitive-load ,file))
  209. files))))
  210. (define (load-services/safe files)
  211. "This is like 'load-services', but make sure only the subset of FILES that
  212. can be safely reloaded is actually reloaded.
  213. This is done to accommodate the Shepherd < 0.15.0 where services lacked the
  214. 'replacement' slot, and where 'register-services' would throw an exception
  215. when passed a service with an already-registered name."
  216. (eval-there `(let* ((services (map primitive-load ',files))
  217. (slots (map slot-definition-name
  218. (class-slots <service>)))
  219. (can-replace? (memq 'replacement slots)))
  220. (define (registered? service)
  221. (not (null? (lookup-services (canonical-name service)))))
  222. (apply register-services
  223. (if can-replace?
  224. services
  225. (remove registered? services))))))
  226. (define* (start-service name #:optional (arguments '()))
  227. (invoke-action name 'start arguments
  228. (lambda (result)
  229. result)))
  230. (define (stop-service name)
  231. (with-shepherd-action name ('stop) result
  232. result))
  233. ;; Local Variables:
  234. ;; eval: (put 'alist-let* 'scheme-indent-function 2)
  235. ;; eval: (put 'with-shepherd 'scheme-indent-function 1)
  236. ;; eval: (put 'with-shepherd-action 'scheme-indent-function 3)
  237. ;; End:
  238. ;;; herd.scm ends here