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