-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathct_pid_enter.c
80 lines (66 loc) · 1.53 KB
/
ct_pid_enter.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
/*
* Test entering into living container with pidns
*/
#include <libct.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>
#include "test.h"
#ifndef CLONE_NEWPID
#define CLONE_NEWPID 0x20000000
#endif
struct ct_arg {
int wait_fd;
int *mark;
};
static int set_ct_alive(void *a)
{
struct ct_arg *cta = a;
char c;
cta->mark[0] = getpid();
cta->mark[1] = getppid();
read(cta->wait_fd, &c, 1);
return 0;
}
static int set_ct_enter(void *a)
{
struct ct_arg *cta = a;
cta->mark[2] = getpid();
cta->mark[3] = getppid();
return 0;
}
int main(int argc, char **argv)
{
struct ct_arg cta;
int p[2], status;
libct_session_t s;
ct_handler_t ct;
ct_process_desc_t pd;
ct_process_t pr;
pipe(p);
cta.mark = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANON, 0, 0);
cta.mark[0] = -1;
cta.mark[1] = -1;
cta.mark[2] = -1;
cta.mark[3] = -1;
cta.wait_fd = p[0];
s = libct_session_open_local();
ct = libct_container_create(s, "test");
pd = libct_process_desc_create(s);
libct_container_set_nsmask(ct, CLONE_NEWPID);
libct_container_spawn_cb(ct, pd, set_ct_alive, &cta);
pr = libct_container_enter_cb(ct, pd, set_ct_enter, &cta);
libct_process_wait(pr, &status);
write(p[1], "a", 1);
libct_container_wait(ct);
libct_container_destroy(ct);
libct_session_close(s);
if (cta.mark[0] == -1)
return fail("CT is not alive");
if (cta.mark[1] == -1)
return fail("CT is not enterable");
printf("pids (%d:%d) (%d:%d)\n", cta.mark[0], cta.mark[1], cta.mark[2], cta.mark[3]);
return pass("OK");
}