shepherd.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018, 2019, 2020, 2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2020 Mathieu Othacehe <othacehe@gnu.org>
  4. ;;; Copyright © 2022 Leo Nikkilä <hello@lnikki.la>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  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.
  12. ;;;
  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.
  17. ;;;
  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/>.
  20. (define-module (gnu build shepherd)
  21. #:use-module (gnu system file-systems)
  22. #:use-module (gnu build linux-container)
  23. #:use-module (guix build utils)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-26)
  26. #:use-module (ice-9 match)
  27. ;; XXX: Lazy-bind the Shepherd to avoid a compile-time dependency.
  28. #:autoload (shepherd service) (fork+exec-command
  29. read-pid-file
  30. exec-command
  31. %precious-signals)
  32. #:autoload (shepherd system) (unblock-signals)
  33. #:export (make-forkexec-constructor/container
  34. fork+exec-command/container))
  35. ;;; Commentary:
  36. ;;;
  37. ;;; This module provides extensions to the GNU Shepherd. In particular, it
  38. ;;; provides a helper to start services in a container.
  39. ;;;
  40. ;;; Code:
  41. (define (clean-up file)
  42. (when file
  43. (catch 'system-error
  44. (lambda ()
  45. (delete-file file))
  46. (lambda args
  47. (unless (= ENOENT (system-error-errno args))
  48. (apply throw args))))))
  49. (define-syntax-rule (catch-system-error exp)
  50. (catch 'system-error
  51. (lambda ()
  52. exp)
  53. (const #f)))
  54. (define (default-namespaces args)
  55. ;; Most daemons are here to talk to the network, and most of them expect to
  56. ;; run under a non-zero UID.
  57. (fold delq %namespaces '(net user)))
  58. (define* (default-mounts #:key (namespaces (default-namespaces '())))
  59. (define (tmpfs directory)
  60. (file-system
  61. (device "none")
  62. (mount-point directory)
  63. (type "tmpfs")
  64. (check? #f)))
  65. (define accounts
  66. ;; This is for processes in the default user namespace but living in a
  67. ;; different mount namespace, so that they can lookup users.
  68. (list (file-system-mapping
  69. (source "/etc/passwd") (target source))
  70. (file-system-mapping
  71. (source "/etc/group") (target source))))
  72. (append (cons (tmpfs "/tmp") %container-file-systems)
  73. (let ((mappings `(,@(if (memq 'net namespaces)
  74. '()
  75. %network-file-mappings)
  76. ,@(if (and (memq 'mnt namespaces)
  77. (not (memq 'user namespaces)))
  78. accounts
  79. '())
  80. ;; Tell the process what timezone we're in. This
  81. ;; makes sure that, for instance, its syslog
  82. ;; messages have the correct timestamp.
  83. ,(file-system-mapping
  84. (source "/etc/localtime")
  85. (target source))
  86. ,%store-mapping))) ;XXX: coarse-grain
  87. (map file-system-mapping->bind-mount
  88. (filter (lambda (mapping)
  89. (file-exists? (file-system-mapping-source mapping)))
  90. mappings)))))
  91. (define* (read-pid-file/container pid pid-file #:key (max-delay 5))
  92. "Read PID-FILE in the container namespaces of PID, which exists in a
  93. separate mount and PID name space. Return the \"outer\" PID. "
  94. (match (container-excursion* pid
  95. (lambda ()
  96. ;; XXX: Trick for Shepherd 0.9: prevent 'read-pid-file' from
  97. ;; using (@ (fibers) sleep), which would try to suspend the
  98. ;; current task, which doesn't work in this extra process.
  99. (with-continuation-barrier
  100. (lambda ()
  101. (read-pid-file pid-file
  102. #:max-delay max-delay)))))
  103. (#f
  104. ;; Send SIGTERM to the whole process group.
  105. (catch-system-error (kill (- pid) SIGTERM))
  106. #f)
  107. ((? integer? container-pid)
  108. ;; XXX: When COMMAND is started in a separate PID namespace, its
  109. ;; PID is always 1, but that's not what Shepherd needs to know.
  110. pid)))
  111. (define* (exec-command* command #:key user group log-file pid-file
  112. (supplementary-groups '())
  113. (directory "/") (environment-variables (environ)))
  114. "Like 'exec-command', but first restore signal handles modified by
  115. shepherd (PID 1)."
  116. ;; First restore the default handlers.
  117. (for-each (cut sigaction <> SIG_DFL) %precious-signals)
  118. ;; Unblock any signals that have been blocked by the parent process.
  119. (unblock-signals %precious-signals)
  120. (mkdir-p "/var/run")
  121. (clean-up pid-file)
  122. (exec-command command
  123. #:user user
  124. #:group group
  125. #:supplementary-groups supplementary-groups
  126. #:log-file log-file
  127. #:directory directory
  128. #:environment-variables environment-variables))
  129. (define* (make-forkexec-constructor/container command
  130. #:key
  131. (namespaces
  132. (default-namespaces args))
  133. (mappings '())
  134. (user #f)
  135. (group #f)
  136. (supplementary-groups '())
  137. (log-file #f)
  138. pid-file
  139. (pid-file-timeout 5)
  140. (directory "/")
  141. (environment-variables
  142. (environ))
  143. #:rest args)
  144. "This is a variant of 'make-forkexec-constructor' that starts COMMAND in
  145. NAMESPACES, a list of Linux namespaces such as '(mnt ipc). MAPPINGS is the
  146. list of <file-system-mapping> to make in the case of a separate mount
  147. namespace, in addition to essential bind-mounts such /proc."
  148. (define container-directory
  149. (match command
  150. ((program _ ...)
  151. (string-append "/var/run/containers/" (basename program)))))
  152. (define auto-mappings
  153. `(,@(if log-file
  154. (list (file-system-mapping
  155. (source log-file)
  156. (target source)
  157. (writable? #t)))
  158. '())))
  159. (define mounts
  160. (append (map file-system-mapping->bind-mount
  161. (append auto-mappings mappings))
  162. (default-mounts #:namespaces namespaces)))
  163. (lambda args
  164. (mkdir-p container-directory)
  165. (when log-file
  166. ;; Create LOG-FILE so we can map it in the container.
  167. (unless (file-exists? log-file)
  168. (call-with-output-file log-file (const #t))
  169. (when user
  170. (let ((pw (getpwnam user)))
  171. (chown log-file (passwd:uid pw) (passwd:gid pw))))))
  172. (let ((pid (run-container container-directory
  173. mounts namespaces 1
  174. (lambda ()
  175. (exec-command* command
  176. #:user user
  177. #:group group
  178. #:supplementary-groups
  179. supplementary-groups
  180. #:pid-file pid-file
  181. #:log-file log-file
  182. #:directory directory
  183. #:environment-variables
  184. environment-variables)))))
  185. (if pid-file
  186. (if (or (memq 'mnt namespaces) (memq 'pid namespaces))
  187. (read-pid-file/container pid pid-file
  188. #:max-delay pid-file-timeout)
  189. (read-pid-file pid-file #:max-delay pid-file-timeout))
  190. pid))))
  191. (define* (fork+exec-command/container command
  192. #:key pid
  193. #:allow-other-keys
  194. #:rest args)
  195. "This is a variant of 'fork+exec-command' procedure, that joins the
  196. namespaces of process PID beforehand. If there is no support for containers,
  197. on Hurd systems for instance, fallback to direct forking."
  198. (define (strip-pid args)
  199. ;; TODO: Replace with 'strip-keyword-arguments' when that no longer pulls
  200. ;; in (guix config).
  201. (let loop ((args args)
  202. (result '()))
  203. (match args
  204. (()
  205. (reverse result))
  206. ((#:pid _ . rest)
  207. (loop rest result))
  208. ((head . rest)
  209. (loop rest (cons head result))))))
  210. (let ((container-support? (file-exists? "/proc/self/ns")))
  211. (if (and container-support?
  212. (not (and pid (= pid (getpid)))))
  213. (container-excursion* pid
  214. (lambda ()
  215. ;; Note: In the Shepherd 0.9, 'fork+exec-command' expects to be
  216. ;; called from the shepherd process (because it creates a pipe to
  217. ;; capture stdout/stderr and spawns a logging fiber) so we cannot
  218. ;; use it here.
  219. (match (primitive-fork)
  220. (0 (dynamic-wind
  221. (const #t)
  222. (lambda ()
  223. (apply exec-command* command (strip-pid args)))
  224. (lambda ()
  225. (primitive-_exit 127))))
  226. (pid pid)))) ;XXX: assuming the same PID namespace
  227. (apply fork+exec-command command (strip-pid args)))))
  228. ;; Local Variables:
  229. ;; eval: (put 'container-excursion* 'scheme-indent-function 1)
  230. ;; End:
  231. ;;; shepherd.scm ends here