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> |
Eric Andersen | a709317 | 1999-10-23 05:42:08 +0000 | [diff] [blame] | 28 | #include <ctype.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 29 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 30 | const char kill_usage[] = "kill [-signal] process-id [process-id ...]\n"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 31 | |
| 32 | struct signal_name { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 33 | const char *name; |
| 34 | int number; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | const struct signal_name signames[] = { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 38 | {"HUP", SIGHUP}, |
| 39 | {"INT", SIGINT}, |
| 40 | {"QUIT", SIGQUIT}, |
| 41 | {"ILL", SIGILL}, |
| 42 | {"TRAP", SIGTRAP}, |
| 43 | {"ABRT", SIGABRT}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 44 | #ifndef __alpha__ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 45 | {"IOT", SIGIOT}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 46 | #endif |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 47 | #if defined(__sparc__) || defined(__alpha__) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 48 | {"EMT", SIGEMT}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 49 | #else |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 50 | {"BUS", SIGBUS}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 51 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 52 | {"FPE", SIGFPE}, |
| 53 | {"KILL", SIGKILL}, |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 54 | #if defined(__sparc__) || defined(__alpha__) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 55 | {"BUS", SIGBUS}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 56 | #else |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 57 | {"USR1", SIGUSR1}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 58 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 59 | {"SEGV", SIGSEGV}, |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 60 | #if defined(__sparc__) || defined(__alpha__) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 61 | {"SYS", SIGSYS}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 62 | #else |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 63 | {"USR2", SIGUSR2}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 64 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 65 | {"PIPE", SIGPIPE}, |
| 66 | {"ALRM", SIGALRM}, |
| 67 | {"TERM", SIGTERM}, |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 68 | #if defined(__sparc__) || defined(__alpha__) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 69 | {"URG", SIGURG}, |
| 70 | {"STOP", SIGSTOP}, |
| 71 | {"TSTP", SIGTSTP}, |
| 72 | {"CONT", SIGCONT}, |
| 73 | {"CHLD", SIGCHLD}, |
| 74 | {"TTIN", SIGTTIN}, |
| 75 | {"TTOU", SIGTTOU}, |
| 76 | {"IO", SIGIO}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 77 | # ifndef __alpha__ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 78 | {"POLL", SIGIO}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 79 | # endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 80 | {"XCPU", SIGXCPU}, |
| 81 | {"XFSZ", SIGXFSZ}, |
| 82 | {"VTALRM", SIGVTALRM}, |
| 83 | {"PROF", SIGPROF}, |
| 84 | {"WINCH", SIGWINCH}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 85 | # ifdef __alpha__ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 86 | {"INFO", SIGINFO}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 87 | # else |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 88 | {"LOST", SIGLOST}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 89 | # endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 90 | {"USR1", SIGUSR1}, |
| 91 | {"USR2", SIGUSR2}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 92 | #else |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 93 | {"STKFLT", SIGSTKFLT}, |
| 94 | {"CHLD", SIGCHLD}, |
| 95 | {"CONT", SIGCONT}, |
| 96 | {"STOP", SIGSTOP}, |
| 97 | {"TSTP", SIGTSTP}, |
| 98 | {"TTIN", SIGTTIN}, |
| 99 | {"TTOU", SIGTTOU}, |
| 100 | {"URG", SIGURG}, |
| 101 | {"XCPU", SIGXCPU}, |
| 102 | {"XFSZ", SIGXFSZ}, |
| 103 | {"VTALRM", SIGVTALRM}, |
| 104 | {"PROF", SIGPROF}, |
| 105 | {"WINCH", SIGWINCH}, |
| 106 | {"IO", SIGIO}, |
| 107 | {"POLL", SIGPOLL}, |
| 108 | {"PWR", SIGPWR}, |
| 109 | {"UNUSED", SIGUNUSED}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 110 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 111 | {0, 0} |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 114 | extern int kill_main (int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 115 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 116 | int sig = SIGTERM; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 117 | |
Eric Andersen | a709317 | 1999-10-23 05:42:08 +0000 | [diff] [blame] | 118 | if ( argc < 2 ) |
| 119 | usage (kill_usage); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 120 | |
Eric Andersen | a709317 | 1999-10-23 05:42:08 +0000 | [diff] [blame] | 121 | if ( **(argv+1) == '-' ) { |
| 122 | if (isdigit( *(*(++argv)+1) )) { |
| 123 | sig = atoi (*argv); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 124 | if (sig < 0 || sig >= NSIG) |
| 125 | goto end; |
Eric Andersen | a709317 | 1999-10-23 05:42:08 +0000 | [diff] [blame] | 126 | } |
| 127 | else { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 128 | const struct signal_name *s = signames; |
Eric Andersen | a709317 | 1999-10-23 05:42:08 +0000 | [diff] [blame] | 129 | while (s->name != 0) { |
| 130 | if (strcasecmp (s->name, *argv+1) == 0) { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 131 | sig = s->number; |
| 132 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 133 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 134 | s++; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 135 | } |
Eric Andersen | a709317 | 1999-10-23 05:42:08 +0000 | [diff] [blame] | 136 | if (s->name == 0) |
| 137 | goto end; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 138 | } |
Eric Andersen | a709317 | 1999-10-23 05:42:08 +0000 | [diff] [blame] | 139 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 140 | |
Eric Andersen | a709317 | 1999-10-23 05:42:08 +0000 | [diff] [blame] | 141 | while (--argc > 1) { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 142 | int pid; |
Eric Andersen | a709317 | 1999-10-23 05:42:08 +0000 | [diff] [blame] | 143 | if (! isdigit( **(++argv))) { |
| 144 | fprintf(stderr, "bad PID: %s\n", *argv); |
| 145 | exit( FALSE); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 146 | } |
Eric Andersen | a709317 | 1999-10-23 05:42:08 +0000 | [diff] [blame] | 147 | pid = atoi (*argv); |
| 148 | if (kill (pid, sig) != 0) { |
| 149 | perror (*argv); |
| 150 | exit ( FALSE); |
| 151 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 152 | } |
Eric Andersen | a709317 | 1999-10-23 05:42:08 +0000 | [diff] [blame] | 153 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 154 | end: |
Eric Andersen | a709317 | 1999-10-23 05:42:08 +0000 | [diff] [blame] | 155 | fprintf(stderr, "bad signal name: %s\n", *argv); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 156 | exit (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 157 | } |
Eric Andersen | a709317 | 1999-10-23 05:42:08 +0000 | [diff] [blame] | 158 | |
| 159 | |