3 // Copyright (C) 2003 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.
24 #include "linuxvirtual.h"
34 #include <sys/types.h>
35 #include <sys/resource.h>
38 #define ENSC_WRAPPERS_VSERVER 1
39 #define ENSC_WRAPPERS_UNISTD 1
42 #define CMD_HELP 0x8000
43 #define CMD_VERSION 0x8001
45 int wrapper_exit_code = 1;
47 static struct option const
49 { "help", no_argument, 0, CMD_HELP },
50 { "version", no_argument, 0, CMD_VERSION },
51 { "xid", required_argument, 0, 'c' },
61 static char const * const SIGNALS[] = {
63 "UNUSED", "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "UNUSED",
64 "FPE", "KILL", "USR1", "SEGV", "USR2", "PIPE", "ALRM", "TERM",
65 "STKFLT", "CHLD", "CONT", "STOP", "TSTP", "TTIN", "TTOU", "IO",
66 "XCPU", "XFSZ", "VTALRM", "PROF", "WINCH",
71 showHelp(int fd, char const *cmd, int res)
73 WRITE_MSG(fd, "Usage: ");
76 " [--xid|-c <xid>] [-s <signal>] [--] <pid>*\n"
77 "Please report bugs to " PACKAGE_BUGREPORT "\n");
85 "vkill " VERSION " -- sends signals to processes within other contexts\n"
86 "This program is part of " PACKAGE_STRING "\n\n"
87 "Copyright (C) 2003 Enrico Scholz\n"
88 VERSION_COPYRIGHT_DISCLAIMER);
93 str2sig(char const *str)
96 int res = strtol(str, &errptr, 10);
98 if (*errptr!='\0') res=-1;
99 if (res==-1 && strncmp(str,"SIG",3)==0) str+=3;
101 char const * const *ptr = SIGNALS;
102 for (;*ptr!=0; ++ptr) {
103 if (strcmp(*ptr,str)!=0) continue;
112 #if defined(VC_ENABLE_API_LEGACY)
113 inline static ALWAYSINLINE int
114 kill_wrapper_legacy(xid_t UNUSED xid, char const *proc, int UNUSED sig)
118 signal(SIGCHLD, SIG_DFL);
124 while ((res=wait4(pid, &status, 0,0))==-1 &&
125 (errno==EAGAIN || errno==EINTR)) {}
127 return (res==0 && WIFEXITED(status) && WEXITSTATUS(status)) ? 0 : 1;
130 execl(LEGACYDIR "/vkill", "legacy/vkill", proc, (void *)(0));
131 perror("vkill: execl()");
136 kill_wrapper(xid_t xid, char const *pid, int sig)
138 //printf("kill_wrapper(%u, %s, %i)\n", xid, pid, sig);
139 if (vc_ctx_kill(xid,atoi(pid),sig)==-1) {
141 if (vc_get_version(VC_CAT_COMPAT)==-1)
142 return kill_wrapper_legacy(xid, pid, sig);
145 perror("vkill: vc_ctx_kill()");
152 #else // VC_ENABLE_API_LEGACY
154 kill_wrapper(xid_t xid, char const *pid, int sig)
156 if (vc_ctx_kill(xid,atoi(pid),sig)==-1) {
157 perror("vkill: vc_ctx_kill()");
165 int main(int argc, char *argv[])
168 struct Arguments args = {
174 int c = getopt_long(argc, argv, "c:s:", CMDLINE_OPTIONS, 0);
178 case CMD_HELP : showHelp(1, argv[0], 0);
179 case CMD_VERSION : showVersion();
180 case 'c' : args.xid = Evc_xidopt2xid(optarg,true); break;
181 case 's' : args.sig = str2sig(optarg); break;
183 WRITE_MSG(2, "Try '");
184 WRITE_STR(2, argv[0]);
185 WRITE_MSG(2, " --help\" for more information.\n");
192 WRITE_MSG(2, "Invalid signal specified\n");
196 if (args.xid==VC_NOCTX && optind==argc) {
197 WRITE_MSG(2, "No pid specified\n");
202 fail += kill_wrapper(args.xid, "0", args.sig);
203 else for (;optind<argc;++optind)
204 fail += kill_wrapper(args.xid, argv[optind], args.sig);
206 return fail==0 ? EXIT_SUCCESS : EXIT_FAILURE;
213 assert(str2sig("0") ==0 );
214 assert(str2sig("1") ==1 );
215 assert(str2sig("10")==10);
216 assert(str2sig("SIGHUP")==1);
217 assert(str2sig("HUP") ==1);
218 assert(str2sig("SIGCHLD")==17);
219 assert(str2sig("CHLD") ==17);
220 assert(str2sig("x")==-1);
221 assert(str2sig("1 0")==-1);