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