]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-ns.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-ns.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <stdlib.h>
22 #include <unistd.h>
23
24 #include "log.h"
25 #include "namespace.h"
26
27 int main(int argc, char *argv[]) {
28 const char * const writable[] = {
29 "/home",
30 "-/home/lennart/projects/foobar", /* this should be masked automatically */
31 NULL
32 };
33
34 const char * const readonly[] = {
35 /* "/", */
36 /* "/usr", */
37 "/boot",
38 "/lib",
39 "/usr/lib",
40 "-/lib64",
41 "-/usr/lib64",
42 NULL
43 };
44
45 const char *inaccessible[] = {
46 "/home/lennart/projects",
47 NULL
48 };
49
50 static const NamespaceInfo ns_info = {
51 .private_dev = true,
52 .protect_control_groups = true,
53 .protect_kernel_tunables = true,
54 .protect_kernel_modules = true,
55 };
56
57 char *root_directory;
58 char *projects_directory;
59 int r;
60 char tmp_dir[] = "/tmp/systemd-private-XXXXXX",
61 var_tmp_dir[] = "/var/tmp/systemd-private-XXXXXX";
62
63 log_set_max_level(LOG_DEBUG);
64
65 assert_se(mkdtemp(tmp_dir));
66 assert_se(mkdtemp(var_tmp_dir));
67
68 root_directory = getenv("TEST_NS_CHROOT");
69 projects_directory = getenv("TEST_NS_PROJECTS");
70
71 if (projects_directory)
72 inaccessible[0] = projects_directory;
73
74 log_info("Inaccessible directory: '%s'", inaccessible[0]);
75 if (root_directory)
76 log_info("Chroot: '%s'", root_directory);
77 else
78 log_info("Not chrooted");
79
80 r = setup_namespace(root_directory,
81 NULL,
82 &ns_info,
83 (char **) writable,
84 (char **) readonly,
85 (char **) inaccessible,
86 NULL,
87 &(BindMount) { .source = (char*) "/usr/bin", .destination = (char*) "/etc/systemd", .read_only = true }, 1,
88 tmp_dir,
89 var_tmp_dir,
90 PROTECT_HOME_NO,
91 PROTECT_SYSTEM_NO,
92 0,
93 0);
94 if (r < 0) {
95 log_error_errno(r, "Failed to setup namespace: %m");
96
97 log_info("Usage:\n"
98 " sudo TEST_NS_PROJECTS=/home/lennart/projects ./test-ns\n"
99 " sudo TEST_NS_CHROOT=/home/alban/debian-tree TEST_NS_PROJECTS=/home/alban/debian-tree/home/alban/Documents ./test-ns");
100
101 return 1;
102 }
103
104 execl("/bin/sh", "/bin/sh", NULL);
105 log_error_errno(errno, "execl(): %m");
106
107 return 1;
108 }