blob: a0b93be2b9caea100665541dbb45aa2a6145f5b7 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc4996011999-10-20 22:08:37 +00002/*
Erik Andersen5661fe02000-04-05 01:00:52 +00003 * Mini kill/killall implementation for busybox
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
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 Andersencc8ed391999-10-05 16:24:54 +000024#include "internal.h"
25#include <stdio.h>
26#include <stdlib.h>
Erik Andersen246cc6d2000-03-07 07:41:42 +000027#include <errno.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000028#include <unistd.h>
29#include <signal.h>
Eric Andersena7093171999-10-23 05:42:08 +000030#include <ctype.h>
Eric Andersenabc0f4f1999-12-08 23:19:36 +000031#include <sys/stat.h>
32#include <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000033
Erik Andersene49d5ec2000-02-08 19:58:47 +000034static const char *kill_usage =
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000035 "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 Andersenabc0f4f1999-12-08 23:19:36 +000041
Erik Andersen6273f652000-03-17 01:12:41 +000042#ifdef BB_KILLALL
Erik Andersen246cc6d2000-03-07 07:41:42 +000043static const char *killall_usage =
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000044 "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 Andersen6273f652000-03-17 01:12:41 +000048#endif
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000049#endif
50 ;
Erik Andersen246cc6d2000-03-07 07:41:42 +000051
52#define KILL 0
53#define KILLALL 1
Eric Andersencc8ed391999-10-05 16:24:54 +000054
55struct signal_name {
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 const char *name;
57 int number;
Eric Andersencc8ed391999-10-05 16:24:54 +000058};
59
60const struct signal_name signames[] = {
Eric Andersen3cfa9ec2000-07-06 17:05:33 +000061 /* Everything, order not important */
Erik Andersene49d5ec2000-02-08 19:58:47 +000062 {"HUP", SIGHUP},
63 {"INT", SIGINT},
64 {"QUIT", SIGQUIT},
65 {"ILL", SIGILL},
66 {"TRAP", SIGTRAP},
67 {"ABRT", SIGABRT},
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 {"FPE", SIGFPE},
69 {"KILL", SIGKILL},
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 {"SEGV", SIGSEGV},
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 {"PIPE", SIGPIPE},
72 {"ALRM", SIGALRM},
73 {"TERM", SIGTERM},
Eric Andersen3cfa9ec2000-07-06 17:05:33 +000074 {"BUS", SIGBUS},
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 {"USR1", SIGUSR1},
76 {"USR2", SIGUSR2},
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 {"STOP", SIGSTOP},
Eric Andersen3cfa9ec2000-07-06 17:05:33 +000078 {"CONT", SIGCONT},
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 {"TTIN", SIGTTIN},
80 {"TTOU", SIGTTOU},
Eric Andersen3cfa9ec2000-07-06 17:05:33 +000081 {"IO", SIGIO},
82 {"TSTP", SIGTSTP},
83 {"CHLD", SIGCHLD},
Erik Andersene49d5ec2000-02-08 19:58:47 +000084 {"XCPU", SIGXCPU},
85 {"XFSZ", SIGXFSZ},
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 {"PROF", SIGPROF},
87 {"WINCH", SIGWINCH},
Eric Andersen3cfa9ec2000-07-06 17:05:33 +000088 {"URG", SIGURG},
89 {"VTALRM", SIGVTALRM},
90#ifndef __alpha__
91 /* everything except alpha */
92 {"IOT", SIGIOT},
Erik Andersene49d5ec2000-02-08 19:58:47 +000093 {"POLL", SIGPOLL},
Eric Andersen3cfa9ec2000-07-06 17:05:33 +000094#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 Andersene49d5ec2000-02-08 19:58:47 +0000115 {"PWR", SIGPWR},
116 {"UNUSED", SIGUNUSED},
Eric Andersencc8ed391999-10-05 16:24:54 +0000117#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118 {0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +0000119};
120
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121extern int kill_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000122{
Erik Andersen246cc6d2000-03-07 07:41:42 +0000123 int whichApp, sig = SIGTERM;
124 const char *appUsage;
125
Erik Andersen6273f652000-03-17 01:12:41 +0000126#ifdef BB_KILLALL
Erik Andersen246cc6d2000-03-07 07:41:42 +0000127 /* Figure out what we are trying to do here */
128 whichApp = (strcmp(*argv, "killall") == 0)?
129 KILLALL : KILL;
130 appUsage = (whichApp == KILLALL)? killall_usage : kill_usage;
Erik Andersen6273f652000-03-17 01:12:41 +0000131#else
132 whichApp = KILL;
133 appUsage = kill_usage;
134#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000135
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000136 argc--;
137 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000138 /* Parse any options */
139 if (argc < 1)
Erik Andersen246cc6d2000-03-07 07:41:42 +0000140 usage(appUsage);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141
142 while (argc > 0 && **argv == '-') {
143 while (*++(*argv)) {
144 switch (**argv) {
145 case 'l':
146 {
147 int col = 0;
148 const struct signal_name *s = signames;
149
150 while (s->name != 0) {
151 col +=
152 fprintf(stderr, "%2d) %-8s", s->number,
153 (s++)->name);
154 if (col > 60) {
155 fprintf(stderr, "\n");
156 col = 0;
157 }
158 }
159 fprintf(stderr, "\n\n");
160 exit(TRUE);
161 }
162 break;
163 case '-':
Erik Andersen246cc6d2000-03-07 07:41:42 +0000164 usage(appUsage);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000165 default:
166 {
167 if (isdigit(**argv)) {
168 sig = atoi(*argv);
169 if (sig < 0 || sig >= NSIG)
170 goto end;
171 else {
172 argc--;
173 argv++;
174 goto do_it_now;
175 }
176 } else {
177 const struct signal_name *s = signames;
178
179 while (s->name != 0) {
180 if (strcasecmp(s->name, *argv) == 0) {
181 sig = s->number;
182 argc--;
183 argv++;
184 goto do_it_now;
185 }
186 s++;
187 }
188 if (s->name == 0)
189 goto end;
190 }
191 }
192 }
193 argc--;
194 argv++;
195 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000196 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000197
Erik Andersene49d5ec2000-02-08 19:58:47 +0000198 do_it_now:
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000199
Erik Andersen246cc6d2000-03-07 07:41:42 +0000200 if (whichApp == KILL) {
201 /* Looks like they want to do a kill. Do that */
202 while (--argc >= 0) {
203 int pid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204
Erik Andersen246cc6d2000-03-07 07:41:42 +0000205 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));
210 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000211 }
Erik Andersen6273f652000-03-17 01:12:41 +0000212 }
213#ifdef BB_KILLALL
214 else {
Pavel Roskin700a5ae2000-06-06 18:10:17 +0000215 int all_found = TRUE;
Erik Andersen0a64de92000-04-18 20:00:03 +0000216 pid_t myPid=getpid();
Erik Andersen246cc6d2000-03-07 07:41:42 +0000217 /* Looks like they want to do a killall. Do that */
218 while (--argc >= 0) {
Erik Andersen825aead2000-04-07 06:00:07 +0000219 pid_t* pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000220
Erik Andersen825aead2000-04-07 06:00:07 +0000221 pidList = findPidByName( *argv);
Pavel Roskin700a5ae2000-06-06 18:10:17 +0000222 if (!pidList) {
223 all_found = FALSE;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000224 errorMsg( "%s: no process killed\n", *argv);
Pavel Roskin700a5ae2000-06-06 18:10:17 +0000225 }
Eric Andersen86ab8a32000-06-02 03:21:42 +0000226
Erik Andersen0a64de92000-04-18 20:00:03 +0000227 for(; pidList && *pidList!=0; pidList++) {
228 if (*pidList==myPid)
229 continue;
Erik Andersen825aead2000-04-07 06:00:07 +0000230 if (kill(*pidList, sig) != 0)
231 fatalError( "Could not kill pid '%d': %s\n", *pidList, strerror(errno));
Erik Andersen246cc6d2000-03-07 07:41:42 +0000232 }
Erik Andersen825aead2000-04-07 06:00:07 +0000233 /* 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 */
Erik Andersen246cc6d2000-03-07 07:41:42 +0000236 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000237 }
Pavel Roskin700a5ae2000-06-06 18:10:17 +0000238 exit (all_found);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000239 }
Erik Andersen6273f652000-03-17 01:12:41 +0000240#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +0000241
Erik Andersene49d5ec2000-02-08 19:58:47 +0000242 exit(TRUE);
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000243
Eric Andersena7093171999-10-23 05:42:08 +0000244
Erik Andersene49d5ec2000-02-08 19:58:47 +0000245 end:
Erik Andersen246cc6d2000-03-07 07:41:42 +0000246 fatalError( "bad signal name: %s\n", *argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000247}