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[] = {
61 /* Everything, order not important */
89 {"VTALRM", SIGVTALRM},
91 /* everything except alpha */
95 #if defined(__sparc__) || defined(__alpha__) || defined(__mips__)
96 /* everthing except intel */
114 {"STKFLT", SIGSTKFLT},
116 {"UNUSED", SIGUNUSED},
121 extern int kill_main(int argc, char **argv)
123 int whichApp, sig = SIGTERM;
124 const char *appUsage;
127 /* Figure out what we are trying to do here */
128 whichApp = (strcmp(*argv, "killall") == 0)?
130 appUsage = (whichApp == KILLALL)? killall_usage : kill_usage;
133 appUsage = kill_usage;
138 /* Parse any options */
142 while (argc > 0 && **argv == '-') {
148 const struct signal_name *s = signames;
150 while (s->name != 0) {
152 fprintf(stderr, "%2d) %-8s", s->number,
155 fprintf(stderr, "\n");
159 fprintf(stderr, "\n\n");
167 if (isdigit(**argv)) {
169 if (sig < 0 || sig >= NSIG)
177 const struct signal_name *s = signames;
179 while (s->name != 0) {
180 if (strcasecmp(s->name, *argv) == 0) {
200 if (whichApp == KILL) {
201 /* Looks like they want to do a kill. Do that */
202 while (--argc >= 0) {
205 if (!isdigit(**argv))
206 fatalError( "Bad PID: %s\n", strerror(errno));
207 pid = strtol(*argv, NULL, 0);
208 if (kill(pid, sig) != 0)
209 fatalError( "Could not kill pid '%d': %s\n", pid, strerror(errno));
215 int all_found = TRUE;
216 pid_t myPid=getpid();
217 /* Looks like they want to do a killall. Do that */
218 while (--argc >= 0) {
221 pidList = findPidByName( *argv);
224 errorMsg( "%s: no process killed\n", *argv);
227 for(; pidList && *pidList!=0; pidList++) {
230 if (kill(*pidList, sig) != 0)
231 fatalError( "Could not kill pid '%d': %s\n", *pidList, strerror(errno));
233 /* Note that we don't bother to free the memory
234 * allocated in findPidByName(). It will be freed
235 * upon exit, so we can save a byte or two */
246 fatalError( "bad signal name: %s\n", *argv);