]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-ns.c
tree-wide: sort includes
[thirdparty/systemd.git] / src / test / test-ns.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
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 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
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 NULL
32 };
33
34 const char * const readonly[] = {
35 "/",
36 "/usr",
37 "/boot",
38 NULL
39 };
40
41 const char *inaccessible[] = {
42 "/home/lennart/projects",
43 NULL
44 };
45 char *root_directory;
46 char *projects_directory;
47
48 int r;
49 char tmp_dir[] = "/tmp/systemd-private-XXXXXX",
50 var_tmp_dir[] = "/var/tmp/systemd-private-XXXXXX";
51
52 assert_se(mkdtemp(tmp_dir));
53 assert_se(mkdtemp(var_tmp_dir));
54
55 root_directory = getenv("TEST_NS_CHROOT");
56 projects_directory = getenv("TEST_NS_PROJECTS");
57
58 if (projects_directory)
59 inaccessible[0] = projects_directory;
60
61 log_info("Inaccessible directory: '%s'", inaccessible[0]);
62 if (root_directory)
63 log_info("Chroot: '%s'", root_directory);
64 else
65 log_info("Not chrooted");
66
67 r = setup_namespace(root_directory,
68 (char **) writable,
69 (char **) readonly,
70 (char **) inaccessible,
71 tmp_dir,
72 var_tmp_dir,
73 NULL,
74 true,
75 PROTECT_HOME_NO,
76 PROTECT_SYSTEM_NO,
77 0);
78 if (r < 0) {
79 log_error_errno(r, "Failed to setup namespace: %m");
80
81 log_info("Usage:\n"
82 " sudo TEST_NS_PROJECTS=/home/lennart/projects ./test-ns\n"
83 " sudo TEST_NS_CHROOT=/home/alban/debian-tree TEST_NS_PROJECTS=/home/alban/debian-tree/home/alban/Documents ./test-ns");
84
85 return 1;
86 }
87
88 execl("/bin/sh", "/bin/sh", NULL);
89 log_error_errno(errno, "execl(): %m");
90
91 return 1;
92 }