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>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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 (guix utils)
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-26)
27 #:use-module (ice-9 match)
28 #:export (make-forkexec-constructor/container
29 fork+exec-command/container))
33 ;;; This module provides extensions to the GNU Shepherd. In particular, it
34 ;;; provides a helper to start services in a container.
38 (define (clean-up file)
44 (unless (= ENOENT (system-error-errno args))
45 (apply throw args))))))
47 (define-syntax-rule (catch-system-error exp)
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)))
58 (define* (default-mounts #:key (namespaces (default-namespaces '())))
59 (define (tmpfs directory)
62 (mount-point directory)
67 ;; This is for processes in the default user namespace but living in a
68 ;; different mount namespace, so that they can lookup users.
69 (list (file-system-mapping
70 (source "/etc/passwd") (target source))
72 (source "/etc/group") (target source))))
74 (append (cons (tmpfs "/tmp") %container-file-systems)
75 (let ((mappings `(,@(if (memq 'net namespaces)
77 %network-file-mappings)
78 ,@(if (and (memq 'mnt namespaces)
79 (not (memq 'user namespaces)))
83 ;; Tell the process what timezone we're in. This
84 ;; makes sure that, for instance, its syslog
85 ;; messages have the correct timestamp.
87 (source "/etc/localtime")
90 ,%store-mapping))) ;XXX: coarse-grain
91 (map file-system-mapping->bind-mount
92 (filter (lambda (mapping)
93 (file-exists? (file-system-mapping-source mapping)))
96 ;; XXX: Lazy-bind the Shepherd to avoid a compile-time dependency.
97 (module-autoload! (current-module)
99 '(fork+exec-command read-pid-file exec-command
101 (module-autoload! (current-module)
102 '(shepherd system) '(unblock-signals))
104 (define* (read-pid-file/container pid pid-file #:key (max-delay 5))
105 "Read PID-FILE in the container namespaces of PID, which exists in a
106 separate mount and PID name space. Return the \"outer\" PID. "
107 (match (container-excursion* pid
109 (read-pid-file pid-file
110 #:max-delay max-delay)))
112 ;; Send SIGTERM to the whole process group.
113 (catch-system-error (kill (- pid) SIGTERM))
115 ((? integer? container-pid)
116 ;; XXX: When COMMAND is started in a separate PID namespace, its
117 ;; PID is always 1, but that's not what Shepherd needs to know.
120 (define* (make-forkexec-constructor/container command
123 (default-namespaces args))
131 (environment-variables
134 "This is a variant of 'make-forkexec-constructor' that starts COMMAND in
135 NAMESPACES, a list of Linux namespaces such as '(mnt ipc). MAPPINGS is the
136 list of <file-system-mapping> to make in the case of a separate mount
137 namespace, in addition to essential bind-mounts such /proc."
138 (define container-directory
141 (string-append "/var/run/containers/" (basename program)))))
143 (define auto-mappings
145 (list (file-system-mapping
152 (append (map file-system-mapping->bind-mount
153 (append auto-mappings mappings))
154 (default-mounts #:namespaces namespaces)))
157 (mkdir-p container-directory)
160 ;; Create LOG-FILE so we can map it in the container.
161 (unless (file-exists? log-file)
162 (call-with-output-file log-file (const #t))
164 (let ((pw (getpwnam user)))
165 (chown log-file (passwd:uid pw) (passwd:gid pw))))))
167 (let ((pid (run-container container-directory
170 ;; First restore the default handlers.
171 (for-each (cut sigaction <> SIG_DFL)
174 ;; Unblock any signals that have been blocked
175 ;; by the parent process.
176 (unblock-signals %precious-signals)
181 (exec-command command
185 #:directory directory
186 #:environment-variables
187 environment-variables)))))
189 (if (or (memq 'mnt namespaces) (memq 'pid namespaces))
190 (read-pid-file/container pid pid-file
191 #:max-delay pid-file-timeout)
192 (read-pid-file pid-file #:max-delay pid-file-timeout))
195 (define* (fork+exec-command/container command
199 "This is a variant of 'fork+exec-command' procedure, that joins the
200 namespaces of process PID beforehand. If there is no support for containers,
201 on Hurd systems for instance, fallback to direct forking."
202 (let ((container-support?
203 (file-exists? "/proc/self/ns"))
204 (fork-proc (lambda ()
205 (apply fork+exec-command command
206 (strip-keyword-arguments '(#:pid) args)))))
207 (if container-support?
208 (container-excursion* pid fork-proc)
212 ;; eval: (put 'container-excursion* 'scheme-indent-function 1)
215 ;;; shepherd.scm ends here