1 // $Id: vkill.c 2491 2007-02-05 20:59:03Z dhozac $ --*- c -*--
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.
26 #include "lib/virtual.h"
35 #include <sys/types.h>
36 #include <sys/resource.h>
39 #define ENSC_WRAPPERS_VSERVER 1
40 #define ENSC_WRAPPERS_UNISTD 1
43 #define CMD_HELP 0x8000
44 #define CMD_VERSION 0x8001
46 int wrapper_exit_code = 1;
48 static struct option const
50 { "help", no_argument, 0, CMD_HELP },
51 { "version", no_argument, 0, CMD_VERSION },
52 { "xid", required_argument, 0, 'c' },
62 static char const * const SIGNALS[] = {
64 "UNUSED", "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "UNUSED",
65 "FPE", "KILL", "USR1", "SEGV", "USR2", "PIPE", "ALRM", "TERM",
66 "STKFLT", "CHLD", "CONT", "STOP", "TSTP", "TTIN", "TTOU", "IO",
67 "XCPU", "XFSZ", "VTALRM", "PROF", "WINCH",
72 showHelp(int fd, char const *cmd, int res)
74 WRITE_MSG(fd, "Usage: ");
77 " [--xid|-c <xid>] [-s <signal>] [--] <pid>*\n"
78 "Please report bugs to " PACKAGE_BUGREPORT "\n");
86 "vkill " VERSION " -- sends signals to processes within other contexts\n"
87 "This program is part of " PACKAGE_STRING "\n\n"
88 "Copyright (C) 2003 Enrico Scholz\n"
89 VERSION_COPYRIGHT_DISCLAIMER);
94 str2sig(char const *str)
97 int res = strtol(str, &errptr, 10);
99 if (*errptr!='\0') res=-1;
100 if (res==-1 && strncmp(str,"SIG",3)==0) str+=3;
102 char const * const *ptr = SIGNALS;
103 for (;*ptr!=0; ++ptr) {
104 if (strcmp(*ptr,str)!=0) continue;
113 #if defined(VC_ENABLE_API_LEGACY)
114 inline static ALWAYSINLINE int
115 kill_wrapper_legacy(xid_t UNUSED xid, char const *proc, int UNUSED sig)
119 signal(SIGCHLD, SIG_DFL);
125 while ((res=wait4(pid, &status, 0,0))==-1 &&
126 (errno==EAGAIN || errno==EINTR)) {}
128 return (res==0 && WIFEXITED(status) && WEXITSTATUS(status)) ? 0 : 1;
131 execl(LEGACYDIR "/vkill", "legacy/vkill", proc, (void *)(0));
132 perror("vkill: execl()");
137 kill_wrapper(xid_t xid, char const *pid, int sig)
139 //printf("kill_wrapper(%u, %s, %i)\n", xid, pid, sig);
140 if (vc_ctx_kill(xid,atoi(pid),sig)==-1) {
142 if (vc_get_version(VC_CAT_COMPAT)==-1)
143 return kill_wrapper_legacy(xid, pid, sig);
146 perror("vkill: vc_ctx_kill()");
153 #else // VC_ENABLE_API_LEGACY
155 kill_wrapper(xid_t xid, char const *pid_s, int sig)
160 if (!isNumber(pid_s, &tmp, true)) {
161 WRITE_MSG(2, "vkill: '");
163 WRITE_MSG(2, "' is not a number\n");
168 xid = vc_get_task_xid(pid);
169 if (vc_ctx_kill(xid,pid,sig)==-1) {
170 perror("vkill: vc_ctx_kill()");
178 int main(int argc, char *argv[])
181 struct Arguments args = {
187 int c = getopt_long(argc, argv, "c:s:", CMDLINE_OPTIONS, 0);
191 case CMD_HELP : showHelp(1, argv[0], 0);
192 case CMD_VERSION : showVersion();
193 case 'c' : args.xid = Evc_xidopt2xid(optarg,true); break;
194 case 's' : args.sig = str2sig(optarg); break;
196 WRITE_MSG(2, "Try '");
197 WRITE_STR(2, argv[0]);
198 WRITE_MSG(2, " --help' for more information.\n");
205 WRITE_MSG(2, "Invalid signal specified\n");
209 if (args.xid==VC_NOCTX && optind==argc) {
210 WRITE_MSG(2, "No pid specified\n");
215 fail += kill_wrapper(args.xid, "0", args.sig);
216 else for (;optind<argc;++optind)
217 fail += kill_wrapper(args.xid, argv[optind], args.sig);
219 return fail==0 ? EXIT_SUCCESS : EXIT_FAILURE;
226 assert(str2sig("0") ==0 );
227 assert(str2sig("1") ==1 );
228 assert(str2sig("10")==10);
229 assert(str2sig("SIGHUP")==1);
230 assert(str2sig("HUP") ==1);
231 assert(str2sig("SIGCHLD")==17);
232 assert(str2sig("CHLD") ==17);
233 assert(str2sig("x")==-1);
234 assert(str2sig("1 0")==-1);