]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/namespace.c
lxc-unshare: Move functions to determine clone flags from command line options to...
[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 <dlezcano at fr.ibm.com>
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 <syscall.h>
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33
34 #include "namespace.h"
35 #include "log.h"
36
37 lxc_log_define(lxc_namespace, lxc);
38
39 struct clone_arg {
40 int (*fn)(void *);
41 void *arg;
42 };
43
44 static int do_clone(void *arg)
45 {
46 struct clone_arg *clone_arg = arg;
47 return clone_arg->fn(clone_arg->arg);
48 }
49
50 pid_t lxc_clone(int (*fn)(void *), void *arg, int flags)
51 {
52 struct clone_arg clone_arg = {
53 .fn = fn,
54 .arg = arg,
55 };
56
57 long stack_size = sysconf(_SC_PAGESIZE);
58 void *stack = alloca(stack_size);
59 pid_t ret;
60
61 #ifdef __ia64__
62 ret = __clone2(do_clone, stack,
63 stack_size, flags | SIGCHLD, &clone_arg);
64 #else
65 ret = clone(do_clone, stack + stack_size, flags | SIGCHLD, &clone_arg);
66 #endif
67 if (ret < 0)
68 ERROR("failed to clone(0x%x): %s", flags, strerror(errno));
69
70 return ret;
71 }
72
73 static char *namespaces_list[] = {
74 "MOUNT", "PID", "UTSNAME", "IPC",
75 "USER", "NETWORK"
76 };
77 static int cloneflags_list[] = {
78 CLONE_NEWNS, CLONE_NEWPID, CLONE_NEWUTS, CLONE_NEWIPC,
79 CLONE_NEWUSER, CLONE_NEWNET
80 };
81
82 int lxc_namespace_2_cloneflag(char *namespace)
83 {
84 int i, len;
85 len = sizeof(namespaces_list)/sizeof(namespaces_list[0]);
86 for (i = 0; i < len; i++)
87 if (!strcmp(namespaces_list[i], namespace))
88 return cloneflags_list[i];
89
90 ERROR("invalid namespace name %s", namespace);
91 return -1;
92 }
93
94 int lxc_fill_namespace_flags(char *flaglist, int *flags)
95 {
96 char *token, *saveptr = NULL;
97 int aflag;
98
99 if (!flaglist) {
100 ERROR("need at least one namespace to unshare");
101 return -1;
102 }
103
104 token = strtok_r(flaglist, "|", &saveptr);
105 while (token) {
106
107 aflag = lxc_namespace_2_cloneflag(token);
108 if (aflag < 0)
109 return -1;
110
111 *flags |= aflag;
112
113 token = strtok_r(NULL, "|", &saveptr);
114 }
115 return 0;
116 }