]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-namespace.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-namespace.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 Zbigniew Jędrzejewski-Szmek
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 <sys/socket.h>
22
23 #include "alloc-util.h"
24 #include "fd-util.h"
25 #include "namespace.h"
26 #include "process-util.h"
27 #include "string-util.h"
28 #include "util.h"
29
30 static void test_tmpdir(const char *id, const char *A, const char *B) {
31 _cleanup_free_ char *a, *b;
32 struct stat x, y;
33 char *c, *d;
34
35 assert_se(setup_tmp_dirs(id, &a, &b) == 0);
36 assert_se(startswith(a, A));
37 assert_se(startswith(b, B));
38
39 assert_se(stat(a, &x) >= 0);
40 assert_se(stat(b, &y) >= 0);
41
42 assert_se(S_ISDIR(x.st_mode));
43 assert_se(S_ISDIR(y.st_mode));
44
45 assert_se((x.st_mode & 01777) == 0700);
46 assert_se((y.st_mode & 01777) == 0700);
47
48 c = strjoina(a, "/tmp");
49 d = strjoina(b, "/tmp");
50
51 assert_se(stat(c, &x) >= 0);
52 assert_se(stat(d, &y) >= 0);
53
54 assert_se(S_ISDIR(x.st_mode));
55 assert_se(S_ISDIR(y.st_mode));
56
57 assert_se((x.st_mode & 01777) == 01777);
58 assert_se((y.st_mode & 01777) == 01777);
59
60 assert_se(rmdir(c) >= 0);
61 assert_se(rmdir(d) >= 0);
62
63 assert_se(rmdir(a) >= 0);
64 assert_se(rmdir(b) >= 0);
65 }
66
67 static void test_netns(void) {
68 _cleanup_close_pair_ int s[2] = { -1, -1 };
69 pid_t pid1, pid2, pid3;
70 int r, n = 0;
71 siginfo_t si;
72
73 if (geteuid() > 0) {
74 log_info("Skipping test: not root");
75 exit(EXIT_TEST_SKIP);
76 }
77
78 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, s) >= 0);
79
80 pid1 = fork();
81 assert_se(pid1 >= 0);
82
83 if (pid1 == 0) {
84 r = setup_netns(s);
85 assert_se(r >= 0);
86 _exit(r);
87 }
88
89 pid2 = fork();
90 assert_se(pid2 >= 0);
91
92 if (pid2 == 0) {
93 r = setup_netns(s);
94 assert_se(r >= 0);
95 exit(r);
96 }
97
98 pid3 = fork();
99 assert_se(pid3 >= 0);
100
101 if (pid3 == 0) {
102 r = setup_netns(s);
103 assert_se(r >= 0);
104 exit(r);
105 }
106
107 r = wait_for_terminate(pid1, &si);
108 assert_se(r >= 0);
109 assert_se(si.si_code == CLD_EXITED);
110 n += si.si_status;
111
112 r = wait_for_terminate(pid2, &si);
113 assert_se(r >= 0);
114 assert_se(si.si_code == CLD_EXITED);
115 n += si.si_status;
116
117 r = wait_for_terminate(pid3, &si);
118 assert_se(r >= 0);
119 assert_se(si.si_code == CLD_EXITED);
120 n += si.si_status;
121
122 assert_se(n == 1);
123 }
124
125 int main(int argc, char *argv[]) {
126 sd_id128_t bid;
127 char boot_id[SD_ID128_STRING_MAX];
128 _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *zz = NULL;
129
130 log_parse_environment();
131 log_open();
132
133 assert_se(sd_id128_get_boot(&bid) >= 0);
134 sd_id128_to_string(bid, boot_id);
135
136 x = strjoin("/tmp/systemd-private-", boot_id, "-abcd.service-");
137 y = strjoin("/var/tmp/systemd-private-", boot_id, "-abcd.service-");
138 assert_se(x && y);
139
140 test_tmpdir("abcd.service", x, y);
141
142 z = strjoin("/tmp/systemd-private-", boot_id, "-sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device-");
143 zz = strjoin("/var/tmp/systemd-private-", boot_id, "-sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device-");
144
145 assert_se(z && zz);
146
147 test_tmpdir("sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device", z, zz);
148
149 test_netns();
150
151 return 0;
152 }