]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/cgpath.c
remove left over debug getchar()
[mirror_lxc.git] / src / tests / cgpath.c
1 /* liblxcapi
2 *
3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 Canonical Ltd.
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 */
19 #include <lxc/lxccontainer.h>
20
21 #include <limits.h>
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>
29 #include "../lxc/cgroup.h"
30 #include "../lxc/lxc.h"
31 #include "../lxc/commands.h"
32
33 #define MYNAME "lxctest1"
34
35 #define TSTERR(fmt, ...) do { \
36 fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
37 } while (0)
38
39 /*
40 * test_running_container: test cgroup functions against a running container
41 *
42 * @group : name of the container group or NULL for default "lxc"
43 * @name : name of the container
44 */
45 static int test_running_container(const char *lxcpath,
46 const char *group, const char *name)
47 {
48 int ret = -1;
49 struct lxc_container *c = NULL;
50 char *cgrelpath;
51 char *cgabspath;
52 char relpath[PATH_MAX+1];
53 char abspath[PATH_MAX+1];
54 char value[NAME_MAX], value_save[NAME_MAX];
55
56 sprintf(relpath, "%s/%s", group ? group : "lxc", name);
57
58 if ((c = lxc_container_new(name, lxcpath)) == NULL) {
59 TSTERR("container %s couldn't instantiate", name);
60 goto err1;
61 }
62 if (!c->is_defined(c)) {
63 TSTERR("container %s does not exist", name);
64 goto err2;
65 }
66
67 cgrelpath = lxc_cmd_get_cgroup_path(c->name, c->config_path, "freezer");
68 if (!cgrelpath) {
69 TSTERR("lxc_cmd_get_cgroup_path returned NULL");
70 goto err2;
71 }
72 if (!strstr(cgrelpath, relpath)) {
73 TSTERR("lxc_cmd_get_cgroup_path %s not in %s", relpath, cgrelpath);
74 goto err3;
75 }
76
77 /* test get/set value using memory.soft_limit_in_bytes file */
78 ret = lxc_cgroup_get("memory.soft_limit_in_bytes", value, sizeof(value),
79 c->name, c->config_path);
80 if (ret < 0) {
81 TSTERR("lxc_cgroup_get failed");
82 goto err3;
83 }
84 strcpy(value_save, value);
85
86 ret = lxc_cgroup_set("memory.soft_limit_in_bytes", "512M", c->name, c->config_path);
87 if (ret < 0) {
88 TSTERR("lxc_cgroup_set failed %d %d", ret, errno);
89 goto err3;
90 }
91 ret = lxc_cgroup_get("memory.soft_limit_in_bytes", value, sizeof(value),
92 c->name, c->config_path);
93 if (ret < 0) {
94 TSTERR("lxc_cgroup_get failed");
95 goto err3;
96 }
97 if (strcmp(value, "536870912\n")) {
98 TSTERR("lxc_cgroup_set_bypath failed to set value >%s<", value);
99 goto err3;
100 }
101
102 /* restore original value */
103 ret = lxc_cgroup_set("memory.soft_limit_in_bytes", value_save,
104 c->name, c->config_path);
105 if (ret < 0) {
106 TSTERR("lxc_cgroup_set failed");
107 goto err3;
108 }
109
110 cgabspath = lxc_cgroup_path_get("freezer", c->name, c->config_path);
111 if (!cgabspath) {
112 TSTERR("lxc_cgroup_path_get returned NULL");
113 goto err3;
114 }
115 sprintf(abspath, "%s/%s/%s", "freezer", group ? group : "lxc", c->name);
116 if (!strstr(cgabspath, abspath)) {
117 TSTERR("lxc_cgroup_path_get %s not in %s", abspath, cgabspath);
118 goto err4;
119 }
120
121 free(cgabspath);
122 cgabspath = lxc_cgroup_path_get("freezer.state", c->name, c->config_path);
123 if (!cgabspath) {
124 TSTERR("lxc_cgroup_path_get returned NULL");
125 goto err3;
126 }
127 sprintf(abspath, "%s/%s/%s", "freezer", group ? group : "lxc", c->name);
128 if (!strstr(cgabspath, abspath)) {
129 TSTERR("lxc_cgroup_path_get %s not in %s", abspath, cgabspath);
130 goto err4;
131 }
132
133 ret = 0;
134 err4:
135 free(cgabspath);
136 err3:
137 free(cgrelpath);
138 err2:
139 lxc_container_put(c);
140 err1:
141 return ret;
142 }
143
144 static int test_container(const char *lxcpath,
145 const char *group, const char *name,
146 const char *template)
147 {
148 int ret;
149 struct lxc_container *c = NULL;
150
151 if (lxcpath) {
152 ret = mkdir(lxcpath, 0755);
153 if (ret < 0 && errno != EEXIST) {
154 TSTERR("failed to mkdir %s %s", lxcpath, strerror(errno));
155 goto out1;
156 }
157 }
158 ret = -1;
159
160 if ((c = lxc_container_new(name, lxcpath)) == NULL) {
161 TSTERR("instantiating container %s", name);
162 goto out1;
163 }
164 if (c->is_defined(c)) {
165 c->stop(c);
166 c->destroy(c);
167 c = lxc_container_new(name, lxcpath);
168 }
169 c->set_config_item(c, "lxc.network.type", "empty");
170 if (!c->createl(c, template, NULL, NULL, 0, NULL)) {
171 TSTERR("creating container %s", name);
172 goto out2;
173 }
174 c->load_config(c, NULL);
175 c->want_daemonize(c);
176 if (!c->startl(c, 0, NULL)) {
177 TSTERR("starting container %s", name);
178 goto out3;
179 }
180
181 ret = test_running_container(lxcpath, group, name);
182
183 c->stop(c);
184 out3:
185 c->destroy(c);
186 out2:
187 lxc_container_put(c);
188 out1:
189 return ret;
190 }
191
192 int main()
193 {
194 int ret = EXIT_FAILURE;
195
196 /* won't require privilege necessarily once users are classified by
197 * pam_cgroup */
198 if (geteuid() != 0) {
199 TSTERR("requires privilege");
200 exit(0);
201 }
202
203 #if TEST_ALREADY_RUNNING_CT
204
205 /*
206 * This is useful for running with valgrind to test for memory
207 * leaks. The container should already be running, we can't start
208 * the container ourselves because valgrind gets confused by lxc's
209 * internal calls to clone.
210 */
211 if (test_running_container(NULL, NULL, "bb01") < 0)
212 goto out;
213 printf("Running container cgroup tests...Passed\n");
214
215 #else
216
217 if (test_container(NULL, NULL, MYNAME, "busybox") < 0)
218 goto out;
219 printf("Container creation tests...Passed\n");
220
221 if (test_container("/var/lib/lxctest2", NULL, MYNAME, "busybox") < 0)
222 goto out;
223 printf("Container creation with LXCPATH tests...Passed\n");
224
225 #endif
226
227 ret = EXIT_SUCCESS;
228 out:
229 return ret;
230 }