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"
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"
43 static const char *killall_usage =
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"
60 const struct signal_name signames[] = {
70 #if defined(__sparc__) || defined(__alpha__)
77 #if defined(__sparc__) || defined(__alpha__)
83 #if defined(__sparc__) || defined(__alpha__)
91 #if defined(__sparc__) || defined(__alpha__)
105 {"VTALRM", SIGVTALRM},
116 {"STKFLT", SIGSTKFLT},
126 {"VTALRM", SIGVTALRM},
132 {"UNUSED", SIGUNUSED},
137 extern int kill_main(int argc, char **argv)
139 int whichApp, sig = SIGTERM;
140 const char *appUsage;
143 /* Figure out what we are trying to do here */
144 whichApp = (strcmp(*argv, "killall") == 0)?
146 appUsage = (whichApp == KILLALL)? killall_usage : kill_usage;
149 appUsage = kill_usage;
154 /* Parse any options */
158 while (argc > 0 && **argv == '-') {
164 const struct signal_name *s = signames;
166 while (s->name != 0) {
168 fprintf(stderr, "%2d) %-8s", s->number,
171 fprintf(stderr, "\n");
175 fprintf(stderr, "\n\n");
183 if (isdigit(**argv)) {
185 if (sig < 0 || sig >= NSIG)
193 const struct signal_name *s = signames;
195 while (s->name != 0) {
196 if (strcasecmp(s->name, *argv) == 0) {
216 if (whichApp == KILL) {
217 /* Looks like they want to do a kill. Do that */
218 while (--argc >= 0) {
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));
231 pid_t myPid=getpid();
232 /* Looks like they want to do a killall. Do that */
233 while (--argc >= 0) {
236 pidList = findPidByName( *argv);
237 for(; pidList && *pidList!=0; pidList++) {
240 if (kill(*pidList, sig) != 0)
241 fatalError( "Could not kill pid '%d': %s\n", *pidList, strerror(errno));
243 /* Note that we don't bother to free the memory
244 * allocated in findPidByName(). It will be freed
245 * upon exit, so we can save a byte or two */
255 fatalError( "bad signal name: %s\n", *argv);