-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathct_kill_nons.c
90 lines (72 loc) · 1.6 KB
/
ct_kill_nons.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
/*
* Test that service cgroup works
*/
#include <libct.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "test.h"
struct ct_arg {
int start_fd;
int *mark;
};
static int loop_in_ct(void *a)
{
struct ct_arg *cta = a;
char c = 'a';
int cpid;
cpid = fork();
if (cpid == 0)
goto loop;
cta->mark[0] = getpid();
cta->mark[1] = cpid;
write(cta->start_fd, &c, 1);
loop:
/*
* Don't close the pipe. If killing cgroup
* failed, test would hang forever FIXME
*/
while (1)
sleep(10);
exit(1);
}
int main(int argc, char **argv)
{
struct ct_arg cta;
int p[2];
libct_session_t s;
ct_handler_t ct;
ct_process_desc_t pd;
ct_process_t pr;
char c;
pipe(p);
cta.mark = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANON, 0, 0);
cta.mark[0] = 0;
cta.mark[1] = 0;
cta.start_fd = p[1];
s = libct_session_open_local();
ct = libct_container_create(s, "test-k");
pd = libct_process_desc_create(s);
if (libct_container_set_option(ct, LIBCT_OPT_KILLABLE, NULL))
return tst_err("can't set killable");
pr = libct_container_spawn_cb(ct, pd, loop_in_ct, &cta);
if (libct_handle_is_err(pr))
return tst_err("can't start CT");
close(p[1]);
read(p[0], &c, 1);
libct_container_kill(ct);
libct_container_wait(ct);
libct_container_destroy(ct);
libct_session_close(s);
if (read(p[0], &c, 1) != 0) /* FIXME -- this may block on error */
return fail("Pipes are alive?");
if (!cta.mark[0])
return fail("CT is not alive");
if (!cta.mark[1])
return fail("CT hasn't forked");
return pass("killed OK");
}