]>
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.
29 #include <sys/reboot.h>
30 #include <sys/types.h>
33 #include "lxc/lxccontainer.h"
35 #include "../lxc/compiler.h"
37 #define TEST_DEFAULT_BUF_SIZE 256
43 char inherited_ipc_ns
[TEST_DEFAULT_BUF_SIZE
];
44 char inherited_net_ns
[TEST_DEFAULT_BUF_SIZE
];
47 __noreturn
static void *ns_sharing_wrapper(void *data
)
52 char owning_ns_init_pid
[100];
53 char proc_ns_path
[TEST_DEFAULT_BUF_SIZE
];
54 char ns_buf
[TEST_DEFAULT_BUF_SIZE
];
55 struct lxc_container
*c
;
56 struct thread_args
*args
= data
;
58 lxc_debug("Starting namespace sharing thread %d\n", args
->thread_id
);
60 sprintf(name
, "share-ns-%d", args
->thread_id
);
61 c
= lxc_container_new(name
, NULL
);
63 lxc_error("Failed to create container \"%s\"\n", name
);
64 goto out_pthread_exit
;
67 if (c
->is_defined(c
)) {
68 lxc_error("Container \"%s\" is defined\n", name
);
72 if (!c
->createl(c
, "busybox", NULL
, NULL
, 0, NULL
)) {
73 lxc_error("Failed to create busybox container \"%s\"\n", name
);
77 if (!c
->is_defined(c
)) {
78 lxc_error("Container \"%s\" is not defined\n", name
);
84 if (!c
->load_config(c
, NULL
)) {
85 lxc_error("Failed to load config for container \"%s\"\n", name
);
89 /* share ipc namespace by container name */
90 if (!c
->set_config_item(c
, "lxc.namespace.share.ipc", "owning-ns")) {
91 lxc_error("Failed to set \"lxc.namespace.share.ipc=owning-ns\" for container \"%s\"\n", name
);
95 /* clear all network configuration */
96 if (!c
->set_config_item(c
, "lxc.net", "")) {
97 lxc_error("Failed to set \"lxc.namespace.share.ipc=owning-ns\" for container \"%s\"\n", name
);
101 if (!c
->set_config_item(c
, "lxc.net.0.type", "empty")) {
102 lxc_error("Failed to set \"lxc.net.0.type=empty\" for container \"%s\"\n", name
);
106 sprintf(owning_ns_init_pid
, "%d", args
->init_pid
);
107 /* share net namespace by pid */
108 if (!c
->set_config_item(c
, "lxc.namespace.share.net", owning_ns_init_pid
)) {
109 lxc_error("Failed to set \"lxc.namespace.share.net=%s\" for container \"%s\"\n", owning_ns_init_pid
, name
);
113 if (!c
->want_daemonize(c
, true)) {
114 lxc_error("Failed to mark container \"%s\" daemonized\n", name
);
118 if (!c
->startl(c
, 0, NULL
)) {
119 lxc_error("Failed to start container \"%s\" daemonized\n", name
);
123 init_pid
= c
->init_pid(c
);
125 lxc_error("Failed to retrieve init pid of container \"%s\"\n", name
);
129 /* Check whether we correctly inherited the ipc namespace. */
130 ret
= snprintf(proc_ns_path
, sizeof(proc_ns_path
), "/proc/%d/ns/ipc", init_pid
);
131 if (ret
< 0 || (size_t)ret
>= sizeof(proc_ns_path
)) {
132 lxc_error("Failed to create string for container \"%s\"\n", name
);
136 ret
= readlink(proc_ns_path
, ns_buf
, sizeof(ns_buf
));
137 if (ret
< 0 || (size_t)ret
>= sizeof(ns_buf
)) {
138 lxc_error("Failed to retrieve ipc namespace for container \"%s\"\n", name
);
143 if (strcmp(args
->inherited_ipc_ns
, ns_buf
) != 0) {
144 lxc_error("Failed to inherit ipc namespace from container \"owning-ns\": %s != %s\n", args
->inherited_ipc_ns
, ns_buf
);
147 lxc_debug("Inherited ipc namespace from container \"owning-ns\": %s == %s\n", args
->inherited_ipc_ns
, ns_buf
);
149 /* Check whether we correctly inherited the net namespace. */
150 ret
= snprintf(proc_ns_path
, sizeof(proc_ns_path
), "/proc/%d/ns/net", init_pid
);
151 if (ret
< 0 || (size_t)ret
>= sizeof(proc_ns_path
)) {
152 lxc_error("Failed to create string for container \"%s\"\n", name
);
156 ret
= readlink(proc_ns_path
, ns_buf
, sizeof(ns_buf
));
157 if (ret
< 0 || (size_t)ret
>= sizeof(ns_buf
)) {
158 lxc_error("Failed to retrieve ipc namespace for container \"%s\"\n", name
);
163 if (strcmp(args
->inherited_net_ns
, ns_buf
) != 0) {
164 lxc_error("Failed to inherit net namespace from container \"owning-ns\": %s != %s\n", args
->inherited_net_ns
, ns_buf
);
167 lxc_debug("Inherited net namespace from container \"owning-ns\": %s == %s\n", args
->inherited_net_ns
, ns_buf
);
169 args
->success
= true;
172 if (c
->is_running(c
) && !c
->stop(c
))
173 lxc_error("Failed to stop container \"%s\"\n", name
);
176 lxc_error("Failed to destroy container \"%s\"\n", name
);
178 lxc_container_put(c
);
184 int main(int argc
, char *argv
[])
186 struct thread_args
*args
= NULL
;
187 pthread_t
*threads
= NULL
;
188 size_t nthreads
= 10;
190 char proc_ns_path
[TEST_DEFAULT_BUF_SIZE
];
191 char ipc_ns_buf
[TEST_DEFAULT_BUF_SIZE
];
192 char net_ns_buf
[TEST_DEFAULT_BUF_SIZE
];
194 struct lxc_container
*c
;
195 int ret
= EXIT_FAILURE
;
197 pthread_attr_init(&attr
);
199 c
= lxc_container_new("owning-ns", NULL
);
201 lxc_error("%s", "Failed to create container \"owning-ns\"");
205 if (c
->is_defined(c
)) {
206 lxc_error("%s\n", "Container \"owning-ns\" is defined");
210 if (!c
->createl(c
, "busybox", NULL
, NULL
, 0, NULL
)) {
211 lxc_error("%s\n", "Failed to create busybox container \"owning-ns\"");
215 if (!c
->is_defined(c
)) {
216 lxc_error("%s\n", "Container \"owning-ns\" is not defined");
222 if (!c
->load_config(c
, NULL
)) {
223 lxc_error("%s\n", "Failed to load config for container \"owning-ns\"");
227 if (!c
->want_daemonize(c
, true)) {
228 lxc_error("%s\n", "Failed to mark container \"owning-ns\" daemonized");
232 if (!c
->startl(c
, 0, NULL
)) {
233 lxc_error("%s\n", "Failed to start container \"owning-ns\" daemonized");
237 init_pid
= c
->init_pid(c
);
239 lxc_error("%s\n", "Failed to retrieve init pid of container \"owning-ns\"");
243 /* record our ipc namespace */
244 ret
= snprintf(proc_ns_path
, sizeof(proc_ns_path
), "/proc/%d/ns/ipc", init_pid
);
245 if (ret
< 0 || (size_t)ret
>= sizeof(proc_ns_path
)) {
246 lxc_error("%s\n", "Failed to create string for container \"owning-ns\"");
250 ret
= readlink(proc_ns_path
, ipc_ns_buf
, sizeof(ipc_ns_buf
));
251 if (ret
< 0 || (size_t)ret
>= sizeof(ipc_ns_buf
)) {
252 lxc_error("%s\n", "Failed to retrieve ipc namespace for container \"owning-ns\"");
256 ipc_ns_buf
[ret
] = '\0';
258 /* record our net namespace */
259 ret
= snprintf(proc_ns_path
, sizeof(proc_ns_path
), "/proc/%d/ns/net", init_pid
);
260 if (ret
< 0 || (size_t)ret
>= sizeof(proc_ns_path
)) {
261 lxc_error("%s\n", "Failed to create string for container \"owning-ns\"");
265 ret
= readlink(proc_ns_path
, net_ns_buf
, sizeof(net_ns_buf
));
266 if (ret
< 0 || (size_t)ret
>= sizeof(net_ns_buf
)) {
267 lxc_error("%s\n", "Failed to retrieve ipc namespace for container \"owning-ns\"");
270 net_ns_buf
[ret
] = '\0';
274 args
= malloc(sizeof(struct thread_args
) * nthreads
);
276 lxc_error("%s\n", "Failed to allocate memory");
280 threads
= malloc(sizeof(pthread_t
) * nthreads
);
282 lxc_error("%s\n", "Failed to allocate memory");
286 for (j
= 0; j
< 10; j
++) {
287 bool had_error
= false;
289 lxc_debug("Starting namespace sharing test iteration %d\n", j
);
291 for (i
= 0; (size_t)i
< nthreads
; i
++) {
292 memset(&args
[i
], 0, sizeof(struct thread_args
));
293 memset(&threads
[i
], 0, sizeof(pthread_t
));
295 args
[i
].thread_id
= i
;
296 args
[i
].success
= false;
297 args
[i
].init_pid
= init_pid
;
298 snprintf(args
[i
].inherited_ipc_ns
, sizeof(args
[i
].inherited_ipc_ns
), "%s", ipc_ns_buf
);
299 snprintf(args
[i
].inherited_net_ns
, sizeof(args
[i
].inherited_net_ns
), "%s", net_ns_buf
);
301 ret
= pthread_create(&threads
[i
], &attr
, ns_sharing_wrapper
, (void *)&args
[i
]);
306 for (i
= 0; (size_t)i
< nthreads
; i
++) {
307 ret
= pthread_join(threads
[i
], NULL
);
311 if (!args
[i
].success
) {
312 lxc_error("ns sharing thread %d failed\n", args
[i
].thread_id
);
326 pthread_attr_destroy(&attr
);
328 if (c
->is_running(c
) && !c
->stop(c
))
329 lxc_error("%s\n", "Failed to stop container \"owning-ns\"");
332 lxc_error("%s\n", "Failed to destroy container \"owning-ns\"");
334 lxc_container_put(c
);
335 if (ret
== EXIT_SUCCESS
)
336 lxc_debug("%s\n", "All state namespace sharing tests passed");