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
41 const struct signal_name signames[] = {
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 */
64 { "TRAP", SIGTRAP }, /* 5 */
67 { "IOT", SIGIOT }, /* 6, same as SIGABRT */
70 { "EMT", SIGEMT }, /* 7 (mips,alpha,sparc*) */
73 { "BUS", SIGBUS }, /* 7 (arm,i386,m68k,ppc), 10 (mips,alpha,sparc*) */
76 { "SYS", SIGSYS }, /* 12 (mips,alpha,sparc*) */
79 { "STKFLT", SIGSTKFLT }, /* 16 (arm,i386,m68k,ppc) */
82 { "URG", SIGURG }, /* 23 (arm,i386,m68k,ppc), 16 (alpha,sparc*), 21 (mips) */
85 { "IO", SIGIO }, /* 29 (arm,i386,m68k,ppc), 23 (alpha,sparc*), 22 (mips) */
88 { "POLL", SIGPOLL }, /* same as SIGIO */
91 { "CLD", SIGCLD }, /* same as SIGCHLD (mips) */
94 { "XCPU", SIGXCPU }, /* 24 (arm,i386,m68k,ppc,alpha,sparc*), 30 (mips) */
97 { "XFSZ", SIGXFSZ }, /* 25 (arm,i386,m68k,ppc,alpha,sparc*), 31 (mips) */
100 { "VTALRM", SIGVTALRM }, /* 26 (arm,i386,m68k,ppc,alpha,sparc*), 28 (mips) */
103 { "PROF", SIGPROF }, /* 27 (arm,i386,m68k,ppc,alpha,sparc*), 29 (mips) */
106 { "PWR", SIGPWR }, /* 30 (arm,i386,m68k,ppc), 29 (alpha,sparc*), 19 (mips) */
109 { "INFO", SIGINFO }, /* 29 (alpha) */
112 { "LOST", SIGLOST }, /* 29 (arm,i386,m68k,ppc,sparc*) */
115 { "WINCH", SIGWINCH }, /* 28 (arm,i386,m68k,ppc,alpha,sparc*), 20 (mips) */
118 { "UNUSED", SIGUNUSED }, /* 31 (arm,i386,m68k,ppc) */
123 extern int kill_main(int argc, char **argv)
125 int whichApp, sig = SIGTERM;
126 const char *appUsage;
129 /* Figure out what we are trying to do here */
130 whichApp = (strcmp(applet_name, "killall") == 0)? KILLALL : KILL;
131 appUsage = (whichApp == KILLALL)? killall_usage : kill_usage;
134 appUsage = kill_usage;
139 /* Parse any options */
143 while (argc > 0 && **argv == '-') {
149 const struct signal_name *s = signames;
151 while (s->name != 0) {
153 fprintf(stderr, "%2d) %-8s", s->number,
156 fprintf(stderr, "\n");
160 fprintf(stderr, "\n\n");
168 if (isdigit(**argv)) {
170 if (sig < 0 || sig >= NSIG)
178 const struct signal_name *s = signames;
180 while (s->name != 0) {
181 if (strcasecmp(s->name, *argv) == 0) {
201 if (whichApp == KILL) {
202 /* Looks like they want to do a kill. Do that */
203 while (--argc >= 0) {
206 if (!isdigit(**argv))
207 fatalError( "Bad PID: %s\n", strerror(errno));
208 pid = strtol(*argv, NULL, 0);
209 if (kill(pid, sig) != 0)
210 fatalError( "Could not kill pid '%d': %s\n", pid, strerror(errno));
216 int all_found = TRUE;
217 pid_t myPid=getpid();
218 /* Looks like they want to do a killall. Do that */
219 while (--argc >= 0) {
222 pidList = findPidByName( *argv);
225 errorMsg( "%s: no process killed\n", *argv);
228 for(; pidList && *pidList!=0; pidList++) {
231 if (kill(*pidList, sig) != 0)
232 fatalError( "Could not kill pid '%d': %s\n", *pidList, strerror(errno));
234 /* Note that we don't bother to free the memory
235 * allocated in findPidByName(). It will be freed
236 * upon exit, so we can save a byte or two */
247 fatalError( "bad signal name: %s\n", *argv);