]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-namespace.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <sys/socket.h>
6 #include "alloc-util.h"
9 #include "process-util.h"
10 #include "string-util.h"
14 static void test_tmpdir(const char *id
, const char *A
, const char *B
) {
15 _cleanup_free_
char *a
, *b
;
19 assert_se(setup_tmp_dirs(id
, &a
, &b
) == 0);
20 assert_se(startswith(a
, A
));
21 assert_se(startswith(b
, B
));
23 assert_se(stat(a
, &x
) >= 0);
24 assert_se(stat(b
, &y
) >= 0);
26 assert_se(S_ISDIR(x
.st_mode
));
27 assert_se(S_ISDIR(y
.st_mode
));
29 assert_se((x
.st_mode
& 01777) == 0700);
30 assert_se((y
.st_mode
& 01777) == 0700);
32 c
= strjoina(a
, "/tmp");
33 d
= strjoina(b
, "/tmp");
35 assert_se(stat(c
, &x
) >= 0);
36 assert_se(stat(d
, &y
) >= 0);
38 assert_se(S_ISDIR(x
.st_mode
));
39 assert_se(S_ISDIR(y
.st_mode
));
41 assert_se((x
.st_mode
& 01777) == 01777);
42 assert_se((y
.st_mode
& 01777) == 01777);
44 assert_se(rmdir(c
) >= 0);
45 assert_se(rmdir(d
) >= 0);
47 assert_se(rmdir(a
) >= 0);
48 assert_se(rmdir(b
) >= 0);
51 static int test_netns(void) {
52 _cleanup_close_pair_
int s
[2] = { -1, -1 };
53 pid_t pid1
, pid2
, pid3
;
58 return log_tests_skipped("not root");
60 assert_se(socketpair(AF_UNIX
, SOCK_DGRAM
, 0, s
) >= 0);
89 r
= wait_for_terminate(pid1
, &si
);
91 assert_se(si
.si_code
== CLD_EXITED
);
94 r
= wait_for_terminate(pid2
, &si
);
96 assert_se(si
.si_code
== CLD_EXITED
);
99 r
= wait_for_terminate(pid3
, &si
);
101 assert_se(si
.si_code
== CLD_EXITED
);
108 int main(int argc
, char *argv
[]) {
110 char boot_id
[SD_ID128_STRING_MAX
];
111 _cleanup_free_
char *x
= NULL
, *y
= NULL
, *z
= NULL
, *zz
= NULL
;
113 test_setup_logging(LOG_INFO
);
115 if (!have_namespaces()) {
116 log_tests_skipped("Don't have namespace support");
117 return EXIT_TEST_SKIP
;
120 assert_se(sd_id128_get_boot(&bid
) >= 0);
121 sd_id128_to_string(bid
, boot_id
);
123 x
= strjoin("/tmp/systemd-private-", boot_id
, "-abcd.service-");
124 y
= strjoin("/var/tmp/systemd-private-", boot_id
, "-abcd.service-");
127 test_tmpdir("abcd.service", x
, y
);
129 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-");
130 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-");
134 test_tmpdir("sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device", z
, zz
);