ready for tagging
[util-vserver.git] / src / vtag.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2007 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
25 #include <vserver.h>
26 #include <getopt.h>
27 #include <errno.h>
28 #include <sys/types.h>
29
30
31 #define ENSC_WRAPPERS_PREFIX    "vtag: "
32 #define ENSC_WRAPPERS_VSERVER   1
33 #include <wrappers.h>
34
35 #define CMD_HELP                0x1000
36 #define CMD_VERSION             0x1001
37 #define CMD_TAG                 0x4000
38 #define CMD_CREATE              0x4001
39 #define CMD_MIGRATE             0x4002
40 #define CMD_SILENTEXIST         0x4003
41 #define CMD_SILENT              0x4004
42
43
44 struct option const
45 CMDLINE_OPTIONS[] = {
46   { "help",         no_argument,       0, CMD_HELP },
47   { "version",      no_argument,       0, CMD_VERSION },
48   { "tag",          required_argument, 0, CMD_TAG },
49   { "create",       no_argument,       0, CMD_CREATE },
50   { "migrate",      no_argument,       0, CMD_MIGRATE },
51   { "silent",       no_argument,       0, CMD_SILENT },
52   { "silentexist",  no_argument,       0, CMD_SILENTEXIST },
53   { 0,0,0,0 },
54 };
55
56 struct Arguments {
57     bool                do_create;
58     bool                do_migrate;
59     bool                is_silentexist;
60     int                 verbosity;
61     tag_t               tag;
62 };
63
64 int             wrapper_exit_code = 255;
65
66 static void
67 showHelp(int fd, char const *cmd, int res)
68 {
69   WRITE_MSG(fd, "Usage:\n    ");
70   WRITE_STR(fd, cmd);
71   WRITE_MSG(fd,
72             " (--create|--migrate) --tag <tag> <opts>* [--] <program> <args>*\n    "
73             "\n"
74             "<opts> can be:\n"
75             "    --silentexist   ...  be silent when context exists already; useful\n"
76             "                         for '--create' only\n"
77             "    --silent        ...  if the feature is not supported, just execute\n"
78             "                         <program>\n"
79             "\n"
80             "'vtag --create' exits with code 254 iff the context exists already.\n"
81             "\n"
82             "Please report bugs to " PACKAGE_BUGREPORT "\n");
83
84   exit(res);
85 }
86
87 static void
88 showVersion()
89 {
90   WRITE_MSG(1,
91             "vtag " VERSION " -- sets the process's filesystem tag\n"
92             "This program is part of " PACKAGE_STRING "\n\n"
93             "Copyright (C) 2007 Daniel Hokka Zakrisson\n"
94             VERSION_COPYRIGHT_DISCLAIMER);
95   exit(0);
96 }
97
98 static inline ALWAYSINLINE int
99 doit(struct Arguments const *args, char *argv[])
100 {
101   tag_t                 tag;
102
103   if (!vc_isSupported(vcFEATURE_PPTAG)) {
104     if (args->verbosity >= 1) {
105       errno = ENOSYS;
106       perror(ENSC_WRAPPERS_PREFIX);
107       return wrapper_exit_code;
108     }
109     else
110       goto exec;
111   }
112
113   if (args->do_create) {
114     tag = vc_tag_create(args->tag);
115     if (tag==VC_NOCTX) {
116       switch (errno) {
117         case EEXIST     :
118           if (!args->is_silentexist)
119             perror(ENSC_WRAPPERS_PREFIX "vc_tag_create()");
120           return 254;
121         default         :
122           perror(ENSC_WRAPPERS_PREFIX "vc_tag_create()");
123           return wrapper_exit_code;
124       }
125     }
126   }
127   else
128     tag = args->tag;
129
130   if (args->do_migrate)
131     Evc_tag_migrate(tag);
132
133 exec:
134   execvp(argv[optind], argv+optind);
135
136   PERROR_Q(ENSC_WRAPPERS_PREFIX "execvp", argv[optind]);
137   return wrapper_exit_code;
138 }
139
140 int main (int argc, char *argv[])
141 {
142   struct Arguments              args = {
143     .tag               = VC_NOCTX,
144     .do_create         = false,
145     .do_migrate        = false,
146     .is_silentexist    = false,
147     .verbosity         = 1,
148   };
149
150   while (1) {
151     int         c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
152     if (c==-1) break;
153
154     switch (c) {
155       case CMD_HELP             :  showHelp(1, argv[0], 0);
156       case CMD_VERSION          :  showVersion();
157       case CMD_CREATE           :  args.do_create      = true;   break;
158       case CMD_MIGRATE          :  args.do_migrate     = true;   break;
159       case CMD_SILENTEXIST      :  args.is_silentexist = true;   break;
160       case CMD_TAG              :  args.tag = Evc_tagopt2tag(optarg,true); break;
161       case CMD_SILENT           :  args.verbosity--;             break;
162
163       default           :
164         WRITE_MSG(2, "Try '");
165         WRITE_STR(2, argv[0]);
166         WRITE_MSG(2, " --help' for more information.\n");
167         return wrapper_exit_code;
168         break;
169     }
170   }
171
172   if (!args.do_create && !args.do_migrate)
173     WRITE_MSG(2, "Neither '--create' nor '--migrate' specified; try '--help' for more information\n");
174   else if (args.do_create && args.do_migrate)
175     WRITE_MSG(2, "Can not specify '--create' and '--migrate' at the same time; try '--help' for more information\n");
176   else if (args.tag==VC_NOCTX)
177     WRITE_MSG(2, "You must specify the tag with '--tag'; try '--help' for more information\n");
178   else if (optind>=argc)
179     WRITE_MSG(2, "No command given; use '--help' for more information.\n");
180   else
181     return doit(&args, argv);
182
183   return wrapper_exit_code;
184 }