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>
5 ;;; Copyright © 2022 Arun Isaac <arunisaac@systemreboot.net>
7 ;;; This file is part of GNU Guix.
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22 (define-module (gnu build shepherd)
23 #:use-module (gnu system file-systems)
24 #:use-module (gnu build linux-container)
25 #:use-module (guix build utils)
26 #:use-module (srfi srfi-1)
27 #:use-module (srfi srfi-26)
28 #:use-module (ice-9 match)
29 ;; XXX: Lazy-bind the Shepherd to avoid a compile-time dependency.
30 #:autoload (shepherd service) (fork+exec-command
34 #:autoload (shepherd system) (unblock-signals)
35 #:export (default-mounts
36 make-forkexec-constructor/container
37 fork+exec-command/container))
41 ;;; This module provides extensions to the GNU Shepherd. In particular, it
42 ;;; provides a helper to start services in a container.
46 (define (clean-up file)
52 (unless (= ENOENT (system-error-errno args))
53 (apply throw args))))))
55 (define-syntax-rule (catch-system-error exp)
61 (define (default-namespaces args)
62 ;; Most daemons are here to talk to the network, and most of them expect to
63 ;; run under a non-zero UID.
64 (fold delq %namespaces '(net user)))
66 (define* (default-mounts #:key (namespaces (default-namespaces '())))
67 (define (tmpfs directory)
70 (mount-point directory)
75 ;; This is for processes in the default user namespace but living in a
76 ;; different mount namespace, so that they can lookup users.
77 (list (file-system-mapping
78 (source "/etc/passwd") (target source))
80 (source "/etc/group") (target source))))
82 (append (cons (tmpfs "/tmp") %container-file-systems)
83 (let ((mappings `(,@(if (memq 'net namespaces)
85 %network-file-mappings)
86 ,@(if (and (memq 'mnt namespaces)
87 (not (memq 'user namespaces)))
91 ;; Tell the process what timezone we're in. This
92 ;; makes sure that, for instance, its syslog
93 ;; messages have the correct timestamp.
95 (source "/etc/localtime")
98 ,%store-mapping))) ;XXX: coarse-grain
99 (map file-system-mapping->bind-mount
100 (filter (lambda (mapping)
101 (file-exists? (file-system-mapping-source mapping)))
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 ;; XXX: Trick for Shepherd 0.9: prevent 'read-pid-file' from
110 ;; using (@ (fibers) sleep), which would try to suspend the
111 ;; current task, which doesn't work in this extra process.
112 (with-continuation-barrier
114 (read-pid-file pid-file
115 #:max-delay max-delay)))))
117 ;; Send SIGTERM to the whole process group.
118 (catch-system-error (kill (- pid) SIGTERM))
120 ((? integer? container-pid)
121 ;; XXX: When COMMAND is started in a separate PID namespace, its
122 ;; PID is always 1, but that's not what Shepherd needs to know.
125 (define* (exec-command* command #:key user group log-file pid-file
126 (supplementary-groups '())
127 (directory "/") (environment-variables (environ)))
128 "Like 'exec-command', but first restore signal handles modified by
130 ;; First restore the default handlers.
131 (for-each (cut sigaction <> SIG_DFL) %precious-signals)
133 ;; Unblock any signals that have been blocked by the parent process.
134 (unblock-signals %precious-signals)
139 (exec-command command
142 #:supplementary-groups supplementary-groups
144 #:directory directory
145 #:environment-variables environment-variables))
147 (define* (make-forkexec-constructor/container command
150 (default-namespaces args))
154 (supplementary-groups '())
159 (environment-variables
162 "This is a variant of 'make-forkexec-constructor' that starts COMMAND in
163 NAMESPACES, a list of Linux namespaces such as '(mnt ipc). MAPPINGS is the
164 list of <file-system-mapping> to make in the case of a separate mount
165 namespace, in addition to essential bind-mounts such /proc."
166 (define container-directory
169 (string-append "/var/run/containers/" (basename program)))))
171 (define auto-mappings
173 (list (file-system-mapping
180 (append (map file-system-mapping->bind-mount
181 (append auto-mappings mappings))
182 (default-mounts #:namespaces namespaces)))
185 (mkdir-p container-directory)
188 ;; Create LOG-FILE so we can map it in the container.
189 (unless (file-exists? log-file)
190 (close (open log-file (logior O_CREAT O_APPEND O_CLOEXEC) #o640))
192 (let ((pw (getpwnam user)))
193 (chown log-file (passwd:uid pw) (passwd:gid pw))))))
195 (let ((pid (run-container container-directory
198 (exec-command* command
201 #:supplementary-groups
205 #:directory directory
206 #:environment-variables
207 environment-variables)))))
209 (if (or (memq 'mnt namespaces) (memq 'pid namespaces))
210 (read-pid-file/container pid pid-file
211 #:max-delay pid-file-timeout)
212 (read-pid-file pid-file #:max-delay pid-file-timeout))
215 (define* (fork+exec-command/container command
219 "This is a variant of 'fork+exec-command' procedure, that joins the
220 namespaces of process PID beforehand. If there is no support for containers,
221 on Hurd systems for instance, fallback to direct forking."
222 (define (strip-pid args)
223 ;; TODO: Replace with 'strip-keyword-arguments' when that no longer pulls
225 (let loop ((args args)
233 (loop rest (cons head result))))))
235 (let ((container-support? (file-exists? "/proc/self/ns")))
236 (if (and container-support?
237 (not (and pid (= pid (getpid)))))
238 (container-excursion* pid
240 ;; Note: In the Shepherd 0.9, 'fork+exec-command' expects to be
241 ;; called from the shepherd process (because it creates a pipe to
242 ;; capture stdout/stderr and spawns a logging fiber) so we cannot
244 (match (primitive-fork)
248 (apply exec-command* command (strip-pid args)))
250 (primitive-_exit 127))))
251 (pid pid)))) ;XXX: assuming the same PID namespace
252 (apply fork+exec-command command (strip-pid args)))))
255 ;; eval: (put 'container-excursion* 'scheme-indent-function 1)
258 ;;; shepherd.scm ends here