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>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; 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
33 #:autoload (shepherd system) (unblock-signals)
34 #:export (default-mounts
35 make-forkexec-constructor/container
36 fork+exec-command/container))
40 ;;; This module provides extensions to the GNU Shepherd. In particular, it
41 ;;; provides a helper to start services in a container.
45 (define (clean-up file)
51 (unless (= ENOENT (system-error-errno args))
52 (apply throw args))))))
54 (define-syntax-rule (catch-system-error exp)
60 (define (default-namespaces args)
61 ;; Most daemons are here to talk to the network, and most of them expect to
62 ;; run under a non-zero UID.
63 (fold delq %namespaces '(net user)))
65 (define* (default-mounts #:key (namespaces (default-namespaces '())))
66 (define (tmpfs directory)
69 (mount-point directory)
74 ;; This is for processes in the default user namespace but living in a
75 ;; different mount namespace, so that they can lookup users.
76 (list (file-system-mapping
77 (source "/etc/passwd") (target source))
79 (source "/etc/group") (target source))))
81 (append (cons (tmpfs "/tmp") %container-file-systems)
82 (let ((mappings `(,@(if (memq 'net namespaces)
84 %network-file-mappings)
85 ,@(if (and (memq 'mnt namespaces)
86 (not (memq 'user namespaces)))
90 ;; Tell the process what timezone we're in. This
91 ;; makes sure that, for instance, its syslog
92 ;; messages have the correct timestamp.
94 (source "/etc/localtime")
97 ,%store-mapping))) ;XXX: coarse-grain
98 (map file-system-mapping->bind-mount
99 (filter (lambda (mapping)
100 (file-exists? (file-system-mapping-source mapping)))
103 (define* (read-pid-file/container pid pid-file #:key (max-delay 5))
104 "Read PID-FILE in the container namespaces of PID, which exists in a
105 separate mount and PID name space. Return the \"outer\" PID. "
106 (match (container-excursion* pid
108 ;; XXX: Trick for Shepherd 0.9: prevent 'read-pid-file' from
109 ;; using (@ (fibers) sleep), which would try to suspend the
110 ;; current task, which doesn't work in this extra process.
111 (with-continuation-barrier
113 (read-pid-file pid-file
114 #:max-delay max-delay)))))
116 ;; Send SIGTERM to the whole process group.
117 (catch-system-error (kill (- pid) SIGTERM))
119 ((? integer? container-pid)
120 ;; XXX: When COMMAND is started in a separate PID namespace, its
121 ;; PID is always 1, but that's not what Shepherd needs to know.
124 (define* (exec-command* command #:key user group log-file pid-file
125 (supplementary-groups '())
126 (directory "/") (environment-variables (environ)))
127 "Like 'exec-command', but first restore signal handles modified by
129 ;; First restore the default handlers.
130 (for-each (cut sigaction <> SIG_DFL) %precious-signals)
132 ;; Unblock any signals that have been blocked by the parent process.
133 (unblock-signals %precious-signals)
138 (exec-command command
141 #:supplementary-groups supplementary-groups
143 #:directory directory
144 #:environment-variables environment-variables))
146 (define* (make-forkexec-constructor/container command
149 (default-namespaces args))
153 (supplementary-groups '())
158 (environment-variables
161 "This is a variant of 'make-forkexec-constructor' that starts COMMAND in
162 NAMESPACES, a list of Linux namespaces such as '(mnt ipc). MAPPINGS is the
163 list of <file-system-mapping> to make in the case of a separate mount
164 namespace, in addition to essential bind-mounts such /proc."
165 (define container-directory
168 (string-append "/var/run/containers/" (basename program)))))
170 (define auto-mappings
172 (list (file-system-mapping
179 (append (map file-system-mapping->bind-mount
180 (append auto-mappings mappings))
181 (default-mounts #:namespaces namespaces)))
184 (mkdir-p container-directory)
187 ;; Create LOG-FILE so we can map it in the container.
188 (unless (file-exists? log-file)
189 (call-with-output-file log-file (const #t))
191 (let ((pw (getpwnam user)))
192 (chown log-file (passwd:uid pw) (passwd:gid pw))))))
194 (let ((pid (run-container container-directory
197 (exec-command* command
200 #:supplementary-groups
204 #:directory directory
205 #:environment-variables
206 environment-variables)))))
208 (if (or (memq 'mnt namespaces) (memq 'pid namespaces))
209 (read-pid-file/container pid pid-file
210 #:max-delay pid-file-timeout)
211 (read-pid-file pid-file #:max-delay pid-file-timeout))
214 (define* (fork+exec-command/container command
218 "This is a variant of 'fork+exec-command' procedure, that joins the
219 namespaces of process PID beforehand. If there is no support for containers,
220 on Hurd systems for instance, fallback to direct forking."
221 (define (strip-pid args)
222 ;; TODO: Replace with 'strip-keyword-arguments' when that no longer pulls
224 (let loop ((args args)
232 (loop rest (cons head result))))))
234 (let ((container-support? (file-exists? "/proc/self/ns")))
235 (if (and container-support?
236 (not (and pid (= pid (getpid)))))
237 (container-excursion* pid
239 ;; Note: In the Shepherd 0.9, 'fork+exec-command' expects to be
240 ;; called from the shepherd process (because it creates a pipe to
241 ;; capture stdout/stderr and spawns a logging fiber) so we cannot
243 (match (primitive-fork)
247 (apply exec-command* command (strip-pid args)))
249 (primitive-_exit 127))))
250 (pid pid)))) ;XXX: assuming the same PID namespace
251 (apply fork+exec-command command (strip-pid args)))))
254 ;; eval: (put 'container-excursion* 'scheme-indent-function 1)
257 ;;; shepherd.scm ends here