s!ctx_t!xid_t!
[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 #include "compat.h"
23
24 #include "vserver.h"
25 #include "linuxvirtual.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 VERSION_COPYRIGHT_DISCLAIMER
39
40 #define CMD_HELP        0x8000
41 #define CMD_VERSION     0x8001
42
43 #define WRITE_MSG(FD,X)         (void)(write(FD,X,sizeof(X)-1))
44 #define WRITE_STR(FD,X)         writeStr(FD,X)
45
46 static struct option const
47 CMDLINE_OPTIONS[] = {
48   { "help",     no_argument,  0, CMD_HELP },
49   { "version",  no_argument,  0, CMD_VERSION },
50   { 0,0,0,0 }
51 };
52
53 struct Arguments
54 {
55     ctx_t               ctx;
56     int                 sig;
57 };
58
59 static char const * const SIGNALS[] = {
60   // 0      1      2          3       4       5       6       7
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",
65   0,
66 };
67
68 inline static void UNUSED
69 writeStr(int fd, char const *cmd)
70 {
71   (void)write(fd, cmd, strlen(cmd));
72 }
73
74 static void
75 showHelp(int fd, char const *cmd, int res)
76 {
77   WRITE_MSG(fd, "Usage:  ");
78   WRITE_STR(fd, cmd);
79   WRITE_MSG(fd,
80             " [-c <ctx>] [-s <signal>] [--] [<pid>]+\n"
81             "Please report bugs to " PACKAGE_BUGREPORT "\n");
82   exit(res);
83 }
84
85 static void
86 showVersion()
87 {
88   WRITE_MSG(1,
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);
93   exit(0);
94 }
95
96 static int
97 str2sig(char const *str)
98 {
99   char  *errptr;
100   int   res = strtol(str, &errptr, 10);
101   
102   if (*errptr!='\0') res=-1;
103   if (res==-1 && strncmp(str,"SIG",3)==0) str+=3;
104   if (res==-1) {
105     char const * const  *ptr = SIGNALS;
106     for (;*ptr!=0; ++ptr) {
107       if (strcmp(*ptr,str)!=0) continue;
108       res = ptr-SIGNALS;
109       break;
110     }
111   }
112
113   return res;
114 }
115
116 static int
117 kill_wrapper_legacy(ctx_t ctx, char const *proc, int sig)
118 {
119   pid_t         pid = fork();
120   if (pid==-1) {
121     perror("fork()");
122     exit(1);
123   }
124   else if (pid==0) {
125     int         status;
126     int         res;
127     while ((res=wait4(pid, &status, 0,0))==-1 &&
128            (errno==EAGAIN || errno==EINTR)) {}
129
130     return (res==0 && WIFEXITED(status) && WEXITSTATUS(status)) ? 0 : 1;
131   }
132
133   execl(LEGACYDIR "/vkill", "legacy/vkill", proc, (void *)(0));
134   perror("execl()");
135   exit(1);
136 }
137
138 static int
139 kill_wrapper(ctx_t ctx, char const *pid, int sig)
140 {
141   //printf("kill_wrapper(%u, %s, %i)\n", ctx, pid, sig);
142   if (vc_ctx_kill(ctx,atoi(pid),sig)==-1) {
143     int         err = errno;
144     if (vc_get_version(VC_CAT_COMPAT)==-1)
145       return kill_wrapper_legacy(ctx, pid, sig);
146     else {
147       errno = err;
148       perror("vc_ctx_kill()");
149       return 1;
150     }
151   }
152   
153   return 0;
154 }
155
156 int main(int argc, char *argv[])
157 {
158   int                   fail = 0;
159   struct Arguments      args = {
160     .ctx = VC_NOCTX,
161     .sig = SIGTERM,
162   };
163   
164   while (1) {
165     int         c = getopt_long(argc, argv, "c:s:", CMDLINE_OPTIONS, 0);
166     if (c==-1) break;
167
168     switch (c) {
169       case CMD_HELP     :  showHelp(1, argv[0], 0);
170       case CMD_VERSION  :  showVersion();
171       case 'c'          :  args.ctx = atoi(optarg);    break;
172       case 's'          :  args.sig = str2sig(optarg); break;
173       default           :
174         WRITE_MSG(2, "Try '");
175         WRITE_STR(2, argv[0]);
176         WRITE_MSG(2, " --help\" for more information.\n");
177         return EXIT_FAILURE;
178         break;
179     }
180   }
181
182   if (args.sig==-1) {
183     WRITE_MSG(2, "Invalid signal specified\n");
184     return EXIT_FAILURE;
185   }
186
187   if (args.ctx==VC_NOCTX && optind==argc) {
188     WRITE_MSG(2, "No pid specified\n");
189     return EXIT_FAILURE;
190   }
191
192   if (optind==argc)
193     fail += kill_wrapper(args.ctx, "0", args.sig);
194   else for (;optind<argc;++optind)
195     fail += kill_wrapper(args.ctx, argv[optind], args.sig);
196
197   return fail==0 ? EXIT_SUCCESS : EXIT_FAILURE;
198 }
199
200 #ifdef TESTSUITE
201 void
202 vkill_test()
203 {
204   assert(str2sig("0") ==0 );
205   assert(str2sig("1") ==1 );
206   assert(str2sig("10")==10);
207   assert(str2sig("SIGHUP")==1);
208   assert(str2sig("HUP")   ==1);
209   assert(str2sig("SIGCHLD")==17);
210   assert(str2sig("CHLD")   ==17);
211   assert(str2sig("x")==-1);
212   assert(str2sig("1 0")==-1);
213
214   return 0;
215 }
216 #endif