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.
25 #include "linuxvirtual.h"
34 #include <sys/types.h>
35 #include <sys/resource.h>
38 #define VERSION_COPYRIGHT_DISCLAIMER
40 #define CMD_HELP 0x8000
41 #define CMD_VERSION 0x8001
43 #define WRITE_MSG(FD,X) (void)(write(FD,X,sizeof(X)-1))
44 #define WRITE_STR(FD,X) writeStr(FD,X)
46 static struct option const
48 { "help", no_argument, 0, CMD_HELP },
49 { "version", no_argument, 0, CMD_VERSION },
59 static char const * const SIGNALS[] = {
61 "UNUSED", "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "UNUSED",
62 "FPE", "KILL", "USR1", "SEGV", "USR2", "PIPE", "ALRM", "TERM",
63 "STKFLT", "CHLD", "CONT", "STOP", "TSTP", "TTIN", "TTOU", "IO",
64 "XCPU", "XFSZ", "VTALRM", "PROF", "WINCH",
68 inline static void UNUSED
69 writeStr(int fd, char const *cmd)
71 (void)write(fd, cmd, strlen(cmd));
75 showHelp(int fd, char const *cmd, int res)
77 WRITE_MSG(fd, "Usage: ");
80 " [-c <ctx>] [-s <signal>] [--] <pid>*\n"
81 "Please report bugs to " PACKAGE_BUGREPORT "\n");
89 "vkill " VERSION " -- sends signals to processes within other contexts\n"
90 "This program is part of " PACKAGE_STRING "\n\n"
91 "Copyright (C) 2003 Enrico Scholz\n"
92 VERSION_COPYRIGHT_DISCLAIMER);
97 str2sig(char const *str)
100 int res = strtol(str, &errptr, 10);
102 if (*errptr!='\0') res=-1;
103 if (res==-1 && strncmp(str,"SIG",3)==0) str+=3;
105 char const * const *ptr = SIGNALS;
106 for (;*ptr!=0; ++ptr) {
107 if (strcmp(*ptr,str)!=0) continue;
116 #if defined(VC_ENABLE_API_LEGACY)
117 inline static ALWAYSINLINE int
118 kill_wrapper_legacy(xid_t UNUSED ctx, char const *proc, int UNUSED sig)
128 while ((res=wait4(pid, &status, 0,0))==-1 &&
129 (errno==EAGAIN || errno==EINTR)) {}
131 return (res==0 && WIFEXITED(status) && WEXITSTATUS(status)) ? 0 : 1;
134 execl(LEGACYDIR "/vkill", "legacy/vkill", proc, (void *)(0));
140 kill_wrapper(xid_t ctx, char const *pid, int sig)
142 //printf("kill_wrapper(%u, %s, %i)\n", ctx, pid, sig);
143 if (vc_ctx_kill(ctx,atoi(pid),sig)==-1) {
145 if (vc_get_version(VC_CAT_COMPAT)==-1)
146 return kill_wrapper_legacy(ctx, pid, sig);
149 perror("vc_ctx_kill()");
156 #else // VC_ENABLE_API_LEGACY
158 kill_wrapper(xid_t ctx, char const *pid, int sig)
160 if (vc_ctx_kill(ctx,atoi(pid),sig)==-1) {
161 perror("vc_ctx_kill()");
169 int main(int argc, char *argv[])
172 struct Arguments args = {
178 int c = getopt_long(argc, argv, "c:s:", CMDLINE_OPTIONS, 0);
182 case CMD_HELP : showHelp(1, argv[0], 0);
183 case CMD_VERSION : showVersion();
184 case 'c' : args.ctx = atoi(optarg); break;
185 case 's' : args.sig = str2sig(optarg); break;
187 WRITE_MSG(2, "Try '");
188 WRITE_STR(2, argv[0]);
189 WRITE_MSG(2, " --help\" for more information.\n");
196 WRITE_MSG(2, "Invalid signal specified\n");
200 if (args.ctx==VC_NOCTX && optind==argc) {
201 WRITE_MSG(2, "No pid specified\n");
206 fail += kill_wrapper(args.ctx, "0", args.sig);
207 else for (;optind<argc;++optind)
208 fail += kill_wrapper(args.ctx, argv[optind], args.sig);
210 return fail==0 ? EXIT_SUCCESS : EXIT_FAILURE;
217 assert(str2sig("0") ==0 );
218 assert(str2sig("1") ==1 );
219 assert(str2sig("10")==10);
220 assert(str2sig("SIGHUP")==1);
221 assert(str2sig("HUP") ==1);
222 assert(str2sig("SIGCHLD")==17);
223 assert(str2sig("CHLD") ==17);
224 assert(str2sig("x")==-1);
225 assert(str2sig("1 0")==-1);