-
Notifications
You must be signed in to change notification settings - Fork 25
/
ct_private_subdir_ns.c
84 lines (70 loc) · 1.68 KB
/
ct_private_subdir_ns.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Test subdir as private FS in new mount namespace
*/
#include <unistd.h>
#include <libct.h>
#include <stdio.h>
#include <sys/mman.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "test.h"
#ifndef CLONE_NEWNS
#define CLONE_NEWNS 0x00020000
#endif
#define FS_ROOT "libct_test_root_ns"
#define FS_PRIVATE "libct_test_private_ns"
#define FS_FILE "libct_test_file_ns"
static int check_fs_data(void *a)
{
int fd;
int *fs_data = a;
fd = open("/" FS_FILE, O_RDONLY);
if (fd < 0)
return 0;
*fs_data = 1;
close(fd);
return 0;
}
int main(int argc, char **argv)
{
char *fs_data;
libct_session_t s;
ct_handler_t ct;
ct_process_desc_t p;
int fs_err = 0;
mkdir(FS_ROOT, 0600);
mkdir(FS_PRIVATE, 0600);
if (creat(FS_PRIVATE "/" FS_FILE, 0600) < 0) {
tst_perr("Can't create file");
return 2;
}
unlink(FS_ROOT "/" FS_FILE);
fs_data = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANON, 0, 0);
fs_data[0] = '\0';
s = libct_session_open_local();
ct = libct_container_create(s, "test");
p = libct_process_desc_create(s);
libct_container_set_nsmask(ct, CLONE_NEWNS);
libct_fs_set_root(ct, FS_ROOT);
libct_fs_set_private(ct, CT_FS_SUBDIR, FS_PRIVATE);
libct_container_spawn_cb(ct, p, check_fs_data, fs_data);
libct_container_wait(ct);
libct_container_destroy(ct);
libct_session_close(s);
if (unlink(FS_PRIVATE "/" FS_FILE) < 0)
fs_err |= 1;
if (rmdir(FS_PRIVATE) < 0)
fs_err |= 2;
if (rmdir(FS_ROOT) < 0)
fs_err |= 3;
if (fs_err) {
printf("FS remove failed %x\n", fs_err);
return fail("FS broken");
}
if (!fs_data[0])
return fail("FS private not accessible");
return pass("Subdir as private is OK");
}