]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-ns.c
log: minimize includes in log.h
[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 <errno.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24
25 #include "log.h"
26 #include "namespace.h"
27
28 int main(int argc, char *argv[]) {
29 const char * const writable[] = {
30 "/home",
31 "-/home/lennart/projects/foobar", /* this should be masked automatically */
32 NULL
33 };
34
35 const char * const readonly[] = {
36 /* "/", */
37 /* "/usr", */
38 "/boot",
39 "/lib",
40 "/usr/lib",
41 "-/lib64",
42 "-/usr/lib64",
43 NULL
44 };
45
46 const char *inaccessible[] = {
47 "/home/lennart/projects",
48 NULL
49 };
50
51 static const NamespaceInfo ns_info = {
52 .private_dev = true,
53 .protect_control_groups = true,
54 .protect_kernel_tunables = true,
55 .protect_kernel_modules = true,
56 };
57
58 char *root_directory;
59 char *projects_directory;
60 int r;
61 char tmp_dir[] = "/tmp/systemd-private-XXXXXX",
62 var_tmp_dir[] = "/var/tmp/systemd-private-XXXXXX";
63
64 log_set_max_level(LOG_DEBUG);
65
66 assert_se(mkdtemp(tmp_dir));
67 assert_se(mkdtemp(var_tmp_dir));
68
69 root_directory = getenv("TEST_NS_CHROOT");
70 projects_directory = getenv("TEST_NS_PROJECTS");
71
72 if (projects_directory)
73 inaccessible[0] = projects_directory;
74
75 log_info("Inaccessible directory: '%s'", inaccessible[0]);
76 if (root_directory)
77 log_info("Chroot: '%s'", root_directory);
78 else
79 log_info("Not chrooted");
80
81 r = setup_namespace(root_directory,
82 NULL,
83 &ns_info,
84 (char **) writable,
85 (char **) readonly,
86 (char **) inaccessible,
87 NULL,
88 &(BindMount) { .source = (char*) "/usr/bin", .destination = (char*) "/etc/systemd", .read_only = true }, 1,
89 tmp_dir,
90 var_tmp_dir,
91 PROTECT_HOME_NO,
92 PROTECT_SYSTEM_NO,
93 0,
94 0);
95 if (r < 0) {
96 log_error_errno(r, "Failed to setup namespace: %m");
97
98 log_info("Usage:\n"
99 " sudo TEST_NS_PROJECTS=/home/lennart/projects ./test-ns\n"
100 " sudo TEST_NS_CHROOT=/home/alban/debian-tree TEST_NS_PROJECTS=/home/alban/debian-tree/home/alban/Documents ./test-ns");
101
102 return 1;
103 }
104
105 execl("/bin/sh", "/bin/sh", NULL);
106 log_error_errno(errno, "execl(): %m");
107
108 return 1;
109 }