CLONE_NEWNS and CLONE_FS aren't default spaces.
[util-vserver.git] / src / vspace.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // Copyright (C) 2007 Daniel Hokka Zakrisson
5 //  
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.
9 //  
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.
14 //  
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.
18
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include "util.h"
25 #include <lib_internal/sys_clone.h>
26
27 #include <vserver.h>
28
29 #include <getopt.h>
30 #include <libgen.h>
31 #include <errno.h>
32 #include <signal.h>
33 #include <sched.h>
34
35 #define ENSC_WRAPPERS_PREFIX    "vspace: "
36 #define ENSC_WRAPPERS_UNISTD    1
37 #define ENSC_WRAPPERS_VSERVER   1
38 #include <wrappers.h>
39
40 #define CMD_HELP                0x1000
41 #define CMD_VERSION             0x1001
42
43 int             wrapper_exit_code  =  255;
44
45 struct option const
46 CMDLINE_OPTIONS[] = {
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 },
55
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 },
70   {0,0,0,0}
71 };
72
73 static void
74 showHelp(int fd, char const *cmd, int res)
75 {
76   WRITE_MSG(fd, "Usage: ");
77   WRITE_STR(fd, cmd);
78   WRITE_MSG(fd,
79             " <operation> <spaces>* [--] [<program> <args>*]\n"
80             "\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"
86             "                           case\n"
87             "    --set|-s          ...  assign the current spaces to the current context\n"
88             "\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"
100             "\n"
101             "Please report bugs to " PACKAGE_BUGREPORT "\n");
102
103   exit(res);
104 }
105
106 static void
107 showVersion()
108 {
109   WRITE_MSG(1,
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);
115   exit(0);
116 }
117
118 static void
119 newSpaces(uint_least64_t mask)
120 {
121   pid_t pid;
122
123   mask &= ~CLONE_FS;
124
125   signal(SIGCHLD, SIG_DFL);
126
127 #ifdef NDEBUG
128   pid = sys_clone((int) mask | CLONE_VFORK|SIGCHLD, 0);
129 #else
130   pid = sys_clone((int) mask | SIGCHLD, 0);
131 #endif
132
133   switch (pid) {
134     case -1     :
135       perror(ENSC_WRAPPERS_PREFIX "clone()");
136       exit(wrapper_exit_code);
137     case 0      :
138       break;
139     default     :
140       vc_exitLikeProcess(pid, wrapper_exit_code);
141   }
142 }
143
144 static void
145 enterSpaces(xid_t xid, uint_least64_t mask)
146 {
147   if (vc_enter_namespace(xid, mask)==-1) {
148     perror(ENSC_WRAPPERS_PREFIX "vc_enter_namespace()");
149     exit(wrapper_exit_code);
150   }
151 }
152
153 static void
154 setSpaces(xid_t xid, uint_least64_t mask)
155 {
156   if (vc_set_namespace(xid, mask)==-1) {
157     perror(ENSC_WRAPPERS_PREFIX "vc_set_namespace()");
158     exit(wrapper_exit_code);
159   }
160 }
161
162 int main(int argc, char *argv[])
163 {
164   bool                  do_new     = false;
165   bool                  do_enter   = false;
166   bool                  do_set     = false;
167   uint_least64_t        mask       = 0;
168   xid_t                 xid        = VC_NOCTX;
169   int                   sum        = 0;
170   
171   while (1) {
172     int                 c = getopt_long(argc, argv, "+nsce:m:" "MFIUSPN", CMDLINE_OPTIONS, 0);
173     uint_least64_t      thisbit = 0;
174     if (c==-1) break;
175
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;
181       case 'e'          :
182         do_enter = true;
183         xid      = Evc_xidopt2xid(optarg,true);
184         break;
185       case 'm'          :  {
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;
192         }
193         mask = mask_l;
194         break;
195       }
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;
203       case 'd'          :
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 = 0;
209         }
210         break;
211
212       default           :
213         WRITE_MSG(2, "Try '");
214         WRITE_STR(2, argv[0]);
215         WRITE_MSG(2, " --help' for more information.\n");
216         return 255;
217         break;
218     }
219     /* ~ option used */
220     if (c & 0xFFFF0000)
221       mask &= ~thisbit;
222     else
223       mask |= thisbit;
224   }
225
226   sum = ((do_new ? 1 : 0) + (do_enter ? 1 : 0) +
227          (do_set ? 1 : 0));
228   
229   if (sum==0)
230     WRITE_MSG(2, "No operation was specified; try '--help' for more information\n");
231   else if (sum>1)
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");
235   else {
236     if      (do_new)     newSpaces(mask);
237     else if (do_set)     setSpaces(VC_SAMECTX, mask);
238     else if (do_enter)   enterSpaces(xid, mask);
239
240     if (optind<argc)
241       EexecvpD(argv[optind], argv+optind);
242
243     return EXIT_SUCCESS;
244   }
245
246   return 255;
247 }