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