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