Tagging module util-vserver - util-vserver-0.30.215-6
[util-vserver.git] / src / vspace.c
1 // $Id: vspace.c 2693 2008-03-01 00:26:31Z dhozac $    --*- 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
54   { "mount",      no_argument,       0, 'M' },
55   { "fs",         no_argument,       0, 'F' },
56   { "ipc",        no_argument,       0, 'I' },
57   { "uts",        no_argument,       0, 'U' },
58   { "user",       no_argument,       0, 'S' },
59   { "pid",        no_argument,       0, 'P' },
60   { "net",        no_argument,       0, 'N' },
61   {0,0,0,0}
62 };
63
64 static void
65 showHelp(int fd, char const *cmd, int res)
66 {
67   WRITE_MSG(fd, "Usage: ");
68   WRITE_STR(fd, cmd);
69   WRITE_MSG(fd,
70             " <operation> <spaces>* [--] [<program> <args>*]\n"
71             "\n"
72             "<operation> can be one of:\n"
73             "    --new|-n          ...  create new spaces and execute <program> there;\n"
74             "                           <program> is mandatory in this case\n"
75             "    --enter|-e <xid>  ...  enter the spaces of context <xid> and execute\n"
76             "                           <program> there; <program> is mandatory in this\n"
77             "                           case\n"
78             "    --set|-s          ...  assign the current spaces to the current context\n"
79             "\n"
80             "<spaces>* specifies the spaces to manipulate.\n"
81             "It can be any combination of:\n"
82             "    --mask <mask>     ...  specify a mask of spaces\n"
83             "    --mount           ...  the mount namespace\n"
84             "    --fs              ...  the fs_struct\n"
85             "    --ipc             ...  the IPC namespace\n"
86             "    --uts             ...  the uts namespace\n"
87             "    --user            ...  the user namespace\n"
88             "    --pid             ...  the pid namespace\n"
89             "    --net             ...  the network namespace\n"
90             "\n"
91             "Please report bugs to " PACKAGE_BUGREPORT "\n");
92
93   exit(res);
94 }
95
96 static void
97 showVersion()
98 {
99   WRITE_MSG(1,
100             "vspace " VERSION " -- manages spaces\n"
101             "This program is part of " PACKAGE_STRING "\n\n"
102             "Copyright (C) 2004 Enrico Scholz\n"
103             "Copyright (C) 2007 Daniel Hokka Zakrisson\n"
104             VERSION_COPYRIGHT_DISCLAIMER);
105   exit(0);
106 }
107
108 static void
109 newSpaces(uint_least64_t mask, const char *cmd)
110 {
111   pid_t pid;
112
113   /* optimize default case */
114   if (mask == 0)
115     return;
116
117   signal(SIGCHLD, SIG_DFL);
118
119 #ifdef NDEBUG
120   pid = sys_clone((int) mask | CLONE_VFORK|SIGCHLD, 0);
121 #else
122   pid = sys_clone((int) mask | SIGCHLD, 0);
123 #endif
124
125   switch (pid) {
126     case -1     :
127       perror(ENSC_WRAPPERS_PREFIX "clone()");
128       exit(wrapper_exit_code);
129     case 0      :
130       break;
131     default     :
132       exitLikeProcess(pid, cmd, wrapper_exit_code);
133   }
134 }
135
136 static void
137 enterSpaces(xid_t xid, uint_least64_t mask)
138 {
139   if (vc_enter_namespace(xid, mask)==-1) {
140     perror(ENSC_WRAPPERS_PREFIX "vc_enter_namespace()");
141     exit(wrapper_exit_code);
142   }
143 }
144
145 static void
146 setSpaces(xid_t xid, uint_least64_t mask)
147 {
148   if (vc_set_namespace(xid, mask)==-1) {
149     perror(ENSC_WRAPPERS_PREFIX "vc_set_namespace()");
150     exit(wrapper_exit_code);
151   }
152 }
153
154 int main(int argc, char *argv[])
155 {
156   bool                  do_new     = false;
157   bool                  do_enter   = false;
158   bool                  do_set     = false;
159   uint_least64_t        mask       = 0;
160   xid_t                 xid        = VC_NOCTX;
161   int                   sum        = 0;
162   
163   while (1) {
164     int         c = getopt_long(argc, argv, "+nsce:m:" "MFIUSPN", CMDLINE_OPTIONS, 0);
165     if (c==-1) break;
166
167     switch (c) {
168       case CMD_HELP     :  showHelp(1, argv[0], 0);
169       case CMD_VERSION  :  showVersion();
170       case 'n'          :  do_new     = true; break;
171       case 's'          :  do_set     = true; break;
172       case 'e'          :
173         do_enter = true;
174         xid      = Evc_xidopt2xid(optarg,true);
175         break;
176       case 'm'          :  {
177         unsigned long   mask_l;
178         if (!isNumberUnsigned(optarg, &mask_l, true)) {
179           WRITE_MSG(2, "Invalid mask '");
180           WRITE_STR(2, optarg);
181           WRITE_MSG(2, "'; try '--help' for more information\n");
182           return wrapper_exit_code;
183         }
184         mask = mask_l;
185         break;
186       }
187       case 'M'          :  mask |= CLONE_NEWNS;         break;
188       case 'F'          :  mask |= CLONE_FS;            break;
189       case 'I'          :  mask |= CLONE_NEWIPC;        break;
190       case 'U'          :  mask |= CLONE_NEWUTS;        break;
191       case 'S'          :  mask |= CLONE_NEWUSER;       break;
192       case 'P'          :  mask |= CLONE_NEWPID;        break;
193       case 'N'          :  mask |= CLONE_NEWNET;        break;
194
195       default           :
196         WRITE_MSG(2, "Try '");
197         WRITE_STR(2, argv[0]);
198         WRITE_MSG(2, " --help' for more information.\n");
199         return 255;
200         break;
201     }
202   }
203
204   sum = ((do_new ? 1 : 0) + (do_enter ? 1 : 0) +
205          (do_set ? 1 : 0));
206   
207   if (sum==0)
208     WRITE_MSG(2, "No operation was specified; try '--help' for more information\n");
209   else if (sum>1)
210     WRITE_MSG(2, "Can not specify multiple operations; try '--help' for more information\n");
211   else if (optind==argc && (do_new || do_enter))
212     WRITE_MSG(2, "No command specified; try '--help' for more information\n");
213   else {
214     if      (do_new)     newSpaces(mask, argv[optind]);
215     else if (do_set)     setSpaces(VC_SAMECTX, mask);
216     else if (do_enter)   enterSpaces(xid, mask);
217
218     if (optind<argc)
219       EexecvpD(argv[optind], argv+optind);
220
221     return EXIT_SUCCESS;
222   }
223
224   return 255;
225 }