]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/namespace.c
Change author email address
[mirror_lxc.git] / src / lxc / namespace.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2009
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #include <unistd.h>
25 #include <alloca.h>
26 #include <errno.h>
27 #include <signal.h>
28 #include <sys/param.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32
33 #include "namespace.h"
34 #include "log.h"
35
36 lxc_log_define(lxc_namespace, lxc);
37
38 struct clone_arg {
39 int (*fn)(void *);
40 void *arg;
41 };
42
43 static int do_clone(void *arg)
44 {
45 struct clone_arg *clone_arg = arg;
46 return clone_arg->fn(clone_arg->arg);
47 }
48
49 pid_t lxc_clone(int (*fn)(void *), void *arg, int flags)
50 {
51 struct clone_arg clone_arg = {
52 .fn = fn,
53 .arg = arg,
54 };
55
56 long stack_size = sysconf(_SC_PAGESIZE);
57 void *stack = alloca(stack_size);
58 pid_t ret;
59
60 #ifdef __ia64__
61 ret = __clone2(do_clone, stack,
62 stack_size, flags | SIGCHLD, &clone_arg);
63 #else
64 ret = clone(do_clone, stack + stack_size, flags | SIGCHLD, &clone_arg);
65 #endif
66 if (ret < 0)
67 ERROR("failed to clone(0x%x): %s", flags, strerror(errno));
68
69 return ret;
70 }
71
72 static char *namespaces_list[] = {
73 "MOUNT", "PID", "UTSNAME", "IPC",
74 "USER", "NETWORK"
75 };
76 static int cloneflags_list[] = {
77 CLONE_NEWNS, CLONE_NEWPID, CLONE_NEWUTS, CLONE_NEWIPC,
78 CLONE_NEWUSER, CLONE_NEWNET
79 };
80
81 int lxc_namespace_2_cloneflag(char *namespace)
82 {
83 int i, len;
84 len = sizeof(namespaces_list)/sizeof(namespaces_list[0]);
85 for (i = 0; i < len; i++)
86 if (!strcmp(namespaces_list[i], namespace))
87 return cloneflags_list[i];
88
89 ERROR("invalid namespace name %s", namespace);
90 return -1;
91 }
92
93 int lxc_fill_namespace_flags(char *flaglist, int *flags)
94 {
95 char *token, *saveptr = NULL;
96 int aflag;
97
98 if (!flaglist) {
99 ERROR("need at least one namespace to unshare");
100 return -1;
101 }
102
103 token = strtok_r(flaglist, "|", &saveptr);
104 while (token) {
105
106 aflag = lxc_namespace_2_cloneflag(token);
107 if (aflag < 0)
108 return -1;
109
110 *flags |= aflag;
111
112 token = strtok_r(NULL, "|", &saveptr);
113 }
114 return 0;
115 }