blob: 34cbc4d6aa2b43e7327b1bd74d39f228387cd69c [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 <stdio.h>
25#include <stdlib.h>
Erik Andersen246cc6d2000-03-07 07:41:42 +000026#include <errno.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000027#include <unistd.h>
28#include <signal.h>
Eric Andersena7093171999-10-23 05:42:08 +000029#include <ctype.h>
Eric Andersened3ef502001-01-27 08:24:39 +000030#include <string.h>
Eric Andersenabc0f4f1999-12-08 23:19:36 +000031#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000032#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000033
Mark Whitley59ab0252001-01-23 22:30:04 +000034static const int KILL = 0;
35static const int KILLALL = 1;
Eric Andersencc8ed391999-10-05 16:24:54 +000036
37struct signal_name {
Erik Andersene49d5ec2000-02-08 19:58:47 +000038 const char *name;
39 int number;
Eric Andersencc8ed391999-10-05 16:24:54 +000040};
41
Eric Andersen3e6ff902001-03-09 21:24:12 +000042static const struct signal_name signames[] = {
Pavel Roskin5d513f72000-09-13 14:03:48 +000043 /* POSIX signals */
44 { "HUP", SIGHUP }, /* 1 */
45 { "INT", SIGINT }, /* 2 */
46 { "QUIT", SIGQUIT }, /* 3 */
47 { "ILL", SIGILL }, /* 4 */
48 { "ABRT", SIGABRT }, /* 6 */
49 { "FPE", SIGFPE }, /* 8 */
50 { "KILL", SIGKILL }, /* 9 */
51 { "SEGV", SIGSEGV }, /* 11 */
52 { "PIPE", SIGPIPE }, /* 13 */
53 { "ALRM", SIGALRM }, /* 14 */
54 { "TERM", SIGTERM }, /* 15 */
55 { "USR1", SIGUSR1 }, /* 10 (arm,i386,m68k,ppc), 30 (alpha,sparc*), 16 (mips) */
56 { "USR2", SIGUSR2 }, /* 12 (arm,i386,m68k,ppc), 31 (alpha,sparc*), 17 (mips) */
57 { "CHLD", SIGCHLD }, /* 17 (arm,i386,m68k,ppc), 20 (alpha,sparc*), 18 (mips) */
58 { "CONT", SIGCONT }, /* 18 (arm,i386,m68k,ppc), 19 (alpha,sparc*), 25 (mips) */
59 { "STOP", SIGSTOP }, /* 19 (arm,i386,m68k,ppc), 17 (alpha,sparc*), 23 (mips) */
60 { "TSTP", SIGTSTP }, /* 20 (arm,i386,m68k,ppc), 18 (alpha,sparc*), 24 (mips) */
61 { "TTIN", SIGTTIN }, /* 21 (arm,i386,m68k,ppc,alpha,sparc*), 26 (mips) */
62 { "TTOU", SIGTTOU }, /* 22 (arm,i386,m68k,ppc,alpha,sparc*), 27 (mips) */
63 /* Miscellaneous other signals */
64#ifdef SIGTRAP
65 { "TRAP", SIGTRAP }, /* 5 */
Eric Andersen3cfa9ec2000-07-06 17:05:33 +000066#endif
Pavel Roskin5d513f72000-09-13 14:03:48 +000067#ifdef SIGIOT
68 { "IOT", SIGIOT }, /* 6, same as SIGABRT */
Eric Andersen3cfa9ec2000-07-06 17:05:33 +000069#endif
Pavel Roskin5d513f72000-09-13 14:03:48 +000070#ifdef SIGEMT
71 { "EMT", SIGEMT }, /* 7 (mips,alpha,sparc*) */
Eric Andersen3cfa9ec2000-07-06 17:05:33 +000072#endif
Pavel Roskin5d513f72000-09-13 14:03:48 +000073#ifdef SIGBUS
74 { "BUS", SIGBUS }, /* 7 (arm,i386,m68k,ppc), 10 (mips,alpha,sparc*) */
Eric Andersen3cfa9ec2000-07-06 17:05:33 +000075#endif
Pavel Roskin5d513f72000-09-13 14:03:48 +000076#ifdef SIGSYS
77 { "SYS", SIGSYS }, /* 12 (mips,alpha,sparc*) */
78#endif
79#ifdef SIGSTKFLT
80 { "STKFLT", SIGSTKFLT }, /* 16 (arm,i386,m68k,ppc) */
81#endif
82#ifdef SIGURG
83 { "URG", SIGURG }, /* 23 (arm,i386,m68k,ppc), 16 (alpha,sparc*), 21 (mips) */
84#endif
85#ifdef SIGIO
86 { "IO", SIGIO }, /* 29 (arm,i386,m68k,ppc), 23 (alpha,sparc*), 22 (mips) */
87#endif
88#ifdef SIGPOLL
89 { "POLL", SIGPOLL }, /* same as SIGIO */
90#endif
91#ifdef SIGCLD
92 { "CLD", SIGCLD }, /* same as SIGCHLD (mips) */
93#endif
94#ifdef SIGXCPU
95 { "XCPU", SIGXCPU }, /* 24 (arm,i386,m68k,ppc,alpha,sparc*), 30 (mips) */
96#endif
97#ifdef SIGXFSZ
98 { "XFSZ", SIGXFSZ }, /* 25 (arm,i386,m68k,ppc,alpha,sparc*), 31 (mips) */
99#endif
100#ifdef SIGVTALRM
101 { "VTALRM", SIGVTALRM }, /* 26 (arm,i386,m68k,ppc,alpha,sparc*), 28 (mips) */
102#endif
103#ifdef SIGPROF
104 { "PROF", SIGPROF }, /* 27 (arm,i386,m68k,ppc,alpha,sparc*), 29 (mips) */
105#endif
106#ifdef SIGPWR
107 { "PWR", SIGPWR }, /* 30 (arm,i386,m68k,ppc), 29 (alpha,sparc*), 19 (mips) */
108#endif
109#ifdef SIGINFO
110 { "INFO", SIGINFO }, /* 29 (alpha) */
111#endif
112#ifdef SIGLOST
113 { "LOST", SIGLOST }, /* 29 (arm,i386,m68k,ppc,sparc*) */
114#endif
115#ifdef SIGWINCH
116 { "WINCH", SIGWINCH }, /* 28 (arm,i386,m68k,ppc,alpha,sparc*), 20 (mips) */
117#endif
118#ifdef SIGUNUSED
119 { "UNUSED", SIGUNUSED }, /* 31 (arm,i386,m68k,ppc) */
Eric Andersencc8ed391999-10-05 16:24:54 +0000120#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121 {0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +0000122};
123
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124extern int kill_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000125{
Erik Andersen246cc6d2000-03-07 07:41:42 +0000126 int whichApp, sig = SIGTERM;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000127
Erik Andersen6273f652000-03-17 01:12:41 +0000128#ifdef BB_KILLALL
Erik Andersen246cc6d2000-03-07 07:41:42 +0000129 /* Figure out what we are trying to do here */
Matt Kraaie58771e2000-07-12 15:38:49 +0000130 whichApp = (strcmp(applet_name, "killall") == 0)? KILLALL : KILL;
Erik Andersen6273f652000-03-17 01:12:41 +0000131#else
132 whichApp = KILL;
Erik Andersen6273f652000-03-17 01:12:41 +0000133#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000134
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000135 argc--;
136 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137 /* Parse any options */
138 if (argc < 1)
Eric Andersen67991cf2001-02-14 21:23:06 +0000139 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140
141 while (argc > 0 && **argv == '-') {
142 while (*++(*argv)) {
143 switch (**argv) {
144 case 'l':
145 {
146 int col = 0;
147 const struct signal_name *s = signames;
148
149 while (s->name != 0) {
Eric Andersen20aab262001-07-19 22:28:02 +0000150 col += fprintf(stderr, "%2d) %-8s", s->number, s->name);
151 s++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152 if (col > 60) {
153 fprintf(stderr, "\n");
154 col = 0;
155 }
156 }
157 fprintf(stderr, "\n\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000158 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000159 }
160 break;
161 case '-':
Eric Andersen67991cf2001-02-14 21:23:06 +0000162 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 default:
164 {
165 if (isdigit(**argv)) {
166 sig = atoi(*argv);
167 if (sig < 0 || sig >= NSIG)
168 goto end;
169 else {
170 argc--;
171 argv++;
172 goto do_it_now;
173 }
174 } else {
175 const struct signal_name *s = signames;
176
177 while (s->name != 0) {
178 if (strcasecmp(s->name, *argv) == 0) {
179 sig = s->number;
180 argc--;
181 argv++;
182 goto do_it_now;
183 }
184 s++;
185 }
186 if (s->name == 0)
187 goto end;
188 }
189 }
190 }
191 argc--;
192 argv++;
193 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000194 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000195
Erik Andersene49d5ec2000-02-08 19:58:47 +0000196 do_it_now:
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000197
Erik Andersen246cc6d2000-03-07 07:41:42 +0000198 if (whichApp == KILL) {
199 /* Looks like they want to do a kill. Do that */
200 while (--argc >= 0) {
201 int pid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202
Erik Andersen246cc6d2000-03-07 07:41:42 +0000203 if (!isdigit(**argv))
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000204 perror_msg_and_die( "Bad PID");
Erik Andersen246cc6d2000-03-07 07:41:42 +0000205 pid = strtol(*argv, NULL, 0);
206 if (kill(pid, sig) != 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000207 perror_msg_and_die( "Could not kill pid '%d'", pid);
Erik Andersen246cc6d2000-03-07 07:41:42 +0000208 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000209 }
Erik Andersen6273f652000-03-17 01:12:41 +0000210 }
211#ifdef BB_KILLALL
212 else {
Pavel Roskin700a5ae2000-06-06 18:10:17 +0000213 int all_found = TRUE;
Erik Andersen0a64de92000-04-18 20:00:03 +0000214 pid_t myPid=getpid();
Erik Andersen246cc6d2000-03-07 07:41:42 +0000215 /* Looks like they want to do a killall. Do that */
216 while (--argc >= 0) {
Erik Andersen825aead2000-04-07 06:00:07 +0000217 pid_t* pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000218
Mark Whitleyf57c9442000-12-07 19:56:48 +0000219 pidList = find_pid_by_name( *argv);
Eric Andersen8e75f6d2001-07-05 16:27:34 +0000220 if (!pidList || *pidList<=0) {
Pavel Roskin700a5ae2000-06-06 18:10:17 +0000221 all_found = FALSE;
Eric Andersen8e75f6d2001-07-05 16:27:34 +0000222 error_msg_and_die( "%s: no process killed", *argv);
Pavel Roskin700a5ae2000-06-06 18:10:17 +0000223 }
Eric Andersen86ab8a32000-06-02 03:21:42 +0000224
Erik Andersen0a64de92000-04-18 20:00:03 +0000225 for(; pidList && *pidList!=0; pidList++) {
226 if (*pidList==myPid)
227 continue;
Erik Andersen825aead2000-04-07 06:00:07 +0000228 if (kill(*pidList, sig) != 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000229 perror_msg_and_die( "Could not kill pid '%d'", *pidList);
Erik Andersen246cc6d2000-03-07 07:41:42 +0000230 }
Erik Andersen825aead2000-04-07 06:00:07 +0000231 /* Note that we don't bother to free the memory
Mark Whitleyf57c9442000-12-07 19:56:48 +0000232 * allocated in find_pid_by_name(). It will be freed
Erik Andersen825aead2000-04-07 06:00:07 +0000233 * upon exit, so we can save a byte or two */
Erik Andersen246cc6d2000-03-07 07:41:42 +0000234 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000236 if (all_found == FALSE)
237 return EXIT_FAILURE;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000238 }
Erik Andersen6273f652000-03-17 01:12:41 +0000239#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +0000240
Matt Kraai3e856ce2000-12-01 02:55:13 +0000241 return EXIT_SUCCESS;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000242
Eric Andersena7093171999-10-23 05:42:08 +0000243
Erik Andersene49d5ec2000-02-08 19:58:47 +0000244 end:
Matt Kraaidd19c692001-01-31 19:00:21 +0000245 error_msg_and_die( "bad signal name: %s", *argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000246}