3 // Copyright (C) 2007 Daniel Hokka Zakrisson
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.
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.
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.
28 #include <sys/types.h>
31 #define ENSC_WRAPPERS_PREFIX "vtag: "
32 #define ENSC_WRAPPERS_VSERVER 1
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
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 },
64 int wrapper_exit_code = 255;
67 showHelp(int fd, char const *cmd, int res)
69 WRITE_MSG(fd, "Usage:\n ");
72 " (--create|--migrate) --tag <tag> <opts>* [--] <program> <args>*\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"
80 "'vtag --create' exits with code 254 iff the context exists already.\n"
82 "Please report bugs to " PACKAGE_BUGREPORT "\n");
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);
98 static inline ALWAYSINLINE int
99 doit(struct Arguments const *args, char *argv[])
103 if (!vc_isSupported(vcFEATURE_PPTAG)) {
104 if (args->verbosity >= 1) {
106 perror(ENSC_WRAPPERS_PREFIX);
107 return wrapper_exit_code;
112 if (args->tag == VC_DYNAMIC_XID) {
113 if (args->verbosity >= 1) {
114 WRITE_MSG(2, "You must specify the tag with '--tag'; try '--help' for more information\n");
115 return wrapper_exit_code;
118 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "WARNING: A dynamic tag has been specified, this is not supported\n");
123 if (args->do_create) {
124 tag = vc_tag_create(args->tag);
128 if (!args->is_silentexist)
129 perror(ENSC_WRAPPERS_PREFIX "vc_tag_create()");
132 perror(ENSC_WRAPPERS_PREFIX "vc_tag_create()");
133 return wrapper_exit_code;
140 if (args->do_migrate)
141 Evc_tag_migrate(tag);
144 execvp(argv[optind], argv+optind);
146 PERROR_Q(ENSC_WRAPPERS_PREFIX "execvp", argv[optind]);
147 return wrapper_exit_code;
150 int main (int argc, char *argv[])
152 struct Arguments args = {
153 .tag = VC_DYNAMIC_XID,
156 .is_silentexist = false,
161 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
165 case CMD_HELP : showHelp(1, argv[0], 0);
166 case CMD_VERSION : showVersion();
167 case CMD_CREATE : args.do_create = true; break;
168 case CMD_MIGRATE : args.do_migrate = true; break;
169 case CMD_SILENTEXIST : args.is_silentexist = true; break;
170 case CMD_TAG : args.tag = Evc_tagopt2tag(optarg,true); break;
171 case CMD_SILENT : args.verbosity--; break;
174 WRITE_MSG(2, "Try '");
175 WRITE_STR(2, argv[0]);
176 WRITE_MSG(2, " --help' for more information.\n");
177 return wrapper_exit_code;
182 if (!args.do_create && !args.do_migrate)
183 WRITE_MSG(2, "Neither '--create' nor '--migrate' specified; try '--help' for more information\n");
184 else if (args.do_create && args.do_migrate)
185 WRITE_MSG(2, "Can not specify '--create' and '--migrate' at the same time; try '--help' for more information\n");
186 else if (optind>=argc)
187 WRITE_MSG(2, "No command given; use '--help' for more information.\n");
189 return doit(&args, argv);
191 return wrapper_exit_code;