]>
git.ipfire.org Git - people/ms/systemd.git/blob - namespace.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd 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 2 of the License, or
11 (at your option) any later version.
13 systemd 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 GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/mount.h>
28 #include <sys/types.h>
30 #include <sys/syscall.h>
35 #include "namespace.h"
38 typedef enum PathMode
{
39 /* This is ordered by priority! */
51 static int append_paths(Path
**p
, char **strv
, PathMode mode
) {
54 STRV_FOREACH(i
, strv
) {
56 if (!path_is_absolute(*i
))
67 static int path_compare(const void *a
, const void *b
) {
68 const Path
*p
= a
, *q
= b
;
70 if (path_equal(p
->path
, q
->path
)) {
72 /* If the paths are equal, check the mode */
73 if (p
->mode
< q
->mode
)
76 if (p
->mode
> q
->mode
)
82 /* If the paths are not equal, then order prefixes first */
83 if (path_startswith(p
->path
, q
->path
))
86 if (path_startswith(q
->path
, p
->path
))
92 static void drop_duplicates(Path
*p
, unsigned *n
, bool *need_inaccessible
, bool *need_private
) {
93 Path
*f
, *t
, *previous
;
97 assert(need_inaccessible
);
100 for (f
= p
, t
= p
, previous
= NULL
; f
< p
+*n
; f
++) {
102 if (previous
&& path_equal(f
->path
, previous
->path
))
108 if (t
->mode
== PRIVATE
)
109 *need_private
= true;
111 if (t
->mode
== INACCESSIBLE
)
112 *need_inaccessible
= true;
122 static int apply_mount(Path
*p
, const char *root_dir
, const char *inaccessible_dir
, const char *private_dir
, unsigned long flags
) {
126 bool read_only
= false;
130 assert(inaccessible_dir
);
133 if (!(where
= strappend(root_dir
, p
->path
)))
139 what
= inaccessible_dir
;
156 if ((r
= mount(what
, where
, NULL
, MS_BIND
|MS_REC
, NULL
)) >= 0) {
157 log_debug("Successfully mounted %s to %s", what
, where
);
159 /* The bind mount will always inherit the original
160 * flags. If we want to set any flag we need
161 * to do so in a second indepdant step. */
163 r
= mount(NULL
, where
, NULL
, MS_REMOUNT
|MS_REC
|flags
, NULL
);
165 /* Avoid expontial growth of trees */
166 if (r
>= 0 && path_equal(p
->path
, "/"))
167 r
= mount(NULL
, where
, NULL
, MS_REMOUNT
|MS_UNBINDABLE
, NULL
);
169 if (r
>= 0 && read_only
)
170 r
= mount(NULL
, where
, NULL
, MS_REMOUNT
|MS_RDONLY
, NULL
);
174 umount2(where
, MNT_DETACH
);
187 unsigned long flags
) {
190 tmp_dir
[] = "/tmp/systemd-namespace-XXXXXX",
191 root_dir
[] = "/tmp/systemd-namespace-XXXXXX/root",
192 old_root_dir
[] = "/tmp/systemd-namespace-XXXXXX/root/tmp/old-root-XXXXXX",
193 inaccessible_dir
[] = "/tmp/systemd-namespace-XXXXXX/inaccessible",
194 private_dir
[] = "/tmp/systemd-namespace-XXXXXX/private";
198 bool need_private
= false, need_inaccessible
= false;
199 bool remove_tmp
= false, remove_root
= false, remove_old_root
= false, remove_inaccessible
= false, remove_private
= false;
204 strv_length(writable
) +
205 strv_length(readable
) +
206 strv_length(inaccessible
) +
207 (private_tmp
? 2 : 1);
209 if (!(paths
= new(Path
, n
)))
213 if ((r
= append_paths(&p
, writable
, READWRITE
)) < 0 ||
214 (r
= append_paths(&p
, readable
, READONLY
)) < 0 ||
215 (r
= append_paths(&p
, inaccessible
, INACCESSIBLE
)) < 0)
228 assert(paths
+ n
== p
);
230 qsort(paths
, n
, sizeof(Path
), path_compare
);
231 drop_duplicates(paths
, &n
, &need_inaccessible
, &need_private
);
233 if (!mkdtemp(tmp_dir
)) {
239 memcpy(root_dir
, tmp_dir
, sizeof(tmp_dir
)-1);
240 if (mkdir(root_dir
, 0777) < 0) {
246 if (need_inaccessible
) {
247 memcpy(inaccessible_dir
, tmp_dir
, sizeof(tmp_dir
)-1);
248 if (mkdir(inaccessible_dir
, 0) < 0) {
252 remove_inaccessible
= true;
256 memcpy(private_dir
, tmp_dir
, sizeof(tmp_dir
)-1);
257 if (mkdir(private_dir
, 0777 + S_ISVTX
) < 0) {
261 remove_private
= true;
264 if (unshare(CLONE_NEWNS
) < 0) {
269 /* We assume that by default mount events from us won't be
270 * propagated to the root namespace. */
272 for (p
= paths
; p
< paths
+ n
; p
++)
273 if ((r
= apply_mount(p
, root_dir
, inaccessible_dir
, private_dir
, flags
)) < 0)
276 memcpy(old_root_dir
, tmp_dir
, sizeof(tmp_dir
)-1);
277 if (!mkdtemp(old_root_dir
)) {
281 remove_old_root
= true;
283 if (chdir(root_dir
) < 0) {
288 if (pivot_root(root_dir
, old_root_dir
) < 0) {
293 t
= old_root_dir
+ sizeof(root_dir
) - 1;
294 if (umount2(t
, MNT_DETACH
) < 0)
295 /* At this point it's too late to turn anything back,
296 * since we are already in the new root. */
306 for (p
--; p
>= paths
; p
--) {
307 char full_path
[PATH_MAX
];
309 snprintf(full_path
, sizeof(full_path
), "%s%s", root_dir
, p
->path
);
310 char_array_0(full_path
);
312 umount2(full_path
, MNT_DETACH
);
319 if (remove_inaccessible
)
320 rmdir(inaccessible_dir
);