shepherd.scm 10 KB

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