blob: 0487f6a66190b003e898aaa0f7107b62a5bd0e8b [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc4996011999-10-20 22:08:37 +00002/*
Erik Andersen5661fe02000-04-05 01:00:52 +00003 * Mini kill/killall implementation for busybox
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23
Eric Andersencc8ed391999-10-05 16:24:54 +000024#include "internal.h"
25#include <stdio.h>
26#include <stdlib.h>
Erik Andersen246cc6d2000-03-07 07:41:42 +000027#include <errno.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000028#include <unistd.h>
29#include <signal.h>
Eric Andersena7093171999-10-23 05:42:08 +000030#include <ctype.h>
Eric Andersenabc0f4f1999-12-08 23:19:36 +000031#include <sys/stat.h>
32#include <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000033
Erik Andersene49d5ec2000-02-08 19:58:47 +000034static const char *kill_usage =
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000035 "kill [-signal] process-id [process-id ...]\n"
36#ifndef BB_FEATURE_TRIVIAL_HELP
37 "\nSend a signal (default is SIGTERM) to the specified process(es).\n\n"
38 "Options:\n" "\t-l\tList all signal names and numbers.\n\n"
39#endif
40 ;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000041
Erik Andersen6273f652000-03-17 01:12:41 +000042#ifdef BB_KILLALL
Erik Andersen246cc6d2000-03-07 07:41:42 +000043static const char *killall_usage =
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000044 "killall [-signal] process-name [process-name ...]\n"
45#ifndef BB_FEATURE_TRIVIAL_HELP
46 "\nSend a signal (default is SIGTERM) to the specified process(es).\n\n"
47 "Options:\n" "\t-l\tList all signal names and numbers.\n\n"
Erik Andersen6273f652000-03-17 01:12:41 +000048#endif
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000049#endif
50 ;
Erik Andersen246cc6d2000-03-07 07:41:42 +000051
52#define KILL 0
53#define KILLALL 1
Eric Andersencc8ed391999-10-05 16:24:54 +000054
55struct signal_name {
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 const char *name;
57 int number;
Eric Andersencc8ed391999-10-05 16:24:54 +000058};
59
60const struct signal_name signames[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 {"HUP", SIGHUP},
62 {"INT", SIGINT},
63 {"QUIT", SIGQUIT},
64 {"ILL", SIGILL},
65 {"TRAP", SIGTRAP},
66 {"ABRT", SIGABRT},
Eric Andersencc8ed391999-10-05 16:24:54 +000067#ifndef __alpha__
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 {"IOT", SIGIOT},
Eric Andersencc8ed391999-10-05 16:24:54 +000069#endif
Eric Andersene77ae3a1999-10-19 20:03:34 +000070#if defined(__sparc__) || defined(__alpha__)
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 {"EMT", SIGEMT},
Eric Andersencc8ed391999-10-05 16:24:54 +000072#else
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 {"BUS", SIGBUS},
Eric Andersencc8ed391999-10-05 16:24:54 +000074#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 {"FPE", SIGFPE},
76 {"KILL", SIGKILL},
Eric Andersene77ae3a1999-10-19 20:03:34 +000077#if defined(__sparc__) || defined(__alpha__)
Erik Andersene49d5ec2000-02-08 19:58:47 +000078 {"BUS", SIGBUS},
Eric Andersencc8ed391999-10-05 16:24:54 +000079#else
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 {"USR1", SIGUSR1},
Eric Andersencc8ed391999-10-05 16:24:54 +000081#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000082 {"SEGV", SIGSEGV},
Eric Andersene77ae3a1999-10-19 20:03:34 +000083#if defined(__sparc__) || defined(__alpha__)
Erik Andersene49d5ec2000-02-08 19:58:47 +000084 {"SYS", SIGSYS},
Eric Andersencc8ed391999-10-05 16:24:54 +000085#else
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 {"USR2", SIGUSR2},
Eric Andersencc8ed391999-10-05 16:24:54 +000087#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 {"PIPE", SIGPIPE},
89 {"ALRM", SIGALRM},
90 {"TERM", SIGTERM},
Eric Andersene77ae3a1999-10-19 20:03:34 +000091#if defined(__sparc__) || defined(__alpha__)
Erik Andersene49d5ec2000-02-08 19:58:47 +000092 {"URG", SIGURG},
93 {"STOP", SIGSTOP},
94 {"TSTP", SIGTSTP},
95 {"CONT", SIGCONT},
96 {"CHLD", SIGCHLD},
97 {"TTIN", SIGTTIN},
98 {"TTOU", SIGTTOU},
99 {"IO", SIGIO},
Eric Andersencc8ed391999-10-05 16:24:54 +0000100# ifndef __alpha__
Erik Andersene49d5ec2000-02-08 19:58:47 +0000101 {"POLL", SIGIO},
Eric Andersencc8ed391999-10-05 16:24:54 +0000102# endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000103 {"XCPU", SIGXCPU},
104 {"XFSZ", SIGXFSZ},
105 {"VTALRM", SIGVTALRM},
106 {"PROF", SIGPROF},
107 {"WINCH", SIGWINCH},
Eric Andersencc8ed391999-10-05 16:24:54 +0000108# ifdef __alpha__
Erik Andersene49d5ec2000-02-08 19:58:47 +0000109 {"INFO", SIGINFO},
Eric Andersencc8ed391999-10-05 16:24:54 +0000110# else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111 {"LOST", SIGLOST},
Eric Andersencc8ed391999-10-05 16:24:54 +0000112# endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000113 {"USR1", SIGUSR1},
114 {"USR2", SIGUSR2},
Eric Andersencc8ed391999-10-05 16:24:54 +0000115#else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000116 {"STKFLT", SIGSTKFLT},
117 {"CHLD", SIGCHLD},
118 {"CONT", SIGCONT},
119 {"STOP", SIGSTOP},
120 {"TSTP", SIGTSTP},
121 {"TTIN", SIGTTIN},
122 {"TTOU", SIGTTOU},
123 {"URG", SIGURG},
124 {"XCPU", SIGXCPU},
125 {"XFSZ", SIGXFSZ},
126 {"VTALRM", SIGVTALRM},
127 {"PROF", SIGPROF},
128 {"WINCH", SIGWINCH},
129 {"IO", SIGIO},
130 {"POLL", SIGPOLL},
131 {"PWR", SIGPWR},
132 {"UNUSED", SIGUNUSED},
Eric Andersencc8ed391999-10-05 16:24:54 +0000133#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134 {0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +0000135};
136
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137extern int kill_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000138{
Erik Andersen246cc6d2000-03-07 07:41:42 +0000139 int whichApp, sig = SIGTERM;
140 const char *appUsage;
141
Erik Andersen6273f652000-03-17 01:12:41 +0000142#ifdef BB_KILLALL
Erik Andersen246cc6d2000-03-07 07:41:42 +0000143 /* Figure out what we are trying to do here */
144 whichApp = (strcmp(*argv, "killall") == 0)?
145 KILLALL : KILL;
146 appUsage = (whichApp == KILLALL)? killall_usage : kill_usage;
Erik Andersen6273f652000-03-17 01:12:41 +0000147#else
148 whichApp = KILL;
149 appUsage = kill_usage;
150#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000151
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000152 argc--;
153 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000154 /* Parse any options */
155 if (argc < 1)
Erik Andersen246cc6d2000-03-07 07:41:42 +0000156 usage(appUsage);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157
158 while (argc > 0 && **argv == '-') {
159 while (*++(*argv)) {
160 switch (**argv) {
161 case 'l':
162 {
163 int col = 0;
164 const struct signal_name *s = signames;
165
166 while (s->name != 0) {
167 col +=
168 fprintf(stderr, "%2d) %-8s", s->number,
169 (s++)->name);
170 if (col > 60) {
171 fprintf(stderr, "\n");
172 col = 0;
173 }
174 }
175 fprintf(stderr, "\n\n");
176 exit(TRUE);
177 }
178 break;
179 case '-':
Erik Andersen246cc6d2000-03-07 07:41:42 +0000180 usage(appUsage);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000181 default:
182 {
183 if (isdigit(**argv)) {
184 sig = atoi(*argv);
185 if (sig < 0 || sig >= NSIG)
186 goto end;
187 else {
188 argc--;
189 argv++;
190 goto do_it_now;
191 }
192 } else {
193 const struct signal_name *s = signames;
194
195 while (s->name != 0) {
196 if (strcasecmp(s->name, *argv) == 0) {
197 sig = s->number;
198 argc--;
199 argv++;
200 goto do_it_now;
201 }
202 s++;
203 }
204 if (s->name == 0)
205 goto end;
206 }
207 }
208 }
209 argc--;
210 argv++;
211 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000212 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000213
Erik Andersene49d5ec2000-02-08 19:58:47 +0000214 do_it_now:
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000215
Erik Andersen246cc6d2000-03-07 07:41:42 +0000216 if (whichApp == KILL) {
217 /* Looks like they want to do a kill. Do that */
218 while (--argc >= 0) {
219 int pid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220
Erik Andersen246cc6d2000-03-07 07:41:42 +0000221 if (!isdigit(**argv))
222 fatalError( "Bad PID: %s\n", strerror(errno));
223 pid = strtol(*argv, NULL, 0);
224 if (kill(pid, sig) != 0)
225 fatalError( "Could not kill pid '%d': %s\n", pid, strerror(errno));
226 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000227 }
Erik Andersen6273f652000-03-17 01:12:41 +0000228 }
229#ifdef BB_KILLALL
230 else {
Erik Andersen0a64de92000-04-18 20:00:03 +0000231 pid_t myPid=getpid();
Erik Andersen246cc6d2000-03-07 07:41:42 +0000232 /* Looks like they want to do a killall. Do that */
233 while (--argc >= 0) {
Erik Andersen825aead2000-04-07 06:00:07 +0000234 pid_t* pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000235
Erik Andersen825aead2000-04-07 06:00:07 +0000236 pidList = findPidByName( *argv);
Erik Andersen0a64de92000-04-18 20:00:03 +0000237 for(; pidList && *pidList!=0; pidList++) {
238 if (*pidList==myPid)
239 continue;
Erik Andersen825aead2000-04-07 06:00:07 +0000240 if (kill(*pidList, sig) != 0)
241 fatalError( "Could not kill pid '%d': %s\n", *pidList, strerror(errno));
Erik Andersen246cc6d2000-03-07 07:41:42 +0000242 }
Erik Andersen825aead2000-04-07 06:00:07 +0000243 /* Note that we don't bother to free the memory
244 * allocated in findPidByName(). It will be freed
245 * upon exit, so we can save a byte or two */
Erik Andersen246cc6d2000-03-07 07:41:42 +0000246 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000247 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000248 }
Erik Andersen6273f652000-03-17 01:12:41 +0000249#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +0000250
Erik Andersene49d5ec2000-02-08 19:58:47 +0000251 exit(TRUE);
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000252
Eric Andersena7093171999-10-23 05:42:08 +0000253
Erik Andersene49d5ec2000-02-08 19:58:47 +0000254 end:
Erik Andersen246cc6d2000-03-07 07:41:42 +0000255 fatalError( "bad signal name: %s\n", *argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000256}