1 /* vi: set sw=4 ts=4: */
3 * Mini kill/killall implementation for busybox
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
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.
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.
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
34 static const int KILL = 0;
35 static const int KILLALL = 1;
42 static const struct signal_name signames[] = {
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 */
65 { "TRAP", SIGTRAP }, /* 5 */
68 { "IOT", SIGIOT }, /* 6, same as SIGABRT */
71 { "EMT", SIGEMT }, /* 7 (mips,alpha,sparc*) */
74 { "BUS", SIGBUS }, /* 7 (arm,i386,m68k,ppc), 10 (mips,alpha,sparc*) */
77 { "SYS", SIGSYS }, /* 12 (mips,alpha,sparc*) */
80 { "STKFLT", SIGSTKFLT }, /* 16 (arm,i386,m68k,ppc) */
83 { "URG", SIGURG }, /* 23 (arm,i386,m68k,ppc), 16 (alpha,sparc*), 21 (mips) */
86 { "IO", SIGIO }, /* 29 (arm,i386,m68k,ppc), 23 (alpha,sparc*), 22 (mips) */
89 { "POLL", SIGPOLL }, /* same as SIGIO */
92 { "CLD", SIGCLD }, /* same as SIGCHLD (mips) */
95 { "XCPU", SIGXCPU }, /* 24 (arm,i386,m68k,ppc,alpha,sparc*), 30 (mips) */
98 { "XFSZ", SIGXFSZ }, /* 25 (arm,i386,m68k,ppc,alpha,sparc*), 31 (mips) */
101 { "VTALRM", SIGVTALRM }, /* 26 (arm,i386,m68k,ppc,alpha,sparc*), 28 (mips) */
104 { "PROF", SIGPROF }, /* 27 (arm,i386,m68k,ppc,alpha,sparc*), 29 (mips) */
107 { "PWR", SIGPWR }, /* 30 (arm,i386,m68k,ppc), 29 (alpha,sparc*), 19 (mips) */
110 { "INFO", SIGINFO }, /* 29 (alpha) */
113 { "LOST", SIGLOST }, /* 29 (arm,i386,m68k,ppc,sparc*) */
116 { "WINCH", SIGWINCH }, /* 28 (arm,i386,m68k,ppc,alpha,sparc*), 20 (mips) */
119 { "UNUSED", SIGUNUSED }, /* 31 (arm,i386,m68k,ppc) */
124 extern int kill_main(int argc, char **argv)
126 int whichApp, sig = SIGTERM;
129 /* Figure out what we are trying to do here */
130 whichApp = (strcmp(applet_name, "killall") == 0)? KILLALL : KILL;
137 /* Parse any options */
141 while (argc > 0 && **argv == '-') {
147 const struct signal_name *s = signames;
149 while (s->name != 0) {
150 col += fprintf(stderr, "%2d) %-8s", s->number, s->name);
153 fprintf(stderr, "\n");
157 fprintf(stderr, "\n\n");
165 if (isdigit(**argv)) {
167 if (sig < 0 || sig >= NSIG)
175 const struct signal_name *s = signames;
177 while (s->name != 0) {
178 if (strcasecmp(s->name, *argv) == 0) {
198 if (whichApp == KILL) {
199 /* Looks like they want to do a kill. Do that */
200 while (--argc >= 0) {
203 if (!isdigit(**argv))
204 perror_msg_and_die( "Bad PID");
205 pid = strtol(*argv, NULL, 0);
206 if (kill(pid, sig) != 0)
207 perror_msg_and_die( "Could not kill pid '%d'", pid);
213 int all_found = TRUE;
214 pid_t myPid=getpid();
215 /* Looks like they want to do a killall. Do that */
216 while (--argc >= 0) {
219 pidList = find_pid_by_name( *argv);
220 if (!pidList || *pidList<=0) {
222 error_msg_and_die( "%s: no process killed", *argv);
225 for(; pidList && *pidList!=0; pidList++) {
228 if (kill(*pidList, sig) != 0)
229 perror_msg_and_die( "Could not kill pid '%d'", *pidList);
231 /* Note that we don't bother to free the memory
232 * allocated in find_pid_by_name(). It will be freed
233 * upon exit, so we can save a byte or two */
236 if (all_found == FALSE)
245 error_msg_and_die( "bad signal name: %s", *argv);