chiark / gitweb /
Remove src/sysv-generator
[elogind.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 "namespace.h"
25 #include "util.h"
26
27 static void test_tmpdir(const char *id, const char *A, const char *B) {
28         _cleanup_free_ char *a, *b;
29         struct stat x, y;
30         char *c, *d;
31
32         assert_se(setup_tmp_dirs(id, &a, &b) == 0);
33         assert_se(startswith(a, A));
34         assert_se(startswith(b, B));
35
36         assert_se(stat(a, &x) >= 0);
37         assert_se(stat(b, &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) == 0700);
43         assert_se((y.st_mode & 01777) == 0700);
44
45         c = strjoina(a, "/tmp");
46         d = strjoina(b, "/tmp");
47
48         assert_se(stat(c, &x) >= 0);
49         assert_se(stat(d, &y) >= 0);
50
51         assert_se(S_ISDIR(x.st_mode));
52         assert_se(S_ISDIR(y.st_mode));
53
54         assert_se((x.st_mode & 01777) == 01777);
55         assert_se((y.st_mode & 01777) == 01777);
56
57         assert_se(rmdir(c) >= 0);
58         assert_se(rmdir(d) >= 0);
59
60         assert_se(rmdir(a) >= 0);
61         assert_se(rmdir(b) >= 0);
62 }
63
64 static void test_netns(void) {
65         _cleanup_close_pair_ int s[2] = { -1, -1 };
66         pid_t pid1, pid2, pid3;
67         int r, n = 0;
68         siginfo_t si;
69
70         if (geteuid() > 0)
71                 return;
72
73         assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, s) >= 0);
74
75         pid1 = fork();
76         assert_se(pid1 >= 0);
77
78         if (pid1 == 0) {
79                 r = setup_netns(s);
80                 assert_se(r >= 0);
81                 _exit(r);
82         }
83
84         pid2 = fork();
85         assert_se(pid2 >= 0);
86
87         if (pid2 == 0) {
88                 r = setup_netns(s);
89                 assert_se(r >= 0);
90                 exit(r);
91         }
92
93         pid3 = fork();
94         assert_se(pid3 >= 0);
95
96         if (pid3 == 0) {
97                 r = setup_netns(s);
98                 assert_se(r >= 0);
99                 exit(r);
100         }
101
102         r = wait_for_terminate(pid1, &si);
103         assert_se(r >= 0);
104         assert_se(si.si_code == CLD_EXITED);
105         n += si.si_status;
106
107         r = wait_for_terminate(pid2, &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(pid3, &si);
113         assert_se(r >= 0);
114         assert_se(si.si_code == CLD_EXITED);
115         n += si.si_status;
116
117         assert_se(n == 1);
118 }
119
120 int main(int argc, char *argv[]) {
121         sd_id128_t bid;
122         char boot_id[SD_ID128_STRING_MAX];
123         _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *zz = NULL;
124
125         assert_se(sd_id128_get_boot(&bid) >= 0);
126         sd_id128_to_string(bid, boot_id);
127
128         x = strjoin("/tmp/systemd-private-", boot_id, "-abcd.service-", NULL);
129         y = strjoin("/var/tmp/systemd-private-", boot_id, "-abcd.service-", NULL);
130         assert_se(x && y);
131
132         test_tmpdir("abcd.service", x, y);
133
134         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);
135         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);
136
137         assert_se(z && zz);
138
139         test_tmpdir("sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device", z, zz);
140
141         test_netns();
142
143         return 0;
144 }