]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-namespace.c
nss-systemd: remove useless define
[thirdparty/systemd.git] / src / test / test-namespace.c
CommitLineData
d8c9d3a4
ZJS
1/***
2 This file is part of systemd.
3
4 Copyright 2013 Zbigniew Jędrzejewski-Szmek
5
6 systemd is free software; you can redistribute it and/or modify it
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
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
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
613b411c 20#include <sys/socket.h>
d8c9d3a4 21
b5efdb8a 22#include "alloc-util.h"
3ffd4af2 23#include "fd-util.h"
d8c9d3a4 24#include "namespace.h"
0b452006 25#include "process-util.h"
07630cea
LP
26#include "string-util.h"
27#include "util.h"
d8c9d3a4
ZJS
28
29static void test_tmpdir(const char *id, const char *A, const char *B) {
30 _cleanup_free_ char *a, *b;
613b411c
LP
31 struct stat x, y;
32 char *c, *d;
d8c9d3a4 33
613b411c
LP
34 assert_se(setup_tmp_dirs(id, &a, &b) == 0);
35 assert_se(startswith(a, A));
36 assert_se(startswith(b, B));
d8c9d3a4 37
613b411c
LP
38 assert_se(stat(a, &x) >= 0);
39 assert_se(stat(b, &y) >= 0);
d8c9d3a4 40
613b411c
LP
41 assert_se(S_ISDIR(x.st_mode));
42 assert_se(S_ISDIR(y.st_mode));
d8c9d3a4 43
613b411c
LP
44 assert_se((x.st_mode & 01777) == 0700);
45 assert_se((y.st_mode & 01777) == 0700);
46
63c372cb
LP
47 c = strjoina(a, "/tmp");
48 d = strjoina(b, "/tmp");
613b411c
LP
49
50 assert_se(stat(c, &x) >= 0);
51 assert_se(stat(d, &y) >= 0);
52
53 assert_se(S_ISDIR(x.st_mode));
54 assert_se(S_ISDIR(y.st_mode));
55
56 assert_se((x.st_mode & 01777) == 01777);
57 assert_se((y.st_mode & 01777) == 01777);
58
59 assert_se(rmdir(c) >= 0);
60 assert_se(rmdir(d) >= 0);
61
62 assert_se(rmdir(a) >= 0);
63 assert_se(rmdir(b) >= 0);
64}
65
66static void test_netns(void) {
3d94f76c 67 _cleanup_close_pair_ int s[2] = { -1, -1 };
613b411c
LP
68 pid_t pid1, pid2, pid3;
69 int r, n = 0;
70 siginfo_t si;
71
d2528deb
EV
72 if (geteuid() > 0) {
73 log_info("Skipping test: not root");
74 exit(EXIT_TEST_SKIP);
75 }
613b411c
LP
76
77 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, s) >= 0);
78
79 pid1 = fork();
80 assert_se(pid1 >= 0);
81
82 if (pid1 == 0) {
83 r = setup_netns(s);
84 assert_se(r >= 0);
85 _exit(r);
86 }
87
88 pid2 = fork();
89 assert_se(pid2 >= 0);
90
91 if (pid2 == 0) {
92 r = setup_netns(s);
93 assert_se(r >= 0);
94 exit(r);
95 }
96
97 pid3 = fork();
98 assert_se(pid3 >= 0);
99
100 if (pid3 == 0) {
101 r = setup_netns(s);
102 assert_se(r >= 0);
103 exit(r);
104 }
105
106 r = wait_for_terminate(pid1, &si);
107 assert_se(r >= 0);
108 assert_se(si.si_code == CLD_EXITED);
109 n += si.si_status;
110
111 r = wait_for_terminate(pid2, &si);
112 assert_se(r >= 0);
113 assert_se(si.si_code == CLD_EXITED);
114 n += si.si_status;
115
116 r = wait_for_terminate(pid3, &si);
117 assert_se(r >= 0);
118 assert_se(si.si_code == CLD_EXITED);
119 n += si.si_status;
120
121 assert_se(n == 1);
d8c9d3a4
ZJS
122}
123
124int main(int argc, char *argv[]) {
6b46ea73
LP
125 sd_id128_t bid;
126 char boot_id[SD_ID128_STRING_MAX];
127 _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *zz = NULL;
d8c9d3a4 128
d2528deb
EV
129 log_parse_environment();
130 log_open();
131
6b46ea73
LP
132 assert_se(sd_id128_get_boot(&bid) >= 0);
133 sd_id128_to_string(bid, boot_id);
134
135 x = strjoin("/tmp/systemd-private-", boot_id, "-abcd.service-", NULL);
136 y = strjoin("/var/tmp/systemd-private-", boot_id, "-abcd.service-", NULL);
137 assert_se(x && y);
138
139 test_tmpdir("abcd.service", x, y);
140
141 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-", NULL);
142 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-", NULL);
143
144 assert_se(z && zz);
145
146 test_tmpdir("sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device", z, zz);
d8c9d3a4 147
613b411c
LP
148 test_netns();
149
d8c9d3a4
ZJS
150 return 0;
151}