]>
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 | */ | |
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 "shortlived" | |
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 | if (pid == 0) { | |
258611dd CB |
43 | execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL); |
44 | exit(EXIT_FAILURE); | |
3df9fa82 CB |
45 | } |
46 | again: | |
47 | ret = waitpid(pid, &status, 0); | |
48 | if (ret == -1) { | |
49 | if (errno == EINTR) | |
50 | goto again; | |
51 | perror("waitpid"); | |
52 | return -1; | |
53 | } | |
54 | if (ret != pid) | |
55 | goto again; | |
56 | if (!WIFEXITED(status)) { // did not exit normally | |
57 | fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__); | |
58 | return -1; | |
59 | } | |
60 | return WEXITSTATUS(status); | |
61 | } | |
62 | ||
63 | static int create_container(void) | |
64 | { | |
65 | int status, ret; | |
66 | pid_t pid = fork(); | |
67 | ||
68 | if (pid < 0) { | |
69 | perror("fork"); | |
70 | return -1; | |
71 | } | |
72 | if (pid == 0) { | |
258611dd CB |
73 | execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL); |
74 | exit(EXIT_FAILURE); | |
3df9fa82 CB |
75 | } |
76 | again: | |
77 | ret = waitpid(pid, &status, 0); | |
78 | if (ret == -1) { | |
79 | if (errno == EINTR) | |
80 | goto again; | |
81 | perror("waitpid"); | |
82 | return -1; | |
83 | } | |
84 | if (ret != pid) | |
85 | goto again; | |
86 | if (!WIFEXITED(status)) { // did not exit normally | |
87 | fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__); | |
88 | return -1; | |
89 | } | |
90 | return WEXITSTATUS(status); | |
91 | } | |
92 | ||
93 | int main(int argc, char *argv[]) | |
94 | { | |
95 | struct lxc_container *c; | |
96 | const char *s; | |
97 | bool b; | |
98 | int i; | |
99 | int ret = 0; | |
100 | ||
101 | ret = 1; | |
102 | /* test a real container */ | |
103 | c = lxc_container_new(MYNAME, NULL); | |
104 | if (!c) { | |
105 | fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, MYNAME); | |
106 | ret = 1; | |
107 | goto out; | |
108 | } | |
109 | ||
110 | if (c->is_defined(c)) { | |
111 | fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME); | |
112 | goto out; | |
113 | } | |
114 | ||
115 | ret = create_container(); | |
116 | if (ret) { | |
117 | fprintf(stderr, "%d: failed to create a container\n", __LINE__); | |
118 | goto out; | |
119 | } | |
120 | ||
121 | b = c->is_defined(c); | |
122 | if (!b) { | |
123 | fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME); | |
124 | goto out; | |
125 | } | |
126 | ||
127 | s = c->state(c); | |
128 | if (!s || strcmp(s, "STOPPED")) { | |
129 | fprintf(stderr, "%d: %s is in state %s, not in STOPPED.\n", __LINE__, c->name, s ? s : "undefined"); | |
130 | goto out; | |
131 | } | |
132 | ||
133 | b = c->load_config(c, NULL); | |
134 | if (!b) { | |
135 | fprintf(stderr, "%d: %s failed to read its config\n", __LINE__, c->name); | |
136 | goto out; | |
137 | } | |
138 | ||
139 | if (!c->set_config_item(c, "lxc.init.cmd", "echo hello")) { | |
140 | fprintf(stderr, "%d: failed setting lxc.init.cmd\n", __LINE__); | |
141 | goto out; | |
142 | } | |
143 | ||
144 | c->want_daemonize(c, true); | |
145 | ||
146 | /* Test whether we can start a really short-lived daemonized container. | |
147 | */ | |
148 | for (i = 0; i < 10; i++) { | |
149 | if (!c->startl(c, 0, NULL)) { | |
150 | fprintf(stderr, "%d: %s failed to start\n", __LINE__, c->name); | |
151 | goto out; | |
152 | } | |
153 | sleep(1); | |
154 | } | |
155 | ||
156 | if (!c->set_config_item(c, "lxc.init.cmd", "you-shall-fail")) { | |
157 | fprintf(stderr, "%d: failed setting lxc.init.cmd\n", __LINE__); | |
158 | goto out; | |
159 | } | |
160 | ||
161 | /* Test whether we catch the start failure of a really short-lived | |
162 | * daemonized container. | |
163 | */ | |
164 | for (i = 0; i < 10; i++) { | |
165 | if (c->startl(c, 0, NULL)) { | |
166 | fprintf(stderr, "%d: %s failed to start\n", __LINE__, c->name); | |
167 | goto out; | |
168 | } | |
169 | sleep(1); | |
170 | } | |
171 | ||
172 | c->stop(c); | |
173 | ||
174 | fprintf(stderr, "all lxc_container tests passed for %s\n", c->name); | |
175 | ret = 0; | |
176 | ||
177 | out: | |
178 | if (c) { | |
179 | c->stop(c); | |
180 | destroy_container(); | |
181 | } | |
182 | lxc_container_put(c); | |
183 | exit(ret); | |
184 | } |