fixed copyright date
[util-vserver.git] / util-vserver / src / vrsetup.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 "wrappers.h"
24 #include "util.h"
25
26 #include <getopt.h>
27 #include <stdlib.h>
28
29 int wrapper_exit_code = 1;
30
31
32 #define VROOT_SET_DEV           0x5600
33 #define VROOT_CLR_DEV           0x5601
34 #define VROOT_INC_USE           0x56FE
35 #define VROOT_DEC_USE           0x56FF
36
37 struct option const
38 CMDLINE_OPTIONS[] = {
39   { "help",     no_argument,  0, 'h' },
40   { "version",  no_argument,  0, 'v' },
41   { 0,0,0,0 }
42 };
43
44 static void
45 showHelp(int fd, char const *cmd, int res)
46 {
47   WRITE_MSG(fd, "Usage:  ");
48   WRITE_STR(fd, cmd);
49   WRITE_MSG(fd,
50             " [-dID] <rootdev>\n"
51             "    or  ");
52   WRITE_STR(fd, cmd);
53   WRITE_MSG(fd,
54             " <rootdev> <real-rootdev>\n\n"
55             "Please report bugs to " PACKAGE_BUGREPORT "\n");
56   exit(res);
57 }
58
59 static void
60 showVersion()
61 {
62   WRITE_MSG(1,
63             "vrsetup " VERSION " -- set up and control vroot devices\n"
64             "This program is part of " PACKAGE_STRING "\n\n"
65             "Copyright (C) 2004 Enrico Scholz\n"
66             VERSION_COPYRIGHT_DISCLAIMER);
67   exit(0);
68 }
69
70
71 int main(int argc, char *argv[])
72 {
73   bool          do_delete    = false;
74   bool          do_decrement = false;
75   bool          do_increment = false;
76   bool          do_setup     = false;
77   char const *  root_device      = 0;
78   char const *  real_root_device = 0;
79   int           fd;
80   
81   while (1) {
82     int         c = getopt_long(argc, argv, "dID", CMDLINE_OPTIONS, 0);
83     if (c==-1) break;
84
85     switch (c) {
86       case 'h'          :  showHelp(1, argv[0], 0);
87       case 'v'          :  showVersion();
88       case 'd'          :  do_delete    = true; break;
89       case 'D'          :  do_decrement = true; break;
90       case 'I'          :  do_increment = true; break;
91       default           :
92         WRITE_MSG(2, "Try '");
93         WRITE_STR(2, argv[0]);
94         WRITE_MSG(2, " --help\" for more information.\n");
95         return EXIT_FAILURE;
96         break;
97     }
98   }
99
100   do_setup = !(do_delete || do_decrement || do_increment);
101
102   if (optind+1>argc) {
103     WRITE_MSG(2, "No vroot-device given\n");
104     return EXIT_FAILURE;
105   }
106   
107   if (do_setup && optind+2>argc) {
108     WRITE_MSG(2, "No real root-device given\n");
109     return EXIT_FAILURE;
110   }
111         
112   root_device = argv[optind];
113   if (do_setup) real_root_device = argv[optind+1];
114
115   fd = Eopen(root_device, O_RDONLY, 0);
116   if      (do_increment) Eioctl(fd, VROOT_INC_USE, 0);
117   else if (do_decrement) Eioctl(fd, VROOT_DEC_USE, 0);
118   else if (do_delete)    Eioctl(fd, VROOT_CLR_DEV, 0);
119   else {
120     int         dfd = Eopen(real_root_device, O_RDONLY, 0);
121     Eioctl(fd, VROOT_SET_DEV, (void*)dfd);
122     Eclose(dfd);
123   }
124
125   Eclose(fd);
126   return EXIT_SUCCESS;
127 }