]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/namespace.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2009
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/param.h>
33 #include <sys/syscall.h>
34 #include <sys/types.h>
39 #include "memory_utils.h"
40 #include "namespace.h"
43 lxc_log_define(namespace, lxc
);
46 * Let's use the "standard stack limit" (i.e. glibc thread size default) for
49 #define __LXC_STACK_SIZE (8 * 1024 * 1024)
50 pid_t
lxc_clone(int (*fn
)(void *), void *arg
, int flags
, int *pidfd
)
55 stack
= malloc(__LXC_STACK_SIZE
);
57 SYSERROR("Failed to allocate clone stack");
62 ret
= __clone2(fn
, stack
, __LXC_STACK_SIZE
, flags
| SIGCHLD
, arg
, pidfd
);
64 ret
= clone(fn
, stack
+ __LXC_STACK_SIZE
, flags
| SIGCHLD
, arg
, pidfd
);
67 SYSERROR("Failed to clone (%#x)", flags
);
72 /* Leave the user namespace at the first position in the array of structs so
73 * that we always attach to it first when iterating over the struct and using
74 * setns() to switch namespaces. This especially affects lxc_attach(): Suppose
75 * you cloned a new user namespace and mount namespace as an unprivileged user
76 * on the host and want to setns() to the mount namespace. This requires you to
77 * attach to the user namespace first otherwise the kernel will fail this check:
79 * if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
80 * !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
81 * !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
86 * linux/fs/namespace.c:mntns_install().
88 const struct ns_info ns_info
[LXC_NS_MAX
] = {
89 [LXC_NS_USER
] = { "user", CLONE_NEWUSER
, "CLONE_NEWUSER", "LXC_USER_NS" },
90 [LXC_NS_MNT
] = { "mnt", CLONE_NEWNS
, "CLONE_NEWNS", "LXC_MNT_NS" },
91 [LXC_NS_PID
] = { "pid", CLONE_NEWPID
, "CLONE_NEWPID", "LXC_PID_NS" },
92 [LXC_NS_UTS
] = { "uts", CLONE_NEWUTS
, "CLONE_NEWUTS", "LXC_UTS_NS" },
93 [LXC_NS_IPC
] = { "ipc", CLONE_NEWIPC
, "CLONE_NEWIPC", "LXC_IPC_NS" },
94 [LXC_NS_NET
] = { "net", CLONE_NEWNET
, "CLONE_NEWNET", "LXC_NET_NS" },
95 [LXC_NS_CGROUP
] = { "cgroup", CLONE_NEWCGROUP
, "CLONE_NEWCGROUP", "LXC_CGROUP_NS" }
98 int lxc_namespace_2_cloneflag(const char *namespace)
102 for (i
= 0; i
< LXC_NS_MAX
; i
++)
103 if (!strcasecmp(ns_info
[i
].proc_name
, namespace))
104 return ns_info
[i
].clone_flag
;
106 ERROR("Invalid namespace name \"%s\"", namespace);
110 int lxc_namespace_2_ns_idx(const char *namespace)
114 for (i
= 0; i
< LXC_NS_MAX
; i
++)
115 if (!strcmp(ns_info
[i
].proc_name
, namespace))
118 ERROR("Invalid namespace name \"%s\"", namespace);
122 extern int lxc_namespace_2_std_identifiers(char *namespaces
)
127 /* The identifiers for namespaces used with lxc-attach and lxc-unshare
128 * as given on the manpage do not align with the standard identifiers.
129 * This affects network, mount, and uts namespaces. The standard identifiers
130 * are: "mnt", "uts", and "net" whereas lxc-attach and lxc-unshare uses
131 * "MOUNT", "UTSNAME", and "NETWORK". So let's use some cheap memmove()s
132 * to replace them by their standard identifiers.
133 * Let's illustrate this with an example:
140 * dest: del + 1 == OUNT|PID
141 * src: del + 3 == NT|PID
146 while ((del
= strstr(namespaces
, "MOUNT")))
147 memmove(del
+ 1, del
+ 3, strlen(del
) - 2);
149 for (it
= (char *[]){"NETWORK", "UTSNAME", NULL
}; it
&& *it
; it
++)
150 while ((del
= strstr(namespaces
, *it
)))
151 memmove(del
+ 3, del
+ 7, strlen(del
) - 6);
156 int lxc_fill_namespace_flags(char *flaglist
, int *flags
)
162 ERROR("At least one namespace is needed.");
166 lxc_iterate_parts(token
, flaglist
, "|") {
167 aflag
= lxc_namespace_2_cloneflag(token
);