Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mini kill implementation for busybox |
| 3 | * |
| 4 | * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 23 | #include "internal.h" |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <unistd.h> |
| 27 | #include <signal.h> |
| 28 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 29 | const char kill_usage[] = "kill [-signal] process-id [process-id ...]\n"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 30 | |
| 31 | struct signal_name { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 32 | const char *name; |
| 33 | int number; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | const struct signal_name signames[] = { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 37 | {"HUP", SIGHUP}, |
| 38 | {"INT", SIGINT}, |
| 39 | {"QUIT", SIGQUIT}, |
| 40 | {"ILL", SIGILL}, |
| 41 | {"TRAP", SIGTRAP}, |
| 42 | {"ABRT", SIGABRT}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 43 | #ifndef __alpha__ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 44 | {"IOT", SIGIOT}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 45 | #endif |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 46 | #if defined(__sparc__) || defined(__alpha__) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 47 | {"EMT", SIGEMT}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 48 | #else |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 49 | {"BUS", SIGBUS}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 50 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 51 | {"FPE", SIGFPE}, |
| 52 | {"KILL", SIGKILL}, |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 53 | #if defined(__sparc__) || defined(__alpha__) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 54 | {"BUS", SIGBUS}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 55 | #else |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 56 | {"USR1", SIGUSR1}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 57 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 58 | {"SEGV", SIGSEGV}, |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 59 | #if defined(__sparc__) || defined(__alpha__) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 60 | {"SYS", SIGSYS}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 61 | #else |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 62 | {"USR2", SIGUSR2}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 63 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 64 | {"PIPE", SIGPIPE}, |
| 65 | {"ALRM", SIGALRM}, |
| 66 | {"TERM", SIGTERM}, |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 67 | #if defined(__sparc__) || defined(__alpha__) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 68 | {"URG", SIGURG}, |
| 69 | {"STOP", SIGSTOP}, |
| 70 | {"TSTP", SIGTSTP}, |
| 71 | {"CONT", SIGCONT}, |
| 72 | {"CHLD", SIGCHLD}, |
| 73 | {"TTIN", SIGTTIN}, |
| 74 | {"TTOU", SIGTTOU}, |
| 75 | {"IO", SIGIO}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 76 | # ifndef __alpha__ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 77 | {"POLL", SIGIO}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 78 | # endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 79 | {"XCPU", SIGXCPU}, |
| 80 | {"XFSZ", SIGXFSZ}, |
| 81 | {"VTALRM", SIGVTALRM}, |
| 82 | {"PROF", SIGPROF}, |
| 83 | {"WINCH", SIGWINCH}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 84 | # ifdef __alpha__ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 85 | {"INFO", SIGINFO}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 86 | # else |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 87 | {"LOST", SIGLOST}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 88 | # endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 89 | {"USR1", SIGUSR1}, |
| 90 | {"USR2", SIGUSR2}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 91 | #else |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 92 | {"STKFLT", SIGSTKFLT}, |
| 93 | {"CHLD", SIGCHLD}, |
| 94 | {"CONT", SIGCONT}, |
| 95 | {"STOP", SIGSTOP}, |
| 96 | {"TSTP", SIGTSTP}, |
| 97 | {"TTIN", SIGTTIN}, |
| 98 | {"TTOU", SIGTTOU}, |
| 99 | {"URG", SIGURG}, |
| 100 | {"XCPU", SIGXCPU}, |
| 101 | {"XFSZ", SIGXFSZ}, |
| 102 | {"VTALRM", SIGVTALRM}, |
| 103 | {"PROF", SIGPROF}, |
| 104 | {"WINCH", SIGWINCH}, |
| 105 | {"IO", SIGIO}, |
| 106 | {"POLL", SIGPOLL}, |
| 107 | {"PWR", SIGPWR}, |
| 108 | {"UNUSED", SIGUNUSED}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 109 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 110 | {0, 0} |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 113 | extern int kill_main (int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 114 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 115 | int had_error = 0; |
| 116 | int sig = SIGTERM; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 117 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 118 | |
| 119 | |
| 120 | if (argv[1][0] == '-') { |
| 121 | if (argv[1][1] >= '0' && argv[1][1] <= '9') { |
| 122 | sig = atoi (&argv[1][1]); |
| 123 | if (sig < 0 || sig >= NSIG) |
| 124 | goto end; |
| 125 | } else { |
| 126 | const struct signal_name *s = signames; |
| 127 | for (;;) { |
| 128 | if (strcmp (s->name, &argv[1][1]) == 0) { |
| 129 | sig = s->number; |
| 130 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 131 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 132 | s++; |
| 133 | if (s->name == 0) |
| 134 | goto end; |
| 135 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 136 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 137 | argv++; |
| 138 | argc--; |
| 139 | |
| 140 | } |
| 141 | while (argc > 1) { |
| 142 | int pid; |
| 143 | if (argv[1][0] < '0' || argv[1][0] > '9') |
| 144 | goto end; |
| 145 | pid = atoi (argv[1]); |
| 146 | if (kill (pid, sig) != 0) { |
| 147 | had_error = 1; |
| 148 | perror (argv[1]); |
| 149 | } |
| 150 | argv++; |
| 151 | argc--; |
| 152 | } |
| 153 | if (had_error) { |
| 154 | end: |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 155 | usage (kill_usage); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 156 | } |
| 157 | exit (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 158 | } |