]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-ns.c
namespace: fall back gracefully when kernel doesn't support network namespaces (...
[thirdparty/systemd.git] / src / test / test-ns.c
CommitLineData
15ae422b
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
15ae422b
LP
9 (at your option) any later version.
10
11 systemd 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 GNU
5430f7f2 14 Lesser General Public License for more details.
15ae422b 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
15ae422b
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include <stdlib.h>
15ae422b 21#include <unistd.h>
15ae422b 22
15ae422b 23#include "log.h"
cf0fbc49 24#include "namespace.h"
15ae422b
LP
25
26int main(int argc, char *argv[]) {
27 const char * const writable[] = {
28 "/home",
d944dc95 29 "-/home/lennart/projects/foobar", /* this should be masked automatically */
15ae422b
LP
30 NULL
31 };
32
ac0930c8 33 const char * const readonly[] = {
d944dc95
LP
34 /* "/", */
35 /* "/usr", */
5dcfe57b 36 "/boot",
d944dc95
LP
37 "/lib",
38 "/usr/lib",
39 "-/lib64",
40 "-/usr/lib64",
15ae422b
LP
41 NULL
42 };
43
ee818b89 44 const char *inaccessible[] = {
15ae422b
LP
45 "/home/lennart/projects",
46 NULL
47 };
c575770b
DH
48
49 static const NameSpaceInfo ns_info = {
50 .private_dev = true,
51 .protect_control_groups = true,
52 .protect_kernel_tunables = true,
53 .protect_kernel_modules = true,
54 };
55
ee818b89
AC
56 char *root_directory;
57 char *projects_directory;
15ae422b 58 int r;
c17ec25e
MS
59 char tmp_dir[] = "/tmp/systemd-private-XXXXXX",
60 var_tmp_dir[] = "/var/tmp/systemd-private-XXXXXX";
15ae422b 61
fe3c2583
LP
62 log_set_max_level(LOG_DEBUG);
63
c17ec25e
MS
64 assert_se(mkdtemp(tmp_dir));
65 assert_se(mkdtemp(var_tmp_dir));
66
ee818b89
AC
67 root_directory = getenv("TEST_NS_CHROOT");
68 projects_directory = getenv("TEST_NS_PROJECTS");
69
70 if (projects_directory)
71 inaccessible[0] = projects_directory;
72
73 log_info("Inaccessible directory: '%s'", inaccessible[0]);
74 if (root_directory)
75 log_info("Chroot: '%s'", root_directory);
76 else
77 log_info("Not chrooted");
78
79 r = setup_namespace(root_directory,
915e6d16 80 NULL,
c575770b 81 &ns_info,
ee818b89 82 (char **) writable,
c17ec25e
MS
83 (char **) readonly,
84 (char **) inaccessible,
6c47cd7d 85 NULL,
d2d6c096 86 &(BindMount) { .source = (char*) "/usr/bin", .destination = (char*) "/etc/systemd", .read_only = true }, 1,
c17ec25e
MS
87 tmp_dir,
88 var_tmp_dir,
1b8689f9
LP
89 PROTECT_HOME_NO,
90 PROTECT_SYSTEM_NO,
915e6d16 91 0,
c17ec25e 92 0);
ac0930c8 93 if (r < 0) {
da927ba9 94 log_error_errno(r, "Failed to setup namespace: %m");
ee818b89
AC
95
96 log_info("Usage:\n"
97 " sudo TEST_NS_PROJECTS=/home/lennart/projects ./test-ns\n"
98 " sudo TEST_NS_CHROOT=/home/alban/debian-tree TEST_NS_PROJECTS=/home/alban/debian-tree/home/alban/Documents ./test-ns");
99
15ae422b
LP
100 return 1;
101 }
102
103 execl("/bin/sh", "/bin/sh", NULL);
56f64d95 104 log_error_errno(errno, "execl(): %m");
15ae422b
LP
105
106 return 1;
107}