1 /* vi: set sw=4 ts=4: */
3 * Mini kill/killall implementation for busybox
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
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.
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.
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
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";
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";
54 const struct signal_name signames[] = {
64 #if defined(__sparc__) || defined(__alpha__)
71 #if defined(__sparc__) || defined(__alpha__)
77 #if defined(__sparc__) || defined(__alpha__)
85 #if defined(__sparc__) || defined(__alpha__)
99 {"VTALRM", SIGVTALRM},
110 {"STKFLT", SIGSTKFLT},
120 {"VTALRM", SIGVTALRM},
126 {"UNUSED", SIGUNUSED},
131 extern int kill_main(int argc, char **argv)
133 int whichApp, sig = SIGTERM;
134 const char *appUsage;
137 /* Figure out what we are trying to do here */
138 whichApp = (strcmp(*argv, "killall") == 0)?
140 appUsage = (whichApp == KILLALL)? killall_usage : kill_usage;
143 appUsage = kill_usage;
148 /* Parse any options */
152 while (argc > 0 && **argv == '-') {
158 const struct signal_name *s = signames;
160 while (s->name != 0) {
162 fprintf(stderr, "%2d) %-8s", s->number,
165 fprintf(stderr, "\n");
169 fprintf(stderr, "\n\n");
177 if (isdigit(**argv)) {
179 if (sig < 0 || sig >= NSIG)
187 const struct signal_name *s = signames;
189 while (s->name != 0) {
190 if (strcasecmp(s->name, *argv) == 0) {
210 if (whichApp == KILL) {
211 /* Looks like they want to do a kill. Do that */
212 while (--argc >= 0) {
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));
225 /* Looks like they want to do a killall. Do that */
226 while (--argc >= 0) {
229 pidList = findPidByName( *argv);
230 for(; pidList && pidList!=0; pidList++) {
231 if (kill(*pidList, sig) != 0)
232 fatalError( "Could not kill pid '%d': %s\n", *pidList, strerror(errno));
234 /* Note that we don't bother to free the memory
235 * allocated in findPidByName(). It will be freed
236 * upon exit, so we can save a byte or two */
246 fatalError( "bad signal name: %s\n", *argv);