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