]>
Commit | Line | Data |
---|---|---|
3df9fa82 CB |
1 | /* liblxcapi |
2 | * | |
3 | * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>. | |
4 | * Copyright © 2017 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 | */ | |
e49c56d6 CB |
19 | |
20 | #include "config.h" | |
21 | ||
3df9fa82 CB |
22 | #include <lxc/lxccontainer.h> |
23 | ||
24 | #include <unistd.h> | |
25 | #include <signal.h> | |
26 | #include <stdio.h> | |
27 | #include <sys/types.h> | |
28 | #include <sys/wait.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | #include <errno.h> | |
32 | #include <fcntl.h> | |
33 | ||
77f76f31 CB |
34 | #include "lxctest.h" |
35 | #include "utils.h" | |
36 | ||
34498dea | 37 | #if !HAVE_STRLCPY |
58db1a61 | 38 | #include "strlcpy.h" |
18cd4b54 DJ |
39 | #endif |
40 | ||
3df9fa82 CB |
41 | #define MYNAME "shortlived" |
42 | ||
43 | static int destroy_container(void) | |
44 | { | |
45 | int status, ret; | |
46 | pid_t pid = fork(); | |
47 | ||
48 | if (pid < 0) { | |
49 | perror("fork"); | |
50 | return -1; | |
51 | } | |
0ce5c91e | 52 | |
3df9fa82 | 53 | if (pid == 0) { |
258611dd CB |
54 | execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL); |
55 | exit(EXIT_FAILURE); | |
3df9fa82 | 56 | } |
0ce5c91e | 57 | |
3df9fa82 CB |
58 | again: |
59 | ret = waitpid(pid, &status, 0); | |
60 | if (ret == -1) { | |
61 | if (errno == EINTR) | |
62 | goto again; | |
63 | perror("waitpid"); | |
64 | return -1; | |
65 | } | |
0ce5c91e | 66 | |
3df9fa82 CB |
67 | if (ret != pid) |
68 | goto again; | |
0ce5c91e | 69 | |
3df9fa82 CB |
70 | if (!WIFEXITED(status)) { // did not exit normally |
71 | fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__); | |
72 | return -1; | |
73 | } | |
0ce5c91e | 74 | |
3df9fa82 CB |
75 | return WEXITSTATUS(status); |
76 | } | |
77 | ||
78 | static int create_container(void) | |
79 | { | |
80 | int status, ret; | |
81 | pid_t pid = fork(); | |
82 | ||
83 | if (pid < 0) { | |
84 | perror("fork"); | |
85 | return -1; | |
86 | } | |
0ce5c91e | 87 | |
3df9fa82 | 88 | if (pid == 0) { |
258611dd CB |
89 | execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL); |
90 | exit(EXIT_FAILURE); | |
3df9fa82 | 91 | } |
0ce5c91e | 92 | |
3df9fa82 CB |
93 | again: |
94 | ret = waitpid(pid, &status, 0); | |
95 | if (ret == -1) { | |
96 | if (errno == EINTR) | |
97 | goto again; | |
0ce5c91e | 98 | |
3df9fa82 CB |
99 | perror("waitpid"); |
100 | return -1; | |
101 | } | |
0ce5c91e | 102 | |
3df9fa82 CB |
103 | if (ret != pid) |
104 | goto again; | |
0ce5c91e | 105 | |
3df9fa82 CB |
106 | if (!WIFEXITED(status)) { // did not exit normally |
107 | fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__); | |
108 | return -1; | |
109 | } | |
0ce5c91e | 110 | |
3df9fa82 CB |
111 | return WEXITSTATUS(status); |
112 | } | |
113 | ||
114 | int main(int argc, char *argv[]) | |
115 | { | |
5af9369b | 116 | int fd, i; |
3df9fa82 CB |
117 | const char *s; |
118 | bool b; | |
77f76f31 CB |
119 | struct lxc_container *c; |
120 | struct lxc_log log; | |
121 | char template[sizeof(P_tmpdir"/shortlived_XXXXXX")]; | |
5af9369b | 122 | int ret = EXIT_FAILURE; |
3df9fa82 | 123 | |
18cd4b54 DJ |
124 | (void)strlcpy(template, P_tmpdir"/shortlived_XXXXXX", sizeof(template)); |
125 | ||
77f76f31 CB |
126 | i = lxc_make_tmpfile(template, false); |
127 | if (i < 0) { | |
128 | lxc_error("Failed to create temporary log file for container %s\n", MYNAME); | |
129 | exit(EXIT_FAILURE); | |
130 | } else { | |
131 | lxc_debug("Using \"%s\" as temporary log file for container %s\n", template, MYNAME); | |
132 | close(i); | |
133 | } | |
134 | ||
135 | log.name = MYNAME; | |
136 | log.file = template; | |
137 | log.level = "TRACE"; | |
138 | log.prefix = "shortlived"; | |
139 | log.quiet = false; | |
140 | log.lxcpath = NULL; | |
0ce5c91e | 141 | |
77f76f31 CB |
142 | if (lxc_log_init(&log)) |
143 | exit(EXIT_FAILURE); | |
144 | ||
3df9fa82 CB |
145 | /* test a real container */ |
146 | c = lxc_container_new(MYNAME, NULL); | |
147 | if (!c) { | |
148 | fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, MYNAME); | |
3df9fa82 CB |
149 | goto out; |
150 | } | |
151 | ||
152 | if (c->is_defined(c)) { | |
153 | fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME); | |
154 | goto out; | |
155 | } | |
156 | ||
5af9369b | 157 | if (create_container() < 0) { |
3df9fa82 CB |
158 | fprintf(stderr, "%d: failed to create a container\n", __LINE__); |
159 | goto out; | |
160 | } | |
161 | ||
162 | b = c->is_defined(c); | |
163 | if (!b) { | |
164 | fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME); | |
165 | goto out; | |
166 | } | |
167 | ||
168 | s = c->state(c); | |
169 | if (!s || strcmp(s, "STOPPED")) { | |
170 | fprintf(stderr, "%d: %s is in state %s, not in STOPPED.\n", __LINE__, c->name, s ? s : "undefined"); | |
171 | goto out; | |
172 | } | |
173 | ||
174 | b = c->load_config(c, NULL); | |
175 | if (!b) { | |
176 | fprintf(stderr, "%d: %s failed to read its config\n", __LINE__, c->name); | |
177 | goto out; | |
178 | } | |
179 | ||
180 | if (!c->set_config_item(c, "lxc.init.cmd", "echo hello")) { | |
181 | fprintf(stderr, "%d: failed setting lxc.init.cmd\n", __LINE__); | |
182 | goto out; | |
183 | } | |
184 | ||
77f76f31 | 185 | if (!c->set_config_item(c, "lxc.execute.cmd", "echo hello")) { |
5af9369b | 186 | fprintf(stderr, "%d: failed setting lxc.execute.cmd\n", __LINE__); |
77f76f31 CB |
187 | goto out; |
188 | } | |
189 | ||
3df9fa82 CB |
190 | c->want_daemonize(c, true); |
191 | ||
77f76f31 | 192 | /* Test whether we can start a really short-lived daemonized container. */ |
3df9fa82 CB |
193 | for (i = 0; i < 10; i++) { |
194 | if (!c->startl(c, 0, NULL)) { | |
77f76f31 CB |
195 | fprintf(stderr, "%d: %s failed to start on %dth iteration\n", __LINE__, c->name, i); |
196 | goto out; | |
197 | } | |
198 | ||
77f76f31 CB |
199 | if (!c->wait(c, "STOPPED", 30)) { |
200 | fprintf(stderr, "%d: %s failed to wait on %dth iteration\n", __LINE__, c->name, i); | |
201 | goto out; | |
202 | } | |
203 | } | |
204 | ||
205 | /* Test whether we can start a really short-lived daemonized container with lxc-init. */ | |
206 | for (i = 0; i < 10; i++) { | |
207 | if (!c->startl(c, 1, NULL)) { | |
208 | fprintf(stderr, "%d: %s failed to start on %dth iteration\n", __LINE__, c->name, i); | |
209 | goto out; | |
210 | } | |
211 | ||
77f76f31 CB |
212 | if (!c->wait(c, "STOPPED", 30)) { |
213 | fprintf(stderr, "%d: %s failed to wait on %dth iteration\n", __LINE__, c->name, i); | |
3df9fa82 CB |
214 | goto out; |
215 | } | |
3df9fa82 CB |
216 | } |
217 | ||
218 | if (!c->set_config_item(c, "lxc.init.cmd", "you-shall-fail")) { | |
219 | fprintf(stderr, "%d: failed setting lxc.init.cmd\n", __LINE__); | |
220 | goto out; | |
221 | } | |
222 | ||
77f76f31 CB |
223 | if (!c->set_config_item(c, "lxc.execute.cmd", "you-shall-fail")) { |
224 | fprintf(stderr, "%d: failed setting lxc.init.cmd\n", __LINE__); | |
225 | goto out; | |
226 | } | |
227 | ||
228 | /* Test whether we can start a really short-lived daemonized container. */ | |
3df9fa82 CB |
229 | for (i = 0; i < 10; i++) { |
230 | if (c->startl(c, 0, NULL)) { | |
77f76f31 CB |
231 | fprintf(stderr, "%d: %s failed to start on %dth iteration\n", __LINE__, c->name, i); |
232 | goto out; | |
233 | } | |
234 | ||
77f76f31 CB |
235 | if (!c->wait(c, "STOPPED", 30)) { |
236 | fprintf(stderr, "%d: %s failed to wait on %dth iteration\n", __LINE__, c->name, i); | |
237 | goto out; | |
238 | } | |
239 | } | |
240 | ||
241 | /* Test whether we can start a really short-lived daemonized container with lxc-init. */ | |
242 | for (i = 0; i < 10; i++) { | |
243 | /* An container started with lxc-init will always start | |
59d8d30d | 244 | * successfully unless lxc-init has a bug. |
77f76f31 CB |
245 | */ |
246 | if (!c->startl(c, 1, NULL)) { | |
247 | fprintf(stderr, "%d: %s failed to start on %dth iteration\n", __LINE__, c->name, i); | |
248 | goto out; | |
249 | } | |
250 | ||
77f76f31 CB |
251 | if (!c->wait(c, "STOPPED", 30)) { |
252 | fprintf(stderr, "%d: %s failed to wait on %dth iteration\n", __LINE__, c->name, i); | |
3df9fa82 CB |
253 | goto out; |
254 | } | |
3df9fa82 CB |
255 | } |
256 | ||
257 | c->stop(c); | |
258 | ||
259 | fprintf(stderr, "all lxc_container tests passed for %s\n", c->name); | |
260 | ret = 0; | |
261 | ||
262 | out: | |
263 | if (c) { | |
264 | c->stop(c); | |
265 | destroy_container(); | |
266 | } | |
267 | lxc_container_put(c); | |
5af9369b CB |
268 | |
269 | if (ret != 0) { | |
270 | fd = open(template, O_RDONLY); | |
271 | if (fd >= 0) { | |
272 | char buf[4096]; | |
273 | ssize_t buflen; | |
0ce5c91e | 274 | |
5af9369b CB |
275 | while ((buflen = read(fd, buf, 1024)) > 0) { |
276 | buflen = write(STDERR_FILENO, buf, buflen); | |
277 | if (buflen <= 0) | |
278 | break; | |
279 | } | |
0ce5c91e | 280 | |
5af9369b CB |
281 | close(fd); |
282 | } | |
283 | } | |
284 | ||
77f76f31 | 285 | unlink(template); |
3df9fa82 CB |
286 | exit(ret); |
287 | } |