]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-namespace.c
util-lib: split out allocation calls into alloc-util.[ch]
[thirdparty/systemd.git] / src / test / test-namespace.c
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
22 #include <sys/socket.h>
23
24 #include "alloc-util.h"
25 #include "fd-util.h"
26 #include "namespace.h"
27 #include "process-util.h"
28 #include "string-util.h"
29 #include "util.h"
30
31 static void test_tmpdir(const char *id, const char *A, const char *B) {
32 _cleanup_free_ char *a, *b;
33 struct stat x, y;
34 char *c, *d;
35
36 assert_se(setup_tmp_dirs(id, &a, &b) == 0);
37 assert_se(startswith(a, A));
38 assert_se(startswith(b, B));
39
40 assert_se(stat(a, &x) >= 0);
41 assert_se(stat(b, &y) >= 0);
42
43 assert_se(S_ISDIR(x.st_mode));
44 assert_se(S_ISDIR(y.st_mode));
45
46 assert_se((x.st_mode & 01777) == 0700);
47 assert_se((y.st_mode & 01777) == 0700);
48
49 c = strjoina(a, "/tmp");
50 d = strjoina(b, "/tmp");
51
52 assert_se(stat(c, &x) >= 0);
53 assert_se(stat(d, &y) >= 0);
54
55 assert_se(S_ISDIR(x.st_mode));
56 assert_se(S_ISDIR(y.st_mode));
57
58 assert_se((x.st_mode & 01777) == 01777);
59 assert_se((y.st_mode & 01777) == 01777);
60
61 assert_se(rmdir(c) >= 0);
62 assert_se(rmdir(d) >= 0);
63
64 assert_se(rmdir(a) >= 0);
65 assert_se(rmdir(b) >= 0);
66 }
67
68 static void test_netns(void) {
69 _cleanup_close_pair_ int s[2] = { -1, -1 };
70 pid_t pid1, pid2, pid3;
71 int r, n = 0;
72 siginfo_t si;
73
74 if (geteuid() > 0)
75 return;
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);
122 }
123
124 int main(int argc, char *argv[]) {
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;
128
129 assert_se(sd_id128_get_boot(&bid) >= 0);
130 sd_id128_to_string(bid, boot_id);
131
132 x = strjoin("/tmp/systemd-private-", boot_id, "-abcd.service-", NULL);
133 y = strjoin("/var/tmp/systemd-private-", boot_id, "-abcd.service-", NULL);
134 assert_se(x && y);
135
136 test_tmpdir("abcd.service", x, y);
137
138 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);
139 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);
140
141 assert_se(z && zz);
142
143 test_tmpdir("sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device", z, zz);
144
145 test_netns();
146
147 return 0;
148 }