shepherd.scm 8.5 KB

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