blob: 93f02eab47780ef5aaaaae29fb1c10020b2706ff [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 Andersen0a64de92000-04-18 20:00:03 +0000225 pid_t myPid=getpid();
Erik Andersen246cc6d2000-03-07 07:41:42 +0000226 /* Looks like they want to do a killall. Do that */
227 while (--argc >= 0) {
Erik Andersen825aead2000-04-07 06:00:07 +0000228 pid_t* pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000229
Erik Andersen825aead2000-04-07 06:00:07 +0000230 pidList = findPidByName( *argv);
Erik Andersen0a64de92000-04-18 20:00:03 +0000231 for(; pidList && *pidList!=0; pidList++) {
232 if (*pidList==myPid)
233 continue;
Erik Andersen825aead2000-04-07 06:00:07 +0000234 if (kill(*pidList, sig) != 0)
235 fatalError( "Could not kill pid '%d': %s\n", *pidList, strerror(errno));
Erik Andersen246cc6d2000-03-07 07:41:42 +0000236 }
Erik Andersen825aead2000-04-07 06:00:07 +0000237 /* Note that we don't bother to free the memory
238 * allocated in findPidByName(). It will be freed
239 * upon exit, so we can save a byte or two */
Erik Andersen246cc6d2000-03-07 07:41:42 +0000240 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000241 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000242 }
Erik Andersen6273f652000-03-17 01:12:41 +0000243#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +0000244
Erik Andersene49d5ec2000-02-08 19:58:47 +0000245 exit(TRUE);
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000246
Eric Andersena7093171999-10-23 05:42:08 +0000247
Erik Andersene49d5ec2000-02-08 19:58:47 +0000248 end:
Erik Andersen246cc6d2000-03-07 07:41:42 +0000249 fatalError( "bad signal name: %s\n", *argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000250}