blob: 260f4a07428dd85e116de10660b6af6bd7c8138a [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 =
35 "kill [-signal] process-id [process-id ...]\n\n"
36 "Send a signal (default is SIGTERM) to the specified process(es).\n\n"
37 "Options:\n" "\t-l\tList all signal names and numbers.\n\n";
Eric Andersenabc0f4f1999-12-08 23:19:36 +000038
Erik Andersen6273f652000-03-17 01:12:41 +000039#ifdef BB_KILLALL
Erik Andersen246cc6d2000-03-07 07:41:42 +000040static const char *killall_usage =
41 "killall [-signal] process-name [process-name ...]\n\n"
42 "Send a signal (default is SIGTERM) to the specified process(es).\n\n"
43 "Options:\n" "\t-l\tList all signal names and numbers.\n\n";
Erik Andersen6273f652000-03-17 01:12:41 +000044#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +000045
46#define KILL 0
47#define KILLALL 1
Eric Andersencc8ed391999-10-05 16:24:54 +000048
49struct signal_name {
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 const char *name;
51 int number;
Eric Andersencc8ed391999-10-05 16:24:54 +000052};
53
54const struct signal_name signames[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +000055 {"HUP", SIGHUP},
56 {"INT", SIGINT},
57 {"QUIT", SIGQUIT},
58 {"ILL", SIGILL},
59 {"TRAP", SIGTRAP},
60 {"ABRT", SIGABRT},
Eric Andersencc8ed391999-10-05 16:24:54 +000061#ifndef __alpha__
Erik Andersene49d5ec2000-02-08 19:58:47 +000062 {"IOT", SIGIOT},
Eric Andersencc8ed391999-10-05 16:24:54 +000063#endif
Eric Andersene77ae3a1999-10-19 20:03:34 +000064#if defined(__sparc__) || defined(__alpha__)
Erik Andersene49d5ec2000-02-08 19:58:47 +000065 {"EMT", SIGEMT},
Eric Andersencc8ed391999-10-05 16:24:54 +000066#else
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 {"BUS", SIGBUS},
Eric Andersencc8ed391999-10-05 16:24:54 +000068#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000069 {"FPE", SIGFPE},
70 {"KILL", SIGKILL},
Eric Andersene77ae3a1999-10-19 20:03:34 +000071#if defined(__sparc__) || defined(__alpha__)
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 {"BUS", SIGBUS},
Eric Andersencc8ed391999-10-05 16:24:54 +000073#else
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 {"USR1", SIGUSR1},
Eric Andersencc8ed391999-10-05 16:24:54 +000075#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000076 {"SEGV", SIGSEGV},
Eric Andersene77ae3a1999-10-19 20:03:34 +000077#if defined(__sparc__) || defined(__alpha__)
Erik Andersene49d5ec2000-02-08 19:58:47 +000078 {"SYS", SIGSYS},
Eric Andersencc8ed391999-10-05 16:24:54 +000079#else
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 {"USR2", SIGUSR2},
Eric Andersencc8ed391999-10-05 16:24:54 +000081#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000082 {"PIPE", SIGPIPE},
83 {"ALRM", SIGALRM},
84 {"TERM", SIGTERM},
Eric Andersene77ae3a1999-10-19 20:03:34 +000085#if defined(__sparc__) || defined(__alpha__)
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 {"URG", SIGURG},
87 {"STOP", SIGSTOP},
88 {"TSTP", SIGTSTP},
89 {"CONT", SIGCONT},
90 {"CHLD", SIGCHLD},
91 {"TTIN", SIGTTIN},
92 {"TTOU", SIGTTOU},
93 {"IO", SIGIO},
Eric Andersencc8ed391999-10-05 16:24:54 +000094# ifndef __alpha__
Erik Andersene49d5ec2000-02-08 19:58:47 +000095 {"POLL", SIGIO},
Eric Andersencc8ed391999-10-05 16:24:54 +000096# endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000097 {"XCPU", SIGXCPU},
98 {"XFSZ", SIGXFSZ},
99 {"VTALRM", SIGVTALRM},
100 {"PROF", SIGPROF},
101 {"WINCH", SIGWINCH},
Eric Andersencc8ed391999-10-05 16:24:54 +0000102# ifdef __alpha__
Erik Andersene49d5ec2000-02-08 19:58:47 +0000103 {"INFO", SIGINFO},
Eric Andersencc8ed391999-10-05 16:24:54 +0000104# else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000105 {"LOST", SIGLOST},
Eric Andersencc8ed391999-10-05 16:24:54 +0000106# endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000107 {"USR1", SIGUSR1},
108 {"USR2", SIGUSR2},
Eric Andersencc8ed391999-10-05 16:24:54 +0000109#else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000110 {"STKFLT", SIGSTKFLT},
111 {"CHLD", SIGCHLD},
112 {"CONT", SIGCONT},
113 {"STOP", SIGSTOP},
114 {"TSTP", SIGTSTP},
115 {"TTIN", SIGTTIN},
116 {"TTOU", SIGTTOU},
117 {"URG", SIGURG},
118 {"XCPU", SIGXCPU},
119 {"XFSZ", SIGXFSZ},
120 {"VTALRM", SIGVTALRM},
121 {"PROF", SIGPROF},
122 {"WINCH", SIGWINCH},
123 {"IO", SIGIO},
124 {"POLL", SIGPOLL},
125 {"PWR", SIGPWR},
126 {"UNUSED", SIGUNUSED},
Eric Andersencc8ed391999-10-05 16:24:54 +0000127#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128 {0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +0000129};
130
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131extern int kill_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000132{
Erik Andersen246cc6d2000-03-07 07:41:42 +0000133 int whichApp, sig = SIGTERM;
134 const char *appUsage;
135
Erik Andersen6273f652000-03-17 01:12:41 +0000136#ifdef BB_KILLALL
Erik Andersen246cc6d2000-03-07 07:41:42 +0000137 /* Figure out what we are trying to do here */
138 whichApp = (strcmp(*argv, "killall") == 0)?
139 KILLALL : KILL;
140 appUsage = (whichApp == KILLALL)? killall_usage : kill_usage;
Erik Andersen6273f652000-03-17 01:12:41 +0000141#else
142 whichApp = KILL;
143 appUsage = kill_usage;
144#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000145
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000146 argc--;
147 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148 /* Parse any options */
149 if (argc < 1)
Erik Andersen246cc6d2000-03-07 07:41:42 +0000150 usage(appUsage);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151
152 while (argc > 0 && **argv == '-') {
153 while (*++(*argv)) {
154 switch (**argv) {
155 case 'l':
156 {
157 int col = 0;
158 const struct signal_name *s = signames;
159
160 while (s->name != 0) {
161 col +=
162 fprintf(stderr, "%2d) %-8s", s->number,
163 (s++)->name);
164 if (col > 60) {
165 fprintf(stderr, "\n");
166 col = 0;
167 }
168 }
169 fprintf(stderr, "\n\n");
170 exit(TRUE);
171 }
172 break;
173 case '-':
Erik Andersen246cc6d2000-03-07 07:41:42 +0000174 usage(appUsage);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000175 default:
176 {
177 if (isdigit(**argv)) {
178 sig = atoi(*argv);
179 if (sig < 0 || sig >= NSIG)
180 goto end;
181 else {
182 argc--;
183 argv++;
184 goto do_it_now;
185 }
186 } else {
187 const struct signal_name *s = signames;
188
189 while (s->name != 0) {
190 if (strcasecmp(s->name, *argv) == 0) {
191 sig = s->number;
192 argc--;
193 argv++;
194 goto do_it_now;
195 }
196 s++;
197 }
198 if (s->name == 0)
199 goto end;
200 }
201 }
202 }
203 argc--;
204 argv++;
205 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000206 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000207
Erik Andersene49d5ec2000-02-08 19:58:47 +0000208 do_it_now:
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000209
Erik Andersen246cc6d2000-03-07 07:41:42 +0000210 if (whichApp == KILL) {
211 /* Looks like they want to do a kill. Do that */
212 while (--argc >= 0) {
213 int pid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000214
Erik Andersen246cc6d2000-03-07 07:41:42 +0000215 if (!isdigit(**argv))
216 fatalError( "Bad PID: %s\n", strerror(errno));
217 pid = strtol(*argv, NULL, 0);
218 if (kill(pid, sig) != 0)
219 fatalError( "Could not kill pid '%d': %s\n", pid, strerror(errno));
220 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000221 }
Erik Andersen6273f652000-03-17 01:12:41 +0000222 }
223#ifdef BB_KILLALL
224 else {
Erik Andersen246cc6d2000-03-07 07:41:42 +0000225 /* Looks like they want to do a killall. Do that */
226 while (--argc >= 0) {
227 int pid;
228
229 while((pid = findPidByName( *argv))) {
230 if (kill(pid, sig) != 0)
231 fatalError( "Could not kill pid '%d': %s\n", pid, strerror(errno));
232 }
233 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000234 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000235 }
Erik Andersen6273f652000-03-17 01:12:41 +0000236#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +0000237
Erik Andersene49d5ec2000-02-08 19:58:47 +0000238 exit(TRUE);
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000239
Eric Andersena7093171999-10-23 05:42:08 +0000240
Erik Andersene49d5ec2000-02-08 19:58:47 +0000241 end:
Erik Andersen246cc6d2000-03-07 07:41:42 +0000242 fatalError( "bad signal name: %s\n", *argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000243}