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