]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/startone.c
spelling: timeout
[mirror_lxc.git] / src / tests / startone.c
CommitLineData
72d0e1cb
SG
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 */
948955a2 19#include <lxc/lxccontainer.h>
72d0e1cb
SG
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>
95ee490b 27#include <string.h>
72d0e1cb 28#include <errno.h>
612c49e1 29#include <fcntl.h>
72d0e1cb
SG
30
31#define MYNAME "lxctest1"
32
787c3ebe 33static int destroy_container(void)
72d0e1cb
SG
34{
35 int status, ret;
36 pid_t pid = fork();
37
38 if (pid < 0) {
39 perror("fork");
40 return -1;
41 }
dcfbd1c0 42
72d0e1cb 43 if (pid == 0) {
258611dd
CB
44 execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL);
45 exit(EXIT_FAILURE);
72d0e1cb 46 }
dcfbd1c0 47
72d0e1cb
SG
48again:
49 ret = waitpid(pid, &status, 0);
50 if (ret == -1) {
21e624d9 51 if (errno == EINTR)
72d0e1cb 52 goto again;
dcfbd1c0 53
72d0e1cb
SG
54 perror("waitpid");
55 return -1;
56 }
dcfbd1c0 57
72d0e1cb
SG
58 if (ret != pid)
59 goto again;
dcfbd1c0 60
72d0e1cb
SG
61 if (!WIFEXITED(status)) { // did not exit normally
62 fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
63 return -1;
64 }
dcfbd1c0 65
72d0e1cb
SG
66 return WEXITSTATUS(status);
67}
68
787c3ebe 69static int create_container(void)
72d0e1cb
SG
70{
71 int status, ret;
72 pid_t pid = fork();
73
74 if (pid < 0) {
75 perror("fork");
76 return -1;
77 }
dcfbd1c0 78
72d0e1cb 79 if (pid == 0) {
258611dd
CB
80 execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL);
81 exit(EXIT_FAILURE);
72d0e1cb 82 }
dcfbd1c0 83
72d0e1cb
SG
84again:
85 ret = waitpid(pid, &status, 0);
86 if (ret == -1) {
21e624d9 87 if (errno == EINTR)
72d0e1cb 88 goto again;
dcfbd1c0 89
72d0e1cb
SG
90 perror("waitpid");
91 return -1;
92 }
dcfbd1c0 93
72d0e1cb
SG
94 if (ret != pid)
95 goto again;
dcfbd1c0 96
72d0e1cb
SG
97 if (!WIFEXITED(status)) { // did not exit normally
98 fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
99 return -1;
100 }
dcfbd1c0 101
72d0e1cb
SG
102 return WEXITSTATUS(status);
103}
104
105int main(int argc, char *argv[])
106{
107 struct lxc_container *c;
108 int ret = 0;
109 const char *s;
110 bool b;
794dd120
SH
111 char buf[201];
112 int len;
72d0e1cb
SG
113
114 ret = 1;
dcfbd1c0 115
72d0e1cb 116 /* test a real container */
afeecbba 117 c = lxc_container_new(MYNAME, NULL);
72d0e1cb
SG
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
787c3ebe 129 ret = create_container();
72d0e1cb 130 if (ret) {
787c3ebe 131 fprintf(stderr, "%d: failed to create a container\n", __LINE__);
72d0e1cb
SG
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
794dd120
SH
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) {
2ed2e5ff 150 fprintf(stderr, "%d: %s not running but could set cgroup settings\n", __LINE__, MYNAME);
794dd120
SH
151 goto out;
152 }
153
72d0e1cb
SG
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
b67771bc 166 if (!c->set_config_item(c, "lxc.uts.name", "bobo")) {
167 fprintf(stderr, "%d: failed setting lxc.uts.name\n", __LINE__);
72d0e1cb
SG
168 goto out;
169 }
170
72d0e1cb
SG
171 if (!lxc_container_get(c)) {
172 fprintf(stderr, "%d: failed to get extra ref to container\n", __LINE__);
173 exit(1);
174 }
4fd0838c
SG
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);
72d0e1cb
SG
180 }
181
182 sleep(3);
dcfbd1c0 183
72d0e1cb
SG
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
794dd120
SH
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);
a38c2e6f 197 if (len <= 0 || strncmp(buf, "0", 1)) {
794dd120
SH
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
dcfbd1c0 209 sprintf(buf, "XXX");
794dd120
SH
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
72d0e1cb
SG
218 c->stop(c);
219
794dd120 220 /* feh - multilib has moved the lxc-init crap */
c928f41f 221#if 0
794dd120
SH
222 goto ok;
223
e29bf450 224 ret = system("mkdir -p " LXCPATH "/lxctest1/rootfs//usr/local/libexec/lxc");
9c9b9845 225 if (!ret)
e29bf450 226 ret = system("mkdir -p " LXCPATH "/lxctest1/rootfs/usr/lib/lxc/");
9c9b9845 227 if (!ret)
e29bf450 228 ret = system("cp src/lxc/lxc-init " LXCPATH "/lxctest1/rootfs//usr/local/libexec/lxc");
9c9b9845 229 if (!ret)
e29bf450 230 ret = system("cp src/lxc/liblxc.so " LXCPATH "/lxctest1/rootfs/usr/lib/lxc");
9c9b9845 231 if (!ret)
e29bf450 232 ret = system("cp src/lxc/liblxc.so " LXCPATH "/lxctest1/rootfs/usr/lib/lxc/liblxc.so.0");
9c9b9845 233 if (!ret)
e29bf450 234 ret = system("cp src/lxc/liblxc.so " LXCPATH "/lxctest1/rootfs/usr/lib");
9c9b9845 235 if (!ret)
e29bf450 236 ret = system("mkdir -p " LXCPATH "/lxctest1/rootfs/dev/shm");
9c9b9845 237 if (!ret)
e29bf450 238 ret = system("chroot " LXCPATH "/lxctest1/rootfs apt-get install --no-install-recommends lxc");
9c9b9845
SH
239 if (ret) {
240 fprintf(stderr, "%d: failed to installing lxc-init in container\n", __LINE__);
241 goto out;
242 }
72d0e1cb
SG
243 // next write out the config file; does it match?
244 if (!c->startl(c, 1, "/bin/hostname", NULL)) {
9c9b9845 245 fprintf(stderr, "%d: failed to lxc-execute /bin/hostname\n", __LINE__);
72d0e1cb
SG
246 goto out;
247 }
248 // auto-check result? ('bobo' is printed on stdout)
249
794dd120 250ok:
c928f41f 251#endif
72d0e1cb
SG
252 fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
253 ret = 0;
254
255out:
256 if (c) {
257 c->stop(c);
787c3ebe 258 destroy_container();
72d0e1cb
SG
259 }
260 lxc_container_put(c);
261 exit(ret);
262}