blob: 2fabf56d2c37c0788c477b6c7ef27376eeddee5f [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>
28
Eric Andersen3cf52d11999-10-12 22:26:06 +000029const char kill_usage[] = "kill [-signal] process-id [process-id ...]\n";
Eric Andersencc8ed391999-10-05 16:24:54 +000030
31struct signal_name {
Eric Andersen3cf52d11999-10-12 22:26:06 +000032 const char *name;
33 int number;
Eric Andersencc8ed391999-10-05 16:24:54 +000034};
35
36const struct signal_name signames[] = {
Eric Andersen3cf52d11999-10-12 22:26:06 +000037 {"HUP", SIGHUP},
38 {"INT", SIGINT},
39 {"QUIT", SIGQUIT},
40 {"ILL", SIGILL},
41 {"TRAP", SIGTRAP},
42 {"ABRT", SIGABRT},
Eric Andersencc8ed391999-10-05 16:24:54 +000043#ifndef __alpha__
Eric Andersen3cf52d11999-10-12 22:26:06 +000044 {"IOT", SIGIOT},
Eric Andersencc8ed391999-10-05 16:24:54 +000045#endif
Eric Andersene77ae3a1999-10-19 20:03:34 +000046#if defined(__sparc__) || defined(__alpha__)
Eric Andersen3cf52d11999-10-12 22:26:06 +000047 {"EMT", SIGEMT},
Eric Andersencc8ed391999-10-05 16:24:54 +000048#else
Eric Andersen3cf52d11999-10-12 22:26:06 +000049 {"BUS", SIGBUS},
Eric Andersencc8ed391999-10-05 16:24:54 +000050#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +000051 {"FPE", SIGFPE},
52 {"KILL", SIGKILL},
Eric Andersene77ae3a1999-10-19 20:03:34 +000053#if defined(__sparc__) || defined(__alpha__)
Eric Andersen3cf52d11999-10-12 22:26:06 +000054 {"BUS", SIGBUS},
Eric Andersencc8ed391999-10-05 16:24:54 +000055#else
Eric Andersen3cf52d11999-10-12 22:26:06 +000056 {"USR1", SIGUSR1},
Eric Andersencc8ed391999-10-05 16:24:54 +000057#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +000058 {"SEGV", SIGSEGV},
Eric Andersene77ae3a1999-10-19 20:03:34 +000059#if defined(__sparc__) || defined(__alpha__)
Eric Andersen3cf52d11999-10-12 22:26:06 +000060 {"SYS", SIGSYS},
Eric Andersencc8ed391999-10-05 16:24:54 +000061#else
Eric Andersen3cf52d11999-10-12 22:26:06 +000062 {"USR2", SIGUSR2},
Eric Andersencc8ed391999-10-05 16:24:54 +000063#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +000064 {"PIPE", SIGPIPE},
65 {"ALRM", SIGALRM},
66 {"TERM", SIGTERM},
Eric Andersene77ae3a1999-10-19 20:03:34 +000067#if defined(__sparc__) || defined(__alpha__)
Eric Andersen3cf52d11999-10-12 22:26:06 +000068 {"URG", SIGURG},
69 {"STOP", SIGSTOP},
70 {"TSTP", SIGTSTP},
71 {"CONT", SIGCONT},
72 {"CHLD", SIGCHLD},
73 {"TTIN", SIGTTIN},
74 {"TTOU", SIGTTOU},
75 {"IO", SIGIO},
Eric Andersencc8ed391999-10-05 16:24:54 +000076# ifndef __alpha__
Eric Andersen3cf52d11999-10-12 22:26:06 +000077 {"POLL", SIGIO},
Eric Andersencc8ed391999-10-05 16:24:54 +000078# endif
Eric Andersen3cf52d11999-10-12 22:26:06 +000079 {"XCPU", SIGXCPU},
80 {"XFSZ", SIGXFSZ},
81 {"VTALRM", SIGVTALRM},
82 {"PROF", SIGPROF},
83 {"WINCH", SIGWINCH},
Eric Andersencc8ed391999-10-05 16:24:54 +000084# ifdef __alpha__
Eric Andersen3cf52d11999-10-12 22:26:06 +000085 {"INFO", SIGINFO},
Eric Andersencc8ed391999-10-05 16:24:54 +000086# else
Eric Andersen3cf52d11999-10-12 22:26:06 +000087 {"LOST", SIGLOST},
Eric Andersencc8ed391999-10-05 16:24:54 +000088# endif
Eric Andersen3cf52d11999-10-12 22:26:06 +000089 {"USR1", SIGUSR1},
90 {"USR2", SIGUSR2},
Eric Andersencc8ed391999-10-05 16:24:54 +000091#else
Eric Andersen3cf52d11999-10-12 22:26:06 +000092 {"STKFLT", SIGSTKFLT},
93 {"CHLD", SIGCHLD},
94 {"CONT", SIGCONT},
95 {"STOP", SIGSTOP},
96 {"TSTP", SIGTSTP},
97 {"TTIN", SIGTTIN},
98 {"TTOU", SIGTTOU},
99 {"URG", SIGURG},
100 {"XCPU", SIGXCPU},
101 {"XFSZ", SIGXFSZ},
102 {"VTALRM", SIGVTALRM},
103 {"PROF", SIGPROF},
104 {"WINCH", SIGWINCH},
105 {"IO", SIGIO},
106 {"POLL", SIGPOLL},
107 {"PWR", SIGPWR},
108 {"UNUSED", SIGUNUSED},
Eric Andersencc8ed391999-10-05 16:24:54 +0000109#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +0000110 {0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +0000111};
112
Eric Andersen3cf52d11999-10-12 22:26:06 +0000113extern int kill_main (int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000114{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000115 int had_error = 0;
116 int sig = SIGTERM;
Eric Andersencc8ed391999-10-05 16:24:54 +0000117
Eric Andersen3cf52d11999-10-12 22:26:06 +0000118
119
120 if (argv[1][0] == '-') {
121 if (argv[1][1] >= '0' && argv[1][1] <= '9') {
122 sig = atoi (&argv[1][1]);
123 if (sig < 0 || sig >= NSIG)
124 goto end;
125 } else {
126 const struct signal_name *s = signames;
127 for (;;) {
128 if (strcmp (s->name, &argv[1][1]) == 0) {
129 sig = s->number;
130 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000131 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000132 s++;
133 if (s->name == 0)
134 goto end;
135 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000136 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000137 argv++;
138 argc--;
139
140 }
141 while (argc > 1) {
142 int pid;
143 if (argv[1][0] < '0' || argv[1][0] > '9')
144 goto end;
145 pid = atoi (argv[1]);
146 if (kill (pid, sig) != 0) {
147 had_error = 1;
148 perror (argv[1]);
149 }
150 argv++;
151 argc--;
152 }
153 if (had_error) {
154end:
Eric Andersenb0e9a701999-10-18 22:28:26 +0000155 usage (kill_usage);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000156 }
157 exit (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000158}