1 // $Id: vwait.c,v 1.1 2005/05/05 09:22:03 ensc Exp $ --*- c -*--
3 // Copyright (C) 2005 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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.
23 #include "lib/vserver.h"
24 #include "lib/internal.h"
25 #include "linuxvirtual.h"
34 #define ENSC_WRAPPERS_PREFIX "vwait: "
35 #define ENSC_WRAPPERS_STDLIB 1
36 #define ENSC_WRAPPERS_UNISTD 1
37 #define ENSC_WRAPPERS_VSERVER 1
40 #define CMD_HELP 0x8000
41 #define CMD_VERSION 0x8001
43 #define CMD_TIMEOUT 0x4000
44 #define CMD_TERMINATE 0x4001
45 #define CMD_STATUS_FD 0x4002
47 static struct option const
49 { "help", no_argument, 0, CMD_HELP },
50 { "version", no_argument, 0, CMD_VERSION },
51 { "timeout", required_argument, 0, CMD_TIMEOUT },
52 { "terminate", no_argument, 0, CMD_TERMINATE },
53 { "status-fd", required_argument, 0, CMD_STATUS_FD },
57 int wrapper_exit_code = 1;
58 static sig_atomic_t aborted = 0;
61 enum {stERROR, stFINISHED, stKILLED,
75 showHelp(char const *cmd)
77 VSERVER_DECLARE_CMD(cmd);
79 WRITE_MSG(1, "Usage: ");
82 " [--timeout <timeout>] [--terminate] [--status-fd <fd>] [--] <xid>\n"
84 "Please report bugs to " PACKAGE_BUGREPORT "\n");
92 "vwait " VERSION " -- waits for a context to finish\n"
93 "This program is part of " PACKAGE_STRING "\n\n"
94 "Copyright (C) 2005 Enrico Scholz\n"
95 VERSION_COPYRIGHT_DISCLAIMER);
100 handler(int UNUSED num)
105 static struct StatusType
106 doit(struct Arguments const *args)
109 struct StatusType res;
111 if (args->timeout>0) {
112 end_time = time(0) + args->timeout;
113 signal(SIGALRM, handler);
114 alarm(args->timeout);
118 res.rc = vc_wait_exit(args->xid);
120 if (res.rc==-1 && errno!=EAGAIN && errno!=EINTR) {
122 res.status = stERROR;
123 perror(ENSC_WRAPPERS_PREFIX "vc_wait_exit()");
125 else if (res.rc==-1 && args->timeout>0 && time(0)>=end_time) {
126 if (!args->do_terminate)
127 res.status = stTIMEOUT;
129 vc_ctx_kill(args->xid, 1, 9);
130 vc_ctx_kill(args->xid, 0, 9);
131 res.status = stKILLED;
137 res.status = stFINISHED;
147 writeStatus(int fd, char const *str, int const *rc, int exit_code)
149 if (fd==-1) exit(exit_code);
153 char buf[sizeof(*rc)*3 + 2];
154 size_t len = utilvserver_fmt_long(buf, *rc);
156 Vwrite (fd, buf, len);
163 int main(int argc, char *argv[])
165 struct StatusType res;
166 struct Arguments args = {
170 .do_terminate = false,
174 int c = getopt_long(argc, argv, "c:", CMDLINE_OPTIONS, 0);
178 case CMD_HELP : showHelp(argv[0]);
179 case CMD_VERSION : showVersion();
180 case CMD_TERMINATE : args.do_terminate = true; break;
181 case CMD_TIMEOUT : args.timeout = atoi(optarg); break;
182 case CMD_STATUS_FD : args.status_fd = atoi(optarg); break;
184 WRITE_MSG(2, "Try '");
185 WRITE_STR(2, argv[0]);
186 WRITE_MSG(2, " --help\" for more information.\n");
192 if (optind+1 > argc) {
193 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "no context specified; try '--help' for more information\n");
194 exit(wrapper_exit_code);
197 if (optind+1 < argc) {
198 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "can not wait for more than one context; try '--help' for more information\n");
199 exit(wrapper_exit_code);
202 args.xid = Evc_xidopt2xid(argv[optind], true);
204 if (args.xid==VC_NOCTX) {
205 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "invalid context specified; try '--help' for more information\n");
206 exit(wrapper_exit_code);
211 switch (res.status) {
212 case stERROR : writeStatus(args.status_fd, "ERROR", &res.rc, 127);
213 case stFINISHED : writeStatus(args.status_fd, "FINISHED", &res.rc, 0);
214 case stKILLED : writeStatus(args.status_fd, "KILLED", 0, 1);
215 case stTIMEOUT : writeStatus(args.status_fd, "TIMEOUT", 0, 2);
216 default : writeStatus(args.status_fd, "???", &res.rc, 126);