Tagging module util-vserver - util-vserver-0.30.215-6
[util-vserver.git] / src / vdevmap.c
1 // $Id: vdevmap.c 2703 2008-03-15 16:05:49Z dhozac $    --*- c -*--
2
3 // Copyright (C) 2006 Daniel Hokka Zakrisson
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 <lib/internal.h>
25
26 #include <vserver.h>
27
28 #include <getopt.h>
29 #include <errno.h>
30
31 #define ENSC_WRAPPERS_PREFIX    "vdevmap: "
32 #define ENSC_WRAPPERS_UNISTD    1
33 #define ENSC_WRAPPERS_VSERVER   1
34 #include <wrappers.h>
35
36 #define CMD_HELP                0x1000
37 #define CMD_VERSION             0x1001
38
39 int             wrapper_exit_code  =  1;
40
41 struct option const
42 CMDLINE_OPTIONS[] = {
43   { "help",       no_argument,       0, CMD_HELP },
44   { "version",    no_argument,       0, CMD_VERSION },
45   { "xid",        required_argument, 0, 'x' },
46   { "set",        no_argument,       0, 's' },
47   { "unset",      no_argument,       0, 'u' },
48   { "open",       no_argument,       0, 'o' },
49   { "create",     no_argument,       0, 'c' },
50   { "remap",      no_argument,       0, 'r' },
51   { "flags",      required_argument, 0, 'f' },
52   { "device",     required_argument, 0, 'd' },
53   { "target",     required_argument, 0, 't' },
54   {0,0,0,0}
55 };
56
57 static void
58 showHelp(int fd, char const *cmd)
59 {
60   WRITE_MSG(fd, "Usage: ");
61   WRITE_STR(fd, cmd);
62   WRITE_MSG(fd,
63             " --xid <xid> {--set|--unset} [--flags <flags>] [--open] [--create]\n"
64             "        [--device <dev>] [--remap --target <dev>]\n"
65             "\n"
66             "    --flags <flags>     Set the specified flags\n"
67             "    --open              Allow opening of the device\n"
68             "    --create            If CAP_MKNOD is given, allow mknod(2)\n"
69             "    --device <dev>      Device to apply the command to\n"
70             "    --remap             Remap the device to the target\n"
71             "    --target <dev>      Target for --remap\n"
72             "\n"
73             "EXAMPLES\n"
74             "  Remap /dev/hda1 to /dev/vroot1 for xid 42\n"
75             "    vdevmap --xid 42 --set --open --device /dev/hda1 --target /dev/vroot1 --remap\n"
76             "  Let xid 42 create all device nodes\n"
77             "    vdevmap --xid 42 --set --open --create --target /dev/null\n"
78             "    vdevmap --xid 42 --set --open --create --target /dev/root\n"
79             "  Let xid 43 create just /dev/null\n"
80             "    vdevmap --xid 43 --set --open --create --device /dev/null\n"
81             "\n"
82             "Please report bugs to " PACKAGE_BUGREPORT "\n");
83
84   exit(0);
85 }
86
87 static void
88 showVersion()
89 {
90   WRITE_MSG(1,
91             "vdevmap " VERSION " -- manages device mappings\n"
92             "This program is part of " PACKAGE_STRING "\n\n"
93             "Copyright (C) 2006 Daniel Hokka Zakrisson\n"
94             VERSION_COPYRIGHT_DISCLAIMER);
95   exit(0);
96 }
97
98 int main(int argc, char *argv[])
99 {
100   xid_t         xid             = VC_NOCTX;
101   bool          allow_open      = false;
102   bool          allow_create    = false;
103   bool          do_remap        = false;
104   uint32_t      flags           = 0;
105   char          *device         = NULL;
106   char          *target         = NULL;
107   bool          set             = true;
108   unsigned long tmp             = 0;
109   
110   while (1) {
111     int         c = getopt_long(argc, argv, "+x:suocrf:d:t:", CMDLINE_OPTIONS, 0);
112     if (c==-1) break;
113
114     switch (c) {
115       case CMD_HELP     :  showHelp(1, argv[0]);
116       case CMD_VERSION  :  showVersion();
117       case 'x'          :  xid = Evc_xidopt2xid(optarg, true);  break;
118       case 'o'          :  allow_open = true;                   break;
119       case 'c'          :  allow_create = true;                 break;
120       case 'r'          :  do_remap = true;                     break;
121       case 'd'          :  device = optarg;                     break;
122       case 't'          :  target = optarg;                     break;
123       case 's'          :  set = 1;                             break;
124       case 'u'          :  set = 0;                             break;
125       case 'f'          :
126         if (!isNumberUnsigned(optarg, &tmp, false)) {
127           WRITE_MSG(2, "Invalid flags argument: '");
128           WRITE_STR(2, optarg);
129           WRITE_MSG(2, "'; try '--help' for more information\n");
130           return EXIT_FAILURE;
131         }
132         flags |= (uint32_t) tmp;
133         break;
134
135       default           :
136         WRITE_MSG(2, "Try '");
137         WRITE_STR(2, argv[0]);
138         WRITE_MSG(2, " --help' for more information.\n");
139         return EXIT_FAILURE;
140         break;
141     }
142   }
143
144   if (allow_open)       flags |= VC_DATTR_OPEN;
145   if (allow_create)     flags |= VC_DATTR_CREATE;
146   if (do_remap)         flags |= VC_DATTR_REMAP;
147
148   if (!target && do_remap)
149     WRITE_MSG(2, "Remapping specified without a target; try '--help' for more information\n");
150   else if (xid==VC_NOCTX)
151     WRITE_MSG(2, "No xid specified; try '--help' for more information\n");
152   else if (optind!=argc)
153     WRITE_MSG(2, "Unused argument(s); try '--help' for more information\n");
154   else if (!device && !target)
155     WRITE_MSG(2, "Device and target are missing; try '--help' for more information\n");
156   else if (set && vc_set_mapping(xid, device, target, flags)==-1)
157       perror("vc_set_mapping()");
158   else if (!set && vc_unset_mapping(xid, device, target, flags)==-1)
159       perror("vc_unset_mapping()");
160   else
161     return EXIT_SUCCESS;
162
163   return EXIT_FAILURE;
164 }