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