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 CMD_HELP 0x8000
39 #define CMD_VERSION 0x8001
41 static struct option const
43 { "help", no_argument, 0, CMD_HELP },
44 { "version", no_argument, 0, CMD_VERSION },
54 static char const * const SIGNALS[] = {
56 "UNUSED", "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "UNUSED",
57 "FPE", "KILL", "USR1", "SEGV", "USR2", "PIPE", "ALRM", "TERM",
58 "STKFLT", "CHLD", "CONT", "STOP", "TSTP", "TTIN", "TTOU", "IO",
59 "XCPU", "XFSZ", "VTALRM", "PROF", "WINCH",
64 showHelp(int fd, char const *cmd, int res)
66 WRITE_MSG(fd, "Usage: ");
69 " [-c <ctx>] [-s <signal>] [--] <pid>*\n"
70 "Please report bugs to " PACKAGE_BUGREPORT "\n");
78 "vkill " VERSION " -- sends signals to processes within other contexts\n"
79 "This program is part of " PACKAGE_STRING "\n\n"
80 "Copyright (C) 2003 Enrico Scholz\n"
81 VERSION_COPYRIGHT_DISCLAIMER);
86 str2sig(char const *str)
89 int res = strtol(str, &errptr, 10);
91 if (*errptr!='\0') res=-1;
92 if (res==-1 && strncmp(str,"SIG",3)==0) str+=3;
94 char const * const *ptr = SIGNALS;
95 for (;*ptr!=0; ++ptr) {
96 if (strcmp(*ptr,str)!=0) continue;
105 #if defined(VC_ENABLE_API_LEGACY)
106 inline static ALWAYSINLINE int
107 kill_wrapper_legacy(xid_t UNUSED ctx, char const *proc, int UNUSED sig)
111 perror("vkill: fork()");
117 while ((res=wait4(pid, &status, 0,0))==-1 &&
118 (errno==EAGAIN || errno==EINTR)) {}
120 return (res==0 && WIFEXITED(status) && WEXITSTATUS(status)) ? 0 : 1;
123 execl(LEGACYDIR "/vkill", "legacy/vkill", proc, (void *)(0));
124 perror("vkill: execl()");
129 kill_wrapper(xid_t ctx, char const *pid, int sig)
131 //printf("kill_wrapper(%u, %s, %i)\n", ctx, pid, sig);
132 if (vc_ctx_kill(ctx,atoi(pid),sig)==-1) {
134 if (vc_get_version(VC_CAT_COMPAT)==-1)
135 return kill_wrapper_legacy(ctx, pid, sig);
138 perror("vkill: vc_ctx_kill()");
145 #else // VC_ENABLE_API_LEGACY
147 kill_wrapper(xid_t ctx, char const *pid, int sig)
149 if (vc_ctx_kill(ctx,atoi(pid),sig)==-1) {
150 perror("vkill: vc_ctx_kill()");
158 int main(int argc, char *argv[])
161 struct Arguments args = {
167 int c = getopt_long(argc, argv, "c:s:", CMDLINE_OPTIONS, 0);
171 case CMD_HELP : showHelp(1, argv[0], 0);
172 case CMD_VERSION : showVersion();
173 case 'c' : args.ctx = atoi(optarg); break;
174 case 's' : args.sig = str2sig(optarg); break;
176 WRITE_MSG(2, "Try '");
177 WRITE_STR(2, argv[0]);
178 WRITE_MSG(2, " --help\" for more information.\n");
185 WRITE_MSG(2, "Invalid signal specified\n");
189 if (args.ctx==VC_NOCTX && optind==argc) {
190 WRITE_MSG(2, "No pid specified\n");
195 fail += kill_wrapper(args.ctx, "0", args.sig);
196 else for (;optind<argc;++optind)
197 fail += kill_wrapper(args.ctx, argv[optind], args.sig);
199 return fail==0 ? EXIT_SUCCESS : EXIT_FAILURE;
206 assert(str2sig("0") ==0 );
207 assert(str2sig("1") ==1 );
208 assert(str2sig("10")==10);
209 assert(str2sig("SIGHUP")==1);
210 assert(str2sig("HUP") ==1);
211 assert(str2sig("SIGCHLD")==17);
212 assert(str2sig("CHLD") ==17);
213 assert(str2sig("x")==-1);
214 assert(str2sig("1 0")==-1);