initial checkin
[util-vserver.git] / util-vserver / src / vnet.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "util.h"
24 #include <vserver.h>
25 #include <getopt.h>
26 #include <errno.h>
27 #include <assert.h>
28
29 #define ENSC_WRAPPERS_PREFIX    "vcontext: "
30 #include <wrappers.h>
31
32 #define CMD_HELP                0x1000
33 #define CMD_VERSION             0x1001
34 #define CMD_NID                 0x4000
35 #define CMD_CREATE              0x4001
36 #define CMD_MIGRATE             0x4003
37 #define CMD_SILENT              0x4007
38 #define CMD_SILENTEXIST         0x400c
39
40 struct option const
41 CMDLINE_OPTIONS[] = {
42   { "help",         no_argument,       0, CMD_HELP },
43   { "version",      no_argument,       0, CMD_VERSION },
44   { "nid",          required_argument, 0, CMD_NID },
45   { "create",       no_argument,       0, CMD_CREATE },
46   { "migrate",      required_argument, 0, CMD_MIGRATE },
47   { "silent",       no_argument,       0, CMD_SILENT },
48   { "silentexist",  no_argument,       0, CMD_SILENTEXIST },
49   { 0,0,0,0 }
50 };
51   
52 struct Arguments {
53     bool                do_create;
54     bool                do_migrate;
55     bool                is_silentexist;
56     int                 verbosity;
57     nid_t               nid;
58 };
59
60 int             wrapper_exit_code = 255;
61
62 static void
63 showHelp(char const *cmd)
64 {
65   WRITE_MSG(1, "Usage:\n    ");
66   WRITE_STR(1, cmd);
67   WRITE_MSG(1,
68             " --create  <opts>* [--] <program> <args>*\n    ");
69   WRITE_STR(1, cmd);
70   WRITE_MSG(1,
71             " --migrate <opts>* [--] <program> <args>*\n");
72   WRITE_STR(1, cmd);
73   WRITE_MSG(1,
74             " --add|--del <ip>/[<prefix>|<mask>] <opts>* [--] <program> <args>*\n");
75   WRITE_STR(1, cmd);
76   WRITE_MSG(1,
77             " (--flag <flags>*)|(--caps <caps>*) <opts>* [--] <program> <args>*\n"
78             "\n"
79             "<opts> can be:\n"
80             "    --nid <nid>     ...  operate on network context <nid>; in combination\n"
81             "                         with '--create' the network context <nid> instead of\n"
82             "                         a dynamic one will be created, and with '--migrate\n"
83             "                         the context <nid> instead of the current one will\n"
84             "                         be entered\n"
85             "    --silent        ...  be silent\n"
86             "    --silentexist   ...  be silent when context exists already; usefully\n"
87             "                         for '--create' only\n"
88             "\n"
89             "'vnet --create' exits with code 254 iff the context exists already.\n"
90             "\n"
91             "Please report bugs to " PACKAGE_BUGREPORT "\n");
92
93   exit(0);
94 }
95
96
97 static void
98 showVersion()
99 {
100   WRITE_MSG(1,
101             "vnet " VERSION " -- manages the creation of network contexts\n"
102             "This program is part of " PACKAGE_STRING "\n\n"
103             "Copyright (C) 2004 Enrico Scholz\n"
104             VERSION_COPYRIGHT_DISCLAIMER);
105   exit(0);
106 }
107
108 static inline ALWAYSINLINE int
109 doit(struct Arguments const *args, char *argv[])
110 {
111   nid_t         nid;
112   
113   if (args->do_create) {
114     nid = vc_net_create_context(args->nid);
115     if (nid==VC_NONID) {
116       switch (errno) {
117         case EEXIST     :
118           if (!args->is_silentexist)
119             perror(ENSC_WRAPPERS_PREFIX "vc_net_create_context()");
120           return 254;
121         default :
122           perror(ENSC_WRAPPERS_PREFIX "vc_net_create_context()");
123           return wrapper_exit_code;
124       }
125     }
126     tellContext(nid, args->verbosity>=1);
127   }
128   else
129     nid = args->nid;
130
131   
132 }
133   
134   
135 int main (int argc, char *argv[])
136 {
137   struct Arguments              args = {
138     .do_create      = false,
139     .do_migrate     = false,
140     .is_silentexist = false,
141     .verbosity      = 1,
142     .nid            = VC_DYNAMIC_NID,
143   };
144   
145   while (1) {
146     int         c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
147     if (c==-1) break;
148     
149     switch (c) {
150       case CMD_HELP             :  showHelp(1, argv[0], 0);
151       case CMD_VERSION          :  showVersion();
152       case CMD_NID              :  args.nid            = optarg(nid); break;
153       case CMD_CREATE           :  args.do_create      = true; break;
154       case CMD_MIGRATE          :  args.do_migrate     = true; break;
155       case CMD_SILENTEXIST      :  args.is_silentexist = true; break;
156       case CMD_SILENT           :  --args.verbosity; break;
157
158       default           :
159         WRITE_MSG(2, "Try '");
160         WRITE_STR(2, argv[0]);
161         WRITE_MSG(2, " --help\" for more information.\n");
162         return 255;
163         break;
164     }
165   }
166
167   if (args.nid==VC_DYNAMIC_NID && args.do_migrate)
168     args.nid = Evc_get_task_nid(0);
169   
170   if (!args.do_create && !args.do_migrate)
171     WRITE_MSG(2, "Neither '--create' nor '--migrate specified; try '--help' for more information\n");
172   else if (args.do_create  &&  args.do_migrate)
173     WRITE_MSG(2, "Can not specify '--create' and '--migrate' at the same time; try '--help' for more information\n");
174   else if (!args.do_create && args.nid==VC_DYNAMIC_NID)
175     WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Can not migrate to an unknown context\n");
176   else if (optind>=argc)
177     WRITE_MSG(2, "No command given; use '--help' for more information.\n");
178   else
179     return doit(&args, argv);
180
181   return 255;
182 }