]>
Commit | Line | Data |
---|---|---|
7acb5ce3 CB |
1 | /* liblxcapi |
2 | * | |
3 | * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2, as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along | |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 | */ | |
18 | ||
19 | #include <alloca.h> | |
20 | #include <errno.h> | |
21 | #include <pthread.h> | |
22 | #include <sched.h> | |
23 | #include <signal.h> | |
24 | #include <stdio.h> | |
25 | #include <string.h> | |
26 | #include <unistd.h> | |
27 | #include <sys/reboot.h> | |
28 | #include <sys/types.h> | |
29 | #include <sys/wait.h> | |
30 | ||
31 | #include "lxc/lxccontainer.h" | |
32 | #include "lxctest.h" | |
33 | ||
34 | struct thread_args { | |
35 | int thread_id; | |
36 | bool success; | |
37 | pid_t init_pid; | |
dab55f77 CB |
38 | char inherited_ipc_ns[4096]; |
39 | char inherited_net_ns[4096]; | |
7acb5ce3 CB |
40 | }; |
41 | ||
42 | void *ns_sharing_wrapper(void *data) | |
43 | { | |
44 | int init_pid; | |
45 | ssize_t ret; | |
46 | char name[100]; | |
47 | char owning_ns_init_pid[100]; | |
dab55f77 CB |
48 | char proc_ns_path[256]; |
49 | char ns_buf[256]; | |
7acb5ce3 CB |
50 | struct lxc_container *c; |
51 | struct thread_args *args = data; | |
52 | ||
53 | lxc_debug("Starting namespace sharing thread %d\n", args->thread_id); | |
54 | ||
55 | sprintf(name, "share-ns-%d", args->thread_id); | |
56 | c = lxc_container_new(name, NULL); | |
57 | if (!c) { | |
58 | lxc_error("Failed to create container \"%s\"\n", name); | |
f38cf5b8 | 59 | return NULL; |
7acb5ce3 CB |
60 | } |
61 | ||
62 | if (c->is_defined(c)) { | |
63 | lxc_error("Container \"%s\" is defined\n", name); | |
64 | goto out; | |
65 | } | |
66 | ||
67 | if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) { | |
68 | lxc_error("Failed to create busybox container \"%s\"\n", name); | |
69 | goto out; | |
70 | } | |
71 | ||
72 | if (!c->is_defined(c)) { | |
73 | lxc_error("Container \"%s\" is not defined\n", name); | |
74 | goto out; | |
75 | } | |
76 | ||
77 | if (!c->load_config(c, NULL)) { | |
78 | lxc_error("Failed to load config for container \"%s\"\n", name); | |
79 | goto out; | |
80 | } | |
81 | ||
82 | /* share ipc namespace by container name */ | |
b074bbf1 CB |
83 | if (!c->set_config_item(c, "lxc.namespace.share.ipc", "owning-ns")) { |
84 | lxc_error("Failed to set \"lxc.namespace.share.ipc=owning-ns\" for container \"%s\"\n", name); | |
7acb5ce3 CB |
85 | goto out; |
86 | } | |
87 | ||
88 | /* clear all network configuration */ | |
89 | if (!c->set_config_item(c, "lxc.net", "")) { | |
b074bbf1 | 90 | lxc_error("Failed to set \"lxc.namespace.share.ipc=owning-ns\" for container \"%s\"\n", name); |
7acb5ce3 CB |
91 | goto out; |
92 | } | |
93 | ||
94 | if (!c->set_config_item(c, "lxc.net.0.type", "empty")) { | |
95 | lxc_error("Failed to set \"lxc.net.0.type=empty\" for container \"%s\"\n", name); | |
96 | goto out; | |
97 | } | |
98 | ||
99 | sprintf(owning_ns_init_pid, "%d", args->init_pid); | |
100 | /* share net namespace by pid */ | |
b074bbf1 CB |
101 | if (!c->set_config_item(c, "lxc.namespace.share.net", owning_ns_init_pid)) { |
102 | lxc_error("Failed to set \"lxc.namespace.share.net=%s\" for container \"%s\"\n", owning_ns_init_pid, name); | |
7acb5ce3 CB |
103 | goto out; |
104 | } | |
105 | ||
106 | if (!c->want_daemonize(c, true)) { | |
107 | lxc_error("Failed to mark container \"%s\" daemonized\n", name); | |
108 | goto out; | |
109 | } | |
110 | ||
111 | if (!c->startl(c, 0, NULL)) { | |
112 | lxc_error("Failed to start container \"%s\" daemonized\n", name); | |
113 | goto out; | |
114 | } | |
115 | ||
116 | init_pid = c->init_pid(c); | |
117 | if (init_pid < 0) { | |
118 | lxc_error("Failed to retrieve init pid of container \"%s\"\n", name); | |
119 | goto out; | |
120 | } | |
121 | ||
122 | /* Check whether we correctly inherited the ipc namespace. */ | |
123 | ret = snprintf(proc_ns_path, sizeof(proc_ns_path), "/proc/%d/ns/ipc", init_pid); | |
124 | if (ret < 0 || (size_t)ret >= sizeof(proc_ns_path)) { | |
125 | lxc_error("Failed to create string for container \"%s\"\n", name); | |
126 | goto out; | |
127 | } | |
128 | ||
129 | ret = readlink(proc_ns_path, ns_buf, sizeof(ns_buf)); | |
130 | if (ret < 0 || (size_t)ret >= sizeof(ns_buf)) { | |
131 | lxc_error("Failed to retrieve ipc namespace for container \"%s\"\n", name); | |
132 | goto out; | |
133 | } | |
134 | ns_buf[ret] = '\0'; | |
135 | ||
136 | if (strcmp(args->inherited_ipc_ns, ns_buf) != 0) { | |
137 | lxc_error("Failed to inherit ipc namespace from container \"owning-ns\": %s != %s\n", args->inherited_ipc_ns, ns_buf); | |
138 | goto out; | |
139 | } | |
140 | lxc_debug("Inherited ipc namespace from container \"owning-ns\": %s == %s\n", args->inherited_ipc_ns, ns_buf); | |
141 | ||
142 | /* Check whether we correctly inherited the net namespace. */ | |
143 | ret = snprintf(proc_ns_path, sizeof(proc_ns_path), "/proc/%d/ns/net", init_pid); | |
144 | if (ret < 0 || (size_t)ret >= sizeof(proc_ns_path)) { | |
145 | lxc_error("Failed to create string for container \"%s\"\n", name); | |
146 | goto out; | |
147 | } | |
148 | ||
149 | ret = readlink(proc_ns_path, ns_buf, sizeof(ns_buf)); | |
150 | if (ret < 0 || (size_t)ret >= sizeof(ns_buf)) { | |
151 | lxc_error("Failed to retrieve ipc namespace for container \"%s\"\n", name); | |
152 | goto out; | |
153 | } | |
154 | ns_buf[ret] = '\0'; | |
155 | ||
156 | if (strcmp(args->inherited_net_ns, ns_buf) != 0) { | |
157 | lxc_error("Failed to inherit net namespace from container \"owning-ns\": %s != %s\n", args->inherited_net_ns, ns_buf); | |
158 | goto out; | |
159 | } | |
160 | lxc_debug("Inherited net namespace from container \"owning-ns\": %s == %s\n", args->inherited_net_ns, ns_buf); | |
161 | ||
162 | args->success = true; | |
163 | ||
164 | out: | |
dab55f77 | 165 | if (c->is_running(c) && !c->stop(c)) |
7acb5ce3 | 166 | lxc_error("Failed to stop container \"%s\"\n", name); |
7acb5ce3 | 167 | |
dab55f77 | 168 | if (!c->destroy(c)) |
7acb5ce3 | 169 | lxc_error("Failed to destroy container \"%s\"\n", name); |
7acb5ce3 CB |
170 | |
171 | pthread_exit(NULL); | |
172 | return NULL; | |
173 | } | |
174 | ||
175 | int main(int argc, char *argv[]) | |
176 | { | |
dab55f77 CB |
177 | struct thread_args *args = NULL; |
178 | size_t nthreads = 10; | |
7acb5ce3 CB |
179 | int i, init_pid, j; |
180 | char proc_ns_path[4096]; | |
181 | char ipc_ns_buf[4096]; | |
182 | char net_ns_buf[4096]; | |
183 | pthread_attr_t attr; | |
184 | pthread_t threads[10]; | |
7acb5ce3 CB |
185 | struct lxc_container *c; |
186 | int ret = EXIT_FAILURE; | |
187 | ||
dab55f77 CB |
188 | pthread_attr_init(&attr); |
189 | ||
7acb5ce3 CB |
190 | c = lxc_container_new("owning-ns", NULL); |
191 | if (!c) { | |
192 | lxc_error("%s", "Failed to create container \"owning-ns\""); | |
193 | exit(ret); | |
194 | } | |
195 | ||
196 | if (c->is_defined(c)) { | |
197 | lxc_error("%s\n", "Container \"owning-ns\" is defined"); | |
198 | goto on_error_put; | |
199 | } | |
200 | ||
201 | if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) { | |
202 | lxc_error("%s\n", "Failed to create busybox container \"owning-ns\""); | |
203 | goto on_error_put; | |
204 | } | |
205 | ||
206 | if (!c->is_defined(c)) { | |
207 | lxc_error("%s\n", "Container \"owning-ns\" is not defined"); | |
208 | goto on_error_put; | |
209 | } | |
210 | ||
211 | c->clear_config(c); | |
212 | ||
213 | if (!c->load_config(c, NULL)) { | |
214 | lxc_error("%s\n", "Failed to load config for container \"owning-ns\""); | |
215 | goto on_error_stop; | |
216 | } | |
217 | ||
218 | if (!c->want_daemonize(c, true)) { | |
219 | lxc_error("%s\n", "Failed to mark container \"owning-ns\" daemonized"); | |
220 | goto on_error_stop; | |
221 | } | |
222 | ||
223 | if (!c->startl(c, 0, NULL)) { | |
224 | lxc_error("%s\n", "Failed to start container \"owning-ns\" daemonized"); | |
225 | goto on_error_stop; | |
226 | } | |
227 | ||
228 | init_pid = c->init_pid(c); | |
229 | if (init_pid < 0) { | |
230 | lxc_error("%s\n", "Failed to retrieve init pid of container \"owning-ns\""); | |
231 | goto on_error_stop; | |
232 | } | |
233 | ||
234 | /* record our ipc namespace */ | |
235 | ret = snprintf(proc_ns_path, sizeof(proc_ns_path), "/proc/%d/ns/ipc", init_pid); | |
236 | if (ret < 0 || (size_t)ret >= sizeof(proc_ns_path)) { | |
237 | lxc_error("%s\n", "Failed to create string for container \"owning-ns\""); | |
238 | goto on_error_stop; | |
239 | } | |
240 | ||
241 | ret = readlink(proc_ns_path, ipc_ns_buf, sizeof(ipc_ns_buf)); | |
242 | if (ret < 0 || (size_t)ret >= sizeof(ipc_ns_buf)) { | |
243 | lxc_error("%s\n", "Failed to retrieve ipc namespace for container \"owning-ns\""); | |
244 | goto on_error_stop; | |
245 | ||
246 | } | |
247 | ipc_ns_buf[ret] = '\0'; | |
248 | ||
249 | /* record our net namespace */ | |
250 | ret = snprintf(proc_ns_path, sizeof(proc_ns_path), "/proc/%d/ns/net", init_pid); | |
251 | if (ret < 0 || (size_t)ret >= sizeof(proc_ns_path)) { | |
252 | lxc_error("%s\n", "Failed to create string for container \"owning-ns\""); | |
253 | goto on_error_stop; | |
254 | } | |
255 | ||
256 | ret = readlink(proc_ns_path, net_ns_buf, sizeof(net_ns_buf)); | |
257 | if (ret < 0 || (size_t)ret >= sizeof(net_ns_buf)) { | |
258 | lxc_error("%s\n", "Failed to retrieve ipc namespace for container \"owning-ns\""); | |
259 | goto on_error_stop; | |
260 | } | |
261 | net_ns_buf[ret] = '\0'; | |
262 | ||
263 | sleep(5); | |
264 | ||
dab55f77 CB |
265 | args = malloc(sizeof(struct thread_args) * nthreads); |
266 | if (!args) { | |
267 | lxc_error("%s\n", "Failed to allocate memory"); | |
268 | goto on_error_stop; | |
269 | } | |
7acb5ce3 CB |
270 | |
271 | for (j = 0; j < 10; j++) { | |
272 | lxc_debug("Starting namespace sharing test iteration %d\n", j); | |
273 | ||
dab55f77 | 274 | for (i = 0; i < nthreads; i++) { |
7acb5ce3 CB |
275 | args[i].thread_id = i; |
276 | args[i].success = false; | |
277 | args[i].init_pid = init_pid; | |
dab55f77 CB |
278 | memcpy(args[i].inherited_ipc_ns, ipc_ns_buf, sizeof(args[i].inherited_ipc_ns)); |
279 | memcpy(args[i].inherited_net_ns, net_ns_buf, sizeof(args[i].inherited_net_ns)); | |
7acb5ce3 | 280 | |
dab55f77 | 281 | ret = pthread_create(&threads[i], &attr, ns_sharing_wrapper, (void *)&args[i]); |
7acb5ce3 CB |
282 | if (ret != 0) |
283 | goto on_error_stop; | |
284 | } | |
285 | ||
dab55f77 | 286 | for (i = 0; i < nthreads; i++) { |
7acb5ce3 CB |
287 | ret = pthread_join(threads[i], NULL); |
288 | if (ret != 0) | |
289 | goto on_error_stop; | |
290 | ||
291 | if (!args[i].success) { | |
292 | lxc_error("ns sharing thread %d failed\n", args[i].thread_id); | |
293 | goto on_error_stop; | |
294 | } | |
295 | } | |
296 | } | |
297 | ||
298 | ret = EXIT_SUCCESS; | |
299 | ||
300 | on_error_stop: | |
dab55f77 CB |
301 | free(args); |
302 | pthread_attr_destroy(&attr); | |
303 | ||
7acb5ce3 CB |
304 | if (c->is_running(c) && !c->stop(c)) |
305 | lxc_error("%s\n", "Failed to stop container \"owning-ns\""); | |
306 | ||
307 | if (!c->destroy(c)) | |
308 | lxc_error("%s\n", "Failed to destroy container \"owning-ns\""); | |
309 | ||
310 | on_error_put: | |
311 | lxc_container_put(c); | |
312 | if (ret == EXIT_SUCCESS) | |
313 | lxc_debug("%s\n", "All state namespace sharing tests passed"); | |
dab55f77 | 314 | |
7acb5ce3 CB |
315 | exit(ret); |
316 | } |