gnu: Add libsmf.
[jackhill/guix/guix.git] / gnu / build / shepherd.scm
CommitLineData
63302a4e
LC
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org>
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
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 (ice-9 match)
25 #:export (make-forkexec-constructor/container))
26
27;;; Commentary:
28;;;
29;;; This module provides extensions to the GNU Shepherd. In particular, it
30;;; provides a helper to start services in a container.
31;;;
32;;; Code:
33
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
43(define-syntax-rule (catch-system-error exp)
44 (catch 'system-error
45 (lambda ()
46 exp)
47 (const #f)))
48
49(define (default-namespaces args)
50 ;; Most daemons are here to talk to the network, and most of them expect to
51 ;; run under a non-zero UID.
52 (fold delq %namespaces '(net user)))
53
54(define* (default-mounts #:key (namespaces (default-namespaces '())))
55 (define (tmpfs directory)
56 (file-system
57 (device "none")
58 (title 'device)
59 (mount-point directory)
60 (type "tmpfs")
61 (check? #f)))
62
63 (define passwd
64 ;; This is for processes in the default user namespace but living in a
65 ;; different mount namespace, so that they can lookup users.
66 (file-system-mapping
67 (source "/etc/passwd") (target source)))
68
69 (define nscd-socket
70 (file-system-mapping
71 (source "/var/run/nscd") (target source)
72 (writable? #t)))
73
74 (append (cons (tmpfs "/tmp") %container-file-systems)
75 (let ((mappings `(,@(if (memq 'net namespaces)
76 '()
77 (cons nscd-socket
78 %network-file-mappings))
79 ,@(if (and (memq 'mnt namespaces)
80 (not (memq 'user namespaces)))
81 (list passwd)
82 '())
83 ,%store-mapping))) ;XXX: coarse-grain
84 (map file-system-mapping->bind-mount
85 (filter (lambda (mapping)
86 (file-exists? (file-system-mapping-source mapping)))
87 mappings)))))
88
89;; XXX: Lazy-bind the Shepherd to avoid a compile-time dependency.
90(module-autoload! (current-module)
91 '(shepherd service) '(read-pid-file exec-command))
92
93(define* (read-pid-file/container pid pid-file #:key (max-delay 5))
94 "Read PID-FILE in the container namespaces of PID, which exists in a
95separate mount and PID name space. Return the \"outer\" PID. "
96 (match (container-excursion* pid
97 (lambda ()
98 (read-pid-file pid-file
99 #:max-delay max-delay)))
100 (#f
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
108(define* (make-forkexec-constructor/container command
109 #:key
110 (namespaces
111 (default-namespaces args))
112 (mappings '())
113 (user #f)
114 (group #f)
115 (log-file #f)
116 pid-file
117 (pid-file-timeout 5)
118 (directory "/")
119 (environment-variables
120 (environ))
121 #:rest args)
122 "This is a variant of 'make-forkexec-constructor' that starts COMMAND in
123NAMESPACES, a list of Linux namespaces such as '(mnt ipc). MAPPINGS is the
124list of <file-system-mapping> to make in the case of a separate mount
125namespace, in addition to essential bind-mounts such /proc."
126 (define container-directory
127 (match command
128 ((program _ ...)
129 (string-append "/var/run/containers/" (basename program)))))
130
131 (define auto-mappings
132 `(,@(if log-file
133 (list (file-system-mapping
134 (source log-file)
135 (target source)
136 (writable? #t)))
137 '())))
138
139 (define mounts
140 (append (map file-system-mapping->bind-mount
141 (append auto-mappings mappings))
142 (default-mounts #:namespaces namespaces)))
143
144 (lambda args
145 (mkdir-p container-directory)
146
147 (when log-file
148 ;; Create LOG-FILE so we can map it in the container.
149 (unless (file-exists? log-file)
150 (call-with-output-file log-file (const #t))))
151
152 (let ((pid (run-container container-directory
153 mounts namespaces 1
154 (lambda ()
155 (mkdir-p "/var/run")
156 (clean-up pid-file)
157 (clean-up log-file)
158
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
173;; Local Variables:
174;; eval: (put 'container-excursion* 'scheme-indent-function 1)
175;; End:
176
177;;; shepherd.scm ends here