3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // Copyright (C) 2007 Daniel Hokka Zakrisson
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; version 2 of the License.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <lib_internal/sys_clone.h>
35 #define ENSC_WRAPPERS_PREFIX "vspace: "
36 #define ENSC_WRAPPERS_UNISTD 1
37 #define ENSC_WRAPPERS_VSERVER 1
40 #define CMD_HELP 0x1000
41 #define CMD_VERSION 0x1001
43 int wrapper_exit_code = 255;
47 { "help", no_argument, 0, CMD_HELP },
48 { "version", no_argument, 0, CMD_VERSION },
49 { "new", no_argument, 0, 'n' },
50 { "enter", required_argument, 0, 'e' },
51 { "set", no_argument, 0, 's' },
52 { "mask", required_argument, 0, 'm' },
53 { "default", no_argument, 0, 'd' },
54 { "~default", no_argument, 0, 'd' | 0x10000 },
56 { "mount", no_argument, 0, 'M' },
57 { "~mount", no_argument, 0, 'M' | 0x10000 },
58 { "fs", no_argument, 0, 'F' },
59 { "~fs", no_argument, 0, 'F' | 0x10000 },
60 { "ipc", no_argument, 0, 'I' },
61 { "~ipc", no_argument, 0, 'I' | 0x10000 },
62 { "uts", no_argument, 0, 'U' },
63 { "~uts", no_argument, 0, 'U' | 0x10000 },
64 { "user", no_argument, 0, 'S' },
65 { "~user", no_argument, 0, 'S' | 0x10000 },
66 { "pid", no_argument, 0, 'P' },
67 { "~pid", no_argument, 0, 'P' | 0x10000 },
68 { "net", no_argument, 0, 'N' },
69 { "~net", no_argument, 0, 'N' | 0x10000 },
74 showHelp(int fd, char const *cmd, int res)
76 WRITE_MSG(fd, "Usage: ");
79 " <operation> <spaces>* [--] [<program> <args>*]\n"
81 "<operation> can be one of:\n"
82 " --new|-n ... create new spaces and execute <program> there;\n"
83 " <program> is mandatory in this case\n"
84 " --enter|-e <xid> ... enter the spaces of context <xid> and execute\n"
85 " <program> there; <program> is mandatory in this\n"
87 " --set|-s ... assign the current spaces to the current context\n"
89 "<spaces>* specifies the spaces to manipulate.\n"
90 "It can be any combination of:\n"
91 " --mask <mask> ... specify a mask of spaces\n"
92 " --default ... the default spaces for this kernel\n"
93 " --mount ... the mount namespace\n"
94 " --fs ... the fs_struct\n"
95 " --ipc ... the IPC namespace\n"
96 " --uts ... the uts namespace\n"
97 " --user ... the user namespace\n"
98 " --pid ... the pid namespace\n"
99 " --net ... the network namespace\n"
101 "Please report bugs to " PACKAGE_BUGREPORT "\n");
110 "vspace " VERSION " -- manages spaces\n"
111 "This program is part of " PACKAGE_STRING "\n\n"
112 "Copyright (C) 2004 Enrico Scholz\n"
113 "Copyright (C) 2007 Daniel Hokka Zakrisson\n"
114 VERSION_COPYRIGHT_DISCLAIMER);
119 newSpaces(uint_least64_t mask)
125 signal(SIGCHLD, SIG_DFL);
128 pid = sys_clone((int) mask | CLONE_VFORK|SIGCHLD, 0);
130 pid = sys_clone((int) mask | SIGCHLD, 0);
135 perror(ENSC_WRAPPERS_PREFIX "clone()");
136 exit(wrapper_exit_code);
140 vc_exitLikeProcess(pid, wrapper_exit_code);
145 enterSpaces(xid_t xid, uint_least64_t mask)
147 if (vc_enter_namespace(xid, mask)==-1) {
148 perror(ENSC_WRAPPERS_PREFIX "vc_enter_namespace()");
149 exit(wrapper_exit_code);
154 setSpaces(xid_t xid, uint_least64_t mask)
156 if (vc_set_namespace(xid, mask)==-1) {
157 perror(ENSC_WRAPPERS_PREFIX "vc_set_namespace()");
158 exit(wrapper_exit_code);
162 int main(int argc, char *argv[])
165 bool do_enter = false;
167 uint_least64_t mask = 0;
168 xid_t xid = VC_NOCTX;
172 int c = getopt_long(argc, argv, "+nsce:m:" "MFIUSPN", CMDLINE_OPTIONS, 0);
173 uint_least64_t thisbit = 0;
176 switch (c & 0xFFFF) {
177 case CMD_HELP : showHelp(1, argv[0], 0);
178 case CMD_VERSION : showVersion();
179 case 'n' : do_new = true; break;
180 case 's' : do_set = true; break;
183 xid = Evc_xidopt2xid(optarg,true);
186 unsigned long mask_l;
187 if (!isNumberUnsigned(optarg, &mask_l, true)) {
188 WRITE_MSG(2, "Invalid mask '");
189 WRITE_STR(2, optarg);
190 WRITE_MSG(2, "'; try '--help' for more information\n");
191 return wrapper_exit_code;
196 case 'M' : thisbit = CLONE_NEWNS; break;
197 case 'F' : thisbit = CLONE_FS; break;
198 case 'I' : thisbit = CLONE_NEWIPC; break;
199 case 'U' : thisbit = CLONE_NEWUTS; break;
200 case 'S' : thisbit = CLONE_NEWUSER; break;
201 case 'P' : thisbit = CLONE_NEWPID; break;
202 case 'N' : thisbit = CLONE_NEWNET; break;
204 thisbit = vc_get_space_default();
205 if (thisbit == (__typeof__(thisbit)) -1) {
206 thisbit = vc_get_space_mask();
207 if (thisbit == (__typeof__(thisbit)) -1)
208 thisbit = CLONE_NEWNS | CLONE_FS;
213 WRITE_MSG(2, "Try '");
214 WRITE_STR(2, argv[0]);
215 WRITE_MSG(2, " --help' for more information.\n");
226 sum = ((do_new ? 1 : 0) + (do_enter ? 1 : 0) +
230 WRITE_MSG(2, "No operation was specified; try '--help' for more information\n");
232 WRITE_MSG(2, "Can not specify multiple operations; try '--help' for more information\n");
233 else if (optind==argc && (do_new || do_enter))
234 WRITE_MSG(2, "No command specified; try '--help' for more information\n");
236 if (do_new) newSpaces(mask);
237 else if (do_set) setSpaces(VC_SAMECTX, mask);
238 else if (do_enter) enterSpaces(xid, mask);
241 EexecvpD(argv[optind], argv+optind);