]>
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>
5 #include "alloc-util.h"
8 #include "process-util.h"
9 #include "string-util.h"
13 static void test_tmpdir(const char *id
, const char *A
, const char *B
) {
14 _cleanup_free_
char *a
, *b
;
18 assert_se(setup_tmp_dirs(id
, &a
, &b
) == 0);
19 assert_se(startswith(a
, A
));
20 assert_se(startswith(b
, B
));
22 assert_se(stat(a
, &x
) >= 0);
23 assert_se(stat(b
, &y
) >= 0);
25 assert_se(S_ISDIR(x
.st_mode
));
26 assert_se(S_ISDIR(y
.st_mode
));
28 assert_se((x
.st_mode
& 01777) == 0700);
29 assert_se((y
.st_mode
& 01777) == 0700);
31 c
= strjoina(a
, "/tmp");
32 d
= strjoina(b
, "/tmp");
34 assert_se(stat(c
, &x
) >= 0);
35 assert_se(stat(d
, &y
) >= 0);
37 assert_se(S_ISDIR(x
.st_mode
));
38 assert_se(S_ISDIR(y
.st_mode
));
40 assert_se((x
.st_mode
& 01777) == 01777);
41 assert_se((y
.st_mode
& 01777) == 01777);
43 assert_se(rmdir(c
) >= 0);
44 assert_se(rmdir(d
) >= 0);
46 assert_se(rmdir(a
) >= 0);
47 assert_se(rmdir(b
) >= 0);
50 static int test_netns(void) {
51 _cleanup_close_pair_
int s
[2] = { -1, -1 };
52 pid_t pid1
, pid2
, pid3
;
57 return log_tests_skipped("not root");
59 assert_se(socketpair(AF_UNIX
, SOCK_DGRAM
, 0, s
) >= 0);
88 r
= wait_for_terminate(pid1
, &si
);
90 assert_se(si
.si_code
== CLD_EXITED
);
93 r
= wait_for_terminate(pid2
, &si
);
95 assert_se(si
.si_code
== CLD_EXITED
);
98 r
= wait_for_terminate(pid3
, &si
);
100 assert_se(si
.si_code
== CLD_EXITED
);
107 int main(int argc
, char *argv
[]) {
109 char boot_id
[SD_ID128_STRING_MAX
];
110 _cleanup_free_
char *x
= NULL
, *y
= NULL
, *z
= NULL
, *zz
= NULL
;
112 test_setup_logging(LOG_INFO
);
114 assert_se(sd_id128_get_boot(&bid
) >= 0);
115 sd_id128_to_string(bid
, boot_id
);
117 x
= strjoin("/tmp/systemd-private-", boot_id
, "-abcd.service-");
118 y
= strjoin("/var/tmp/systemd-private-", boot_id
, "-abcd.service-");
121 test_tmpdir("abcd.service", x
, y
);
123 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-");
124 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-");
128 test_tmpdir("sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device", z
, zz
);