]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/startone.c
spelling: could
[mirror_lxc.git] / src / tests / startone.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 <unistd.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <fcntl.h>
30
31 #define MYNAME "lxctest1"
32
33 static int destroy_container(void)
34 {
35 int status, ret;
36 pid_t pid = fork();
37
38 if (pid < 0) {
39 perror("fork");
40 return -1;
41 }
42
43 if (pid == 0) {
44 execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL);
45 exit(EXIT_FAILURE);
46 }
47
48 again:
49 ret = waitpid(pid, &status, 0);
50 if (ret == -1) {
51 if (errno == EINTR)
52 goto again;
53
54 perror("waitpid");
55 return -1;
56 }
57
58 if (ret != pid)
59 goto again;
60
61 if (!WIFEXITED(status)) { // did not exit normally
62 fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
63 return -1;
64 }
65
66 return WEXITSTATUS(status);
67 }
68
69 static int create_container(void)
70 {
71 int status, ret;
72 pid_t pid = fork();
73
74 if (pid < 0) {
75 perror("fork");
76 return -1;
77 }
78
79 if (pid == 0) {
80 execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL);
81 exit(EXIT_FAILURE);
82 }
83
84 again:
85 ret = waitpid(pid, &status, 0);
86 if (ret == -1) {
87 if (errno == EINTR)
88 goto again;
89
90 perror("waitpid");
91 return -1;
92 }
93
94 if (ret != pid)
95 goto again;
96
97 if (!WIFEXITED(status)) { // did not exit normally
98 fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
99 return -1;
100 }
101
102 return WEXITSTATUS(status);
103 }
104
105 int main(int argc, char *argv[])
106 {
107 struct lxc_container *c;
108 int ret = 0;
109 const char *s;
110 bool b;
111 char buf[201];
112 int len;
113
114 ret = 1;
115
116 /* test a real container */
117 c = lxc_container_new(MYNAME, NULL);
118 if (!c) {
119 fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, MYNAME);
120 ret = 1;
121 goto out;
122 }
123
124 if (c->is_defined(c)) {
125 fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
126 goto out;
127 }
128
129 ret = create_container();
130 if (ret) {
131 fprintf(stderr, "%d: failed to create a container\n", __LINE__);
132 goto out;
133 }
134
135 b = c->is_defined(c);
136 if (!b) {
137 fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME);
138 goto out;
139 }
140
141 len = c->get_cgroup_item(c, "cpuset.cpus", buf, 200);
142 if (len >= 0) {
143 fprintf(stderr, "%d: %s not running but had cgroup settings\n", __LINE__, MYNAME);
144 goto out;
145 }
146
147 sprintf(buf, "0");
148 b = c->set_cgroup_item(c, "cpuset.cpus", buf);
149 if (b) {
150 fprintf(stderr, "%d: %s not running but could set cgroup settings\n", __LINE__, MYNAME);
151 goto out;
152 }
153
154 s = c->state(c);
155 if (!s || strcmp(s, "STOPPED")) {
156 fprintf(stderr, "%d: %s is in state %s, not in STOPPED.\n", __LINE__, c->name, s ? s : "undefined");
157 goto out;
158 }
159
160 b = c->load_config(c, NULL);
161 if (!b) {
162 fprintf(stderr, "%d: %s failed to read its config\n", __LINE__, c->name);
163 goto out;
164 }
165
166 if (!c->set_config_item(c, "lxc.uts.name", "bobo")) {
167 fprintf(stderr, "%d: failed setting lxc.uts.name\n", __LINE__);
168 goto out;
169 }
170
171 if (!lxc_container_get(c)) {
172 fprintf(stderr, "%d: failed to get extra ref to container\n", __LINE__);
173 exit(1);
174 }
175
176 c->want_daemonize(c, true);
177 if (!c->startl(c, 0, NULL)) {
178 fprintf(stderr, "%d: %s failed to start\n", __LINE__, c->name);
179 exit(1);
180 }
181
182 sleep(3);
183
184 s = c->state(c);
185 if (!s || strcmp(s, "RUNNING")) {
186 fprintf(stderr, "%d: %s is in state %s, not in RUNNING.\n", __LINE__, c->name, s ? s : "undefined");
187 goto out;
188 }
189
190 len = c->get_cgroup_item(c, "cpuset.cpus", buf, 0);
191 if (len <= 0) {
192 fprintf(stderr, "%d: not able to get length of cpuset.cpus (ret %d)\n", __LINE__, len);
193 goto out;
194 }
195
196 len = c->get_cgroup_item(c, "cpuset.cpus", buf, 200);
197 if (len <= 0 || strncmp(buf, "0", 1)) {
198 fprintf(stderr, "%d: not able to get cpuset.cpus (len %d buf %s)\n", __LINE__, len, buf);
199 goto out;
200 }
201
202 sprintf(buf, "FROZEN");
203 b = c->set_cgroup_item(c, "freezer.state", buf);
204 if (!b) {
205 fprintf(stderr, "%d: not able to set freezer.state.\n", __LINE__);
206 goto out;
207 }
208
209 sprintf(buf, "XXX");
210 len = c->get_cgroup_item(c, "freezer.state", buf, 200);
211 if (len <= 0 || (strcmp(buf, "FREEZING\n") && strcmp(buf, "FROZEN\n"))) {
212 fprintf(stderr, "%d: not able to get freezer.state (len %d buf %s)\n", __LINE__, len, buf);
213 goto out;
214 }
215
216 c->set_cgroup_item(c, "freezer.state", "THAWED");
217
218 c->stop(c);
219
220 /* feh - multilib has moved the lxc-init crap */
221 #if 0
222 goto ok;
223
224 ret = system("mkdir -p " LXCPATH "/lxctest1/rootfs//usr/local/libexec/lxc");
225 if (!ret)
226 ret = system("mkdir -p " LXCPATH "/lxctest1/rootfs/usr/lib/lxc/");
227 if (!ret)
228 ret = system("cp src/lxc/lxc-init " LXCPATH "/lxctest1/rootfs//usr/local/libexec/lxc");
229 if (!ret)
230 ret = system("cp src/lxc/liblxc.so " LXCPATH "/lxctest1/rootfs/usr/lib/lxc");
231 if (!ret)
232 ret = system("cp src/lxc/liblxc.so " LXCPATH "/lxctest1/rootfs/usr/lib/lxc/liblxc.so.0");
233 if (!ret)
234 ret = system("cp src/lxc/liblxc.so " LXCPATH "/lxctest1/rootfs/usr/lib");
235 if (!ret)
236 ret = system("mkdir -p " LXCPATH "/lxctest1/rootfs/dev/shm");
237 if (!ret)
238 ret = system("chroot " LXCPATH "/lxctest1/rootfs apt-get install --no-install-recommends lxc");
239 if (ret) {
240 fprintf(stderr, "%d: failed to installing lxc-init in container\n", __LINE__);
241 goto out;
242 }
243 // next write out the config file; does it match?
244 if (!c->startl(c, 1, "/bin/hostname", NULL)) {
245 fprintf(stderr, "%d: failed to lxc-execute /bin/hostname\n", __LINE__);
246 goto out;
247 }
248 // auto-check result? ('bobo' is printed on stdout)
249
250 ok:
251 #endif
252 fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
253 ret = 0;
254
255 out:
256 if (c) {
257 c->stop(c);
258 destroy_container();
259 }
260 lxc_container_put(c);
261 exit(ret);
262 }