]>
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 | 35 | |
18cd4b54 DJ |
36 | #ifndef HAVE_STRLCPY |
37 | #include "include/strlcpy.h" | |
38 | #endif | |
39 | ||
ae5c8b8e | 40 | #define MYNAME "lxctest1" |
ae5c8b8e | 41 | |
2acf7795 DE |
42 | #define TSTERR(fmt, ...) do { \ |
43 | fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \ | |
ae5c8b8e SH |
44 | } while (0) |
45 | ||
2acf7795 DE |
46 | /* |
47 | * test_running_container: test cgroup functions against a running container | |
48 | * | |
49 | * @group : name of the container group or NULL for default "lxc" | |
50 | * @name : name of the container | |
51 | */ | |
52 | static int test_running_container(const char *lxcpath, | |
53 | const char *group, const char *name) | |
54 | { | |
b98f7d6e | 55 | int ret = -1; |
2acf7795 DE |
56 | struct lxc_container *c = NULL; |
57 | char *cgrelpath; | |
2acf7795 | 58 | char relpath[PATH_MAX+1]; |
2acf7795 | 59 | char value[NAME_MAX], value_save[NAME_MAX]; |
2202afc9 | 60 | struct cgroup_ops *cgroup_ops; |
2acf7795 | 61 | |
2d172fc7 | 62 | sprintf(relpath, "%s/%s", group ? group : "lxc.payload", name); |
2acf7795 DE |
63 | |
64 | if ((c = lxc_container_new(name, lxcpath)) == NULL) { | |
65 | TSTERR("container %s couldn't instantiate", name); | |
66 | goto err1; | |
67 | } | |
68 | if (!c->is_defined(c)) { | |
69 | TSTERR("container %s does not exist", name); | |
70 | goto err2; | |
71 | } | |
72 | ||
b98f7d6e | 73 | cgrelpath = lxc_cmd_get_cgroup_path(c->name, c->config_path, "freezer"); |
2acf7795 DE |
74 | if (!cgrelpath) { |
75 | TSTERR("lxc_cmd_get_cgroup_path returned NULL"); | |
76 | goto err2; | |
77 | } | |
78 | if (!strstr(cgrelpath, relpath)) { | |
79 | TSTERR("lxc_cmd_get_cgroup_path %s not in %s", relpath, cgrelpath); | |
80 | goto err3; | |
81 | } | |
82 | ||
5a087e05 | 83 | cgroup_ops = cgroup_init(c->lxc_conf); |
2202afc9 CB |
84 | if (!cgroup_ops) |
85 | goto err3; | |
86 | ||
1f845c39 | 87 | /* test get/set value using memory.soft_limit_in_bytes file */ |
2202afc9 CB |
88 | ret = cgroup_ops->get(cgroup_ops, "memory.soft_limit_in_bytes", value, |
89 | sizeof(value), c->name, c->config_path); | |
2acf7795 | 90 | if (ret < 0) { |
2202afc9 | 91 | TSTERR("cgroup_get failed"); |
2acf7795 | 92 | goto err3; |
ae5c8b8e | 93 | } |
18cd4b54 | 94 | (void)strlcpy(value_save, value, NAME_MAX); |
ae5c8b8e | 95 | |
2202afc9 CB |
96 | ret = cgroup_ops->set(cgroup_ops, "memory.soft_limit_in_bytes", "512M", |
97 | c->name, c->config_path); | |
2acf7795 | 98 | if (ret < 0) { |
2202afc9 | 99 | TSTERR("cgroup_set failed %d %d", ret, errno); |
2acf7795 | 100 | goto err3; |
ae5c8b8e | 101 | } |
2202afc9 CB |
102 | ret = cgroup_ops->get(cgroup_ops, "memory.soft_limit_in_bytes", value, |
103 | sizeof(value), c->name, c->config_path); | |
2acf7795 | 104 | if (ret < 0) { |
2202afc9 | 105 | TSTERR("cgroup_get failed"); |
2acf7795 DE |
106 | goto err3; |
107 | } | |
1f845c39 | 108 | if (strcmp(value, "536870912\n")) { |
2202afc9 | 109 | TSTERR("cgroup_set_bypath failed to set value >%s<", value); |
2acf7795 DE |
110 | goto err3; |
111 | } | |
112 | ||
113 | /* restore original value */ | |
2202afc9 CB |
114 | ret = cgroup_ops->set(cgroup_ops, "memory.soft_limit_in_bytes", |
115 | value_save, c->name, c->config_path); | |
2acf7795 | 116 | if (ret < 0) { |
2202afc9 | 117 | TSTERR("cgroup_set failed"); |
2acf7795 DE |
118 | goto err3; |
119 | } | |
ae5c8b8e | 120 | |
2acf7795 | 121 | ret = 0; |
4fb3cba5 | 122 | |
2acf7795 DE |
123 | err3: |
124 | free(cgrelpath); | |
125 | err2: | |
126 | lxc_container_put(c); | |
127 | err1: | |
128 | return ret; | |
129 | } | |
130 | ||
131 | static int test_container(const char *lxcpath, | |
132 | const char *group, const char *name, | |
133 | const char *template) | |
134 | { | |
135 | int ret; | |
136 | struct lxc_container *c = NULL; | |
ae5c8b8e | 137 | |
2acf7795 DE |
138 | if (lxcpath) { |
139 | ret = mkdir(lxcpath, 0755); | |
140 | if (ret < 0 && errno != EEXIST) { | |
141 | TSTERR("failed to mkdir %s %s", lxcpath, strerror(errno)); | |
142 | goto out1; | |
143 | } | |
144 | } | |
145 | ret = -1; | |
ae5c8b8e | 146 | |
2acf7795 DE |
147 | if ((c = lxc_container_new(name, lxcpath)) == NULL) { |
148 | TSTERR("instantiating container %s", name); | |
149 | goto out1; | |
ae5c8b8e SH |
150 | } |
151 | if (c->is_defined(c)) { | |
152 | c->stop(c); | |
153 | c->destroy(c); | |
2acf7795 | 154 | c = lxc_container_new(name, lxcpath); |
ae5c8b8e | 155 | } |
7fa3f2e9 | 156 | c->set_config_item(c, "lxc.net.0.type", "empty"); |
dc23c1c8 | 157 | if (!c->createl(c, template, NULL, NULL, 0, NULL)) { |
2acf7795 DE |
158 | TSTERR("creating container %s", name); |
159 | goto out2; | |
ae5c8b8e SH |
160 | } |
161 | c->load_config(c, NULL); | |
540f932a | 162 | c->want_daemonize(c, true); |
ae5c8b8e | 163 | if (!c->startl(c, 0, NULL)) { |
2acf7795 DE |
164 | TSTERR("starting container %s", name); |
165 | goto out3; | |
ae5c8b8e | 166 | } |
ae5c8b8e | 167 | |
2acf7795 | 168 | ret = test_running_container(lxcpath, group, name); |
ae5c8b8e | 169 | |
2acf7795 DE |
170 | c->stop(c); |
171 | out3: | |
172 | c->destroy(c); | |
173 | out2: | |
174 | lxc_container_put(c); | |
175 | out1: | |
176 | return ret; | |
177 | } | |
178 | ||
179 | int main() | |
180 | { | |
181 | int ret = EXIT_FAILURE; | |
182 | ||
183 | /* won't require privilege necessarily once users are classified by | |
184 | * pam_cgroup */ | |
185 | if (geteuid() != 0) { | |
186 | TSTERR("requires privilege"); | |
57017714 | 187 | exit(EXIT_SUCCESS); |
ae5c8b8e SH |
188 | } |
189 | ||
2acf7795 DE |
190 | #if TEST_ALREADY_RUNNING_CT |
191 | ||
192 | /* | |
193 | * This is useful for running with valgrind to test for memory | |
194 | * leaks. The container should already be running, we can't start | |
195 | * the container ourselves because valgrind gets confused by lxc's | |
196 | * internal calls to clone. | |
197 | */ | |
198 | if (test_running_container(NULL, NULL, "bb01") < 0) | |
ae5c8b8e | 199 | goto out; |
2acf7795 DE |
200 | printf("Running container cgroup tests...Passed\n"); |
201 | ||
202 | #else | |
ae5c8b8e | 203 | |
2acf7795 | 204 | if (test_container(NULL, NULL, MYNAME, "busybox") < 0) |
7e1667d7 | 205 | goto out; |
2acf7795 | 206 | printf("Container creation tests...Passed\n"); |
ae5c8b8e | 207 | |
2acf7795 | 208 | if (test_container("/var/lib/lxctest2", NULL, MYNAME, "busybox") < 0) |
ae5c8b8e | 209 | goto out; |
2acf7795 | 210 | printf("Container creation with LXCPATH tests...Passed\n"); |
ae5c8b8e | 211 | |
2acf7795 DE |
212 | #endif |
213 | ||
214 | ret = EXIT_SUCCESS; | |
ae5c8b8e | 215 | out: |
2acf7795 | 216 | return ret; |
ae5c8b8e | 217 | } |