]>
Commit | Line | Data |
---|---|---|
ae5c8b8e SH |
1 | /* liblxcapi |
2 | * | |
8cd80b50 SG |
3 | * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>. |
4 | * Copyright © 2012 Canonical Ltd. | |
ae5c8b8e SH |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2, as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | */ | |
948955a2 | 19 | #include <lxc/lxccontainer.h> |
ae5c8b8e | 20 | |
2acf7795 | 21 | #include <limits.h> |
ae5c8b8e SH |
22 | #include <unistd.h> |
23 | #include <signal.h> | |
24 | #include <stdio.h> | |
25 | #include <sys/types.h> | |
26 | #include <sys/wait.h> | |
27 | #include <stdlib.h> | |
28 | #include <errno.h> | |
95ee490b SG |
29 | #include <string.h> |
30 | #include <sys/stat.h> | |
ca1e6c02 CB |
31 | |
32 | #include "cgroup.h" | |
33 | #include "lxc.h" | |
34 | #include "commands.h" | |
ae5c8b8e SH |
35 | |
36 | #define MYNAME "lxctest1" | |
ae5c8b8e | 37 | |
2acf7795 DE |
38 | #define TSTERR(fmt, ...) do { \ |
39 | fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \ | |
ae5c8b8e SH |
40 | } while (0) |
41 | ||
2acf7795 DE |
42 | /* |
43 | * test_running_container: test cgroup functions against a running container | |
44 | * | |
45 | * @group : name of the container group or NULL for default "lxc" | |
46 | * @name : name of the container | |
47 | */ | |
48 | static int test_running_container(const char *lxcpath, | |
49 | const char *group, const char *name) | |
50 | { | |
b98f7d6e | 51 | int ret = -1; |
2acf7795 DE |
52 | struct lxc_container *c = NULL; |
53 | char *cgrelpath; | |
2acf7795 | 54 | char relpath[PATH_MAX+1]; |
2acf7795 DE |
55 | char value[NAME_MAX], value_save[NAME_MAX]; |
56 | ||
57 | sprintf(relpath, "%s/%s", group ? group : "lxc", name); | |
58 | ||
59 | if ((c = lxc_container_new(name, lxcpath)) == NULL) { | |
60 | TSTERR("container %s couldn't instantiate", name); | |
61 | goto err1; | |
62 | } | |
63 | if (!c->is_defined(c)) { | |
64 | TSTERR("container %s does not exist", name); | |
65 | goto err2; | |
66 | } | |
67 | ||
b98f7d6e | 68 | cgrelpath = lxc_cmd_get_cgroup_path(c->name, c->config_path, "freezer"); |
2acf7795 DE |
69 | if (!cgrelpath) { |
70 | TSTERR("lxc_cmd_get_cgroup_path returned NULL"); | |
71 | goto err2; | |
72 | } | |
73 | if (!strstr(cgrelpath, relpath)) { | |
74 | TSTERR("lxc_cmd_get_cgroup_path %s not in %s", relpath, cgrelpath); | |
75 | goto err3; | |
76 | } | |
77 | ||
1f845c39 DE |
78 | /* test get/set value using memory.soft_limit_in_bytes file */ |
79 | ret = lxc_cgroup_get("memory.soft_limit_in_bytes", value, sizeof(value), | |
33ad9f1a | 80 | c->name, c->config_path); |
2acf7795 DE |
81 | if (ret < 0) { |
82 | TSTERR("lxc_cgroup_get failed"); | |
83 | goto err3; | |
ae5c8b8e | 84 | } |
2acf7795 | 85 | strcpy(value_save, value); |
ae5c8b8e | 86 | |
1f845c39 | 87 | ret = lxc_cgroup_set("memory.soft_limit_in_bytes", "512M", c->name, c->config_path); |
2acf7795 | 88 | if (ret < 0) { |
1f845c39 | 89 | TSTERR("lxc_cgroup_set failed %d %d", ret, errno); |
2acf7795 | 90 | goto err3; |
ae5c8b8e | 91 | } |
1f845c39 | 92 | ret = lxc_cgroup_get("memory.soft_limit_in_bytes", value, sizeof(value), |
33ad9f1a | 93 | c->name, c->config_path); |
2acf7795 DE |
94 | if (ret < 0) { |
95 | TSTERR("lxc_cgroup_get failed"); | |
96 | goto err3; | |
97 | } | |
1f845c39 | 98 | if (strcmp(value, "536870912\n")) { |
2acf7795 DE |
99 | TSTERR("lxc_cgroup_set_bypath failed to set value >%s<", value); |
100 | goto err3; | |
101 | } | |
102 | ||
103 | /* restore original value */ | |
1f845c39 | 104 | ret = lxc_cgroup_set("memory.soft_limit_in_bytes", value_save, |
33ad9f1a | 105 | c->name, c->config_path); |
2acf7795 DE |
106 | if (ret < 0) { |
107 | TSTERR("lxc_cgroup_set failed"); | |
108 | goto err3; | |
109 | } | |
ae5c8b8e | 110 | |
2acf7795 | 111 | ret = 0; |
4fb3cba5 | 112 | |
2acf7795 DE |
113 | err3: |
114 | free(cgrelpath); | |
115 | err2: | |
116 | lxc_container_put(c); | |
117 | err1: | |
118 | return ret; | |
119 | } | |
120 | ||
121 | static int test_container(const char *lxcpath, | |
122 | const char *group, const char *name, | |
123 | const char *template) | |
124 | { | |
125 | int ret; | |
126 | struct lxc_container *c = NULL; | |
ae5c8b8e | 127 | |
2acf7795 DE |
128 | if (lxcpath) { |
129 | ret = mkdir(lxcpath, 0755); | |
130 | if (ret < 0 && errno != EEXIST) { | |
131 | TSTERR("failed to mkdir %s %s", lxcpath, strerror(errno)); | |
132 | goto out1; | |
133 | } | |
134 | } | |
135 | ret = -1; | |
ae5c8b8e | 136 | |
2acf7795 DE |
137 | if ((c = lxc_container_new(name, lxcpath)) == NULL) { |
138 | TSTERR("instantiating container %s", name); | |
139 | goto out1; | |
ae5c8b8e SH |
140 | } |
141 | if (c->is_defined(c)) { | |
142 | c->stop(c); | |
143 | c->destroy(c); | |
2acf7795 | 144 | c = lxc_container_new(name, lxcpath); |
ae5c8b8e | 145 | } |
7fa3f2e9 | 146 | c->set_config_item(c, "lxc.net.0.type", "empty"); |
dc23c1c8 | 147 | if (!c->createl(c, template, NULL, NULL, 0, NULL)) { |
2acf7795 DE |
148 | TSTERR("creating container %s", name); |
149 | goto out2; | |
ae5c8b8e SH |
150 | } |
151 | c->load_config(c, NULL); | |
540f932a | 152 | c->want_daemonize(c, true); |
ae5c8b8e | 153 | if (!c->startl(c, 0, NULL)) { |
2acf7795 DE |
154 | TSTERR("starting container %s", name); |
155 | goto out3; | |
ae5c8b8e | 156 | } |
ae5c8b8e | 157 | |
2acf7795 | 158 | ret = test_running_container(lxcpath, group, name); |
ae5c8b8e | 159 | |
2acf7795 DE |
160 | c->stop(c); |
161 | out3: | |
162 | c->destroy(c); | |
163 | out2: | |
164 | lxc_container_put(c); | |
165 | out1: | |
166 | return ret; | |
167 | } | |
168 | ||
169 | int main() | |
170 | { | |
171 | int ret = EXIT_FAILURE; | |
172 | ||
173 | /* won't require privilege necessarily once users are classified by | |
174 | * pam_cgroup */ | |
175 | if (geteuid() != 0) { | |
176 | TSTERR("requires privilege"); | |
177 | exit(0); | |
ae5c8b8e SH |
178 | } |
179 | ||
2acf7795 DE |
180 | #if TEST_ALREADY_RUNNING_CT |
181 | ||
182 | /* | |
183 | * This is useful for running with valgrind to test for memory | |
184 | * leaks. The container should already be running, we can't start | |
185 | * the container ourselves because valgrind gets confused by lxc's | |
186 | * internal calls to clone. | |
187 | */ | |
188 | if (test_running_container(NULL, NULL, "bb01") < 0) | |
ae5c8b8e | 189 | goto out; |
2acf7795 DE |
190 | printf("Running container cgroup tests...Passed\n"); |
191 | ||
192 | #else | |
ae5c8b8e | 193 | |
2acf7795 | 194 | if (test_container(NULL, NULL, MYNAME, "busybox") < 0) |
7e1667d7 | 195 | goto out; |
2acf7795 | 196 | printf("Container creation tests...Passed\n"); |
ae5c8b8e | 197 | |
2acf7795 | 198 | if (test_container("/var/lib/lxctest2", NULL, MYNAME, "busybox") < 0) |
ae5c8b8e | 199 | goto out; |
2acf7795 | 200 | printf("Container creation with LXCPATH tests...Passed\n"); |
ae5c8b8e | 201 | |
2acf7795 DE |
202 | #endif |
203 | ||
204 | ret = EXIT_SUCCESS; | |
ae5c8b8e | 205 | out: |
2acf7795 | 206 | return ret; |
ae5c8b8e | 207 | } |