Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini kill implementation for busybox |
| 4 | * |
| 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 24 | #include "internal.h" |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 27 | #include <errno.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | #include <signal.h> |
Eric Andersen | a709317 | 1999-10-23 05:42:08 +0000 | [diff] [blame] | 30 | #include <ctype.h> |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 31 | #include <sys/stat.h> |
| 32 | #include <unistd.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 33 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 34 | static const char *kill_usage = |
| 35 | "kill [-signal] process-id [process-id ...]\n\n" |
| 36 | "Send a signal (default is SIGTERM) to the specified process(es).\n\n" |
| 37 | "Options:\n" "\t-l\tList all signal names and numbers.\n\n"; |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 38 | |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 39 | #ifdef BB_KILLALL |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 40 | static const char *killall_usage = |
| 41 | "killall [-signal] process-name [process-name ...]\n\n" |
| 42 | "Send a signal (default is SIGTERM) to the specified process(es).\n\n" |
| 43 | "Options:\n" "\t-l\tList all signal names and numbers.\n\n"; |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 44 | #endif |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 45 | |
| 46 | #define KILL 0 |
| 47 | #define KILLALL 1 |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 48 | |
| 49 | struct signal_name { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | const char *name; |
| 51 | int number; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | const struct signal_name signames[] = { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | {"HUP", SIGHUP}, |
| 56 | {"INT", SIGINT}, |
| 57 | {"QUIT", SIGQUIT}, |
| 58 | {"ILL", SIGILL}, |
| 59 | {"TRAP", SIGTRAP}, |
| 60 | {"ABRT", SIGABRT}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 61 | #ifndef __alpha__ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 62 | {"IOT", SIGIOT}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 63 | #endif |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 64 | #if defined(__sparc__) || defined(__alpha__) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 65 | {"EMT", SIGEMT}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 66 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 67 | {"BUS", SIGBUS}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 68 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 69 | {"FPE", SIGFPE}, |
| 70 | {"KILL", SIGKILL}, |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 71 | #if defined(__sparc__) || defined(__alpha__) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | {"BUS", SIGBUS}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 73 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | {"USR1", SIGUSR1}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 75 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 76 | {"SEGV", SIGSEGV}, |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 77 | #if defined(__sparc__) || defined(__alpha__) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 78 | {"SYS", SIGSYS}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 79 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 80 | {"USR2", SIGUSR2}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 81 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 82 | {"PIPE", SIGPIPE}, |
| 83 | {"ALRM", SIGALRM}, |
| 84 | {"TERM", SIGTERM}, |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 85 | #if defined(__sparc__) || defined(__alpha__) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 86 | {"URG", SIGURG}, |
| 87 | {"STOP", SIGSTOP}, |
| 88 | {"TSTP", SIGTSTP}, |
| 89 | {"CONT", SIGCONT}, |
| 90 | {"CHLD", SIGCHLD}, |
| 91 | {"TTIN", SIGTTIN}, |
| 92 | {"TTOU", SIGTTOU}, |
| 93 | {"IO", SIGIO}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 94 | # ifndef __alpha__ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 95 | {"POLL", SIGIO}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 96 | # endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 97 | {"XCPU", SIGXCPU}, |
| 98 | {"XFSZ", SIGXFSZ}, |
| 99 | {"VTALRM", SIGVTALRM}, |
| 100 | {"PROF", SIGPROF}, |
| 101 | {"WINCH", SIGWINCH}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 102 | # ifdef __alpha__ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 103 | {"INFO", SIGINFO}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 104 | # else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 105 | {"LOST", SIGLOST}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 106 | # endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 107 | {"USR1", SIGUSR1}, |
| 108 | {"USR2", SIGUSR2}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 109 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 110 | {"STKFLT", SIGSTKFLT}, |
| 111 | {"CHLD", SIGCHLD}, |
| 112 | {"CONT", SIGCONT}, |
| 113 | {"STOP", SIGSTOP}, |
| 114 | {"TSTP", SIGTSTP}, |
| 115 | {"TTIN", SIGTTIN}, |
| 116 | {"TTOU", SIGTTOU}, |
| 117 | {"URG", SIGURG}, |
| 118 | {"XCPU", SIGXCPU}, |
| 119 | {"XFSZ", SIGXFSZ}, |
| 120 | {"VTALRM", SIGVTALRM}, |
| 121 | {"PROF", SIGPROF}, |
| 122 | {"WINCH", SIGWINCH}, |
| 123 | {"IO", SIGIO}, |
| 124 | {"POLL", SIGPOLL}, |
| 125 | {"PWR", SIGPWR}, |
| 126 | {"UNUSED", SIGUNUSED}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 127 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 128 | {0, 0} |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 131 | extern int kill_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 132 | { |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 133 | int whichApp, sig = SIGTERM; |
| 134 | const char *appUsage; |
| 135 | |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 136 | #ifdef BB_KILLALL |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 137 | /* Figure out what we are trying to do here */ |
| 138 | whichApp = (strcmp(*argv, "killall") == 0)? |
| 139 | KILLALL : KILL; |
| 140 | appUsage = (whichApp == KILLALL)? killall_usage : kill_usage; |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 141 | #else |
| 142 | whichApp = KILL; |
| 143 | appUsage = kill_usage; |
| 144 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 145 | |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 146 | argc--; |
| 147 | argv++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 148 | /* Parse any options */ |
| 149 | if (argc < 1) |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 150 | usage(appUsage); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 151 | |
| 152 | while (argc > 0 && **argv == '-') { |
| 153 | while (*++(*argv)) { |
| 154 | switch (**argv) { |
| 155 | case 'l': |
| 156 | { |
| 157 | int col = 0; |
| 158 | const struct signal_name *s = signames; |
| 159 | |
| 160 | while (s->name != 0) { |
| 161 | col += |
| 162 | fprintf(stderr, "%2d) %-8s", s->number, |
| 163 | (s++)->name); |
| 164 | if (col > 60) { |
| 165 | fprintf(stderr, "\n"); |
| 166 | col = 0; |
| 167 | } |
| 168 | } |
| 169 | fprintf(stderr, "\n\n"); |
| 170 | exit(TRUE); |
| 171 | } |
| 172 | break; |
| 173 | case '-': |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 174 | usage(appUsage); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 175 | default: |
| 176 | { |
| 177 | if (isdigit(**argv)) { |
| 178 | sig = atoi(*argv); |
| 179 | if (sig < 0 || sig >= NSIG) |
| 180 | goto end; |
| 181 | else { |
| 182 | argc--; |
| 183 | argv++; |
| 184 | goto do_it_now; |
| 185 | } |
| 186 | } else { |
| 187 | const struct signal_name *s = signames; |
| 188 | |
| 189 | while (s->name != 0) { |
| 190 | if (strcasecmp(s->name, *argv) == 0) { |
| 191 | sig = s->number; |
| 192 | argc--; |
| 193 | argv++; |
| 194 | goto do_it_now; |
| 195 | } |
| 196 | s++; |
| 197 | } |
| 198 | if (s->name == 0) |
| 199 | goto end; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | argc--; |
| 204 | argv++; |
| 205 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 206 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 207 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 208 | do_it_now: |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 209 | |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 210 | if (whichApp == KILL) { |
| 211 | /* Looks like they want to do a kill. Do that */ |
| 212 | while (--argc >= 0) { |
| 213 | int pid; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 214 | |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 215 | if (!isdigit(**argv)) |
| 216 | fatalError( "Bad PID: %s\n", strerror(errno)); |
| 217 | pid = strtol(*argv, NULL, 0); |
| 218 | if (kill(pid, sig) != 0) |
| 219 | fatalError( "Could not kill pid '%d': %s\n", pid, strerror(errno)); |
| 220 | argv++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 221 | } |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 222 | } |
| 223 | #ifdef BB_KILLALL |
| 224 | else { |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 225 | /* Looks like they want to do a killall. Do that */ |
| 226 | while (--argc >= 0) { |
| 227 | int pid; |
| 228 | |
| 229 | while((pid = findPidByName( *argv))) { |
| 230 | if (kill(pid, sig) != 0) |
| 231 | fatalError( "Could not kill pid '%d': %s\n", pid, strerror(errno)); |
| 232 | } |
| 233 | argv++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 234 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 235 | } |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 236 | #endif |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 237 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 238 | exit(TRUE); |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 239 | |
Eric Andersen | a709317 | 1999-10-23 05:42:08 +0000 | [diff] [blame] | 240 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 241 | end: |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 242 | fatalError( "bad signal name: %s\n", *argv); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 243 | } |