blob: 19ca187a758e275c1accb5dcbce695ed27dc0e0b [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 Andersen3570a342000-09-25 21:45:58 +000024#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000025#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 <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000032
Mark Whitley59ab0252001-01-23 22:30:04 +000033static const int KILL = 0;
34static const int KILLALL = 1;
Eric Andersencc8ed391999-10-05 16:24:54 +000035
36struct signal_name {
Erik Andersene49d5ec2000-02-08 19:58:47 +000037 const char *name;
38 int number;
Eric Andersencc8ed391999-10-05 16:24:54 +000039};
40
41const struct signal_name signames[] = {
Pavel Roskin5d513f72000-09-13 14:03:48 +000042 /* POSIX signals */
43 { "HUP", SIGHUP }, /* 1 */
44 { "INT", SIGINT }, /* 2 */
45 { "QUIT", SIGQUIT }, /* 3 */
46 { "ILL", SIGILL }, /* 4 */
47 { "ABRT", SIGABRT }, /* 6 */
48 { "FPE", SIGFPE }, /* 8 */
49 { "KILL", SIGKILL }, /* 9 */
50 { "SEGV", SIGSEGV }, /* 11 */
51 { "PIPE", SIGPIPE }, /* 13 */
52 { "ALRM", SIGALRM }, /* 14 */
53 { "TERM", SIGTERM }, /* 15 */
54 { "USR1", SIGUSR1 }, /* 10 (arm,i386,m68k,ppc), 30 (alpha,sparc*), 16 (mips) */
55 { "USR2", SIGUSR2 }, /* 12 (arm,i386,m68k,ppc), 31 (alpha,sparc*), 17 (mips) */
56 { "CHLD", SIGCHLD }, /* 17 (arm,i386,m68k,ppc), 20 (alpha,sparc*), 18 (mips) */
57 { "CONT", SIGCONT }, /* 18 (arm,i386,m68k,ppc), 19 (alpha,sparc*), 25 (mips) */
58 { "STOP", SIGSTOP }, /* 19 (arm,i386,m68k,ppc), 17 (alpha,sparc*), 23 (mips) */
59 { "TSTP", SIGTSTP }, /* 20 (arm,i386,m68k,ppc), 18 (alpha,sparc*), 24 (mips) */
60 { "TTIN", SIGTTIN }, /* 21 (arm,i386,m68k,ppc,alpha,sparc*), 26 (mips) */
61 { "TTOU", SIGTTOU }, /* 22 (arm,i386,m68k,ppc,alpha,sparc*), 27 (mips) */
62 /* Miscellaneous other signals */
63#ifdef SIGTRAP
64 { "TRAP", SIGTRAP }, /* 5 */
Eric Andersen3cfa9ec2000-07-06 17:05:33 +000065#endif
Pavel Roskin5d513f72000-09-13 14:03:48 +000066#ifdef SIGIOT
67 { "IOT", SIGIOT }, /* 6, same as SIGABRT */
Eric Andersen3cfa9ec2000-07-06 17:05:33 +000068#endif
Pavel Roskin5d513f72000-09-13 14:03:48 +000069#ifdef SIGEMT
70 { "EMT", SIGEMT }, /* 7 (mips,alpha,sparc*) */
Eric Andersen3cfa9ec2000-07-06 17:05:33 +000071#endif
Pavel Roskin5d513f72000-09-13 14:03:48 +000072#ifdef SIGBUS
73 { "BUS", SIGBUS }, /* 7 (arm,i386,m68k,ppc), 10 (mips,alpha,sparc*) */
Eric Andersen3cfa9ec2000-07-06 17:05:33 +000074#endif
Pavel Roskin5d513f72000-09-13 14:03:48 +000075#ifdef SIGSYS
76 { "SYS", SIGSYS }, /* 12 (mips,alpha,sparc*) */
77#endif
78#ifdef SIGSTKFLT
79 { "STKFLT", SIGSTKFLT }, /* 16 (arm,i386,m68k,ppc) */
80#endif
81#ifdef SIGURG
82 { "URG", SIGURG }, /* 23 (arm,i386,m68k,ppc), 16 (alpha,sparc*), 21 (mips) */
83#endif
84#ifdef SIGIO
85 { "IO", SIGIO }, /* 29 (arm,i386,m68k,ppc), 23 (alpha,sparc*), 22 (mips) */
86#endif
87#ifdef SIGPOLL
88 { "POLL", SIGPOLL }, /* same as SIGIO */
89#endif
90#ifdef SIGCLD
91 { "CLD", SIGCLD }, /* same as SIGCHLD (mips) */
92#endif
93#ifdef SIGXCPU
94 { "XCPU", SIGXCPU }, /* 24 (arm,i386,m68k,ppc,alpha,sparc*), 30 (mips) */
95#endif
96#ifdef SIGXFSZ
97 { "XFSZ", SIGXFSZ }, /* 25 (arm,i386,m68k,ppc,alpha,sparc*), 31 (mips) */
98#endif
99#ifdef SIGVTALRM
100 { "VTALRM", SIGVTALRM }, /* 26 (arm,i386,m68k,ppc,alpha,sparc*), 28 (mips) */
101#endif
102#ifdef SIGPROF
103 { "PROF", SIGPROF }, /* 27 (arm,i386,m68k,ppc,alpha,sparc*), 29 (mips) */
104#endif
105#ifdef SIGPWR
106 { "PWR", SIGPWR }, /* 30 (arm,i386,m68k,ppc), 29 (alpha,sparc*), 19 (mips) */
107#endif
108#ifdef SIGINFO
109 { "INFO", SIGINFO }, /* 29 (alpha) */
110#endif
111#ifdef SIGLOST
112 { "LOST", SIGLOST }, /* 29 (arm,i386,m68k,ppc,sparc*) */
113#endif
114#ifdef SIGWINCH
115 { "WINCH", SIGWINCH }, /* 28 (arm,i386,m68k,ppc,alpha,sparc*), 20 (mips) */
116#endif
117#ifdef SIGUNUSED
118 { "UNUSED", SIGUNUSED }, /* 31 (arm,i386,m68k,ppc) */
Eric Andersencc8ed391999-10-05 16:24:54 +0000119#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120 {0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +0000121};
122
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123extern int kill_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000124{
Erik Andersen246cc6d2000-03-07 07:41:42 +0000125 int whichApp, sig = SIGTERM;
126 const char *appUsage;
127
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 Andersen246cc6d2000-03-07 07:41:42 +0000131 appUsage = (whichApp == KILLALL)? killall_usage : kill_usage;
Erik Andersen6273f652000-03-17 01:12:41 +0000132#else
133 whichApp = KILL;
134 appUsage = kill_usage;
135#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000136
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000137 argc--;
138 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000139 /* Parse any options */
140 if (argc < 1)
Erik Andersen246cc6d2000-03-07 07:41:42 +0000141 usage(appUsage);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142
143 while (argc > 0 && **argv == '-') {
144 while (*++(*argv)) {
145 switch (**argv) {
146 case 'l':
147 {
148 int col = 0;
149 const struct signal_name *s = signames;
150
151 while (s->name != 0) {
152 col +=
153 fprintf(stderr, "%2d) %-8s", s->number,
154 (s++)->name);
155 if (col > 60) {
156 fprintf(stderr, "\n");
157 col = 0;
158 }
159 }
160 fprintf(stderr, "\n\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000161 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000162 }
163 break;
164 case '-':
Erik Andersen246cc6d2000-03-07 07:41:42 +0000165 usage(appUsage);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000166 default:
167 {
168 if (isdigit(**argv)) {
169 sig = atoi(*argv);
170 if (sig < 0 || sig >= NSIG)
171 goto end;
172 else {
173 argc--;
174 argv++;
175 goto do_it_now;
176 }
177 } else {
178 const struct signal_name *s = signames;
179
180 while (s->name != 0) {
181 if (strcasecmp(s->name, *argv) == 0) {
182 sig = s->number;
183 argc--;
184 argv++;
185 goto do_it_now;
186 }
187 s++;
188 }
189 if (s->name == 0)
190 goto end;
191 }
192 }
193 }
194 argc--;
195 argv++;
196 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000197 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000198
Erik Andersene49d5ec2000-02-08 19:58:47 +0000199 do_it_now:
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000200
Erik Andersen246cc6d2000-03-07 07:41:42 +0000201 if (whichApp == KILL) {
202 /* Looks like they want to do a kill. Do that */
203 while (--argc >= 0) {
204 int pid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205
Erik Andersen246cc6d2000-03-07 07:41:42 +0000206 if (!isdigit(**argv))
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000207 perror_msg_and_die( "Bad PID");
Erik Andersen246cc6d2000-03-07 07:41:42 +0000208 pid = strtol(*argv, NULL, 0);
209 if (kill(pid, sig) != 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000210 perror_msg_and_die( "Could not kill pid '%d'", pid);
Erik Andersen246cc6d2000-03-07 07:41:42 +0000211 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000212 }
Erik Andersen6273f652000-03-17 01:12:41 +0000213 }
214#ifdef BB_KILLALL
215 else {
Pavel Roskin700a5ae2000-06-06 18:10:17 +0000216 int all_found = TRUE;
Erik Andersen0a64de92000-04-18 20:00:03 +0000217 pid_t myPid=getpid();
Erik Andersen246cc6d2000-03-07 07:41:42 +0000218 /* Looks like they want to do a killall. Do that */
219 while (--argc >= 0) {
Erik Andersen825aead2000-04-07 06:00:07 +0000220 pid_t* pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000221
Mark Whitleyf57c9442000-12-07 19:56:48 +0000222 pidList = find_pid_by_name( *argv);
Pavel Roskin700a5ae2000-06-06 18:10:17 +0000223 if (!pidList) {
224 all_found = FALSE;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000225 error_msg( "%s: no process killed\n", *argv);
Pavel Roskin700a5ae2000-06-06 18:10:17 +0000226 }
Eric Andersen86ab8a32000-06-02 03:21:42 +0000227
Erik Andersen0a64de92000-04-18 20:00:03 +0000228 for(; pidList && *pidList!=0; pidList++) {
229 if (*pidList==myPid)
230 continue;
Erik Andersen825aead2000-04-07 06:00:07 +0000231 if (kill(*pidList, sig) != 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000232 perror_msg_and_die( "Could not kill pid '%d'", *pidList);
Erik Andersen246cc6d2000-03-07 07:41:42 +0000233 }
Erik Andersen825aead2000-04-07 06:00:07 +0000234 /* Note that we don't bother to free the memory
Mark Whitleyf57c9442000-12-07 19:56:48 +0000235 * allocated in find_pid_by_name(). It will be freed
Erik Andersen825aead2000-04-07 06:00:07 +0000236 * upon exit, so we can save a byte or two */
Erik Andersen246cc6d2000-03-07 07:41:42 +0000237 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000238 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000239 if (all_found == FALSE)
240 return EXIT_FAILURE;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000241 }
Erik Andersen6273f652000-03-17 01:12:41 +0000242#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +0000243
Matt Kraai3e856ce2000-12-01 02:55:13 +0000244 return EXIT_SUCCESS;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000245
Eric Andersena7093171999-10-23 05:42:08 +0000246
Erik Andersene49d5ec2000-02-08 19:58:47 +0000247 end:
Mark Whitleyf57c9442000-12-07 19:56:48 +0000248 error_msg_and_die( "bad signal name: %s\n", *argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000249}