]>
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.
28 #include <sys/reboot.h>
29 #include <sys/types.h>
32 #include "lxc/lxccontainer.h"
34 #include "../lxc/compiler.h"
36 #define TEST_DEFAULT_BUF_SIZE 256
42 char inherited_ipc_ns
[TEST_DEFAULT_BUF_SIZE
];
43 char inherited_net_ns
[TEST_DEFAULT_BUF_SIZE
];
46 __noreturn
static void *ns_sharing_wrapper(void *data
)
51 char owning_ns_init_pid
[100];
52 char proc_ns_path
[TEST_DEFAULT_BUF_SIZE
];
53 char ns_buf
[TEST_DEFAULT_BUF_SIZE
];
54 struct lxc_container
*c
;
55 struct thread_args
*args
= data
;
57 lxc_debug("Starting namespace sharing thread %d\n", args
->thread_id
);
59 sprintf(name
, "share-ns-%d", args
->thread_id
);
60 c
= lxc_container_new(name
, NULL
);
62 lxc_error("Failed to create container \"%s\"\n", name
);
63 goto out_pthread_exit
;
66 if (c
->is_defined(c
)) {
67 lxc_error("Container \"%s\" is defined\n", name
);
71 if (!c
->createl(c
, "busybox", NULL
, NULL
, 0, NULL
)) {
72 lxc_error("Failed to create busybox container \"%s\"\n", name
);
76 if (!c
->is_defined(c
)) {
77 lxc_error("Container \"%s\" is not defined\n", name
);
83 if (!c
->load_config(c
, NULL
)) {
84 lxc_error("Failed to load config for container \"%s\"\n", name
);
88 /* share ipc namespace by container name */
89 if (!c
->set_config_item(c
, "lxc.namespace.share.ipc", "owning-ns")) {
90 lxc_error("Failed to set \"lxc.namespace.share.ipc=owning-ns\" for container \"%s\"\n", name
);
94 /* clear all network configuration */
95 if (!c
->set_config_item(c
, "lxc.net", "")) {
96 lxc_error("Failed to set \"lxc.namespace.share.ipc=owning-ns\" for container \"%s\"\n", name
);
100 if (!c
->set_config_item(c
, "lxc.net.0.type", "empty")) {
101 lxc_error("Failed to set \"lxc.net.0.type=empty\" for container \"%s\"\n", name
);
105 sprintf(owning_ns_init_pid
, "%d", args
->init_pid
);
106 /* share net namespace by pid */
107 if (!c
->set_config_item(c
, "lxc.namespace.share.net", owning_ns_init_pid
)) {
108 lxc_error("Failed to set \"lxc.namespace.share.net=%s\" for container \"%s\"\n", owning_ns_init_pid
, name
);
112 if (!c
->want_daemonize(c
, true)) {
113 lxc_error("Failed to mark container \"%s\" daemonized\n", name
);
117 if (!c
->startl(c
, 0, NULL
)) {
118 lxc_error("Failed to start container \"%s\" daemonized\n", name
);
122 init_pid
= c
->init_pid(c
);
124 lxc_error("Failed to retrieve init pid of container \"%s\"\n", name
);
128 /* Check whether we correctly inherited the ipc namespace. */
129 ret
= snprintf(proc_ns_path
, sizeof(proc_ns_path
), "/proc/%d/ns/ipc", init_pid
);
130 if (ret
< 0 || (size_t)ret
>= sizeof(proc_ns_path
)) {
131 lxc_error("Failed to create string for container \"%s\"\n", name
);
135 ret
= readlink(proc_ns_path
, ns_buf
, sizeof(ns_buf
));
136 if (ret
< 0 || (size_t)ret
>= sizeof(ns_buf
)) {
137 lxc_error("Failed to retrieve ipc namespace for container \"%s\"\n", name
);
142 if (strcmp(args
->inherited_ipc_ns
, ns_buf
) != 0) {
143 lxc_error("Failed to inherit ipc namespace from container \"owning-ns\": %s != %s\n", args
->inherited_ipc_ns
, ns_buf
);
146 lxc_debug("Inherited ipc namespace from container \"owning-ns\": %s == %s\n", args
->inherited_ipc_ns
, ns_buf
);
148 /* Check whether we correctly inherited the net namespace. */
149 ret
= snprintf(proc_ns_path
, sizeof(proc_ns_path
), "/proc/%d/ns/net", init_pid
);
150 if (ret
< 0 || (size_t)ret
>= sizeof(proc_ns_path
)) {
151 lxc_error("Failed to create string for container \"%s\"\n", name
);
155 ret
= readlink(proc_ns_path
, ns_buf
, sizeof(ns_buf
));
156 if (ret
< 0 || (size_t)ret
>= sizeof(ns_buf
)) {
157 lxc_error("Failed to retrieve ipc namespace for container \"%s\"\n", name
);
162 if (strcmp(args
->inherited_net_ns
, ns_buf
) != 0) {
163 lxc_error("Failed to inherit net namespace from container \"owning-ns\": %s != %s\n", args
->inherited_net_ns
, ns_buf
);
166 lxc_debug("Inherited net namespace from container \"owning-ns\": %s == %s\n", args
->inherited_net_ns
, ns_buf
);
168 args
->success
= true;
171 if (c
->is_running(c
) && !c
->stop(c
))
172 lxc_error("Failed to stop container \"%s\"\n", name
);
175 lxc_error("Failed to destroy container \"%s\"\n", name
);
177 lxc_container_put(c
);
183 int main(int argc
, char *argv
[])
185 struct thread_args
*args
= NULL
;
186 pthread_t
*threads
= NULL
;
187 size_t nthreads
= 10;
189 char proc_ns_path
[TEST_DEFAULT_BUF_SIZE
];
190 char ipc_ns_buf
[TEST_DEFAULT_BUF_SIZE
];
191 char net_ns_buf
[TEST_DEFAULT_BUF_SIZE
];
193 struct lxc_container
*c
;
194 int ret
= EXIT_FAILURE
;
196 pthread_attr_init(&attr
);
198 c
= lxc_container_new("owning-ns", NULL
);
200 lxc_error("%s", "Failed to create container \"owning-ns\"");
204 if (c
->is_defined(c
)) {
205 lxc_error("%s\n", "Container \"owning-ns\" is defined");
209 if (!c
->createl(c
, "busybox", NULL
, NULL
, 0, NULL
)) {
210 lxc_error("%s\n", "Failed to create busybox container \"owning-ns\"");
214 if (!c
->is_defined(c
)) {
215 lxc_error("%s\n", "Container \"owning-ns\" is not defined");
221 if (!c
->load_config(c
, NULL
)) {
222 lxc_error("%s\n", "Failed to load config for container \"owning-ns\"");
226 if (!c
->want_daemonize(c
, true)) {
227 lxc_error("%s\n", "Failed to mark container \"owning-ns\" daemonized");
231 if (!c
->startl(c
, 0, NULL
)) {
232 lxc_error("%s\n", "Failed to start container \"owning-ns\" daemonized");
236 init_pid
= c
->init_pid(c
);
238 lxc_error("%s\n", "Failed to retrieve init pid of container \"owning-ns\"");
242 /* record our ipc namespace */
243 ret
= snprintf(proc_ns_path
, sizeof(proc_ns_path
), "/proc/%d/ns/ipc", init_pid
);
244 if (ret
< 0 || (size_t)ret
>= sizeof(proc_ns_path
)) {
245 lxc_error("%s\n", "Failed to create string for container \"owning-ns\"");
249 ret
= readlink(proc_ns_path
, ipc_ns_buf
, sizeof(ipc_ns_buf
));
250 if (ret
< 0 || (size_t)ret
>= sizeof(ipc_ns_buf
)) {
251 lxc_error("%s\n", "Failed to retrieve ipc namespace for container \"owning-ns\"");
255 ipc_ns_buf
[ret
] = '\0';
257 /* record our net namespace */
258 ret
= snprintf(proc_ns_path
, sizeof(proc_ns_path
), "/proc/%d/ns/net", init_pid
);
259 if (ret
< 0 || (size_t)ret
>= sizeof(proc_ns_path
)) {
260 lxc_error("%s\n", "Failed to create string for container \"owning-ns\"");
264 ret
= readlink(proc_ns_path
, net_ns_buf
, sizeof(net_ns_buf
));
265 if (ret
< 0 || (size_t)ret
>= sizeof(net_ns_buf
)) {
266 lxc_error("%s\n", "Failed to retrieve ipc namespace for container \"owning-ns\"");
269 net_ns_buf
[ret
] = '\0';
273 args
= malloc(sizeof(struct thread_args
) * nthreads
);
275 lxc_error("%s\n", "Failed to allocate memory");
279 threads
= malloc(sizeof(pthread_t
) * nthreads
);
281 lxc_error("%s\n", "Failed to allocate memory");
285 for (j
= 0; j
< 10; j
++) {
286 bool had_error
= false;
288 lxc_debug("Starting namespace sharing test iteration %d\n", j
);
290 for (i
= 0; i
< nthreads
; i
++) {
291 memset(&args
[i
], 0, sizeof(struct thread_args
));
292 memset(&threads
[i
], 0, sizeof(pthread_t
));
294 args
[i
].thread_id
= i
;
295 args
[i
].success
= false;
296 args
[i
].init_pid
= init_pid
;
297 snprintf(args
[i
].inherited_ipc_ns
, sizeof(args
[i
].inherited_ipc_ns
), "%s", ipc_ns_buf
);
298 snprintf(args
[i
].inherited_net_ns
, sizeof(args
[i
].inherited_net_ns
), "%s", net_ns_buf
);
300 ret
= pthread_create(&threads
[i
], &attr
, ns_sharing_wrapper
, (void *)&args
[i
]);
305 for (i
= 0; i
< nthreads
; i
++) {
306 ret
= pthread_join(threads
[i
], NULL
);
310 if (!args
[i
].success
) {
311 lxc_error("ns sharing thread %d failed\n", args
[i
].thread_id
);
325 pthread_attr_destroy(&attr
);
327 if (c
->is_running(c
) && !c
->stop(c
))
328 lxc_error("%s\n", "Failed to stop container \"owning-ns\"");
331 lxc_error("%s\n", "Failed to destroy container \"owning-ns\"");
333 lxc_container_put(c
);
334 if (ret
== EXIT_SUCCESS
)
335 lxc_debug("%s\n", "All state namespace sharing tests passed");