shepherd.scm 8.9 KB

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