]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-namespace.c
fb02d9fd5f7006cda0b9e86ee7cca22605038e13
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 Zbigniew Jędrzejewski-Szmek
6 #include <sys/socket.h>
8 #include "alloc-util.h"
10 #include "namespace.h"
11 #include "process-util.h"
12 #include "string-util.h"
15 static void test_tmpdir(const char *id
, const char *A
, const char *B
) {
16 _cleanup_free_
char *a
, *b
;
20 assert_se(setup_tmp_dirs(id
, &a
, &b
) == 0);
21 assert_se(startswith(a
, A
));
22 assert_se(startswith(b
, B
));
24 assert_se(stat(a
, &x
) >= 0);
25 assert_se(stat(b
, &y
) >= 0);
27 assert_se(S_ISDIR(x
.st_mode
));
28 assert_se(S_ISDIR(y
.st_mode
));
30 assert_se((x
.st_mode
& 01777) == 0700);
31 assert_se((y
.st_mode
& 01777) == 0700);
33 c
= strjoina(a
, "/tmp");
34 d
= strjoina(b
, "/tmp");
36 assert_se(stat(c
, &x
) >= 0);
37 assert_se(stat(d
, &y
) >= 0);
39 assert_se(S_ISDIR(x
.st_mode
));
40 assert_se(S_ISDIR(y
.st_mode
));
42 assert_se((x
.st_mode
& 01777) == 01777);
43 assert_se((y
.st_mode
& 01777) == 01777);
45 assert_se(rmdir(c
) >= 0);
46 assert_se(rmdir(d
) >= 0);
48 assert_se(rmdir(a
) >= 0);
49 assert_se(rmdir(b
) >= 0);
52 static void test_netns(void) {
53 _cleanup_close_pair_
int s
[2] = { -1, -1 };
54 pid_t pid1
, pid2
, pid3
;
59 log_info("Skipping test: not root");
63 assert_se(socketpair(AF_UNIX
, SOCK_DGRAM
, 0, s
) >= 0);
92 r
= wait_for_terminate(pid1
, &si
);
94 assert_se(si
.si_code
== CLD_EXITED
);
97 r
= wait_for_terminate(pid2
, &si
);
99 assert_se(si
.si_code
== CLD_EXITED
);
102 r
= wait_for_terminate(pid3
, &si
);
104 assert_se(si
.si_code
== CLD_EXITED
);
110 int main(int argc
, char *argv
[]) {
112 char boot_id
[SD_ID128_STRING_MAX
];
113 _cleanup_free_
char *x
= NULL
, *y
= NULL
, *z
= NULL
, *zz
= NULL
;
115 log_parse_environment();
118 assert_se(sd_id128_get_boot(&bid
) >= 0);
119 sd_id128_to_string(bid
, boot_id
);
121 x
= strjoin("/tmp/systemd-private-", boot_id
, "-abcd.service-");
122 y
= strjoin("/var/tmp/systemd-private-", boot_id
, "-abcd.service-");
125 test_tmpdir("abcd.service", x
, y
);
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-");
132 test_tmpdir("sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device", z
, zz
);