-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathct_service.c
102 lines (84 loc) · 1.75 KB
/
ct_service.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
/*
* Test that service cgroup works
*/
#include <libct.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "test.h"
struct ct_arg {
int start_fd;
int wait_fd;
int *mark;
};
static int set_ct_alive(void *a)
{
struct ct_arg *cta = a;
char c = 'a';
cta->mark[0] = getpid();
write(cta->start_fd, &c, 1);
read(cta->wait_fd, &c, 1);
return 0;
}
static int check_service_cg(int pid)
{
FILE *f;
char buf[32];
f = fopen("/sys/fs/cgroup/.libct/test-s/tasks", "r");
if (!f) {
perror("No file\n");
return 0;
}
memset(buf, 0, sizeof(buf));
if (!fgets(buf, sizeof(buf), f)) {
fclose(f);
return 0;
}
fclose(f);
return atoi(buf) == pid;
}
int main(int argc, char **argv)
{
struct ct_arg cta;
int p[2], p2[2];
libct_session_t s;
ct_handler_t ct;
ct_process_desc_t pd;
ct_process_t pr;
int cg_ok = 0;
char c;
pipe(p);
pipe(p2);
cta.mark = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANON, 0, 0);
cta.mark[0] = 0;
cta.start_fd = p2[1];
cta.wait_fd = p[0];
s = libct_session_open_local();
ct = libct_container_create(s, "test-s");
pd = libct_process_desc_create(s);
if (libct_container_set_option(ct, LIBCT_OPT_KILLABLE, NULL)) {
tst_err("can't set killable");
return 2;
}
pr = libct_container_spawn_cb(ct, pd, set_ct_alive, &cta);
if (libct_handle_is_err(pr)) {
tst_err("can't start CT");
return 2;
}
read(p2[0], &c, 1);
if (cta.mark[0])
cg_ok = check_service_cg(cta.mark[0]);
write(p[1], &c, 1);
libct_container_wait(ct);
libct_container_destroy(ct);
libct_session_close(s);
if (!cta.mark[0])
return fail("CT is not alive");
if (!cg_ok)
return fail("Service CG is not there");
return pass("service CG works OK");
}