]>
git.proxmox.com Git - mirror_lxc.git/blob - src/tests/share_ns.c
3 * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
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.
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.
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.
27 #include <sys/reboot.h>
28 #include <sys/types.h>
31 #include "lxc/lxccontainer.h"
38 char *inherited_ipc_ns
;
39 char *inherited_net_ns
;
42 void *ns_sharing_wrapper(void *data
)
47 char owning_ns_init_pid
[100];
48 char proc_ns_path
[4096];
50 struct lxc_container
*c
;
51 struct thread_args
*args
= data
;
53 lxc_debug("Starting namespace sharing thread %d\n", args
->thread_id
);
55 sprintf(name
, "share-ns-%d", args
->thread_id
);
56 c
= lxc_container_new(name
, NULL
);
58 lxc_error("Failed to create container \"%s\"\n", name
);
62 if (c
->is_defined(c
)) {
63 lxc_error("Container \"%s\" is defined\n", name
);
67 if (!c
->createl(c
, "busybox", NULL
, NULL
, 0, NULL
)) {
68 lxc_error("Failed to create busybox container \"%s\"\n", name
);
72 if (!c
->is_defined(c
)) {
73 lxc_error("Container \"%s\" is not defined\n", name
);
77 if (!c
->load_config(c
, NULL
)) {
78 lxc_error("Failed to load config for container \"%s\"\n", name
);
82 /* share ipc namespace by container name */
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
);
88 /* clear all network configuration */
89 if (!c
->set_config_item(c
, "lxc.net", "")) {
90 lxc_error("Failed to set \"lxc.namespace.share.ipc=owning-ns\" for container \"%s\"\n", name
);
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
);
99 sprintf(owning_ns_init_pid
, "%d", args
->init_pid
);
100 /* share net namespace by pid */
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
);
106 if (!c
->want_daemonize(c
, true)) {
107 lxc_error("Failed to mark container \"%s\" daemonized\n", name
);
111 if (!c
->startl(c
, 0, NULL
)) {
112 lxc_error("Failed to start container \"%s\" daemonized\n", name
);
116 init_pid
= c
->init_pid(c
);
118 lxc_error("Failed to retrieve init pid of container \"%s\"\n", name
);
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
);
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
);
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
);
140 lxc_debug("Inherited ipc namespace from container \"owning-ns\": %s == %s\n", args
->inherited_ipc_ns
, ns_buf
);
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
);
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
);
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
);
160 lxc_debug("Inherited net namespace from container \"owning-ns\": %s == %s\n", args
->inherited_net_ns
, ns_buf
);
162 args
->success
= true;
165 if (c
->is_running(c
) && !c
->stop(c
)) {
166 lxc_error("Failed to stop container \"%s\"\n", name
);
170 if (!c
->destroy(c
)) {
171 lxc_error("Failed to destroy container \"%s\"\n", name
);
179 int main(int argc
, char *argv
[])
182 char proc_ns_path
[4096];
183 char ipc_ns_buf
[4096];
184 char net_ns_buf
[4096];
186 pthread_t threads
[10];
187 struct thread_args args
[10];
188 struct lxc_container
*c
;
189 int ret
= EXIT_FAILURE
;
191 c
= lxc_container_new("owning-ns", NULL
);
193 lxc_error("%s", "Failed to create container \"owning-ns\"");
197 if (c
->is_defined(c
)) {
198 lxc_error("%s\n", "Container \"owning-ns\" is defined");
202 if (!c
->createl(c
, "busybox", NULL
, NULL
, 0, NULL
)) {
203 lxc_error("%s\n", "Failed to create busybox container \"owning-ns\"");
207 if (!c
->is_defined(c
)) {
208 lxc_error("%s\n", "Container \"owning-ns\" is not defined");
214 if (!c
->load_config(c
, NULL
)) {
215 lxc_error("%s\n", "Failed to load config for container \"owning-ns\"");
219 if (!c
->want_daemonize(c
, true)) {
220 lxc_error("%s\n", "Failed to mark container \"owning-ns\" daemonized");
224 if (!c
->startl(c
, 0, NULL
)) {
225 lxc_error("%s\n", "Failed to start container \"owning-ns\" daemonized");
229 init_pid
= c
->init_pid(c
);
231 lxc_error("%s\n", "Failed to retrieve init pid of container \"owning-ns\"");
235 /* record our ipc namespace */
236 ret
= snprintf(proc_ns_path
, sizeof(proc_ns_path
), "/proc/%d/ns/ipc", init_pid
);
237 if (ret
< 0 || (size_t)ret
>= sizeof(proc_ns_path
)) {
238 lxc_error("%s\n", "Failed to create string for container \"owning-ns\"");
242 ret
= readlink(proc_ns_path
, ipc_ns_buf
, sizeof(ipc_ns_buf
));
243 if (ret
< 0 || (size_t)ret
>= sizeof(ipc_ns_buf
)) {
244 lxc_error("%s\n", "Failed to retrieve ipc namespace for container \"owning-ns\"");
248 ipc_ns_buf
[ret
] = '\0';
250 /* record our net namespace */
251 ret
= snprintf(proc_ns_path
, sizeof(proc_ns_path
), "/proc/%d/ns/net", init_pid
);
252 if (ret
< 0 || (size_t)ret
>= sizeof(proc_ns_path
)) {
253 lxc_error("%s\n", "Failed to create string for container \"owning-ns\"");
257 ret
= readlink(proc_ns_path
, net_ns_buf
, sizeof(net_ns_buf
));
258 if (ret
< 0 || (size_t)ret
>= sizeof(net_ns_buf
)) {
259 lxc_error("%s\n", "Failed to retrieve ipc namespace for container \"owning-ns\"");
262 net_ns_buf
[ret
] = '\0';
266 pthread_attr_init(&attr
);
268 for (j
= 0; j
< 10; j
++) {
269 lxc_debug("Starting namespace sharing test iteration %d\n", j
);
271 for (i
= 0; i
< 10; i
++) {
274 args
[i
].thread_id
= i
;
275 args
[i
].success
= false;
276 args
[i
].init_pid
= init_pid
;
277 args
[i
].inherited_ipc_ns
= ipc_ns_buf
;
278 args
[i
].inherited_net_ns
= net_ns_buf
;
280 ret
= pthread_create(&threads
[i
], &attr
, ns_sharing_wrapper
, (void *) &args
[i
]);
285 for (i
= 0; i
< 10; i
++) {
288 ret
= pthread_join(threads
[i
], NULL
);
292 if (!args
[i
].success
) {
293 lxc_error("ns sharing thread %d failed\n", args
[i
].thread_id
);
302 if (c
->is_running(c
) && !c
->stop(c
))
303 lxc_error("%s\n", "Failed to stop container \"owning-ns\"");
306 lxc_error("%s\n", "Failed to destroy container \"owning-ns\"");
309 lxc_container_put(c
);
310 if (ret
== EXIT_SUCCESS
)
311 lxc_debug("%s\n", "All state namespace sharing tests passed");