added Vector_zeroEnd() function
[util-vserver.git] / util-vserver / src / vkill.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
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.
8 //  
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.
13 //  
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.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "vserver.h"
24 #include "linuxvirtual.h"
25 #include "util.h"
26
27 #include <getopt.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <sys/resource.h>
36 #include <sys/wait.h>
37
38 #define ENSC_WRAPPERS_VSERVER   1
39 #include <wrappers.h>
40
41 #define CMD_HELP        0x8000
42 #define CMD_VERSION     0x8001
43
44 int             wrapper_exit_code = 1;
45
46 static struct option const
47 CMDLINE_OPTIONS[] = {
48   { "help",     no_argument,        0, CMD_HELP },
49   { "version",  no_argument,        0, CMD_VERSION },
50   { "xid",      required_argument,  0, 'c' },
51   { 0,0,0,0 }
52 };
53
54 struct Arguments
55 {
56     xid_t               xid;
57     int                 sig;
58 };
59
60 static char const * const SIGNALS[] = {
61   // 0      1      2          3       4       5       6       7
62   "UNUSED", "HUP", "INT",     "QUIT", "ILL",  "TRAP", "ABRT", "UNUSED",
63   "FPE",    "KILL", "USR1",   "SEGV", "USR2", "PIPE", "ALRM", "TERM",
64   "STKFLT", "CHLD", "CONT",   "STOP", "TSTP", "TTIN", "TTOU", "IO",
65   "XCPU",   "XFSZ", "VTALRM", "PROF", "WINCH",
66   0,
67 };
68
69 static void
70 showHelp(int fd, char const *cmd, int res)
71 {
72   WRITE_MSG(fd, "Usage:  ");
73   WRITE_STR(fd, cmd);
74   WRITE_MSG(fd,
75             " [--xid|-c <xid>] [-s <signal>] [--] <pid>*\n"
76             "Please report bugs to " PACKAGE_BUGREPORT "\n");
77   exit(res);
78 }
79
80 static void
81 showVersion()
82 {
83   WRITE_MSG(1,
84             "vkill " VERSION " -- sends signals to processes within other contexts\n"
85             "This program is part of " PACKAGE_STRING "\n\n"
86             "Copyright (C) 2003 Enrico Scholz\n"
87             VERSION_COPYRIGHT_DISCLAIMER);
88   exit(0);
89 }
90
91 static int
92 str2sig(char const *str)
93 {
94   char  *errptr;
95   int   res = strtol(str, &errptr, 10);
96   
97   if (*errptr!='\0') res=-1;
98   if (res==-1 && strncmp(str,"SIG",3)==0) str+=3;
99   if (res==-1) {
100     char const * const  *ptr = SIGNALS;
101     for (;*ptr!=0; ++ptr) {
102       if (strcmp(*ptr,str)!=0) continue;
103       res = ptr-SIGNALS;
104       break;
105     }
106   }
107
108   return res;
109 }
110
111 #if defined(VC_ENABLE_API_LEGACY)
112 inline static ALWAYSINLINE int
113 kill_wrapper_legacy(xid_t UNUSED xid, char const *proc, int UNUSED sig)
114 {
115   pid_t         pid;
116
117   signal(SIGCHLD, SIG_DFL);
118   pid = Efork();
119
120   if (pid==0) {
121     int         status;
122     int         res;
123     while ((res=wait4(pid, &status, 0,0))==-1 &&
124            (errno==EAGAIN || errno==EINTR)) {}
125
126     return (res==0 && WIFEXITED(status) && WEXITSTATUS(status)) ? 0 : 1;
127   }
128
129   execl(LEGACYDIR "/vkill", "legacy/vkill", proc, (void *)(0));
130   perror("vkill: execl()");
131   exit(1);
132 }
133
134 static int
135 kill_wrapper(xid_t xid, char const *pid, int sig)
136 {
137   //printf("kill_wrapper(%u, %s, %i)\n", xid, pid, sig);
138   if (vc_ctx_kill(xid,atoi(pid),sig)==-1) {
139     int         err = errno;
140     if (vc_get_version(VC_CAT_COMPAT)==-1)
141       return kill_wrapper_legacy(xid, pid, sig);
142     else {
143       errno = err;
144       perror("vkill: vc_ctx_kill()");
145       return 1;
146     }
147   }
148   
149   return 0;
150 }
151 #else // VC_ENABLE_API_LEGACY
152 inline static int
153 kill_wrapper(xid_t xid, char const *pid, int sig)
154 {
155   if (vc_ctx_kill(xid,atoi(pid),sig)==-1) {
156     perror("vkill: vc_ctx_kill()");
157     return 1;
158   }
159   return 0;
160 }
161 #endif
162
163
164 int main(int argc, char *argv[])
165 {
166   int                   fail = 0;
167   struct Arguments      args = {
168     .xid = VC_NOCTX,
169     .sig = SIGTERM,
170   };
171   
172   while (1) {
173     int         c = getopt_long(argc, argv, "c:s:", CMDLINE_OPTIONS, 0);
174     if (c==-1) break;
175
176     switch (c) {
177       case CMD_HELP     :  showHelp(1, argv[0], 0);
178       case CMD_VERSION  :  showVersion();
179       case 'c'          :  args.xid = Evc_xidopt2xid(optarg,true); break;
180       case 's'          :  args.sig = str2sig(optarg);             break;
181       default           :
182         WRITE_MSG(2, "Try '");
183         WRITE_STR(2, argv[0]);
184         WRITE_MSG(2, " --help\" for more information.\n");
185         return EXIT_FAILURE;
186         break;
187     }
188   }
189
190   if (args.sig==-1) {
191     WRITE_MSG(2, "Invalid signal specified\n");
192     return EXIT_FAILURE;
193   }
194
195   if (args.xid==VC_NOCTX && optind==argc) {
196     WRITE_MSG(2, "No pid specified\n");
197     return EXIT_FAILURE;
198   }
199
200   if (optind==argc)
201     fail += kill_wrapper(args.xid, "0", args.sig);
202   else for (;optind<argc;++optind)
203     fail += kill_wrapper(args.xid, argv[optind], args.sig);
204
205   return fail==0 ? EXIT_SUCCESS : EXIT_FAILURE;
206 }
207
208 #ifdef TESTSUITE
209 void
210 vkill_test()
211 {
212   assert(str2sig("0") ==0 );
213   assert(str2sig("1") ==1 );
214   assert(str2sig("10")==10);
215   assert(str2sig("SIGHUP")==1);
216   assert(str2sig("HUP")   ==1);
217   assert(str2sig("SIGCHLD")==17);
218   assert(str2sig("CHLD")   ==17);
219   assert(str2sig("x")==-1);
220   assert(str2sig("1 0")==-1);
221
222   return 0;
223 }
224 #endif