shepherd.scm 8.8 KB

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