-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathct_ext_mount.c
123 lines (105 loc) · 2.7 KB
/
ct_ext_mount.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Test external bind mount works
*/
#include <unistd.h>
#include <libct.h>
#include <stdio.h>
#include <sys/mman.h>
#include <string.h>
#include <fcntl.h>
#include <sys/vfs.h>
#include <linux/magic.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "test.h"
#define FS_ROOT "libct_test_root"
#define FS_EXT "libct_test_external"
#define FS_ACT "libct_test_actions"
#define FS_PTS "libct_test_devpts"
#define FS_DIR "dir"
#define FS_FILE "file"
static int check_fs_data(void *a)
{
int fd;
int *fs_data = a;
struct statfs sfs;
fd = open("/" FS_DIR "/" FS_FILE, O_RDONLY);
if (fd < 0)
return 0;
close(fd);
if (statfs("/" FS_PTS, &sfs)) {
printf("stafs(%s): %m", FS_PTS);
return 1;
}
if (sfs.f_type != DEVPTS_SUPER_MAGIC) {
printf("Unexpected fs magic %lx instead of %x\n", sfs.f_type, DEVPTS_SUPER_MAGIC);
return 1;
}
if (access(FS_ACT "/hello", F_OK)) {
tst_err("Unable to access %s", FS_ACT "/hello");
return 1;
}
*fs_data = 1;
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;
char *preargv[] = {"echo", "premount", NULL};
struct libct_cmd premount = {
.path = "echo",
.argv = preargv,
};
char *postargv[] = {"touch", FS_ROOT "/" FS_ACT "/hello", NULL};
struct libct_cmd postmount = {
.path = "touch",
.argv = postargv,
};
mkdir(FS_EXT, 0600);
if (creat(FS_EXT "/" FS_FILE, 0600) < 0)
return tst_perr("Can't create file");
mkdir(FS_ROOT, 0600);
mkdir(FS_ROOT "/" FS_DIR, 0600);
unlink(FS_ROOT "/" FS_DIR "/" 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);
printf("Set root\n");
libct_fs_set_root(ct, FS_ROOT);
printf("Set bind\n");
libct_fs_add_bind_mount(ct, FS_EXT, FS_DIR, 0);
libct_fs_add_mount(ct, "test_devpts", FS_PTS, 0, "devpts", "newinstance");
libct_fs_add_mount_with_actions(ct, "test_actions", FS_ACT, 0, "tmpfs", "", &premount, &postmount);
printf("Spawn\n");
libct_container_spawn_cb(ct, p, check_fs_data, fs_data);
printf("Done\n");
libct_container_wait(ct);
libct_container_destroy(ct);
libct_session_close(s);
if (rmdir(FS_ROOT "/" FS_DIR) < 0)
fs_err |= 1;
if (rmdir(FS_ROOT "/" FS_PTS) < 0)
fs_err |= 16;
if (rmdir(FS_ROOT "/" FS_ACT) < 0)
fs_err |= 32;
if (rmdir(FS_ROOT) < 0)
fs_err |= 2;
if (unlink(FS_EXT "/" FS_FILE) < 0)
fs_err |= 4;
if (rmdir(FS_EXT) < 0)
fs_err |= 8;
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");
}