blob: e72b73493928829d452ee9952cba166a399cbbfd [file] [log] [blame]
Eric Andersenc4996011999-10-20 22:08:37 +00001/*
2 * Mini kill implementation for busybox
3 *
4 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22
Eric Andersencc8ed391999-10-05 16:24:54 +000023#include "internal.h"
24#include <stdio.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <signal.h>
Eric Andersena7093171999-10-23 05:42:08 +000028#include <ctype.h>
Eric Andersenabc0f4f1999-12-08 23:19:36 +000029#include <sys/stat.h>
30#include <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000031
Eric Andersenabc0f4f1999-12-08 23:19:36 +000032static const char* kill_usage = "kill [-signal] process-id [process-id ...]\n\n"
33"Send a signal (default is SIGTERM) to the specified process(es).\n\n"
34"Options:\n"
35"\t-l\tList all signal names and numbers.\n\n";
36
Eric Andersencc8ed391999-10-05 16:24:54 +000037
38struct signal_name {
Eric Andersen3cf52d11999-10-12 22:26:06 +000039 const char *name;
40 int number;
Eric Andersencc8ed391999-10-05 16:24:54 +000041};
42
43const struct signal_name signames[] = {
Eric Andersen3cf52d11999-10-12 22:26:06 +000044 {"HUP", SIGHUP},
45 {"INT", SIGINT},
46 {"QUIT", SIGQUIT},
47 {"ILL", SIGILL},
48 {"TRAP", SIGTRAP},
49 {"ABRT", SIGABRT},
Eric Andersencc8ed391999-10-05 16:24:54 +000050#ifndef __alpha__
Eric Andersen3cf52d11999-10-12 22:26:06 +000051 {"IOT", SIGIOT},
Eric Andersencc8ed391999-10-05 16:24:54 +000052#endif
Eric Andersene77ae3a1999-10-19 20:03:34 +000053#if defined(__sparc__) || defined(__alpha__)
Eric Andersen3cf52d11999-10-12 22:26:06 +000054 {"EMT", SIGEMT},
Eric Andersencc8ed391999-10-05 16:24:54 +000055#else
Eric Andersen3cf52d11999-10-12 22:26:06 +000056 {"BUS", SIGBUS},
Eric Andersencc8ed391999-10-05 16:24:54 +000057#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +000058 {"FPE", SIGFPE},
59 {"KILL", SIGKILL},
Eric Andersene77ae3a1999-10-19 20:03:34 +000060#if defined(__sparc__) || defined(__alpha__)
Eric Andersen3cf52d11999-10-12 22:26:06 +000061 {"BUS", SIGBUS},
Eric Andersencc8ed391999-10-05 16:24:54 +000062#else
Eric Andersen3cf52d11999-10-12 22:26:06 +000063 {"USR1", SIGUSR1},
Eric Andersencc8ed391999-10-05 16:24:54 +000064#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +000065 {"SEGV", SIGSEGV},
Eric Andersene77ae3a1999-10-19 20:03:34 +000066#if defined(__sparc__) || defined(__alpha__)
Eric Andersen3cf52d11999-10-12 22:26:06 +000067 {"SYS", SIGSYS},
Eric Andersencc8ed391999-10-05 16:24:54 +000068#else
Eric Andersen3cf52d11999-10-12 22:26:06 +000069 {"USR2", SIGUSR2},
Eric Andersencc8ed391999-10-05 16:24:54 +000070#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +000071 {"PIPE", SIGPIPE},
72 {"ALRM", SIGALRM},
73 {"TERM", SIGTERM},
Eric Andersene77ae3a1999-10-19 20:03:34 +000074#if defined(__sparc__) || defined(__alpha__)
Eric Andersen3cf52d11999-10-12 22:26:06 +000075 {"URG", SIGURG},
76 {"STOP", SIGSTOP},
77 {"TSTP", SIGTSTP},
78 {"CONT", SIGCONT},
79 {"CHLD", SIGCHLD},
80 {"TTIN", SIGTTIN},
81 {"TTOU", SIGTTOU},
82 {"IO", SIGIO},
Eric Andersencc8ed391999-10-05 16:24:54 +000083# ifndef __alpha__
Eric Andersen3cf52d11999-10-12 22:26:06 +000084 {"POLL", SIGIO},
Eric Andersencc8ed391999-10-05 16:24:54 +000085# endif
Eric Andersen3cf52d11999-10-12 22:26:06 +000086 {"XCPU", SIGXCPU},
87 {"XFSZ", SIGXFSZ},
88 {"VTALRM", SIGVTALRM},
89 {"PROF", SIGPROF},
90 {"WINCH", SIGWINCH},
Eric Andersencc8ed391999-10-05 16:24:54 +000091# ifdef __alpha__
Eric Andersen3cf52d11999-10-12 22:26:06 +000092 {"INFO", SIGINFO},
Eric Andersencc8ed391999-10-05 16:24:54 +000093# else
Eric Andersen3cf52d11999-10-12 22:26:06 +000094 {"LOST", SIGLOST},
Eric Andersencc8ed391999-10-05 16:24:54 +000095# endif
Eric Andersen3cf52d11999-10-12 22:26:06 +000096 {"USR1", SIGUSR1},
97 {"USR2", SIGUSR2},
Eric Andersencc8ed391999-10-05 16:24:54 +000098#else
Eric Andersen3cf52d11999-10-12 22:26:06 +000099 {"STKFLT", SIGSTKFLT},
100 {"CHLD", SIGCHLD},
101 {"CONT", SIGCONT},
102 {"STOP", SIGSTOP},
103 {"TSTP", SIGTSTP},
104 {"TTIN", SIGTTIN},
105 {"TTOU", SIGTTOU},
106 {"URG", SIGURG},
107 {"XCPU", SIGXCPU},
108 {"XFSZ", SIGXFSZ},
109 {"VTALRM", SIGVTALRM},
110 {"PROF", SIGPROF},
111 {"WINCH", SIGWINCH},
112 {"IO", SIGIO},
113 {"POLL", SIGPOLL},
114 {"PWR", SIGPWR},
115 {"UNUSED", SIGUNUSED},
Eric Andersencc8ed391999-10-05 16:24:54 +0000116#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +0000117 {0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +0000118};
119
Eric Andersen3cf52d11999-10-12 22:26:06 +0000120extern int kill_main (int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000121{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000122 int sig = SIGTERM;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000123
124 argc--;
125 argv++;
126 /* Parse any options */
127 if (argc < 1)
128 usage(kill_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +0000129
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000130 while (argc > 0 && **argv == '-') {
131 while (*++(*argv)) {
132 switch (**argv) {
133 case 'l':
134 {
135 int col=0;
136 const struct signal_name *s = signames;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000137
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000138 while (s->name != 0) {
139 col+=fprintf(stderr, "%2d) %-8s", s->number, (s++)->name);
140 if (col>60) {
141 fprintf(stderr, "\n");
142 col=0;
143 }
144 }
145 fprintf(stderr, "\n\n");
146 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000147 }
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000148 break;
149 case '-':
150 usage(kill_usage);
151 default:
152 {
153 if (isdigit( **argv)) {
154 sig = atoi (*argv);
155 if (sig < 0 || sig >= NSIG)
156 goto end;
157 else {
158 argc--;
159 argv++;
160 goto do_it_now;
161 }
162 }
163 else {
164 const struct signal_name *s = signames;
165 while (s->name != 0) {
166 if (strcasecmp (s->name, *argv) == 0) {
167 sig = s->number;
168 argc--;
169 argv++;
170 goto do_it_now;
171 }
172 s++;
173 }
174 if (s->name == 0)
175 goto end;
176 }
177 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000178 }
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000179 argc--;
180 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +0000181 }
Eric Andersena7093171999-10-23 05:42:08 +0000182 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000183
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000184do_it_now:
185
Erik Andersen7dc16072000-01-04 01:10:25 +0000186 while (--argc >= 0) {
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000187 int pid;
188 struct stat statbuf;
189 char pidpath[20]="/proc/";
190
191 if (! isdigit( **argv)) {
192 fprintf(stderr, "bad PID: %s\n", *argv);
193 exit( FALSE);
194 }
195 pid = atoi (*argv);
196 snprintf(pidpath, 20, "/proc/%s/stat", *argv);
197 if (stat( pidpath, &statbuf)!=0) {
198 fprintf(stderr, "kill: (%d) - No such pid\n", pid);
199 exit( FALSE);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000200 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000201 fprintf(stderr, "sig = %d\n", sig);
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000202 if (kill (pid, sig) != 0) {
203 perror (*argv);
204 exit ( FALSE);
205 }
206 argv++;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000207 }
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000208 exit ( TRUE);
209
Eric Andersena7093171999-10-23 05:42:08 +0000210
Eric Andersen3cf52d11999-10-12 22:26:06 +0000211end:
Eric Andersena7093171999-10-23 05:42:08 +0000212 fprintf(stderr, "bad signal name: %s\n", *argv);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000213 exit (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000214}
Eric Andersena7093171999-10-23 05:42:08 +0000215
216