2fabf56d2c37c0788c477b6c7ef27376eeddee5f
[oweals/busybox.git] / procps / kill.c
1 /*
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
23 #include "internal.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <signal.h>
28
29 const char kill_usage[] = "kill [-signal] process-id [process-id ...]\n";
30
31 struct signal_name {
32     const char *name;
33     int number;
34 };
35
36 const struct signal_name signames[] = {
37     {"HUP", SIGHUP},
38     {"INT", SIGINT},
39     {"QUIT", SIGQUIT},
40     {"ILL", SIGILL},
41     {"TRAP", SIGTRAP},
42     {"ABRT", SIGABRT},
43 #ifndef __alpha__
44     {"IOT", SIGIOT},
45 #endif
46 #if defined(__sparc__) || defined(__alpha__)
47     {"EMT", SIGEMT},
48 #else
49     {"BUS", SIGBUS},
50 #endif
51     {"FPE", SIGFPE},
52     {"KILL", SIGKILL},
53 #if defined(__sparc__) || defined(__alpha__)
54     {"BUS", SIGBUS},
55 #else
56     {"USR1", SIGUSR1},
57 #endif
58     {"SEGV", SIGSEGV},
59 #if defined(__sparc__) || defined(__alpha__)
60     {"SYS", SIGSYS},
61 #else
62     {"USR2", SIGUSR2},
63 #endif
64     {"PIPE", SIGPIPE},
65     {"ALRM", SIGALRM},
66     {"TERM", SIGTERM},
67 #if defined(__sparc__) || defined(__alpha__)
68     {"URG", SIGURG},
69     {"STOP", SIGSTOP},
70     {"TSTP", SIGTSTP},
71     {"CONT", SIGCONT},
72     {"CHLD", SIGCHLD},
73     {"TTIN", SIGTTIN},
74     {"TTOU", SIGTTOU},
75     {"IO", SIGIO},
76 # ifndef __alpha__
77     {"POLL", SIGIO},
78 # endif
79     {"XCPU", SIGXCPU},
80     {"XFSZ", SIGXFSZ},
81     {"VTALRM", SIGVTALRM},
82     {"PROF", SIGPROF},
83     {"WINCH", SIGWINCH},
84 # ifdef __alpha__
85     {"INFO", SIGINFO},
86 # else
87     {"LOST", SIGLOST},
88 # endif
89     {"USR1", SIGUSR1},
90     {"USR2", SIGUSR2},
91 #else
92     {"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},
109 #endif
110     {0, 0}
111 };
112
113 extern int kill_main (int argc, char **argv)
114 {
115     int had_error = 0;
116     int sig = SIGTERM;
117
118
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;
131                 }
132                 s++;
133                 if (s->name == 0)
134                     goto end;
135             }
136         }
137         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) {
154 end:
155         usage (kill_usage);
156     }
157     exit (TRUE);
158 }